1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from django.test import TestCase, Client
19 from rest_framework import status
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
29 class TestNFInstantiate(TestCase):
31 self.client = Client()
32 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
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')
45 VmInstModel.objects.all().delete()
46 NetworkInstModel.objects.all().delete()
47 SubNetworkInstModel.objects.all().delete()
48 PortInstModel.objects.all().delete()
50 def assert_job_result(self, job_id, job_progress, job_detail):
51 jobs = JobStatusModel.objects.filter(
53 progress=job_progress,
55 self.assertEqual(1, len(jobs))
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)
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]
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())
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)
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")
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.")
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")
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.")
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")
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.")
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]
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.")
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]
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")
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]
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")
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
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")
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
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.")
214 "flavourId": "flavour_1",
215 "instantiationLevelId": "instantiationLevel_1",
225 "protocolType": "http"
228 "tenant": "tenant_vCPE",
230 "password": "vCPE_321"
232 "interfaceEndpoint": "http://10.43.21.105:80/"
234 "resourceId": "1246",
240 "addressType": "MAC",
241 "l2AddressData": "00:f3:43:20:a2:a3"
246 "iPAddressType": "IPv4",
247 "iPAddress": "192.168.104.2"
251 "numDynamicAddresses": 0
256 "localizationLanguage": "en_US",
257 "additionalParams": {}
261 'local_storages': [],
264 'volumn_storages': [],
268 'local_storages': [],
269 'vdu_id': u'vdu_omm.001',
270 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
276 'support_scaling': False,
279 'storage_policy': '',
282 'availability_zone': '',
287 'inject_data_list': [],
291 'local_affinity_antiaffinity_rule': {},
292 'template_id': u'omm.001',
293 'manual_scale_select_vim': False},
294 'description': u'singleommvm'},
296 'volumn_storages': [],
300 'local_storages': [],
302 'image_file': u'sss',
308 'support_scaling': False,
311 'storage_policy': '',
314 'availability_zone': '',
319 'inject_data_list': [],
323 'local_affinity_antiaffinity_rule': {},
325 'manual_scale_select_vim': False},
326 'description': u'ompvm'},
328 'volumn_storages': [],
332 'local_storages': [],
334 'image_file': u'sss',
340 'support_scaling': False,
343 'storage_policy': '',
346 'availability_zone': '',
351 'inject_data_list': [],
355 'local_affinity_antiaffinity_rule': {},
357 'manual_scale_select_vim': False},
358 'description': u'ompvm'},
360 'volumn_storages': [],
364 'local_storages': [],
366 'image_file': u'sss',
372 'support_scaling': False,
375 'storage_policy': '',
378 'availability_zone': '',
383 'inject_data_list': [],
387 'local_affinity_antiaffinity_rule': {},
389 'manual_scale_select_vim': False},
390 'description': u'ompvm'},
392 'volumn_storages': [],
396 'local_storages': [],
398 'image_file': u'sss',
404 'support_scaling': False,
407 'storage_policy': '',
410 'availability_zone': '',
415 'inject_data_list': [],
419 'local_affinity_antiaffinity_rule': {},
420 'template_id': u'10',
421 'manual_scale_select_vim': False},
422 'description': u'ppvm'},
424 'volumn_storages': [],
428 'local_storages': [],
430 'image_file': u'sss',
436 'support_scaling': False,
439 'storage_policy': '',
442 'availability_zone': '',
447 'inject_data_list': [],
451 'local_affinity_antiaffinity_rule': {},
452 'template_id': u'11',
453 'manual_scale_select_vim': False},
454 'description': u'ppvm'},
456 'volumn_storages': [],
460 'local_storages': [],
462 'image_file': u'sss',
468 'support_scaling': False,
471 'storage_policy': '',
474 'availability_zone': '',
479 'inject_data_list': [],
483 'local_affinity_antiaffinity_rule': {},
484 'template_id': u'12',
485 'manual_scale_select_vim': False},
486 'description': u'ppvm'}],
487 'volumn_storages': [],
491 'policy_id': u'policy_scale_sss-vnf-template',
493 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
499 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
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',
505 'hypervisor_type': 'kvm'},
506 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
512 'disk_format': u'VMDK',
513 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
514 'container_type': 'vm',
516 'hypervisor_type': 'kvm'},
517 'image_file_id': u'sss'}],
524 'domain_type': u'CN',
525 'version': u'v4.14.10',
526 'vmnumber_overquota_alarm': False,
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",
543 "protocolType": "http"
546 "tenant": "tenant_vCPE",
548 "password": "vCPE_321"
550 "interfaceEndpoint": "http://10.43.21.105:80/"
552 "resourceId": "1246",
558 "addressType": "MAC",
559 "l2AddressData": "00:f3:43:20:a2:a3"
564 "iPAddressType": "IPv4",
565 "iPAddress": "192.168.104.2"
569 "numDynamicAddresses": 0
574 "localizationLanguage": "en_US",
575 "additionalParams": {}