Modify test case 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_get_rawdata_by_csarid_failed(self, mock_call_req):
105         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='todo',
106                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
107                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
108         r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200']  # get csar_id from nslcm by vnfd_id
109         r2 = [1, json.JSONEncoder().encode(''), '200']
110         mock_call_req.side_effect = [r1, r2]
111         self.nf_inst_id = '1111'
112         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
113         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
114         data = inst_req_data
115         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
116         self.assert_job_result(self.job_id, 255, "Failed to query rawdata of CSAR(2222) from catalog.")
117
118     # @mock.patch.object(restcall, 'call_req')
119     # def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
120     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
121     #     r2 = [0, json.JSONEncoder().encode(''), '200']
122     #     mock_call_req.side_effect = [r1, r2]
123     #     create_data = {
124     #         "vnfdId": "111",
125     #         "vnfInstanceName": "vFW_01",
126     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
127     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
128     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
129     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
130     #     data = inst_req_data
131     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
132     #     self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
133     #
134     # @mock.patch.object(restcall, 'call_req')
135     # def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
136     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
137     #     r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
138     #     mock_call_req.side_effect = [r1, r2]
139     #     create_data = {
140     #         "vnfdId": "111",
141     #         "vnfInstanceName": "vFW_01",
142     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
143     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
144     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
145     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
146     #     data = inst_req_data
147     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
148     #     self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
149     #
150     # @mock.patch.object(restcall, 'call_req')
151     # def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
152     #     NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
153     #                                     nfvouser='root', nfvopassword='root123')
154     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
155     #     r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
156     #     r3 = [1, json.JSONEncoder().encode(''), '200']
157     #     mock_call_req.side_effect = [r1, r2, r3]
158     #     create_data = {
159     #         "vnfdId": "111",
160     #         "vnfInstanceName": "vFW_01",
161     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
162     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
163     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
164     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
165     #     data = inst_req_data
166     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
167     #     self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
168
169     # @mock.patch.object(restcall, 'call_req')
170     # # @mock.patch.object(adaptor, 'create_vim_res')
171     # def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
172     #     NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
173     #                                     nfvouser='root', nfvopassword='root123')
174     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
175     #     r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
176     #     r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
177     #     # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
178     #     mock_call_req.side_effect = [r1, r2, r3]
179     #     # mock_create_vim_res.re.return_value = None
180     #     create_data = {
181     #         "vnfdId": "111",
182     #         "vnfInstanceName": "vFW_01",
183     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
184     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
185     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
186     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
187     #     data = inst_req_data
188     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
189     #     self.assert_job_result(self.job_id, 255, "Create resource failed")
190
191     # @mock.patch.object(restcall, 'call_req')
192     # # @mock.patch.object(adaptor, 'create_vim_res')
193     # def test_instantiate_vnf_success(self, mock_call_req):
194     #     NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
195     #                                     nfvouser='root', nfvopassword='root123')
196     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
197     #     r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
198     #     r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
199     #     r4 = [0, json.JSONEncoder().encode('None'), '200']
200     #     mock_call_req.side_effect = [r1, r2, r3, r4]
201     #     # mock_create_vim_res.re.return_value = None
202     #     create_data = {
203     #         "vnfdId": "111",
204     #         "vnfInstanceName": "vFW_01",
205     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
206     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
207     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
208     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
209     #     data = inst_req_data
210     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
211     #     self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
212
213 inst_req_data = {
214     "flavourId": "flavour_1",
215     "instantiationLevelId": "instantiationLevel_1",
216     "extVirtualLinks": [
217         {
218             "vlInstanceId": "1",
219             "vim": {
220                 "vimInfoId": "1",
221                 "vimId": "1",
222                 "interfaceInfo": {
223                     "vimType": "vim",
224                     "apiVersion": "v2",
225                     "protocolType": "http"
226                 },
227                 "accessInfo": {
228                     "tenant": "tenant_vCPE",
229                     "username": "vCPE",
230                     "password": "vCPE_321"
231                 },
232                 "interfaceEndpoint": "http://10.43.21.105:80/"
233             },
234             "resourceId": "1246",
235             "extCps": [
236                 {
237                     "cpdId": "11",
238                     "addresses": [
239                         {
240                             "addressType": "MAC",
241                             "l2AddressData": "00:f3:43:20:a2:a3"
242                         },
243                         {
244                             "addressType": "IP",
245                             "l3AddressData": {
246                                 "iPAddressType": "IPv4",
247                                 "iPAddress": "192.168.104.2"
248                             }
249                         }
250                     ],
251                     "numDynamicAddresses": 0
252                 }
253             ]
254         }
255     ],
256     "localizationLanguage": "en_US",
257     "additionalParams": {}
258 }
259
260 vnfd_model_dict = {
261     'local_storages': [],
262     'vdus': [
263         {
264             'volumn_storages': [],
265             'nfv_compute': {
266                 'mem_size': '',
267                 'num_cpus': u'2'},
268             'local_storages': [],
269             'vdu_id': u'vdu_omm.001',
270             'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
271             'dependencies': [],
272             'vls': [],
273             'cps': [],
274             'properties': {
275                 'key_vdu': '',
276                 'support_scaling': False,
277                 'vdu_type': '',
278                 'name': '',
279                 'storage_policy': '',
280                 'location_info': {
281                     'vimId': '',
282                     'availability_zone': '',
283                     'region': '',
284                     'dc': '',
285                     'host': '',
286                     'tenant': ''},
287                 'inject_data_list': [],
288                 'watchdog': {
289                     'action': '',
290                     'enabledelay': ''},
291                 'local_affinity_antiaffinity_rule': {},
292                 'template_id': u'omm.001',
293                 'manual_scale_select_vim': False},
294             'description': u'singleommvm'},
295         {
296             'volumn_storages': [],
297             'nfv_compute': {
298                 'mem_size': '',
299                 'num_cpus': u'4'},
300             'local_storages': [],
301             'vdu_id': u'vdu_1',
302             'image_file': u'sss',
303             'dependencies': [],
304             'vls': [],
305             'cps': [],
306             'properties': {
307                 'key_vdu': '',
308                 'support_scaling': False,
309                 'vdu_type': '',
310                 'name': '',
311                 'storage_policy': '',
312                 'location_info': {
313                     'vimId': '',
314                     'availability_zone': '',
315                     'region': '',
316                     'dc': '',
317                     'host': '',
318                     'tenant': ''},
319                 'inject_data_list': [],
320                 'watchdog': {
321                     'action': '',
322                     'enabledelay': ''},
323                 'local_affinity_antiaffinity_rule': {},
324                 'template_id': u'1',
325                 'manual_scale_select_vim': False},
326             'description': u'ompvm'},
327         {
328             'volumn_storages': [],
329             'nfv_compute': {
330                 'mem_size': '',
331                 'num_cpus': u'14'},
332             'local_storages': [],
333             'vdu_id': u'vdu_2',
334             'image_file': u'sss',
335             'dependencies': [],
336             'vls': [],
337             'cps': [],
338             'properties': {
339                 'key_vdu': '',
340                 'support_scaling': False,
341                 'vdu_type': '',
342                 'name': '',
343                 'storage_policy': '',
344                 'location_info': {
345                     'vimId': '',
346                     'availability_zone': '',
347                     'region': '',
348                     'dc': '',
349                     'host': '',
350                     'tenant': ''},
351                 'inject_data_list': [],
352                 'watchdog': {
353                     'action': '',
354                     'enabledelay': ''},
355                 'local_affinity_antiaffinity_rule': {},
356                 'template_id': u'2',
357                 'manual_scale_select_vim': False},
358             'description': u'ompvm'},
359         {
360             'volumn_storages': [],
361             'nfv_compute': {
362                 'mem_size': '',
363                 'num_cpus': u'14'},
364             'local_storages': [],
365             'vdu_id': u'vdu_3',
366             'image_file': u'sss',
367             'dependencies': [],
368             'vls': [],
369             'cps': [],
370             'properties': {
371                 'key_vdu': '',
372                 'support_scaling': False,
373                 'vdu_type': '',
374                 'name': '',
375                 'storage_policy': '',
376                 'location_info': {
377                     'vimId': '',
378                     'availability_zone': '',
379                     'region': '',
380                     'dc': '',
381                     'host': '',
382                     'tenant': ''},
383                 'inject_data_list': [],
384                 'watchdog': {
385                     'action': '',
386                     'enabledelay': ''},
387                 'local_affinity_antiaffinity_rule': {},
388                 'template_id': u'3',
389                 'manual_scale_select_vim': False},
390             'description': u'ompvm'},
391         {
392             'volumn_storages': [],
393             'nfv_compute': {
394                 'mem_size': '',
395                 'num_cpus': u'4'},
396             'local_storages': [],
397             'vdu_id': u'vdu_10',
398             'image_file': u'sss',
399             'dependencies': [],
400             'vls': [],
401             'cps': [],
402             'properties': {
403                 'key_vdu': '',
404                 'support_scaling': False,
405                 'vdu_type': '',
406                 'name': '',
407                 'storage_policy': '',
408                 'location_info': {
409                     'vimId': '',
410                     'availability_zone': '',
411                     'region': '',
412                     'dc': '',
413                     'host': '',
414                     'tenant': ''},
415                 'inject_data_list': [],
416                 'watchdog': {
417                     'action': '',
418                     'enabledelay': ''},
419                 'local_affinity_antiaffinity_rule': {},
420                 'template_id': u'10',
421                 'manual_scale_select_vim': False},
422             'description': u'ppvm'},
423         {
424             'volumn_storages': [],
425             'nfv_compute': {
426                 'mem_size': '',
427                 'num_cpus': u'14'},
428             'local_storages': [],
429             'vdu_id': u'vdu_11',
430             'image_file': u'sss',
431             'dependencies': [],
432             'vls': [],
433             'cps': [],
434             'properties': {
435                 'key_vdu': '',
436                 'support_scaling': False,
437                 'vdu_type': '',
438                 'name': '',
439                 'storage_policy': '',
440                 'location_info': {
441                     'vimId': '',
442                     'availability_zone': '',
443                     'region': '',
444                     'dc': '',
445                     'host': '',
446                     'tenant': ''},
447                 'inject_data_list': [],
448                 'watchdog': {
449                     'action': '',
450                     'enabledelay': ''},
451                 'local_affinity_antiaffinity_rule': {},
452                 'template_id': u'11',
453                 'manual_scale_select_vim': False},
454             'description': u'ppvm'},
455         {
456             'volumn_storages': [],
457             'nfv_compute': {
458                 'mem_size': '',
459                 'num_cpus': u'14'},
460             'local_storages': [],
461             'vdu_id': u'vdu_12',
462             'image_file': u'sss',
463             'dependencies': [],
464             'vls': [],
465             'cps': [],
466             'properties': {
467                 'key_vdu': '',
468                 'support_scaling': False,
469                 'vdu_type': '',
470                 'name': '',
471                 'storage_policy': '',
472                 'location_info': {
473                     'vimId': '',
474                     'availability_zone': '',
475                     'region': '',
476                     'dc': '',
477                     'host': '',
478                     'tenant': ''},
479                 'inject_data_list': [],
480                 'watchdog': {
481                     'action': '',
482                     'enabledelay': ''},
483                 'local_affinity_antiaffinity_rule': {},
484                 'template_id': u'12',
485                 'manual_scale_select_vim': False},
486             'description': u'ppvm'}],
487     'volumn_storages': [],
488     'policies': {
489         'scaling': {
490             'targets': {},
491             'policy_id': u'policy_scale_sss-vnf-template',
492             'properties': {
493                 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
494             'description': ''}},
495     'image_files': [
496         {
497             'description': '',
498             'properties': {
499                 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
500                 'checksum': '',
501                 'disk_format': u'VMDK',
502                 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
503                 'container_type': 'vm',
504                 'version': '',
505                 'hypervisor_type': 'kvm'},
506             'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
507         {
508             'description': '',
509             'properties': {
510                 'name': u'sss.vmdk',
511                 'checksum': '',
512                 'disk_format': u'VMDK',
513                 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
514                 'container_type': 'vm',
515                 'version': '',
516                 'hypervisor_type': 'kvm'},
517             'image_file_id': u'sss'}],
518     'vls': [],
519     'cps': [],
520     'metadata': {
521         'vendor': u'zte',
522         'is_shared': False,
523         'description': '',
524         'domain_type': u'CN',
525         'version': u'v4.14.10',
526         'vmnumber_overquota_alarm': False,
527         'cross_dc': False,
528         'vnf_type': u'SSS',
529         'vnfd_version': u'V00000001',
530         'id': u'sss-vnf-template',
531         'name': u'sss-vnf-template'},
532     "flavourId": "flavour_1",
533     "instantiationLevelId": "instantiationLevel_1",
534     "extVirtualLinks": [
535         {
536             "vlInstanceId": "1",
537             "vim": {
538                 "vimInfoId": "1",
539                 "vimId": "1",
540                 "interfaceInfo": {
541                     "vimType": "vim",
542                     "apiVersion": "v2",
543                     "protocolType": "http"
544                 },
545                 "accessInfo": {
546                     "tenant": "tenant_vCPE",
547                     "username": "vCPE",
548                     "password": "vCPE_321"
549                 },
550                 "interfaceEndpoint": "http://10.43.21.105:80/"
551             },
552             "resourceId": "1246",
553             "extCps": [
554                 {
555                     "cpdId": "11",
556                     "addresses": [
557                         {
558                             "addressType": "MAC",
559                             "l2AddressData": "00:f3:43:20:a2:a3"
560                         },
561                         {
562                             "addressType": "IP",
563                             "l3AddressData": {
564                                 "iPAddressType": "IPv4",
565                                 "iPAddress": "192.168.104.2"
566                             }
567                         }
568                     ],
569                     "numDynamicAddresses": 0
570                 }
571             ]
572         }
573     ],
574     "localizationLanguage": "en_US",
575     "additionalParams": {}
576 }