All Classes Functions
ids.py
1 from vnfs_operations import VNFSOperations
2 import os
3 
4 special_files = ['rx_bytes', 'tx_bytes', 'pkt_drops', 'status']
5 action_files = ['action']
6 
7 
8 def full_path(root, partial_path):
9  if partial_path.startswith("/"):
10  partial_path = partial_path[1:]
11  return os.path.join(root, partial_path)
12 
13 
14 def _mkdir(root, path, mode):
15  vnfs_ops = VNFSOperations(root)
16  result = vnfs_ops.vnfs_create_vnf_instance(path, mode)
17  return result
18 
19 
20 def _getattr(root, path, fh=None):
21  vnfs_ops = VNFSOperations(root)
22  f_path = full_path(root, path)
23  st = os.lstat(f_path)
24  file_name = vnfs_ops.vnfs_get_file_name(f_path)
25  return_dictionary = dict()
26  return_dictionary['st_atime'] = st.st_atime
27  return_dictionary['st_ctime'] = st.st_ctime
28  return_dictionary['st_gid'] = st.st_gid
29  return_dictionary['st_mode'] = st.st_mode
30  return_dictionary['st_mtime'] = st.st_mtime
31  return_dictionary['st_nlink'] = st.st_nlink
32  return_dictionary['st_size'] = st.st_size
33  return_dictionary['st_uid'] = st.st_uid
34  if file_name in special_files:
35  return_dictionary['st_size'] = 1000
36  return return_dictionary
37 
38 
39 def _read(root, path, length, offset, fh):
40  f_path = full_path(root, path)
41  vnfs_ops = VNFSOperations(root)
42  file_name = vnfs_ops.vnfs_get_file_name(f_path)
43  nf_path = ''
44  if file_name in special_files:
45  tokens = f_path.encode('ascii').split('/')
46  last_index_to_keep = tokens.index('nf-types') + 3
47  nf_path = "/".join(tokens[0:last_index_to_keep])
48  if file_name == "rx_bytes":
49  ret_str = vnfs_ops.vnfs_get_rx_bytes(nf_path)
50  if offset >= len(ret_str):
51  ret_str = ''
52  elif file_name == 'tx_bytes':
53  ret_str = vnfs_ops.vnfs_get_tx_bytes(nf_path)
54  if offset >= len(ret_str):
55  ret_str = ''
56  elif file_name == 'pkt_drops':
57  ret_str = vnfs_ops.vnfs_get_pkt_drops(nf_path)
58  if offset >= len(ret_str):
59  ret_str = ''
60  elif file_name == 'status':
61  ret_str = vnfs_ops.vnfs_get_status(nf_path)
62  if offset >= len(ret_str):
63  ret_str = ''
64  else:
65  os.lseek(fh, offset, os.SEEK_SET)
66  ret_str = os.read(fh, length)
67  return ret_str
68 
69 
70 def _write(root, path, buf, offset, fh):
71  f_path = full_path(root, path)
72  vnfs_ops = VNFSOperations(root)
73  file_name = vnfs_ops.vnfs_get_file_name(f_path)
74  if file_name == "action":
75  path_tokens = f_path.split("/")
76  nf_path = "/".join(path_tokens[0:path_tokens.index("nf-types") + 3])
77  if buf.rstrip("\n") == "activate":
78  vnfs_ops.vnfs_deploy_nf(nf_path)
79  elif buf.rstrip("\n") == "stop":
80  vnfs_ops.vnfs_stop_vnf(nf_path)
81  elif buf.rstrip("\n") == "start":
82  vnfs_ops.vnfs_start_vnf(nf_path)
83  elif buf.rstrip("\n") == "destroy":
84  vnfs_ops.vnfs_destroy_vnf(nf_path)
85  os.lseek(fh, offset, os.SEEK_SET)
86  os.write(fh, buf.rstrip("\n"))
87  return len(buf)
88  else:
89  os.lseek(fh, offset, os.SEEK_SET)
90  return os.write(fh, buf)