2 # Copyright 2018 EveryUP Srl
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 # from lib.rdcl_graph import RdclGraph
22 logging
.basicConfig(level
=logging
.DEBUG
)
23 log
= logging
.getLogger('OsmParser')
26 class RdclGraph(object):
27 """ Operates on the graph representation used for the GUI graph views """
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
):
52 edge_obj
.update(optional
)
53 if edge_obj
not in graph_object
['edges']:
54 graph_object
['edges'].append(edge_obj
)
56 def add_node(self
, id, type, group
, positions
, graph_object
, optional
={}):
59 node
= next((x
for x
in graph_object
['vertices'] if x
['id'] == id), None)
61 node
['info']['group'].append(group
)
63 node
= copy
.deepcopy(self
.node_t3d_base
)
65 node
['info']['type'] = type
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)
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:
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
88 class OsmParser(RdclGraph
):
89 """ Operates on the graph representation used for the GUI graph views """
91 def nsr_to_graph(self
, nsr_full
):
93 graph
= {'vertices': [], 'edges': [], 'model': {
117 "callback": "addLink",
118 "direct_edge": False,
126 "callback": "addLink",
127 "direct_edge": False,
149 "direct_edge": False,
152 "direct_edge": False,
155 "direct_edge": False,
162 "direct_edge": False,
169 "direct_edge": False,
172 "direct_edge": False,
189 }, 'graph_parameters': {'view': {'nsr': {}, 'vnfr': {}}}}
191 nsr
= nsr_full
['nsr']
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
204 map_vnf_index_to_id
= {}
205 for vnfr_id
in nsr
['constituent-vnfr-ref']:
206 current_vnfr
= nsr_full
['vnfr'][vnfr_id
]
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']
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
})
222 for cp
in current_vnfr
['connection-point']:
224 cp_id
= vnfr_label
+ ':' + cp
['id']
225 self
.add_node(cp_id
, 'cp', vnfr_id
, None, graph
, {'osm': cp
})
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
)
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
)
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,
265 if __name__
== '__main__':
267 print parser
.nsr_to_graph({})