1bfd7d7632d925c5cf689081e1fa42a9f072fd67
[osm/SO.git] / common / python / rift / mano / config_data / test / test_converter.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import pytest
19 import uuid
20 from gi.repository import NsdYang, VnfdYang
21 from ..config import ConfigPrimitiveConvertor
22 import yaml
23
24 @pytest.fixture(scope="function")
25 def nsd():
26 catalog = NsdYang.YangData_Nsd_NsdCatalog()
27 nsd = catalog.nsd.add()
28 nsd.id = str(uuid.uuid1())
29 return nsd
30
31 @pytest.fixture(scope="function")
32 def vnfd():
33 catalog = VnfdYang.YangData_Vnfd_VnfdCatalog()
34 vnfd = catalog.vnfd.add()
35 vnfd.id = str(uuid.uuid1())
36 return vnfd
37
38 @pytest.fixture(scope="session")
39 def convertor():
40 return ConfigPrimitiveConvertor()
41
42 def test_nsd_config(nsd, convertor):
43 nsd.service_primitive.add().from_dict(
44 {
45 "parameter_group": [
46 {
47 "parameter": [
48 {
49 "data_type": "INTEGER",
50 "default_value": "3000",
51 "name": "Vlan ID",
52 "mandatory": True
53 }
54 ],
55 "name": "PE1",
56 "mandatory": False
57 },
58 {
59 "parameter": [
60 {
61 "data_type": "INTEGER",
62 "default_value": "3000",
63 "name": "Vlan ID",
64 "mandatory": True
65 }
66 ],
67 "name": "PE2",
68 "mandatory": False
69 }
70 ],
71 "parameter": [
72 {
73 "data_type": "INTEGER",
74 "default_value": "10",
75 "name": "Tunnel Key",
76 "mandatory": True,
77 }
78 ],
79 "name": "Add SP Test Corporation",
80 "user_defined_script": "add_corporation.py"
81 })
82
83 expected_yaml = """Add SP Test Corporation:
84 parameter:
85 Tunnel Key: '10'
86 parameter_group:
87 PE1:
88 Vlan ID: '3000'
89 PE2:
90 Vlan ID: '3000'
91 """
92
93 assert expected_yaml == \
94 convertor.extract_nsd_config(nsd)
95
96
97 def test_nsd_multiple_config(nsd, convertor):
98 nsd.service_primitive.add().from_dict(
99 {
100 "parameter_group": [{
101 "parameter": [
102 {
103 "data_type": "INTEGER",
104 "default_value": "3000",
105 "name": "Vlan ID",
106 "mandatory": True
107 }
108 ],
109 "name": "PE1",
110 "mandatory": False
111 }],
112 "parameter": [
113 {
114 "data_type": "INTEGER",
115 "default_value": "10",
116 "name": "Tunnel Key",
117 "mandatory": True,
118 }
119 ],
120 "name": "Add SP Test Corporation",
121 "user_defined_script": "add_corporation.py"
122 })
123
124 nsd.service_primitive.add().from_dict(
125 {
126 "parameter_group": [{
127 "parameter": [
128 {
129 "data_type": "INTEGER",
130 "default_value": "3000",
131 "name": "Vlan ID",
132 "mandatory": True
133 }
134 ],
135 "name": "PE2",
136 "mandatory": False
137 }],
138 "parameter": [
139 {
140 "data_type": "INTEGER",
141 "default_value": "10",
142 "name": "Tunnel Key",
143 "mandatory": True,
144 }
145 ],
146 "name": "Add SP Test Corporation 2",
147 "user_defined_script": "add_corporation.py"
148 })
149
150 expected_yaml = """Add SP Test Corporation:
151 parameter:
152 Tunnel Key: '10'
153 parameter_group:
154 PE1:
155 Vlan ID: '3000'
156 Add SP Test Corporation 2:
157 parameter:
158 Tunnel Key: '10'
159 parameter_group:
160 PE2:
161 Vlan ID: '3000'
162 """
163
164 assert yaml.load(expected_yaml) == \
165 yaml.load(convertor.extract_nsd_config(nsd))
166
167
168 def test_vnfd_config(vnfd, convertor):
169 vnf_config = vnfd.vnf_configuration
170
171 # Set the initital-config
172 vnf_config.initial_config_primitive.add().from_dict({
173 "seq": 1,
174 "name": "config",
175 "parameter": [
176 {"name": "vpe-router", "value": "<rw_mgmt_ip>"},
177 {"name": "user", "value": "root"},
178 {"name": "pass", "value": "6windos"}
179 ]
180 })
181
182 vnf_config.initial_config_primitive.add().from_dict({
183 "name": "configure-interface",
184 "seq": 2,
185 "parameter": [
186 {"value": "10.10.10.2/30", "name": "cidr"}
187 ],
188 })
189
190 expected_yaml = """initial_config_primitive:
191 config:
192 parameter:
193 pass: 6windos
194 user: root
195 vpe-router: <rw_mgmt_ip>
196 configure-interface:
197 parameter:
198 cidr: 10.10.10.2/30
199 """
200
201 assert expected_yaml == convertor.extract_vnfd_config(vnfd)
202
203 def test_vnfd_config_prim(vnfd, convertor):
204 vnf_config = vnfd.vnf_configuration
205
206 # Set the initital-config
207 vnf_config.initial_config_primitive.add().from_dict({
208 "seq": 1,
209 "name": "config",
210 "parameter": [
211 {"name": "vpe-router", "value": "<rw_mgmt_ip>"},
212 {"name": "user", "value": "root"},
213 {"name": "pass", "value": "6windos"}
214 ]
215 })
216
217 vnf_config.initial_config_primitive.add().from_dict({
218 "name": "configure-interface",
219 "seq": 2,
220 "parameter": [
221 {"value": "10.10.10.2/30", "name": "cidr"}
222 ],
223 })
224
225 vnf_config.service_primitive.add().from_dict({
226 "name": "PE1",
227 "parameter": [
228 {"name": "Foo", "default_value": "Bar"}
229 ]
230 })
231
232 expected_yaml = """service_primitive:
233 PE1:
234 parameter:
235 Foo: Bar
236 initial_config_primitive:
237 config:
238 parameter:
239 pass: 6windos
240 user: root
241 vpe-router: <rw_mgmt_ip>
242 configure-interface:
243 parameter:
244 cidr: 10.10.10.2/30
245 """
246
247 assert expected_yaml == convertor.extract_vnfd_config(vnfd)
248
249
250
251 def test_vnfd_merge(vnfd, convertor):
252 vnf_config = vnfd.vnf_configuration
253
254 # Set the initital-config
255 vnf_config.initial_config_primitive.add().from_dict({
256 "seq": 1,
257 "name": "config",
258 "parameter": [{"name": "vpe-router"},
259 {"name": "user"},
260 {"name": "pass"}
261 ]
262 })
263
264 vnf_config.initial_config_primitive.add().from_dict({
265 "name": "configure-interface",
266 "seq": 2,
267 "parameter": [{"name": "cidr"}],
268 })
269
270 vnf_config.service_primitive.add().from_dict({
271 "name": "PE1",
272 "parameter": [{"name": "Foo",}]
273 })
274
275 ip_yaml = """service_primitive:
276 PE1:
277 parameter:
278 Foo: Bar
279 initial_config_primitive:
280 config:
281 parameter:
282 pass: 6windos
283 user: root
284 vpe-router: <rw_mgmt_ip>
285 configure-interface:
286 parameter:
287 cidr: 10.10.10.2/30
288 """
289
290 catalog = VnfdYang.YangData_Vnfd_VnfdCatalog()
291 expected_vnfd = catalog.vnfd.add()
292 vnf_config = expected_vnfd.vnf_configuration
293 expected_vnfd.id = vnfd.id
294
295 # Set the initital-confi
296 vnf_config.initial_config_primitive.add().from_dict({
297 "seq": 1,
298 "name": "config",
299 "parameter": [
300 {"name": "vpe-router", "value": "<rw_mgmt_ip>"},
301 {"name": "user", "value": "root"},
302 {"name": "pass", "value": "6windos"}
303 ]
304 })
305
306 vnf_config.initial_config_primitive.add().from_dict({
307 "name": "configure-interface",
308 "seq": 2,
309 "parameter": [
310 {"value": "10.10.10.2/30", "name": "cidr"}
311 ],
312 })
313
314 vnf_config.service_primitive.add().from_dict({
315 "name": "PE1",
316 "parameter": [
317 {"name": "Foo", "default_value": "Bar"}
318 ]
319 })
320
321 convertor.merge_vnfd_config(vnfd, yaml.load(ip_yaml))
322
323 assert vnfd.as_dict() == expected_vnfd.as_dict()
324
325
326 def test_nsd_merge(nsd, convertor):
327 nsd.service_primitive.add().from_dict(
328 {
329 "parameter_group": [
330 {
331 "parameter": [
332 {
333 "data_type": "INTEGER",
334 "default_value": "3000",
335 "name": "Vlan ID",
336 "mandatory": True
337 }
338 ],
339 "name": "PE1",
340 "mandatory": False
341 },
342 {
343 "parameter": [
344 {
345 "data_type": "INTEGER",
346 "default_value": "3000",
347 "name": "Vlan ID",
348 "mandatory": True
349 }
350 ],
351 "name": "PE2",
352 "mandatory": False
353 }
354 ],
355 "parameter": [
356 {
357 "data_type": "INTEGER",
358 "default_value": "10",
359 "name": "Tunnel Key",
360 "mandatory": True,
361 }
362 ],
363 "name": "Add SP Test Corporation",
364 "user_defined_script": "add_corporation.py"
365 })
366
367 ip_yaml = """Add SP Test Corporation:
368 parameter:
369 Tunnel Key: '10'
370 parameter_group:
371 PE1:
372 Vlan ID: '3000'
373 PE2:
374 Vlan ID: '3000'
375 """
376
377 catalog = NsdYang.YangData_Nsd_NsdCatalog()
378 expected_nsd = catalog.nsd.add()
379 expected_nsd.id = nsd.id
380 expected_nsd.service_primitive.add().from_dict(
381 {
382 "parameter_group": [
383 {
384 "parameter": [
385 {
386 "data_type": "INTEGER",
387 "default_value": "3000",
388 "name": "Vlan ID",
389 "mandatory": True
390 }
391 ],
392 "name": "PE1",
393 "mandatory": False
394 },
395 {
396 "parameter": [
397 {
398 "data_type": "INTEGER",
399 "default_value": "3000",
400 "name": "Vlan ID",
401 "mandatory": True
402 }
403 ],
404 "name": "PE2",
405 "mandatory": False
406 }
407 ],
408 "parameter": [
409 {
410 "data_type": "INTEGER",
411 "default_value": "10",
412 "name": "Tunnel Key",
413 "mandatory": True,
414 }
415 ],
416 "name": "Add SP Test Corporation",
417 "user_defined_script": "add_corporation.py"
418 })
419
420 convertor.merge_nsd_config(nsd, yaml.load(ip_yaml))
421
422 assert nsd.as_dict() == expected_nsd.as_dict()
423
424