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