2 from __future__
import with_statement
5 from subprocess
import Popen, PIPE
16 from hypervisor
import hypervisor_factory
18 logger = logging.getLogger(__name__)
31 def __init__(self, vnfs_root):
33 self.
_hypervisor = hypervisor_factory.HypervisorFactory.get_hypervisor_instance()
35 def _full_path(self, partial):
36 if partial.startswith(
"/"):
38 path = os.path.join(self.
vnfs_root, partial)
53 logger.info(
'Creating file/directory structure in ' + path)
55 result = os.mkdir(full_path)
56 default_file_mode = 0o644
60 os.O_WRONLY | os.O_CREAT,
63 os.mkdir(full_path +
"/config", mode)
64 os.open(full_path +
"/config/boot.conf", os.O_WRONLY | os.O_CREAT,
66 os.mkdir(full_path +
"/machine", mode)
67 os.open(full_path +
"/machine/ip", os.O_WRONLY | os.O_CREAT,
69 os.open(full_path +
"/machine/vm.vcpu", os.O_WRONLY | os.O_CREAT,
71 os.open(full_path +
"/machine/vm.memory", os.O_WRONLY | os.O_CREAT,
73 os.open(full_path +
"/machine/vm.image", os.O_WRONLY | os.O_CREAT,
75 os.open(full_path +
"/machine/vm.ip", os.O_WRONLY | os.O_CREAT,
77 os.open(full_path +
"/action", os.O_WRONLY | os.O_CREAT,
80 default_file_mode = 0o444
81 os.mkdir(full_path +
"/stats", mode)
82 os.open(full_path +
"/stats/rx_bytes", os.O_WRONLY | os.O_CREAT,
84 os.open(full_path +
"/stats/tx_bytes", os.O_WRONLY | os.O_CREAT,
86 os.open(full_path +
"/stats/pkt_drops", os.O_WRONLY | os.O_CREAT,
88 logger.info(
'Finished creating file/directory structure in ' + path)
104 tokens = self.
_full_path(path).encode(
'ascii').split(
'/')
105 if "nf-types" in tokens:
106 return VNFSOperations.OP_NF
107 return VNFSOperations.OP_UNDEFINED
123 tokens = self.
_full_path(path).encode(
'ascii').split(
'/')
125 return tokens[tokens.index(
"nf-types") + 1]
143 return path.split(
'/')[-1]
161 tokens = self.
_full_path(path).encode(
'ascii').split(
'/')
162 if (
'nf-types' in tokens)
and len(tokens) > tokens.index(
'nf-types') + \
188 logger.debug(nf_path +
'/machine/ip')
189 with open(nf_path +
'/machine/ip')
as ip_fd:
190 ip_address = ip_fd.readline().rstrip(
'\n')
192 with open(nf_path +
'/machine/vm.image')
as img_fd:
193 image_name = img_fd.readline().rstrip(
'\n')
194 logger.info(
"Instance name: " + nf_instance_name +
", type: "
195 + nf_type +
", host-ip: " + ip_address +
" VNF image: " + image_name)
196 return nf_instance_name, nf_type, ip_address, image_name
208 logger.info(
'Deploying new VNF at ' + nf_path)
211 cont_id = self._hypervisor.deploy(
212 ip_address, getpass.getuser(), image_name, nf_instance_name)
213 logger.debug(cont_id)
215 logger.info(
'Instance: ' + nf_instance_name +
' deployment failed')
217 logger.info(
'Instance: ' + nf_instance_name
218 +
' successfully deployed')
220 logger.info(
'Starting the deployed VNF instance: '
222 self._hypervisor.start(ip_address, cont_id)
224 logger.info(
'Instance: ' + nf_instance_name +
' start failed')
226 self._hypervisor.destroy(ip_address, cont_id)
227 logger.info(
'Instance: ' + nf_instance_name
230 logger.info(
'Instance: ' + nf_instance_name
231 +
' successfully deployed and started')
243 logger.info(
"Stopping VNF at " + nf_path)
246 self._hypervisor.stop(ip_address, getpass.getuser(), nf_instance_name )
247 logger.info(
'Instance: ' + nf_instance_name +
' successfully stopped')
261 logger.info(
"Starting VNF at " + nf_path)
264 self._hypervisor.start(ip_address, getpass.getuser(), nf_instance_name)
265 logger.info(
'Instance: ' + nf_instance_name +
' successfully started')
279 logger.info(
"Destroying VNF at " + nf_path)
282 self._hypervisor.destroy(ip_address, getpass.getuser(), nf_instance_name)
283 logger.info(
'Instance: ' + nf_instance_name +
' successfully destroyed')
296 logger.info(
'Reading rx_bytes at ' + nf_path)
299 command =
"ifconfig eth0 | grep -Eo 'RX bytes:[0-9]+' | cut -d':' -f 2"
300 response = self._hypervisor.execute_in_guest(
302 getpass.getuser(), nf_instance_name,
304 logger.info(
'Successfully read rx_bytes')
318 logger.info(
'Reading tx_bytes at ' + nf_path)
321 command =
"ifconfig eth0 | grep -Eo 'TX bytes:[0-9]+' | cut -d':' -f 2"
322 response = self._hypervisor.execute_in_guest(
324 getpass.getuser(), nf_instance_name,
326 logger.info(
'Successfully read tx_bytes')
340 logger.info(
'Reading pkt_drops at ' + nf_path)
343 command =
"ifconfig eth0 | grep -Eo 'RX .* dropped:[0-9]+' | cut -d':' -f 4"
344 response = self._hypervisor.execute_in_guest(
346 getpass.getuser(), nf_instance_name,
348 logger.info(
'Successfully read pkt_drops')
365 logger.info(
'Reading status at ' + nf_path)
370 response = self._hypervisor.guest_status(ip_address, getpass.getuser(),
373 logger.info(
'Instance: ' + nf_instance_name +
' does not exist')
374 logger.info(
'Successfully read status')
391 logger.info(
'Reading ip at ' + nf_path)
394 cont_ip = self._hypervisor.get_ip(ip_address, getpass.getuser(), nf_instance_name)
395 logger.debug(
'cont_ip ' + cont_ip)
396 logger.info(
'Successfully read ip')
def vnfs_deploy_nf
Deploys and STARTS a VNF instance.
def vnfs_get_opcode
Determinse the type of operation based on the path.
def vnfs_get_rx_bytes
Reads the number of bytes received by a VNF instance.
Provides a common set of operations for nfio.
def vnfs_stop_vnf
Stops a VNF instance.
def vnfs_start_vnf
Starts a deployed VNF instance.
def vnfs_destroy_vnf
Destroys a deployed VNF instance.
def vnfs_get_status
Get the status of a VNF instance, e.g., the VNF is running/suspended/stopped etc. ...
def vnfs_is_nf_instance
Determines if a path represents an nf instance directory.
def vnfs_get_instance_configuration
Return the configuration parameters related to a VNF instance.
def vnfs_get_file_name
Return the name of the file represented by a path.
def vnfs_get_pkt_drops
Reads the number of packets dropped by a VNF instance.
def vnfs_get_ip
Get the status of a VNF instance, e.g., the VNF is running/suspended/stopped etc. ...
def vnfs_get_nf_type
Parse the type of VNF from path.
def vnfs_get_tx_bytes
Reads the number of bytes sent by a VNF instance.
def vnfs_create_vnf_instance
Create the file system structure for a VNF.