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.const import vnfd_rawdata, c1_data_get_tenant_id, c4_data_create_network, c2_data_create_volume, \
22 c5_data_create_subnet, c3_data_get_volume, c6_data_create_port, c7_data_create_flavor, c8_data_list_image, c9_data_create_vm, \
24 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
25 from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, \
26 SubNetworkInstModel, PortInstModel
27 from lcm.pub.utils import restcall
28 from lcm.pub.utils.jobutil import JobUtil
29 from lcm.pub.utils.timeutil import now_time
30 from lcm.pub.vimapi import api
33 class TestNFInstantiate(TestCase):
35 self.client = Client()
36 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
38 VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2",
39 vmname="test_02", operationalstate=1)
40 NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
41 tenant='admin', insttype=0, instid='1')
42 SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
43 name='sub_pnet', tenant='admin', insttype=0, instid='1')
44 PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
45 name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
49 VmInstModel.objects.all().delete()
50 NetworkInstModel.objects.all().delete()
51 SubNetworkInstModel.objects.all().delete()
52 PortInstModel.objects.all().delete()
54 def assert_job_result(self, job_id, job_progress, job_detail):
55 jobs = JobStatusModel.objects.filter(
57 progress=job_progress,
59 self.assertEqual(1, len(jobs))
61 def test_swagger_ok(self):
62 response = self.client.get("/openoapi/vnflcm/v1/swagger.json", format='json')
63 self.assertEqual(response.status_code, status.HTTP_200_OK)
65 @mock.patch.object(restcall, 'call_req')
66 def test_create_vnf_identifier(self, mock_call_req):
67 r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
68 r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200'] # get rawdata from catalog by csar_id
69 mock_call_req.side_effect = [r1, r2]
72 "vnfInstanceName": "vFW_01",
73 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"}
74 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances", data=data, format='json')
75 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
76 context = json.loads(response.content)
77 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
79 @mock.patch.object(InstVnf, 'run')
80 def test_instantiate_vnf(self, mock_run):
81 mock_run.re.return_value = None
82 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
83 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
85 def test_instantiate_vnf_when_inst_id_not_exist(self):
86 self.nf_inst_id = str(uuid.uuid4())
87 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
88 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
89 jobs = JobStatusModel.objects.filter(
92 descp="INST_VNF_READY")
93 self.assertEqual(1, len(jobs))
95 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
96 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
98 @mock.patch.object(restcall, 'call_req')
99 def test_instantiate_vnf_when_get_package_info_by_vnfdid_failed(self, mock_call_req):
100 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
101 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
102 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
103 r1 = [1, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
104 mock_call_req.side_effect = [r1]
105 self.nf_inst_id = '1111'
106 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
107 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
109 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
110 self.assert_job_result(self.job_id, 255, "Failed to query package_info of vnfdid(111) from nslcm.")
112 @mock.patch.object(restcall, 'call_req')
113 def test_instantiate_vnf_when_get_rawdata_by_csarid_failed(self, mock_call_req):
114 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
115 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
116 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
117 r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
118 r2 = [1, json.JSONEncoder().encode(vnfd_rawdata), '200'] # get rawdata from catalog by csar_id
119 mock_call_req.side_effect = [r1, r2]
120 self.nf_inst_id = '1111'
121 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
122 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
124 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
125 self.assert_job_result(self.job_id, 255, "Failed to query rawdata of CSAR(2222) from catalog.")
127 @mock.patch.object(restcall, 'call_req')
128 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
129 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
130 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
131 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
132 r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}), '200'] # get csar_id from nslcm by vnfd_id
133 r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200'] # get rawdata from catalog by csar_id
134 r3 = [1, json.JSONEncoder().encode({"vim":
137 "accessinfo": {"tenant": '2'}
139 }), '200'] # apply_grant_to_nfvo
140 mock_call_req.side_effect = [r1, r2, r3]
141 self.nf_inst_id = '1111'
142 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
143 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
145 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
146 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
148 @mock.patch.object(restcall, 'call_req')
149 @mock.patch.object(api, 'call')
150 def test_instantiate_vnf_when_(self, mock_call, mock_call_req):
151 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
152 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
153 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
154 r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}), '200'] # get csar_id from nslcm by vnfd_id
155 r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200'] # get rawdata from catalog by csar_id
156 r3 = [0, json.JSONEncoder().encode({"vim":{"vimid": '1', "accessinfo": {"tenant": '2'}}}), '200'] # apply_grant_to_nfvo
157 mock_call_req.side_effect = [r1, r2, r3]
158 c1_data = { # get_tenant_id
162 "name": "tenantname_1"
171 "id": "3c9eebdbbfd345658269340b9ea6fb73",
173 "tenantId": "tenant1",
174 "networkName": "ommnet",
176 "vlanTransparent": 1,
177 "networkType": "vlan",
178 "segmentationId": 202,
179 "physicalNetwork": "ctrl",
182 c3_data = { # get_volume
183 "status": "available11",
187 "device": "/dev/vdc",
188 "serverId": "3030e666-528e-4954-88f5-cc21dab1262b",
189 "volumeId": "4bd3e9eb-cd8b-456a-8589-910836a0ab31",
191 "id": "4bd3e9eb-cd8b-456a-8589-910836a0ab31"
194 "createTime": "2015-12-02T06:39:40.000000",
196 "id": "4bd3e9eb-cd8b-456a-8589-910836a0ab31",
199 mock_call.side_effect = [c1_data, c2_data, c3_data]
201 self.nf_inst_id = '1111'
202 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
203 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
205 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
206 self.assert_job_result(self.job_id, 255, "unexpected exception")
213 @mock.patch.object(restcall, 'call_req')
214 @mock.patch.object(api, 'call')
215 def test_instantiate_vnf_when_111(self, mock_call, mock_call_req):
216 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
217 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
218 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
219 r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}),
220 '200'] # get csar_id from nslcm by vnfd_id
221 r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200'] # get rawdata from catalog by csar_id
222 r3 = [0, json.JSONEncoder().encode({"vim": {"vimid": 'vimid_1', "accessinfo": {"tenant": 'tenantname_1'}}}),
223 '200'] # apply_grant_to_nfvo
224 r4 = [0, None, '200']
225 mock_call_req.side_effect = [r1, r2, r3, r4]
226 mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
227 c4_data_create_network, c5_data_create_subnet, c6_data_create_port,
228 c7_data_create_flavor, c8_data_list_image, c9_data_create_vm, c10_data_get_vm]
230 self.nf_inst_id = '1111'
231 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
232 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
234 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
235 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
240 # @mock.patch.object(restcall, 'call_req')
241 # # @mock.patch.object(adaptor, 'create_vim_res')
242 # def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
243 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
244 # nfvouser='root', nfvopassword='root123')
245 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
246 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
247 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
248 # # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
249 # mock_call_req.side_effect = [r1, r2, r3]
250 # # mock_create_vim_res.re.return_value = None
253 # "vnfInstanceName": "vFW_01",
254 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
255 # self.nf_inst_id = CreateVnf(create_data).do_biz()
256 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
257 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
258 # data = inst_req_data
259 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
260 # self.assert_job_result(self.job_id, 255, "Create resource failed")
262 # @mock.patch.object(restcall, 'call_req')
263 # # @mock.patch.object(adaptor, 'create_vim_res')
264 # def test_instantiate_vnf_success(self, mock_call_req):
265 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
266 # nfvouser='root', nfvopassword='root123')
267 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
268 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
269 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
270 # r4 = [0, json.JSONEncoder().encode('None'), '200']
271 # mock_call_req.side_effect = [r1, r2, r3, r4]
272 # # mock_create_vim_res.re.return_value = None
275 # "vnfInstanceName": "vFW_01",
276 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
277 # self.nf_inst_id = CreateVnf(create_data).do_biz()
278 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
279 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
280 # data = inst_req_data
281 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
282 # self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
285 "flavourId": "flavour_1",
286 "instantiationLevelId": "instantiationLevel_1",
296 "protocolType": "http"
299 "tenant": "tenant_vCPE",
301 "password": "vCPE_321"
303 "interfaceEndpoint": "http://10.43.21.105:80/"
305 "resourceId": "1246",
311 "addressType": "MAC",
312 "l2AddressData": "00:f3:43:20:a2:a3"
317 "iPAddressType": "IPv4",
318 "iPAddress": "192.168.104.2"
322 "numDynamicAddresses": 0
327 "localizationLanguage": "en_US",
328 "additionalParams": {}