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)
48 nf_id = vnfs_ops._hypervisor.get_id(host, getpass.getuser(),
52 return {
'nf_id':nf_id,
53 'nf_image_name':nf_image_name,
54 'nf_instance_name':nf_instance_name,
56 'username':getpass.getuser()
60 def _mkdir(root, path, mode):
61 vnfs_ops = VNFSOperations(root)
62 result = vnfs_ops.vnfs_create_vnf_instance(path, mode)
66 def _getattr(root, path, fh=None):
67 vnfs_ops = VNFSOperations(root)
68 f_path = full_path(root, path)
70 file_name = vnfs_ops.vnfs_get_file_name(f_path)
71 return_dictionary = dict()
72 return_dictionary[
'st_atime'] = st.st_atime
73 return_dictionary[
'st_ctime'] = st.st_ctime
74 return_dictionary[
'st_gid'] = st.st_gid
75 return_dictionary[
'st_mode'] = st.st_mode
76 return_dictionary[
'st_mtime'] = st.st_mtime
77 return_dictionary[
'st_nlink'] = st.st_nlink
78 return_dictionary[
'st_size'] = st.st_size
79 return_dictionary[
'st_uid'] = st.st_uid
80 if file_name
in special_files:
81 return_dictionary[
'st_size'] = 1000
82 return return_dictionary
85 def _read(root, path, length, offset, fh):
86 f_path = full_path(root, path)
87 vnfs_ops = VNFSOperations(root)
88 file_name = vnfs_ops.vnfs_get_file_name(f_path)
91 if file_name
in special_files
and special_files[file_name]+
'_read' in globals():
94 nf_config = get_nf_config(vnfs_ops, f_path)
96 logger.info(
'Reading ' + file_name +
' from ' +
97 nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
98 ret_str = globals()[special_files[file_name]+
'_read'](vnfs_ops._hypervisor,
101 logger.debug(
'raised OSErro ' + str(ex.errno))
102 raise OSError(ex.errno, os.strerror(ex.errno))
103 logger.info(
'Successfully read ' + file_name +
104 ' from ' + nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
105 if offset >= len(ret_str):
129 os.lseek(fh, offset, os.SEEK_SET)
130 ret_str = os.read(fh, length)
134 def _write(root, path, buf, offset, fh):
135 f_path = full_path(root, path)
136 vnfs_ops = VNFSOperations(root)
137 file_name = vnfs_ops.vnfs_get_file_name(f_path)
139 if file_name
in special_files
and special_files[file_name]+
'_write' in globals():
141 nf_config = get_nf_config(vnfs_ops, f_path)
143 logger.info(
'Writing to ' + file_name +
' in ' +
144 nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
145 ret_str = globals()[special_files[file_name]+
'_write'](vnfs_ops._hypervisor,
146 nf_config, buf.rstrip(
"\n"))
148 logger.debug(
'raised OSErro ' + str(ex.errno))
149 raise OSError(ex.errno, os.strerror(ex.errno))
150 logger.info(
'Successfully wrote ' + file_name +
151 ' in ' + nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
165 os.lseek(fh, offset, os.SEEK_SET)
166 os.write(fh, buf.rstrip(
"\n"))
169 os.lseek(fh, offset, os.SEEK_SET)
170 return os.write(fh, buf)
172 def rx_bytes_read(hypervisor_driver, nf_config):
173 command =
"ifconfig eth0 | grep -Eo 'RX bytes:[0-9]+' | cut -d':' -f 2"
174 return hypervisor_driver.execute_in_guest(nf_config[
'host'],
175 nf_config[
'nf_id'], command)
177 def tx_bytes_read(hypervisor_driver, nf_config):
178 command =
"ifconfig eth0 | grep -Eo 'TX bytes:[0-9]+' | cut -d':' -f 2"
179 return hypervisor_driver.execute_in_guest(nf_config[
'host'],
180 nf_config[
'nf_id'], command)
182 def pkt_drops_read(hypervisor_driver, nf_config):
183 command =
"ifconfig eth0 | grep -Eo 'RX .* dropped:[0-9]+' | cut -d':' -f 4"
184 return hypervisor_driver.execute_in_guest(nf_config[
'host'],
185 nf_config[
'nf_id'], command)
190 def _start(hypervisor_driver, nf_config):
191 command = "/user/bin nginx"
192 return hypervisor_driver.execute_in_guest(nf_config['host'],
193 nf_config['nf_id'], command)
196 def _nginx_signal(hypervisor_driver, nf_config, signal):
197 if signal not in ["stop", "quit", "reload", 'reopen']:
198 logger.info("invalid signal")
200 command = "nginx -s %s" % signal
201 return hypervisor_driver.execute_in_guest(nf_config['host'],
202 nf_config['nf_id'], command)
208 def status_read(hypervisor_driver, nf_config):
209 return hypervisor_driver.guest_status(nf_config[
'host'],
212 def vm_ip_read(hypervisor_driver, nf_config):
213 return hypervisor_driver.get_ip(nf_config[
'host'],
216 def action_write(hypervisor_driver, nf_config, data):
217 if data ==
"activate":
218 logger.info(
'Deploying new VNF instance ' + nf_config[
'nf_instance_name'] +
219 ' @ ' + nf_config[
'host'])
220 nf_id = hypervisor_driver.deploy(nf_config[
'host'], nf_config[
'username'],
221 nf_config[
'nf_image_name'], nf_config[
'nf_instance_name'])
222 logger.info(
'VNF deployed, now starting VNF instance ' +
223 nf_config[
'nf_instance_name'] +
' @ ' + nf_config[
'host'])
225 hypervisor_driver.start(nf_config[
'host'], nf_id)
226 logger.info(
'Successfully started VNF instance ' +
227 nf_config[
'nf_instance_name'] +
' @ ' + nf_config[
'host'])
229 logger.error(
'Attempt to start ' + nf_config[
'nf_instance_name'] +
230 '@' + nf_config[
'host'] +
' failed. Destroying depoyed VNF.')
232 hypervisor_driver.destroy(nf_config[
'host'], nf_id)
233 logger.info(
'Successfully destroyed ' +
234 nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'])
236 raise errors.VNFDeploymentError
238 logger.error(
'Failed to destroy partially activated VNF instance ' +
239 nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
240 '. VNF is in inconsistent state.')
243 logger.info(
'Stopping VNF instance ' + nf_config[
'nf_instance_name'] +
244 '@' + nf_config[
'host'])
245 hypervisor_driver.stop(nf_config[
'host'], nf_config[
'nf_id'])
246 logger.info(nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
247 ' successfully stopped')
248 elif data ==
"start":
249 logger.info(
'Starting VNF instance ' + nf_config[
'nf_instance_name'] +
250 '@' + nf_config[
'host'])
251 hypervisor_driver.start(nf_config[
'host'], nf_config[
'nf_id'])
252 logger.info(nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
253 ' successfully started')
254 elif data ==
"destroy":
255 logger.info(
'Destroying VNF instance ' + nf_config[
'nf_instance_name'] +
256 '@' + nf_config[
'host'])
257 hypervisor_driver.destroy(nf_config[
'host'], nf_config[
'nf_id'])
258 logger.info(nf_config[
'nf_instance_name'] +
'@' + nf_config[
'host'] +
259 ' successfully destroyed')