Merge "Save vnfc to DB"
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / tests / test_vnf_create.py
1 # Copyright 2017 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 uuid
17
18 import mock
19 from django.test import TestCase, Client
20 from rest_framework import status
21
22 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
23 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
24 from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel, VmInstModel, NetworkInstModel, \
25     SubNetworkInstModel
26 from lcm.pub.utils import restcall
27 from lcm.pub.utils.jobutil import JobUtil
28
29 inst_req_data = {
30     "flavourId": "flavour_1",
31     "instantiationLevelId": "instantiationLevel_1",
32     "extVirtualLinks": [
33         {
34             "vlInstanceId": "1",
35             "vim": {
36                 "vimInfoId": "1",
37                 "vimId": "1",
38                 "interfaceInfo": {
39                     "vimType": "vim",
40                     "apiVersion": "v2",
41                     "protocolType": "http"
42                 },
43                 "accessInfo": {
44                     "tenant": "tenant_vCPE",
45                     "username": "vCPE",
46                     "password": "vCPE_321"
47                 },
48                 "interfaceEndpoint": "http://10.43.21.105:80/"
49             },
50             "resourceId": "1246",
51             "extCps": [
52                 {
53                     "cpdId": "11",
54                     "addresses": [
55                         {
56                             "addressType": "MAC",
57                             "l2AddressData": "00:f3:43:20:a2:a3"
58                         },
59                         {
60                             "addressType": "IP",
61                             "l3AddressData": {
62                                 "iPAddressType": "IPv4",
63                                 "iPAddress": "192.168.104.2"
64                             }
65                         }
66                     ],
67                     "numDynamicAddresses": 0
68                 }
69             ]
70         }
71     ],
72     "localizationLanguage": "en_US",
73     "additionalParams": {}
74 }
75
76
77 class TestNsInstantiate(TestCase):
78     def setUp(self):
79         self.client = Client()
80         VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
81                                    operationalstate=1)
82         VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2", vmname="test_02",
83                                operationalstate=1)
84         NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
85                                         tenant='admin', insttype=0, instid='1')
86         SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
87                                            name='sub_pnet',tenant='admin', insttype=0, instid='1')
88
89     def tearDown(self):
90         pass
91
92     def assert_job_result(self, job_id, job_progress, job_detail):
93         jobs = JobStatusModel.objects.filter(
94             jobid=job_id,
95             progress=job_progress,
96             descp=job_detail)
97         self.assertEqual(1, len(jobs))
98
99     def test_swagger_ok(self):
100         response = self.client.get("/gvnfmapi/lcm/v1/swagger.json", format='json')
101         self.assertEqual(response.status_code, status.HTTP_200_OK)
102
103     @mock.patch.object(restcall, 'call_req')
104     def test_create_vnf_identifier(self, mock_call_req):
105         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
106         mock_call_req.side_effect = [r1]
107         data = {
108             "vnfdId": "111",
109             "vnfInstanceName": "vFW_01",
110             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
111         response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
112         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
113         context = json.loads(response.content)
114         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
115
116     @mock.patch.object(InstVnf, 'run')
117     def test_instantiate_vnf(self, mock_run):
118         response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
119         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
120
121     def test_instantiate_vnf_when_inst_id_not_exist(self):
122         self.nf_inst_id = str(uuid.uuid4())
123         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
124         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
125         data = inst_req_data
126         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
127         self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
128
129     @mock.patch.object(restcall, 'call_req')
130     def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
131         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
132         r2 = [0, json.JSONEncoder().encode(''), '200']
133         mock_call_req.side_effect = [r1, r2]
134         create_data = {
135             "vnfdId": "111",
136             "vnfInstanceName": "vFW_01",
137             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
138         self.nf_inst_id = CreateVnf(create_data).do_biz()
139         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
140         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
141         data = inst_req_data
142         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
143         self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
144
145     @mock.patch.object(restcall, 'call_req')
146     def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
147         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
148         r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
149         mock_call_req.side_effect = [r1, r2]
150         create_data = {
151             "vnfdId": "111",
152             "vnfInstanceName": "vFW_01",
153             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
154         self.nf_inst_id = CreateVnf(create_data).do_biz()
155         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
156         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
157         data = inst_req_data
158         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
159         self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
160
161     @mock.patch.object(restcall, 'call_req')
162     def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
163         NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
164                                         nfvouser='root', nfvopassword='root123')
165         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
166         r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
167         r3 = [1, json.JSONEncoder().encode(''), '200']
168         mock_call_req.side_effect = [r1, r2, r3]
169         create_data = {
170             "vnfdId": "111",
171             "vnfInstanceName": "vFW_01",
172             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
173         self.nf_inst_id = CreateVnf(create_data).do_biz()
174         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
175         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
176         data = inst_req_data
177         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
178         self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
179
180     @mock.patch.object(restcall, 'call_req')
181     def test_instantiate_vnf_success(self, mock_call_req):
182         NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
183                                         nfvouser='root', nfvopassword='root123')
184         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
185         r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
186         r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
187         r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
188         mock_call_req.side_effect = [r1, r2, r3, r4]
189         create_data = {
190             "vnfdId": "111",
191             "vnfInstanceName": "vFW_01",
192             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
193         self.nf_inst_id = CreateVnf(create_data).do_biz()
194         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
195         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
196         data = inst_req_data
197         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
198         self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
199
200
201 vnfd_model_dict = {
202     'local_storages': [],
203     'vdus': [
204         {
205             'volumn_storages': [],
206             'nfv_compute': {
207                 'mem_size': '',
208                 'num_cpus': u'2'},
209             'local_storages': [],
210             'vdu_id': u'vdu_omm.001',
211             'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
212             'dependencies': [],
213             'vls': [],
214             'cps': [],
215             'properties': {
216                 'key_vdu': '',
217                 'support_scaling': False,
218                 'vdu_type': '',
219                 'name': '',
220                 'storage_policy': '',
221                 'location_info': {
222                     'vimId': '',
223                     'availability_zone': '',
224                     'region': '',
225                     'dc': '',
226                     'host': '',
227                     'tenant': ''},
228                 'inject_data_list': [],
229                 'watchdog': {
230                     'action': '',
231                     'enabledelay': ''},
232                 'local_affinity_antiaffinity_rule': {},
233                 'template_id': u'omm.001',
234                 'manual_scale_select_vim': False},
235             'description': u'singleommvm'},
236         {
237             'volumn_storages': [],
238             'nfv_compute': {
239                 'mem_size': '',
240                 'num_cpus': u'4'},
241             'local_storages': [],
242             'vdu_id': u'vdu_1',
243             'image_file': u'sss',
244             'dependencies': [],
245             'vls': [],
246             'cps': [],
247             'properties': {
248                 'key_vdu': '',
249                 'support_scaling': False,
250                 'vdu_type': '',
251                 'name': '',
252                 'storage_policy': '',
253                 'location_info': {
254                     'vimId': '',
255                     'availability_zone': '',
256                     'region': '',
257                     'dc': '',
258                     'host': '',
259                     'tenant': ''},
260                 'inject_data_list': [],
261                 'watchdog': {
262                     'action': '',
263                     'enabledelay': ''},
264                 'local_affinity_antiaffinity_rule': {},
265                 'template_id': u'1',
266                 'manual_scale_select_vim': False},
267             'description': u'ompvm'},
268         {
269             'volumn_storages': [],
270             'nfv_compute': {
271                 'mem_size': '',
272                 'num_cpus': u'14'},
273             'local_storages': [],
274             'vdu_id': u'vdu_2',
275             'image_file': u'sss',
276             'dependencies': [],
277             'vls': [],
278             'cps': [],
279             'properties': {
280                 'key_vdu': '',
281                 'support_scaling': False,
282                 'vdu_type': '',
283                 'name': '',
284                 'storage_policy': '',
285                 'location_info': {
286                     'vimId': '',
287                     'availability_zone': '',
288                     'region': '',
289                     'dc': '',
290                     'host': '',
291                     'tenant': ''},
292                 'inject_data_list': [],
293                 'watchdog': {
294                     'action': '',
295                     'enabledelay': ''},
296                 'local_affinity_antiaffinity_rule': {},
297                 'template_id': u'2',
298                 'manual_scale_select_vim': False},
299             'description': u'ompvm'},
300         {
301             'volumn_storages': [],
302             'nfv_compute': {
303                 'mem_size': '',
304                 'num_cpus': u'14'},
305             'local_storages': [],
306             'vdu_id': u'vdu_3',
307             'image_file': u'sss',
308             'dependencies': [],
309             'vls': [],
310             'cps': [],
311             'properties': {
312                 'key_vdu': '',
313                 'support_scaling': False,
314                 'vdu_type': '',
315                 'name': '',
316                 'storage_policy': '',
317                 'location_info': {
318                     'vimId': '',
319                     'availability_zone': '',
320                     'region': '',
321                     'dc': '',
322                     'host': '',
323                     'tenant': ''},
324                 'inject_data_list': [],
325                 'watchdog': {
326                     'action': '',
327                     'enabledelay': ''},
328                 'local_affinity_antiaffinity_rule': {},
329                 'template_id': u'3',
330                 'manual_scale_select_vim': False},
331             'description': u'ompvm'},
332         {
333             'volumn_storages': [],
334             'nfv_compute': {
335                 'mem_size': '',
336                 'num_cpus': u'4'},
337             'local_storages': [],
338             'vdu_id': u'vdu_10',
339             'image_file': u'sss',
340             'dependencies': [],
341             'vls': [],
342             'cps': [],
343             'properties': {
344                 'key_vdu': '',
345                 'support_scaling': False,
346                 'vdu_type': '',
347                 'name': '',
348                 'storage_policy': '',
349                 'location_info': {
350                     'vimId': '',
351                     'availability_zone': '',
352                     'region': '',
353                     'dc': '',
354                     'host': '',
355                     'tenant': ''},
356                 'inject_data_list': [],
357                 'watchdog': {
358                     'action': '',
359                     'enabledelay': ''},
360                 'local_affinity_antiaffinity_rule': {},
361                 'template_id': u'10',
362                 'manual_scale_select_vim': False},
363             'description': u'ppvm'},
364         {
365             'volumn_storages': [],
366             'nfv_compute': {
367                 'mem_size': '',
368                 'num_cpus': u'14'},
369             'local_storages': [],
370             'vdu_id': u'vdu_11',
371             'image_file': u'sss',
372             'dependencies': [],
373             'vls': [],
374             'cps': [],
375             'properties': {
376                 'key_vdu': '',
377                 'support_scaling': False,
378                 'vdu_type': '',
379                 'name': '',
380                 'storage_policy': '',
381                 'location_info': {
382                     'vimId': '',
383                     'availability_zone': '',
384                     'region': '',
385                     'dc': '',
386                     'host': '',
387                     'tenant': ''},
388                 'inject_data_list': [],
389                 'watchdog': {
390                     'action': '',
391                     'enabledelay': ''},
392                 'local_affinity_antiaffinity_rule': {},
393                 'template_id': u'11',
394                 'manual_scale_select_vim': False},
395             'description': u'ppvm'},
396         {
397             'volumn_storages': [],
398             'nfv_compute': {
399                 'mem_size': '',
400                 'num_cpus': u'14'},
401             'local_storages': [],
402             'vdu_id': u'vdu_12',
403             'image_file': u'sss',
404             'dependencies': [],
405             'vls': [],
406             'cps': [],
407             'properties': {
408                 'key_vdu': '',
409                 'support_scaling': False,
410                 'vdu_type': '',
411                 'name': '',
412                 'storage_policy': '',
413                 'location_info': {
414                     'vimId': '',
415                     'availability_zone': '',
416                     'region': '',
417                     'dc': '',
418                     'host': '',
419                     'tenant': ''},
420                 'inject_data_list': [],
421                 'watchdog': {
422                     'action': '',
423                     'enabledelay': ''},
424                 'local_affinity_antiaffinity_rule': {},
425                 'template_id': u'12',
426                 'manual_scale_select_vim': False},
427             'description': u'ppvm'}],
428     'volumn_storages': [],
429     'policies': {
430         'scaling': {
431             'targets': {},
432             'policy_id': u'policy_scale_sss-vnf-template',
433             'properties': {
434                 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
435             'description': ''}},
436     'image_files': [
437         {
438             'description': '',
439             'properties': {
440                 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
441                 'checksum': '',
442                 'disk_format': u'VMDK',
443                 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
444                 'container_type': 'vm',
445                 'version': '',
446                 'hypervisor_type': 'kvm'},
447             'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
448         {
449             'description': '',
450             'properties': {
451                 'name': u'sss.vmdk',
452                 'checksum': '',
453                 'disk_format': u'VMDK',
454                 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
455                 'container_type': 'vm',
456                 'version': '',
457                 'hypervisor_type': 'kvm'},
458             'image_file_id': u'sss'}],
459     'vls': [],
460     'cps': [],
461     'metadata': {
462         'vendor': u'zte',
463         'is_shared': False,
464         'description': '',
465         'domain_type': u'CN',
466         'version': u'v4.14.10',
467         'vmnumber_overquota_alarm': False,
468         'cross_dc': False,
469         'vnf_type': u'SSS',
470         'vnfd_version': u'V00000001',
471         'id': u'sss-vnf-template',
472         'name': u'sss-vnf-template'},
473     "flavourId": "flavour_1",
474     "instantiationLevelId": "instantiationLevel_1",
475     "extVirtualLinks": [
476         {
477             "vlInstanceId": "1",
478             "vim": {
479                 "vimInfoId": "1",
480                 "vimId": "1",
481                 "interfaceInfo": {
482                     "vimType": "vim",
483                     "apiVersion": "v2",
484                     "protocolType": "http"
485                 },
486                 "accessInfo": {
487                     "tenant": "tenant_vCPE",
488                     "username": "vCPE",
489                     "password": "vCPE_321"
490                 },
491                 "interfaceEndpoint": "http://10.43.21.105:80/"
492             },
493             "resourceId": "1246",
494             "extCps": [
495                 {
496                     "cpdId": "11",
497                     "addresses": [
498                         {
499                             "addressType": "MAC",
500                             "l2AddressData": "00:f3:43:20:a2:a3"
501                         },
502                         {
503                             "addressType": "IP",
504                             "l3AddressData": {
505                                 "iPAddressType": "IPv4",
506                                 "iPAddress": "192.168.104.2"
507                             }
508                         }
509                     ],
510                     "numDynamicAddresses": 0
511                 }
512             ]
513         }
514     ],
515     "localizationLanguage": "en_US",
516     "additionalParams": {}
517 }