Modify code and test case of vnflcm
[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.const import vnfd_rawdata
22 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
23 from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, \
24     SubNetworkInstModel, PortInstModel
25 from lcm.pub.utils import restcall
26 from lcm.pub.utils.jobutil import JobUtil
27 from lcm.pub.utils.timeutil import now_time
28 from lcm.pub.vimapi import adaptor
29 from lcm.pub.vimapi import api
30
31
32 class TestNFInstantiate(TestCase):
33     def setUp(self):
34         self.client = Client()
35         VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
36                                    operationalstate=1)
37         VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2",
38                                    vmname="test_02", operationalstate=1)
39         NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
40                                         tenant='admin', insttype=0, instid='1')
41         SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
42                                            name='sub_pnet', tenant='admin', insttype=0, instid='1')
43         PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
44                                      name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
45
46     def tearDown(self):
47         pass
48         VmInstModel.objects.all().delete()
49         NetworkInstModel.objects.all().delete()
50         SubNetworkInstModel.objects.all().delete()
51         PortInstModel.objects.all().delete()
52
53     def assert_job_result(self, job_id, job_progress, job_detail):
54         jobs = JobStatusModel.objects.filter(
55             jobid=job_id,
56             progress=job_progress,
57             descp=job_detail)
58         self.assertEqual(1, len(jobs))
59
60     def test_swagger_ok(self):
61         response = self.client.get("/openoapi/vnflcm/v1/swagger.json", format='json')
62         self.assertEqual(response.status_code, status.HTTP_200_OK)
63
64     @mock.patch.object(restcall, 'call_req')
65     def test_create_vnf_identifier(self, mock_call_req):
66         r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200']  # get csar_id from nslcm by vnfd_id
67         r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
68         mock_call_req.side_effect = [r1, r2]
69         data = {
70             "vnfdId": "111",
71             "vnfInstanceName": "vFW_01",
72             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"}
73         response = self.client.post("/openoapi/vnflcm/v1/vnf_instances", data=data, format='json')
74         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
75         context = json.loads(response.content)
76         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
77
78     @mock.patch.object(InstVnf, 'run')
79     def test_instantiate_vnf(self, mock_run):
80         mock_run.re.return_value = None
81         response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
82         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
83
84     def test_instantiate_vnf_when_inst_id_not_exist(self):
85         self.nf_inst_id = str(uuid.uuid4())
86         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
87         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
88         data = inst_req_data
89         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
90         self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
91
92     @mock.patch.object(restcall, 'call_req')
93     def test_instantiate_vnf_when_get_package_info_by_vnfdid_failed(self, mock_call_req):
94         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
95                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
96                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
97         r1 = [1, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200']  # get csar_id from nslcm by vnfd_id
98         mock_call_req.side_effect = [r1]
99         self.nf_inst_id = '1111'
100         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
101         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
102         data = inst_req_data
103         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
104         self.assert_job_result(self.job_id, 255, "Failed to query package_info of vnfdid(111) from nslcm.")
105
106     @mock.patch.object(restcall, 'call_req')
107     def test_instantiate_vnf_when_get_rawdata_by_csarid_failed(self, mock_call_req):
108         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
109                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
110                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
111         r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200']  # get csar_id from nslcm by vnfd_id
112         r2 = [1, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
113         mock_call_req.side_effect = [r1, r2]
114         self.nf_inst_id = '1111'
115         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
116         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
117         data = inst_req_data
118         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
119         self.assert_job_result(self.job_id, 255, "Failed to query rawdata of CSAR(2222) from catalog.")
120
121     @mock.patch.object(restcall, 'call_req')
122     def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
123         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
124                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
125                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
126         r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}), '200']  # get csar_id from nslcm by vnfd_id
127         r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
128         r3 = [1, json.JSONEncoder().encode({"vim":
129                                                 {
130                                                     "vimid": '1',
131                                                     "accessinfo": {"tenant": '2'}
132                                                  }
133                                             }), '200']  # apply_grant_to_nfvo
134         mock_call_req.side_effect = [r1, r2, r3]
135         self.nf_inst_id = '1111'
136         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
137         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
138         data = inst_req_data
139         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
140         self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
141     
142     # @mock.patch.object(restcall, 'call_req')
143     # @mock.patch.object(api, 'call')
144     # def test_instantiate_vnf_when_(self, mock_call, mock_call_req):
145     #     NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
146     #                                version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
147     #                                nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
148     #     r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}), '200']  # get csar_id from nslcm by vnfd_id
149     #     r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
150     #     r3 = [0, json.JSONEncoder().encode({"vim":{"vimid": '1', "accessinfo": {"tenant": '2'}}}), '200']  # apply_grant_to_nfvo
151     #     mock_call_req.side_effect = [r1, r2, r3]
152     #     c1_data = {  # get_tenant_id
153     #         "tenants": [
154     #             {
155     #                 "id": "1",
156     #                 "name": "tenantname_1"
157     #             }
158     #         ]
159     #     }
160     #     c2_data = {  # create_volume
161     #         "id": "bc9eebdbbfd356458269340b9ea6fb73",
162     #         "name": "volume1",
163     #         # "returnCode": 1,
164     #         "vimId": "vim_volume_1",
165     #         "vimName": "vim_volume_1",
166     #         "tenantId": "vim_volume_1",
167     #         "volumeType": "123",
168     #         "availabilityZone": "availabilityZone",
169     #         "status": "avaluable"
170     #     }
171     #     c3_data = {  # get_volume
172     #         "status": "available11",
173     #         "name": "wangsong",
174     #         "attachments": [
175     #             {
176     #                 "device": "/dev/vdc",
177     #                 "serverId": "3030e666-528e-4954-88f5-cc21dab1262b",
178     #                 "volumeId": "4bd3e9eb-cd8b-456a-8589-910836a0ab31",
179     #                 "hostName": None,
180     #                 "id": "4bd3e9eb-cd8b-456a-8589-910836a0ab31"
181     #             }
182     #         ],
183     #         "createTime": "2015-12-02T06:39:40.000000",
184     #         "type": None,
185     #         "id": "4bd3e9eb-cd8b-456a-8589-910836a0ab31",
186     #         "size": 40
187     #     }
188     #     mock_call.side_effect = [c1_data, c2_data, c3_data]
189     #
190     #     self.nf_inst_id = '1111'
191     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
192     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
193     #     data = inst_req_data
194     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
195     #     self.assert_job_result(self.job_id, 255, "unexpected exception")
196
197
198
199
200
201
202     # @mock.patch.object(restcall, 'call_req')
203     # @mock.patch.object(api, 'call')
204     # def test_instantiate_vnf_when_111(self, mock_call, mock_call_req):
205     #     NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
206     #                                version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
207     #                                nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
208     #     r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}),
209     #           '200']  # get csar_id from nslcm by vnfd_id
210     #     r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
211     #     r3 = [0, json.JSONEncoder().encode({"vim": {"vimid": '1', "accessinfo": {"tenant": '2'}}}),
212     #           '200']  # apply_grant_to_nfvo
213     #     mock_call_req.side_effect = [r1, r2, r3]
214     #     c1_data_get_tenant_id = {  # get_tenant_id
215     #         "tenants": [
216     #             {
217     #                 "id": "1",
218     #                 "name": "tenantname_1"
219     #             }
220     #         ]
221     #     }
222     #     c2_data_create_volume = {
223     #         "id": "4bd3e9eb-cd8b-456a-8589-910836a0ab31",
224     #         "name": "volume1",
225     #         "returnCode": 1,
226     #         "vimId": "vim_volume_1",
227     #         "vimName": "vim_volume_1",
228     #         "tenantId": "vim_volume_1",
229     #         "volumeType": "123",
230     #         "availabilityZone": "availabilityZone",
231     #         "status": "availuable",
232     #         "createTime": "2015-12-02T06:39:40.000000",
233     #         "type": None,
234     #         "size": 40
235     #     }
236     #     c3_data_get_volume = {  # get_volume
237     #         "status": "available",
238     #         "name": "wangsong",
239     #         "attachments": [
240     #             {
241     #                 "device": "/dev/vdc",
242     #                 "serverId": "3030e666-528e-4954-88f5-cc21dab1262b",
243     #                 "volumeId": "4bd3e9eb-cd8b-456a-8589-910836a0ab31",
244     #                 "hostName": None,
245     #                 "id": "4bd3e9eb-cd8b-456a-8589-910836a0ab31"
246     #             }
247     #         ],
248     #         "createTime": "2015-12-02T06:39:40.000000",
249     #         "type": None,
250     #         "id": "4bd3e9eb-cd8b-456a-8589-910836a0ab31",
251     #         "size": 40
252     #     }
253     #     c4_data_create_network = {  # create_network
254     #         "returnCode": 0,
255     #         "vimId": "11111",
256     #         "vimName": "11111",
257     #         "status": "ACTIVE",
258     #         "id": "3c9eebdbbfd345658269340b9ea6fb73",
259     #         "name": "net1",
260     #         "tenantId": "tenant1",
261     #         "networkName": "ommnet",
262     #         "shared": True,
263     #         "vlanTransparent": True,
264     #         "networkType": "vlan",
265     #         "segmentationId": 202,
266     #         "physicalNetwork": "ctrl",
267     #         "routerExternal": False
268     #     }
269     #     c5_data_create_subnet = {
270     #         "returnCode": 0,
271     #         "vimId": "11111",
272     #         "vimName": "11111",
273     #         "status": " ACTIVE",
274     #         "id": " d62019d3-bc6e-4319-9c1d-6722fc136a23",
275     #         "tenantId": "tenant1",
276     #         "networkId": "d32019d3-bc6e-4319-9c1d-6722fc136a22",
277     #         "name": "subnet1",
278     #         "cidr": "10.43.35.0/24",
279     #         "ipVersion": 4,
280     #         "enableDhcp": 1,
281     #         "gatewayIp": "10.43.35.1",
282     #         "dnsNameservers": [],
283     #         "allocationPools": [
284     #             {
285     #                 "start": "192.168.199.2",
286     #                 "end": "192.168.199.254"
287     #             }
288     #         ],
289     #         "hostRoutes": []
290     #     }
291     #     mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
292     #                              c4_data_create_network, c5_data_create_subnet]
293     #
294     #     self.nf_inst_id = '1111'
295     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
296     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
297     #     data = inst_req_data
298     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
299     #     self.assert_job_result(self.job_id, 255, "unexpected exception")
300
301
302
303
304     # @mock.patch.object(restcall, 'call_req')
305     # # @mock.patch.object(adaptor, 'create_vim_res')
306     # def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
307     #     NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
308     #                                     nfvouser='root', nfvopassword='root123')
309     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
310     #     r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
311     #     r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
312     #     # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
313     #     mock_call_req.side_effect = [r1, r2, r3]
314     #     # mock_create_vim_res.re.return_value = None
315     #     create_data = {
316     #         "vnfdId": "111",
317     #         "vnfInstanceName": "vFW_01",
318     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
319     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
320     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
321     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
322     #     data = inst_req_data
323     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
324     #     self.assert_job_result(self.job_id, 255, "Create resource failed")
325
326     # @mock.patch.object(restcall, 'call_req')
327     # # @mock.patch.object(adaptor, 'create_vim_res')
328     # def test_instantiate_vnf_success(self, mock_call_req):
329     #     NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
330     #                                     nfvouser='root', nfvopassword='root123')
331     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
332     #     r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
333     #     r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
334     #     r4 = [0, json.JSONEncoder().encode('None'), '200']
335     #     mock_call_req.side_effect = [r1, r2, r3, r4]
336     #     # mock_create_vim_res.re.return_value = None
337     #     create_data = {
338     #         "vnfdId": "111",
339     #         "vnfInstanceName": "vFW_01",
340     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
341     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
342     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
343     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
344     #     data = inst_req_data
345     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
346     #     self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
347
348 inst_req_data = {
349     "flavourId": "flavour_1",
350     "instantiationLevelId": "instantiationLevel_1",
351     "extVirtualLinks": [
352         {
353             "vlInstanceId": "1",
354             "vim": {
355                 "vimInfoId": "1",
356                 "vimId": "1",
357                 "interfaceInfo": {
358                     "vimType": "vim",
359                     "apiVersion": "v2",
360                     "protocolType": "http"
361                 },
362                 "accessInfo": {
363                     "tenant": "tenant_vCPE",
364                     "username": "vCPE",
365                     "password": "vCPE_321"
366                 },
367                 "interfaceEndpoint": "http://10.43.21.105:80/"
368             },
369             "resourceId": "1246",
370             "extCps": [
371                 {
372                     "cpdId": "11",
373                     "addresses": [
374                         {
375                             "addressType": "MAC",
376                             "l2AddressData": "00:f3:43:20:a2:a3"
377                         },
378                         {
379                             "addressType": "IP",
380                             "l3AddressData": {
381                                 "iPAddressType": "IPv4",
382                                 "iPAddress": "192.168.104.2"
383                             }
384                         }
385                     ],
386                     "numDynamicAddresses": 0
387                 }
388             ]
389         }
390     ],
391     "localizationLanguage": "en_US",
392     "additionalParams": {}
393 }