[wip] NS instance topology view
[osm/LW-UI.git] / lib / osm / osm_rdcl_parser.py
1 #
2 # Copyright 2018 EveryUP Srl
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17
18 import logging
19 # from lib.rdcl_graph import RdclGraph
20 import copy
21
22 logging.basicConfig(level=logging.DEBUG)
23 log = logging.getLogger('OsmParser')
24
25
26 class RdclGraph(object):
27 """ Operates on the graph representation used for the GUI graph views """
28 node_ids = []
29 node_t3d_base = {
30 'info': {
31 'property': {
32 'custom_label': '',
33 },
34 'type': '',
35 'group': []
36 }
37 }
38
39 def __init__(self):
40 pass
41
42 def add_link(self, source, target, view, group, graph_object, optional={}):
43 if (source is None or source not in self.node_ids) or (target is None or target not in self.node_ids):
44 return
45 edge_obj = {
46 'source': source,
47 'target': target,
48 'view': view,
49 'group': [group],
50 }
51
52 edge_obj.update(optional)
53 if edge_obj not in graph_object['edges']:
54 graph_object['edges'].append(edge_obj)
55
56 def add_node(self, id, type, group, positions, graph_object, optional={}):
57 if id is None:
58 return
59 node = next((x for x in graph_object['vertices'] if x['id'] == id), None)
60 if node is not None:
61 node['info']['group'].append(group)
62 else:
63 node = copy.deepcopy(self.node_t3d_base)
64 node['id'] = id
65 node['info']['type'] = type
66 if group is not None:
67 node['info']['group'].append(group)
68 if positions and id in positions['vertices'] and 'x' in positions['vertices'][id] and 'y' in \
69 positions['vertices'][id]:
70 node['fx'] = positions['vertices'][id]['x']
71 node['fy'] = positions['vertices'][id]['y']
72 node['info'].update(optional)
73 graph_object['vertices'].append(node)
74 self.node_ids.append(id)
75
76 def is_directed_edge(self, source_type=None, target_type=None, layer=None, model={}):
77 if source_type is None or target_type is None or layer is None:
78 return None
79 if layer in model['layer'] and 'allowed_edges' in model['layer'][layer]:
80 if source_type in model['layer'][layer]['allowed_edges'] and target_type in \
81 model['layer'][layer]['allowed_edges'][source_type]['destination']:
82 edge_pro = model['layer'][layer]['allowed_edges'][source_type]['destination'][target_type]
83 return edge_pro['direct_edge'] if 'direct_edge' in edge_pro else False
84
85 return None
86
87
88 class OsmParser(RdclGraph):
89 """ Operates on the graph representation used for the GUI graph views """
90
91 def nsr_to_graph(self, nsr_full):
92
93 graph = {'vertices': [], 'edges': [], 'model': {
94 "layer": {
95
96 "nsr": {
97 "nodes": {
98 "vnfr": {
99 "addable": {},
100 "removable": {},
101 "expands": "vnfr"
102 },
103 "ns_vl": {
104 "addable": {},
105 "removable": {}
106 },
107 "ns_cp": {
108 "addable": {},
109 "removable": {}
110 },
111
112 },
113 "allowed_edges": {
114 "ns_vl": {
115 "destination": {
116 "vnfr": {
117 "callback": "addLink",
118 "direct_edge": False,
119 "removable": {}
120 }
121 }
122 },
123 "vnfr": {
124 "destination": {
125 "ns_vl": {
126 "callback": "addLink",
127 "direct_edge": False,
128 "removable": {}
129 },
130
131 }
132 }
133
134 }
135 },
136
137 "vnfr": {
138 "nodes": {
139 "vdur": {},
140 "cp": {},
141 "int_cp": {},
142 "vnf_vl": {}
143
144 },
145 "allowed_edges": {
146 "vdur": {
147 "destination": {
148 "cp": {
149 "direct_edge": False,
150 },
151 "int_cp": {
152 "direct_edge": False,
153 },
154 "vnf_vl": {
155 "direct_edge": False,
156 }
157 }
158 },
159 "cp": {
160 "destination": {
161 "vdur": {
162 "direct_edge": False,
163 }
164 }
165 },
166 "int_cp": {
167 "destination": {
168 "vdur": {
169 "direct_edge": False,
170 },
171 "vnf_vl": {
172 "direct_edge": False,
173 }
174 }
175 },
176 "vnf_vl": {
177 "destination": {
178 "vdur": {
179 "direct_edge": False
180 }
181 }
182 }
183 }
184 },
185 "name": "OSM",
186 "version": 1,
187 "description": "osm"
188 }
189 }, 'graph_parameters': {'view': {'nsr': {}, 'vnfr': {}}}}
190
191 nsr = nsr_full['nsr']
192
193 graph['graph_parameters']['view']['nsr'] = {}
194 nsr_graph_param = graph['graph_parameters']['view']['nsr']
195 nsr_graph_param['id'] = nsr['_id'] if '_id' in nsr else None
196 nsr_graph_param['nsdId'] = nsr['nsdId'] if 'nsdId' in nsr else None
197 nsr_graph_param['name-ref'] = nsr['name-ref'] if 'name-ref' in nsr else None
198 nsr_graph_param['operational-status'] = nsr['operational-status'] if 'operational-status' in nsr else None
199 nsr_graph_param['config-status'] = nsr['config-status'] if 'config-status' in nsr else None
200 nsr_graph_param['detailed-status'] = nsr['detailed-status'] if 'detailed-status' in nsr else None
201 nsr_graph_param['create-time'] = nsr['create-time'] if 'create-time' in nsr else None
202 nsr_graph_param['instantiate_params'] = nsr['instantiate_params'] if 'instantiate_params' in nsr else None
203
204 map_vnf_index_to_id = {}
205 for vnfr_id in nsr['constituent-vnfr-ref']:
206 current_vnfr = nsr_full['vnfr'][vnfr_id]
207
208 graph['graph_parameters']['view']['vnfr'][vnfr_id] = {}
209 vnfr_graph_param = graph['graph_parameters']['view']['vnfr'][vnfr_id]
210 vnfr_graph_param['id'] = vnfr_id
211 vnfr_graph_param['vnfd-id'] = current_vnfr['vnfd-id']
212 vnfr_graph_param['vnfd-ref'] = current_vnfr['vnfd-ref']
213 vnfr_graph_param['member-vnf-index-ref'] = current_vnfr['member-vnf-index-ref']
214 vnfr_graph_param['vim-account-id'] = current_vnfr['vim-account-id']
215 vnfr_graph_param['created-time'] = current_vnfr['created-time']
216
217 vnfr_label = current_vnfr['vnfd-ref'] + ':' + current_vnfr['member-vnf-index-ref']
218 map_vnf_index_to_id[current_vnfr['member-vnf-index-ref']] = vnfr_id
219 self.add_node(vnfr_id, 'vnfr', None, None, graph,
220 {'property': {'custom_label': vnfr_label}, 'osm': current_vnfr})
221
222 for cp in current_vnfr['connection-point']:
223 if cp['id']:
224 cp_id = vnfr_label + ':' + cp['id']
225 self.add_node(cp_id, 'cp', vnfr_id, None, graph, {'osm': cp})
226
227 for vdur in current_vnfr['vdur']:
228 vdur_id = vnfr_label + ':' + vdur['vdu-id-ref']
229 self.add_node(vdur_id, 'vdur', vnfr_id, None, graph, {'osm': vdur})
230 if current_vnfr['vnfd-id'] in nsr_full['vnfd']:
231 for vdu in nsr_full['vnfd'][current_vnfr['vnfd-id']]['vdu']:
232 if vdu['id'] == vdur['vdu-id-ref']:
233 if 'internal-connection-point' in vdu:
234 for int_cp in vdu['internal-connection-point']:
235 cp_id = vnfr_label + ':' + int_cp['id']
236 self.add_node(cp_id, 'int_cp', vnfr_id, None, graph, {'osm': int_cp})
237 for interface in vdu['interface']:
238 if interface['type'] == "EXTERNAL":
239 cp_id = vnfr_label + ':' + interface['external-connection-point-ref']
240 self.add_link(cp_id, vdur_id, 'vnfr', vnfr_id, graph)
241 elif interface['type'] == "INTERNAL":
242 cp_id = vnfr_label + ':' + interface['internal-connection-point-ref']
243 self.add_link(cp_id, vdur_id, 'vnfr', vnfr_id, graph)
244
245 if current_vnfr['vnfd-id'] in nsr_full['vnfd'] and 'internal-vld' in nsr_full['vnfd'][
246 current_vnfr['vnfd-id']]:
247 for vnfd_vld in nsr_full['vnfd'][current_vnfr['vnfd-id']]['internal-vld']:
248 vld_id = vnfr_label + ':' + vnfd_vld['id']
249 self.add_node(vld_id, 'vnf_vl', vnfr_id, None, graph, {'osm': vnfd_vld})
250 if vnfd_vld['internal-connection-point']:
251 for int_cp in vnfd_vld['internal-connection-point']:
252 int_cp_id = vnfr_label + ':' + int_cp['id-ref']
253 self.add_link(vld_id, int_cp_id, 'vnfr', vnfr_id, graph)
254
255 for ns_vld in nsr['nsd']['vld']:
256 self.add_node(ns_vld['id'], 'ns_vl', None, None, graph,
257 {'property': {'custom_label': ns_vld['name']}, 'osm': ns_vld})
258 for cp_ref in ns_vld['vnfd-connection-point-ref']:
259 self.add_link(map_vnf_index_to_id[str(cp_ref['member-vnf-index-ref'])], ns_vld['id'], 'nsr', None,
260 graph)
261
262 return graph
263
264
265 if __name__ == '__main__':
266 parser = OsmParser()
267 print parser.nsr_to_graph({})