All Classes Functions
firewall.py
1 ##
2 #
3 # Check the documentation in __init__.py for more information on the methods in
4 # this module.
5 #
6 
7 from vnfs_operations import VNFSOperations
8 import os
9 import logging
10 import getpass
11 import errno
12 import errors
13 ##
14 #
15 # special_files is a list of files specific to this VNF that requires special
16 # handling while reading. For example, if a read system call is issued for
17 # rx_bytes, then the VNF instance needs to be queried for the number of bytes it
18 # has received and that is returned as the read data.
19 #
20 # action_files contains a list of files that are used to represent different
21 # action on a VNF. For example, writing 'stop' to 'action' will stop a VNF
22 # instance.
23 #
24 
25 special_files = { 'rx_bytes':'rx_bytes',
26  'tx_bytes':'tx_bytes',
27  'pkt_drops':'pkt_drops',
28  'status':'status',
29  'vm.ip' : 'vm_ip',
30  'action': 'action'}
31 
32 logger = logging.getLogger(__name__)
33 
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)
38 
39 def get_nf_config(vnfs_ops, full_nf_path):
40  # nf_path for calling vnfs_op function
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])
44 
45  # get info about nf
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,
49  'host':host,
50  'username':getpass.getuser()
51  }
52 
53 def _mkdir(root, path, mode):
54  vnfs_ops = VNFSOperations(root)
55  result = vnfs_ops.vnfs_create_vnf_instance(path, mode)
56  return result
57 
58 def _getattr(root, path, fh=None):
59  vnfs_ops = VNFSOperations(root)
60  f_path = full_path(root, path)
61  st = os.lstat(f_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
75 
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)
80  nf_path = ''
81  ret_str = ''
82  if file_name in special_files and special_files[file_name]+'_read' in globals():
83 
84  try:
85  nf_config = get_nf_config(vnfs_ops, f_path)
86  # call the custom read function
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,
90  nf_config)
91  logger.info('Successfully read ' + file_name +
92  ' from ' + nf_config['nf_instance_name'] + '@' + nf_config['host'])
93  except errors.nfioError, ex:
94  logger.error('Failed to read ' + file_name +
95  ' from ' + nf_config['nf_instance_name'] + '@' + nf_config['host'] +
96  ' : ' + ex.__class__.__name__)
97  #raise OSError(ex.errno, os.strerror(ex.errno))
98  if offset >= len(ret_str):
99  ret_str = ''
100  else:
101  os.lseek(fh, offset, os.SEEK_SET)
102  ret_str = os.read(fh, length)
103  return ret_str
104 
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)
109  #if file_name == "action":
110  if file_name in special_files and special_files[file_name]+'_write' in globals():
111  try:
112  nf_config = get_nf_config(vnfs_ops, f_path)
113  # call the custom write function
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'])
120  except errors.nfioError, ex:
121  logger.error('Failed to write ' + file_name +
122  ' in ' + nf_config['nf_instance_name'] + '@' + nf_config['host'] +
123  ' : ' + ex.__class__.__name__)
124  #raise OSError(ex.errno, os.strerror(ex.errno))
125  os.lseek(fh, offset, os.SEEK_SET)
126  os.write(fh, buf.rstrip("\n"))
127  return len(buf)
128  else:
129  os.lseek(fh, offset, os.SEEK_SET)
130  return os.write(fh, buf)
131 
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)
136 
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)
141 
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)
146 
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'])
150 
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'])
154 
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'])
163  try:
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'])
167  except errors.VNFStartError:
168  logger.error('Attempt to start ' + nf_config['nf_instance_name'] +
169  '@' + nf_config['host'] + ' failed. Destroying depoyed VNF.')
170  try:
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'])
174  # the VNF was deployed, but failed to start so...
175  raise errors.VNFDeploymentError
176  except errors.VNFDestroyError:
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.')
181  elif data == "stop":
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')
202 
203 
This module contains all the custom exceptions defined for nf.io.
Definition: errors.py:6