Merge "fix nslcm middleware"
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_sol_ns_instantiate_api.py
1 # Copyright 2019 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import json
16 import mock
17 import uuid
18 from mock import MagicMock
19 from django.test import TestCase
20 from rest_framework import status
21 from rest_framework.test import APIClient
22 from lcm.ns.biz.ns_instant import BuildInWorkflowThread
23 from lcm.ns.biz.ns_instant import InstantNSService
24 from lcm.pub.database.models import NSInstModel
25 from lcm.pub.utils import restcall
26 from lcm.pub.config import config
27
28 nsd_model = json.dumps({
29     "model": json.dumps({
30         "vnfs": [{
31             "vnf_id": "vnf1",
32             "properties": {
33                 "id": "vnfd1",
34                 "nf_type": "xgw"
35             },
36             "dependencies": [{
37                 "vl_id": "5"
38             }]
39         }],
40         "vls": [{
41             "vl_id": "5",
42             "properties": {}
43         }]
44     })
45 })
46
47 nsd = json.dumps({
48     "vnffgs": [
49     ],
50     "inputs": {
51     },
52     "pnfs": [
53         {
54             "pnf_id": "du",
55             "networks": [
56             ],
57             "description":"",
58             "properties":{
59                 "descriptor_id": "zte_ran_du_0001",
60                 "descriptor_invariant_id": "1111",
61                 "provider": "ZTE",
62                 "version": "1.0",
63                 "function_description": "RAN DU Function",
64                 "name": "ZTE RAN DU"
65             }
66         }
67     ],
68     "ns_exposed": {
69         "external_cps": [
70         ],
71         "forward_cps": [
72         ]
73     },
74     "graph": {
75         "cucp": [
76         ],
77         "du": [
78         ],
79         "vl_flat_net": [
80             "cucp",
81             "cuup"
82         ],
83         "vl_ext_net": [
84             "cucp",
85             "cuup"
86         ],
87         "cuup": [
88         ]
89     },
90     "basepath": "c:\\users\\10030173\\appdata\\local\\temp\\tmpvg5vto",
91     "vnfs": [
92         {
93             "networks": [
94                 {
95                     "key_name": "ran_ext_net",
96                     "vl_id": "vl_ext_net"
97                 },
98                 {
99                     "key_name": "ran_flat_net",
100                     "vl_id": "vl_flat_net"
101                 }
102             ],
103             "dependencies": [
104                 {
105                     "key_name": "ran_ext_net",
106                     "vl_id": "vl_ext_net"
107                 },
108                 {
109                     "key_name": "ran_flat_net",
110                     "vl_id": "vl_flat_net"
111                 }
112             ],
113             "vnf_id": "cucp",
114             "description": "",
115             "properties": {
116                 "descriptor_id": "zte_ran_cucp_0001",
117                 "flavour_description": "default",
118                 "software_version": "1.0.1",
119                 "flavour_id": "1",
120                 "descriptor_version": "1.0",
121                 "provider": "ZTE",
122                 "id": "zte_ran_cucp_0001",
123                 "vnfm_info": [
124                     "GVNFM-Driver"
125                 ],
126                 "product_name":"ran"
127             }
128         },
129         {
130             "networks": [
131                 {
132                     "key_name": "ran_ext_net",
133                     "vl_id": "vl_ext_net"
134                 },
135                 {
136                     "key_name": "ran_flat_net",
137                     "vl_id": "vl_flat_net"
138                 }
139             ],
140             "dependencies": [
141                 {
142                     "key_name": "ran_ext_net",
143                     "vl_id": "vl_ext_net"
144                 },
145                 {
146                     "key_name": "ran_flat_net",
147                     "vl_id": "vl_flat_net"
148                 }
149             ],
150             "vnf_id": "cuup",
151             "description": "",
152             "properties": {
153                 "descriptor_id": "zte_ran_cuup_0001",
154                 "flavour_description": "default",
155                 "software_version": "1.0.1",
156                 "flavour_id": "1",
157                 "descriptor_version": "1.0",
158                 "provider": "ZTE",
159                 "id": "zte_ran_cuup_0001",
160                 "vnfm_info": [
161                     "GVNFM-Driver"
162                 ],
163                 "product_name":"ran"
164             }
165         }
166     ],
167     "fps": [
168     ],
169     "vls": [
170         {
171             "vl_id": "vl_ext_net",
172             "description": "",
173             "properties": {
174                 "connectivity_type": {
175                     "layer_protocol": "ipv4"
176                 },
177                 "vl_profile": {
178                     "cidr": "10.0.0.0/24",
179                     "max_bit_rate_requirements": {
180                         "root": 10000000,
181                         "leaf": 10000000
182                     },
183                     "networkName": "ran_ext_net",
184                     "min_bit_rate_requirements": {
185                         "root": 10000000,
186                         "leaf": 10000000
187                     },
188                     "dhcpEnabled": False
189                 },
190                 "version": "1.0.1"
191             }
192         },
193         {
194             "vl_id": "vl_flat_net",
195             "description": "",
196             "properties": {
197                 "connectivity_type": {
198                     "layer_protocol": "ipv4"
199                 },
200                 "vl_profile": {
201                     "cidr": "10.1.0.0/24",
202                     "max_bit_rate_requirements": {
203                         "root": 10000000,
204                         "leaf": 10000000
205                     },
206                     "networkName": "ran_flat_net",
207                     "min_bit_rate_requirements": {
208                         "root": 10000000,
209                         "leaf": 10000000
210                     },
211                     "dhcpEnabled": False
212                 },
213                 "version": "1.0.1"
214             }
215         }
216     ],
217     "nested_ns": [
218     ],
219     "metadata": {
220         "template_name": "RAN-NS",
221         "template_version": "1.0",
222         "template_author": "ZTE"
223     }
224 })
225 vnfminfo = {"vnfmId": "1"}
226
227
228 class TestInstantiateNsApi(TestCase):
229
230     def setUp(self):
231         self.client = APIClient()
232         NSInstModel.objects.filter().delete()
233         self.url = "/api/nslcm/v1/ns_instances/%s/instantiate"
234         self.req_data = {
235             "additionalParamForNs": {
236                 "location": "2"
237             },
238             "nsFlavourId": 'default',
239             "localizationLanguage": [{
240                 "vnfProfileId": "vnfd1",
241                 "locationConstraints": {
242                     "countryCode": "countryCode",
243                     "vimId": "3",
244                     "civicAddressElement": [
245                         {"caType": "type1",
246                          "caValue": 1
247                          }
248                     ]
249                 }
250             }]
251         }
252         self.nsd_model = nsd_model
253         self.updated_nsd_model = {
254             "vnfs": [{
255                 "dependencies": [{
256                     "vl_id": "5"
257                 }],
258                 "vnf_id": "vnf1",
259                 "properties": {
260                     "nf_type": "xgw",
261                     "id": "vnfd1"
262                 }
263             }],
264             "vls": [{
265                 "vl_id": "5",
266                 "properties": {
267                     "location_info": {
268                         "vimid": "3"
269                     }
270                 }
271             }]
272         }
273         self.vnfms = json.dumps({
274             "esr-vnfm": [{
275                 "vnfm-id": "4"
276             }]
277         })
278         self.vnfm = json.dumps({
279             "type": "xgw",
280             "vim-id": "3",
281             "vnfm-id": "4",
282             "certificate-url": "http://127.0.0.0/ztevnfm/v1/auth",
283             "esr-system-info-list": {
284                 "esr-system-info": [{
285                     "type": "xgw",
286                     "vendor": "zte",
287                     "version": "1.0",
288                     "service-url": "http://127.0.0.0/ztevnfm/v1",
289                     "user-name": "admin",
290                     "password": "admin123"
291                 }]
292             }
293         })
294         self.nsInstanceId = str(uuid.uuid4())
295         NSInstModel(id=self.nsInstanceId, nspackage_id="7", nsd_id="2", status="active").save()
296
297     def tearDown(self):
298         pass
299
300     @mock.patch.object(restcall, 'call_req')
301     @mock.patch('lcm.pub.msapi.sdc_run_catalog.parse_nsd', MagicMock(return_value=nsd_model))
302     @mock.patch.object(BuildInWorkflowThread, 'run')
303     def test_ns_instantiate_when_succeed_to_enter_workflow(self, mock_run, mock_call_req):
304         config.WORKFLOW_OPTION = "buildin"
305         mock_call_req.side_effect = [
306             [0, self.nsd_model, '200'],
307             [0, self.vnfms, '200'],
308             [0, self.vnfm, '200']
309         ]
310         response = self.client.post(self.url % self.nsInstanceId, data=self.req_data, format='json')
311         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)
312         self.assertIsNotNone(response['Location'])
313         response = self.client.get(response['Location'], format='json')
314         self.assertEqual(response.status_code, status.HTTP_200_OK)
315
316     @mock.patch.object(InstantNSService, 'do_biz')
317     def test_ns_instantiate_normal(self, mock_do_biz):
318         mock_do_biz.return_value = {'occ_id': "1"}
319         response = self.client.post(self.url % self.nsInstanceId, data=self.req_data, format='json')
320         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
321
322     @mock.patch.object(restcall, 'call_req')
323     def test_ns_instantiate_when_fail_to_parse_nsd(self, mock_call_req):
324         mock_call_req.return_value = [1, "Failed to parse nsd", '500']
325         resp = self.client.post(self.url % self.nsInstanceId, data=self.req_data, format='json')
326         self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
327
328     @mock.patch('lcm.ns.biz.ns_instantiate_flow.post_deal')
329     @mock.patch.object(restcall, 'call_req')
330     @mock.patch('lcm.ns.biz.ns_instantiate_flow.update_job')
331     @mock.patch('lcm.pub.msapi.sdc_run_catalog.parse_nsd', MagicMock(return_value=nsd))
332     @mock.patch('lcm.pub.msapi.extsys.select_vnfm', MagicMock(return_value=vnfminfo))
333     def test_ns_instantiate_with_pnf(self, mock_updata_job, mock_call_req, mock_post_deal):
334         config.WORKFLOW_OPTION = "grapflow"
335         nsInstanceId = str(uuid.uuid4())
336         NSInstModel(id=nsInstanceId, name="test_ns", nspackage_id="1", status="created").save()
337         ret = [0, json.JSONEncoder().encode({'jobId': "1", "responseDescriptor": {"progress": 100}}), '200']
338         mock_call_req.side_effect = [ret for i in range(1, 20)]
339         data = {
340             "nsFlavourId": 'default',
341             "additionalParamForNs": {
342                 "sdnControllerId": "2"
343             },
344             "additionalParamsForVnf": [
345                 {
346                     "vnfProfileId": "zte_ran_cucp_0001",
347                     "additionalParams": {"vimId": "3"}
348                 },
349                 {
350                     "vnfProfileId": "zte_ran_cuup_0001",
351                     "additionalParams": {"vimId": "3"}
352                 }
353             ],
354             "addpnfData": [{
355                 "pnfId": 1,
356                 "pnfName": "test_pnf",
357                 "pnfdId": "zte_ran_du_0001",
358                 "pnfProfileId": "du"
359             }]
360         }
361         response = self.client.post(self.url % nsInstanceId, data=data, format='json')
362         self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
363         self.assertIsNotNone(response['Location'])
364         response = self.client.get(response['Location'], format='json')
365         self.assertEqual(response.status_code, status.HTTP_200_OK)
366
367     def test_method_not_allowed(self):
368         response = self.client.put(self.url % self.nsInstanceId, data=self.req_data, format='json')
369         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
370         response = self.client.patch(self.url % self.nsInstanceId, data=self.req_data, format='json')
371         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
372         response = self.client.delete(self.url % self.nsInstanceId, data=self.req_data, format='json')
373         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
374         response = self.client.get(self.url % self.nsInstanceId, data=self.req_data, format='json')
375         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
376
377     @mock.patch.object(restcall, 'call_req')
378     @mock.patch('lcm.pub.msapi.sdc_run_catalog.parse_nsd', MagicMock(return_value=nsd_model))
379     @mock.patch.object(BuildInWorkflowThread, 'run')
380     def test_ns_instantiate_vcpe(self, mock_run, mock_call_req):
381         config.WORKFLOW_OPTION = "buildin"
382         vcpe_nsd_model = json.dumps({
383             "model": json.dumps({
384                 "vnfs": [{
385                     "vnf_id": "b1bb0ce7-2222-4fa7-95ed-4840d70a1101",
386                     "properties": {
387                         "id": "vnfd0",
388                         "nf_type": "test"
389                     },
390                     "dependencies": [{
391                         "vl_id": "5"
392                     }]
393                 },
394                     {
395                         "vnf_id": "0408f076-e6c0-4c82-9940-272fddbb82de",
396                         "properties": {
397                             "id": "vnfd1",
398                             "nf_type": "test"
399                         },
400                         "dependencies": [{
401                             "vl_id": "5"
402                         }]
403                 },
404                     {
405                         "vnf_id": "b1bb0ce7-2222-4fa7-95ed-4840d70a1100",
406                         "properties": {
407                             "id": "vnfd2",
408                             "nf_type": "test"
409                         },
410                         "dependencies": [{
411                             "vl_id": "5"
412                         }]
413                 },
414                     {
415                         "vnf_id": "b1bb0ce7-2222-4fa7-95ed-4840d70a1102",
416                         "properties": {
417                             "id": "vnfd3",
418                             "nf_type": "test"
419                         },
420                         "dependencies": [{
421                             "vl_id": "5"
422                         }]
423                 },
424                     {
425                         "vnf_id": "3fca3543-07f5-492f-812c-ed462e4f94f4",
426                         "properties": {
427                             "id": "vnfd4",
428                             "nf_type": "test"
429                         },
430                         "dependencies": [{
431                             "vl_id": "5"
432                         }]
433                 },
434                 ],
435                 "vls": [{
436                     "vl_id": "5",
437                     "properties": {}
438                 }]
439             })
440         })
441         mock_call_req.side_effect = [
442             [0, vcpe_nsd_model, '200'],
443             [0, self.vnfms, '200'],
444             [0, self.vnfm, '200']
445         ]
446         req_data = {
447             "nsFlavourId": 'default',
448             "aditionalParamsForNs": {
449                 "nfvo": "vfc",
450                 "nf_naming": "true",
451                 "multi_stage_design": "false",
452                 "availability_zone_max_count": "1",
453                 "xyz": "123",
454                 "nsd0_providing_service_invariant_uuid": "12204a12-7da2-4ddf-8c2f-992a1a1acebf",
455                 "nsd0_providing_service_uuid": "5791dbeb-19d4-43e8-bf44-5b327ccf6bca"
456             },
457             "additionalParamsForVnf": [
458                 {
459                     "vnfProfileId": "b1bb0ce7-2222-4fa7-95ed-4840d70a1101",
460                     "additionalParams": {
461                         "vimId": "CloudOwner_regionOne"
462                     }
463                 },
464                 {
465                     "vnfProfileId": "0408f076-e6c0-4c82-9940-272fddbb82de",
466                     "additionalParams": {
467                         "vimId": "CloudOwner_regionOne"
468                     }
469                 },
470                 {
471                     "vnfProfileId": "b1bb0ce7-2222-4fa7-95ed-4840d70a1100",
472                     "additionalParams": {
473                         "vimId": "CloudOwner_regionOne"
474                     }
475                 },
476                 {
477                     "vnfProfileId": "b1bb0ce7-2222-4fa7-95ed-4840d70a1102",
478                     "additionalParams": {
479                         "vimId": "CloudOwner_regionOne"
480                     }
481                 },
482                 {
483                     "vnfProfileId": "3fca3543-07f5-492f-812c-ed462e4f94f4",
484                     "additionalParams": {
485                         "vimId": "CloudOwner_regionOne"
486                     }
487                 }
488             ]
489         }
490         response = self.client.post(self.url % self.nsInstanceId, data=req_data, format='json')
491         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)
492         self.assertIsNotNone(response['Location'])
493         response = self.client.get(response['Location'], format='json')
494         self.assertEqual(response.status_code, status.HTTP_200_OK)