7 from vnfs_operations
import VNFSOperations
25 special_files = {
'rx_bytes':
'rx_bytes',
26 'tx_bytes':
'tx_bytes',
27 'pkt_drops':
'pkt_drops',
32 logger = logging.getLogger(__name__)
34 def full_path(root, partial_path):
35 if partial_path.startswith(
"/"):
36 partial_path = partial_path[1:]
37 return os.path.join(root, partial_path)
39 def get_nf_config(vnfs_ops, full_nf_path):
41 tokens = full_nf_path.encode(
'ascii').split(
'/')
42 last_index_to_keep = tokens.index(
'nf-types') + 3
43 nf_path =
"/".join(tokens[0:last_index_to_keep])
46 nf_instance_name, nf_type, host, nf_image_name = vnfs_ops.vnfs_get_instance_configuration(nf_path)
47 return {
'nf_image_name':nf_image_name,
48 'nf_instance_name':nf_instance_name,
50 'username':getpass.getuser()
53 def _mkdir(root, path, mode):
54 vnfs_ops = VNFSOperations(root)
55 result = vnfs_ops.vnfs_create_vnf_instance(path, mode)
58 def _getattr(root, path, fh=None):
59 vnfs_ops = VNFSOperations(root)
60 f_path = full_path(root, path)
62 file_name = vnfs_ops.vnfs_get_file_name(f_path)
63 return_dictionary = dict()
64 return_dictionary[
'st_atime'] = st.st_atime
65 return_dictionary[
'st_ctime'] = st.st_ctime
66 return_dictionary[
'st_gid'] = st.st_gid
67 return_dictionary[
'st_mode'] = st.st_mode
68 return_dictionary[
'st_mtime'] = st.st_mtime
69 return_dictionary[
'st_nlink'] = st.st_nlink
70 return_dictionary[
'st_size'] = st.st_size
71 return_dictionary[
'st_uid'] = st.st_uid
72 if file_name
in special_files:
73 return_dictionary[
'st_size'] = 1000
74 return return_dictionary
76 def _read(root, path, length, offset, fh):
77 f_path = full_path(root, path)
78 vnfs_ops = VNFSOperations(root)
79 file_name = vnfs_ops.vnfs_get_file_name(f_path)
82 if file_name
in special_files
and special_files[file_name]+
'_read' in globals():
85 nf_config = get_nf_config(vnfs_ops, f_path)
87 logger.info(
'Reading ' + file_name +
' from ' +
88 nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
89 ret_str = globals()[special_files[file_name]+
'_read'](vnfs_ops._hypervisor,
91 logger.info(
'Successfully read ' + file_name +
92 ' from ' + nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
94 logger.error(
'Failed to read ' + file_name +
95 ' from ' + nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
96 ' : ' + ex.__class__.__name__)
98 if offset >= len(ret_str):
101 os.lseek(fh, offset, os.SEEK_SET)
102 ret_str = os.read(fh, length)
105 def _write(root, path, buf, offset, fh):
106 f_path = full_path(root, path)
107 vnfs_ops = VNFSOperations(root)
108 file_name = vnfs_ops.vnfs_get_file_name(f_path)
110 if file_name
in special_files
and special_files[file_name]+
'_write' in globals():
112 nf_config = get_nf_config(vnfs_ops, f_path)
114 logger.info(
'Writing to ' + file_name +
' in ' +
115 nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
116 ret_str = globals()[special_files[file_name]+
'_write'](vnfs_ops._hypervisor,
117 nf_config, buf.rstrip(
"\n"))
118 logger.info(
'Successfully wrote ' + file_name +
119 ' in ' + nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
121 logger.error(
'Failed to write ' + file_name +
122 ' in ' + nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
123 ' : ' + ex.__class__.__name__)
125 os.lseek(fh, offset, os.SEEK_SET)
126 os.write(fh, buf.rstrip(
"\n"))
129 os.lseek(fh, offset, os.SEEK_SET)
130 return os.write(fh, buf)
132 def rx_bytes_read(hypervisor_driver, nf_config):
133 command =
"ifconfig eth0 | grep -Eo 'RX bytes:[0-9]+' | cut -d':' -f 2"
134 return hypervisor_driver.execute_in_guest(nf_config[
'host'],
135 nf_config[
'username'], nf_config[
'nf_instance_name'], command)
137 def tx_bytes_read(hypervisor_driver, nf_config):
138 command =
"ifconfig eth0 | grep -Eo 'TX bytes:[0-9]+' | cut -d':' -f 2"
139 return hypervisor_driver.execute_in_guest(nf_config[
'host'],
140 nf_config[
'username'], nf_config[
'nf_instance_name'], command)
142 def pkt_drops_read(hypervisor_driver, nf_config):
143 command =
"ifconfig eth0 | grep -Eo 'RX .* dropped:[0-9]+' | cut -d':' -f 4"
144 return hypervisor_driver.execute_in_guest(nf_config[
'host'],
145 nf_config[
'username'], nf_config[
'nf_instance_name'], command)
147 def status_read(hypervisor_driver, nf_config):
148 return hypervisor_driver.guest_status(nf_config[
'host'],
149 nf_config[
'username'], nf_config[
'nf_instance_name'])
151 def vm_ip_read(hypervisor_driver, nf_config):
152 return hypervisor_driver.get_ip(nf_config[
'host'],
153 nf_config[
'username'], nf_config[
'nf_instance_name'])
155 def action_write(hypervisor_driver, nf_config, data):
156 if data ==
"activate":
157 logger.info(
'Deploying new VNF instance ' + nf_config[
'nf_instance_name'] +
158 ' @ ' + nf_config[
'host'])
159 nf_id = hypervisor_driver.deploy(nf_config[
'host'], nf_config[
'username'],
160 nf_config[
'nf_image_name'], nf_config[
'nf_instance_name'])
161 logger.info(
'VNF deployed, now starting VNF instance ' +
162 nf_config[
'nf_instance_name'] +
' @ ' + nf_config[
'host'])
164 hypervisor_driver.start(nf_config[
'host'], nf_config[
'username'], nf_config[
'nf_instance_name'])
165 logger.info(
'Successfully started VNF instance ' +
166 nf_config[
'nf_instance_name'] +
' @ ' + nf_config[
'host'])
168 logger.error(
'Attempt to start ' + nf_config[
'nf_instance_name'] +
169 '@' + nf_config[
'host'] +
' failed. Destroying depoyed VNF.')
171 hypervisor_driver.destroy(nf_config[
'host'], nf_config[
'username'], nf_config[
'nf_instance_name'])
172 logger.info(
'Successfully destroyed ' +
173 nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
175 raise errors.VNFDeploymentError
177 logger.error(
'Failed to destroy partially activated VNF instance ' +
178 nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
179 '. VNF is in inconsistent state.')
182 logger.info(
'Stopping VNF instance ' + nf_config[
'nf_instance_name'] +
183 '@' + nf_config[
'host'])
184 hypervisor_driver.stop(nf_config[
'host'], nf_config[
'username'],
185 nf_config[
'nf_instance_name'])
186 logger.info(nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
187 ' successfully stopped')
188 elif data ==
"start":
189 logger.info(
'Starting VNF instance ' + nf_config[
'nf_instance_name'] +
190 '@' + nf_config[
'host'])
191 hypervisor_driver.start(nf_config[
'host'], nf_config[
'username'],
192 nf_config[
'nf_instance_name'])
193 logger.info(nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
194 ' successfully started')
195 elif data ==
"destroy":
196 logger.info(
'Destroying VNF instance ' + nf_config[
'nf_instance_name'] +
197 '@' + nf_config[
'host'])
198 hypervisor_driver.destroy(nf_config[
'host'], nf_config[
'username'],
199 nf_config[
'nf_instance_name'])
200 logger.info(nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
201 ' successfully destroyed')
This module contains all the custom exceptions defined for nf.io.