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