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