fix composer bug
[osm/LW-UI.git] / lib / osm / osm_util.py
1 class OsmUtil():
2
3 @staticmethod
4 def remove_node(descriptor_type, descriptor, node_type, element_id, args):
5 if descriptor_type == 'nsd':
6 if 'nsd-catalog' in descriptor:
7 nsd = descriptor['nsd-catalog']['nsd'][0]
8 elif 'nsd:nsd-catalog' in descriptor:
9 nsd = descriptor['nsd:nsd-catalog']['nsd'][0]
10
11 if node_type == 'ns_vl':
12 for k, v in enumerate(nsd['vld']):
13 if v['id'] == args['id']:
14 nsd['vld'].pop(k)
15 elif node_type == 'vnf':
16 for k, v in enumerate(nsd['constituent-vnfd']):
17 if str(v['member-vnf-index']) == str(args['member-vnf-index']) and str(v['vnfd-id-ref']) == str(
18 args['vnfd-id-ref']):
19 nsd['constituent-vnfd'].pop(k)
20 for j, vld in enumerate(nsd['vld']):
21 vld['vnfd-connection-point-ref'] = [item for item in vld['vnfd-connection-point-ref'] if
22 str(item['member-vnf-index-ref']) != str(
23 args['member-vnf-index']) or str(
24 item['vnfd-id-ref']) != str(args['vnfd-id-ref'])]
25 elif node_type == 'ns_cp':
26 for vld in nsd['vld']:
27 if vld['id'] == args['vld_id']:
28 vld['vnfd-connection-point-ref'] = [item for item in vld['vnfd-connection-point-ref'] if
29 str(item['member-vnf-index-ref']) != str(
30 args['member-vnf-index-ref']) or str(
31 item['vnfd-id-ref']) != str(args['vnfd-id-ref'])]
32 elif descriptor_type == 'vnfd':
33 if 'vnfd-catalog' in descriptor:
34 vnfd = descriptor['vnfd-catalog']['vnfd'][0]
35 elif 'vnfd:vnfd-catalog' in descriptor:
36 vnfd = descriptor['vnfd:vnfd-catalog']['vnfd'][0]
37
38 if node_type == 'vnf_vl':
39 vnfd['internal-vld'] = [item for item in vnfd['internal-vld'] if item['id'] != element_id]
40 if node_type == 'cp':
41 vnfd['connection-point'] = [item for item in vnfd['connection-point'] if item['name'] != args['name']]
42 if vnfd['mgmt-interface']['cp'] == args['name']:
43 del vnfd['mgmt-interface']['cp']
44 for vdu in vnfd['vdu']:
45 if 'interface' in vdu:
46 vdu['interface'] = [item for item in vdu['interface'] if 'external-connection-point-ref' not in item
47 or ('external-connection-point-ref'in item and item['external-connection-point-ref'] != args['name'])]
48 if node_type == 'vdu':
49 vdus = []
50 for vdu in vnfd['vdu']:
51 if vdu['name'] != element_id:
52 vdus.append(vdu)
53 else:
54 for intcp in vdu['internal-connection-point']:
55 for intVld in vnfd['internal-vld']:
56 intVld['internal-connection-point'] = [item for item in intVld['internal-connection-point']
57 if item['id-ref'] != intcp['id']]
58 vnfd['vdu'] = vdus
59 if node_type == 'int_cp':
60 OsmUtil.remove_int_cp(element_id, vnfd)
61 return descriptor
62
63 @staticmethod
64 def update_node(descriptor_type, descriptor, node_type, old, updated):
65 if descriptor_type == 'nsd':
66 if 'nsd-catalog' in descriptor:
67 nsd = descriptor['nsd-catalog']['nsd'][0]
68 elif 'nsd:nsd-catalog' in descriptor:
69 nsd = descriptor['nsd:nsd-catalog']['nsd'][0]
70
71 if node_type == 'ns_vl':
72 for k, v in enumerate(nsd['vld']):
73 if v['id'] == old['id']:
74 nsd['vld'][k].update(updated)
75 elif node_type == 'vnf':
76 for k, v in enumerate(nsd['constituent-vnfd']):
77 if str(v['member-vnf-index']) == str(old['member-vnf-index']) and str(v['vnfd-id-ref']) == str(
78 old['vnfd-id-ref']):
79 print 'update here'
80 elif descriptor_type == 'vnfd':
81 if 'vnfd-catalog' in descriptor:
82 vnfd = descriptor['vnfd-catalog']['vnfd'][0]
83 elif 'vnfd:vnfd-catalog' in descriptor:
84 vnfd = descriptor['vnfd:vnfd-catalog']['vnfd'][0]
85
86 if node_type == 'vnf_vl':
87 for k, v in enumerate(vnfd['internal-vld']):
88 if v['id'] == old['id']:
89 vnfd['internal-vld'][k].update(updated)
90 if node_type == 'cp':
91 for k, v in enumerate(vnfd['connection-point']):
92 if v['name'] == old['name']:
93 vnfd['connection-point'][k].update(updated)
94 for vdu in vnfd['vdu']:
95 if 'interface' in vdu:
96 for intf in vdu['interface']:
97 if 'external-connection-point-ref' in intf and intf['external-connection-point-ref'] == old['name']:
98 intf['external-connection-point-ref'] = updated['name']
99 if node_type == 'vdu':
100 for k, v in enumerate(vnfd['vdu']):
101 if v['name'] == old['name']:
102 vnfd['vdu'][k].update(updated)
103
104 return descriptor
105
106 @staticmethod
107 def add_base_node(descriptor_type, descriptor, node_type, element_id, args):
108 if descriptor_type == 'nsd':
109 if 'nsd-catalog' in descriptor:
110 nsd = descriptor['nsd-catalog']['nsd'][0]
111 elif 'nsd:nsd-catalog' in descriptor:
112 nsd = descriptor['nsd:nsd-catalog']['nsd'][0]
113 if node_type == 'ns_vl':
114 nsd['vld'].append({
115 "vim-network-name": "PUBLIC",
116 "name": element_id,
117 "vnfd-connection-point-ref": [],
118 "mgmt-network": "true",
119 "type": "ELAN",
120 "id": element_id
121 })
122 if node_type == 'vnf':
123 indexes = []
124 for cvnfd in nsd['constituent-vnfd']:
125 indexes.append(int(cvnfd["member-vnf-index"]))
126 memberindex = max(indexes) + 1 if len(indexes) > 0 else 1
127 nsd['constituent-vnfd'].append({
128 "member-vnf-index": memberindex,
129 "vnfd-id-ref": element_id
130 })
131 if node_type == 'cp':
132 for vld in nsd['vld']:
133 if vld['id'] == args['vld_id']:
134 if 'vnfd-connection-point-ref' not in vld:
135 vld['vnfd-connection-point-ref'] = []
136 vld['vnfd-connection-point-ref'].append(
137 {
138 "vnfd-connection-point-ref": args['vnfd-connection-point-ref'],
139 "member-vnf-index-ref": args['member-vnf-index-ref'],
140 "vnfd-id-ref": args['vnfd-id-ref']
141 },
142 )
143
144 elif descriptor_type == 'vnfd':
145 if 'vnfd-catalog' in descriptor:
146 vnfd = descriptor['vnfd-catalog']['vnfd'][0]
147 elif 'vnfd:vnfd-catalog' in descriptor:
148 vnfd = descriptor['vnfd:vnfd-catalog']['vnfd'][0]
149 if node_type == 'vdu':
150 if 'vdu' not in vnfd:
151 vnfd['vdu'] = []
152 vnfd['vdu'].append({
153 "count": "1",
154 "description": "",
155 "monitoring-param": [],
156 "internal-connection-point": [],
157 "image": "ubuntu",
158 "cloud-init-file": "",
159 "vm-flavor": {},
160 "interface": [],
161 "id": element_id,
162 "name": element_id
163 })
164 if node_type == 'cp':
165 if 'connection-point' not in vnfd:
166 vnfd['connection-point'] = []
167 vnfd['connection-point'].append({
168 "type": "VPORT",
169 "name": element_id
170 })
171
172 if node_type == 'vnf_vl':
173 if 'internal-vld' not in vnfd:
174 vnfd['internal-vld'] = []
175 vnfd['internal-vld'].append({
176 "short-name": element_id,
177 "name": element_id,
178 "internal-connection-point": [],
179 "type": "ELAN",
180 "ip-profile-ref": "",
181 "id": element_id
182 })
183 if node_type == 'interface':
184 for vdu in vnfd['vdu']:
185 if vdu['id'] == args['vdu_id']:
186 vdu['interface'].append({
187 "virtual-interface": {
188 "type": "VIRTIO"
189 },
190 "name": args["name"],
191 "mgmt-interface": True,
192 "type": "EXTERNAL",
193 "external-connection-point-ref": args["external-connection-point-ref"]
194 })
195 if node_type == 'int_cp':
196 for vdu in vnfd['vdu']:
197 if vdu['id'] == args['vdu_id']:
198 if 'internal-connection-point' not in vdu:
199 vdu['internal-connection-point'] = []
200 vdu['internal-connection-point'].append({
201 "short-name": element_id,
202 "type": "VPORT",
203 "id": element_id,
204 "name": element_id
205 })
206 if 'interface' not in vdu:
207 vdu['interface'] = []
208 vdu['interface'].append({
209 "virtual-interface": {
210 "type": "VIRTIO"
211 },
212 "name": "int_"+element_id,
213 "type": "INTERNAL",
214 "internal-connection-point-ref": element_id
215 })
216 for int_vld in vnfd['internal-vld']:
217 if int_vld['id'] == args['vld_id']:
218 if 'internal-connection-point' not in int_vld:
219 int_vld['internal-connection-point'] = []
220 int_vld['internal-connection-point'].append({
221 'id-ref': element_id
222 })
223 return descriptor
224
225 @staticmethod
226 def update_graph_params(descriptor_type, descriptor, updated):
227 if descriptor_type == 'nsd':
228 if 'nsd-catalog' in descriptor:
229 nsd = descriptor['nsd-catalog']['nsd'][0]
230 elif 'nsd:nsd-catalog' in descriptor:
231 nsd = descriptor['nsd:nsd-catalog']['nsd'][0]
232 nsd.update(updated)
233 elif descriptor_type == 'vnfd':
234 if 'vnfd-catalog' in descriptor:
235 vnfd = descriptor['vnfd-catalog']['vnfd'][0]
236 elif 'vnfd:vnfd-catalog' in descriptor:
237 vnfd = descriptor['vnfd:vnfd-catalog']['vnfd'][0]
238 vnfd.update(updated)
239
240 return descriptor
241
242 @staticmethod
243 def remove_int_cp(element_id, vnfd):
244 for vdu in vnfd['vdu']:
245 if 'interface' in vdu:
246 vdu['interface'] = [item for item in vdu['interface'] if 'internal-connection-point-ref' not in item
247 or ('internal-connection-point-ref' in item and item[
248 'internal-connection-point-ref'] != element_id)]
249 if 'internal-connection-point' in vdu:
250 vdu['internal-connection-point'] = [item for item in vdu['internal-connection-point'] if
251 item['id'] != element_id]
252 for intVld in vnfd['internal-vld']:
253 intVld['internal-connection-point'] = [item for item in intVld['internal-connection-point'] if
254 item['id-ref'] != element_id]
255
256 return vnfd