Modify const file 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, 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
23 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
24 from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, \
25     SubNetworkInstModel, PortInstModel
26 from lcm.pub.utils import restcall
27 from lcm.pub.utils.jobutil import JobUtil
28 from lcm.pub.utils.timeutil import now_time
29 from lcm.pub.vimapi import adaptor
30 from lcm.pub.vimapi import api
31
32
33 class TestNFInstantiate(TestCase):
34     def setUp(self):
35         self.client = Client()
36         VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
37                                    operationalstate=1)
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')
46
47     def tearDown(self):
48         pass
49         VmInstModel.objects.all().delete()
50         NetworkInstModel.objects.all().delete()
51         SubNetworkInstModel.objects.all().delete()
52         PortInstModel.objects.all().delete()
53
54     def assert_job_result(self, job_id, job_progress, job_detail):
55         jobs = JobStatusModel.objects.filter(
56             jobid=job_id,
57             progress=job_progress,
58             descp=job_detail)
59         self.assertEqual(1, len(jobs))
60
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)
64
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]
70         data = {
71             "vnfdId": "111",
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())
78
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)
84
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(
90             jobid=self.job_id,
91             progress=0,
92             descp="INST_VNF_READY")
93         self.assertEqual(1, len(jobs))
94         data = inst_req_data
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.")
97
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")
108         data = inst_req_data
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.")
111
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")
123         data = inst_req_data
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.")
126
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":
135                                                 {
136                                                     "vimid": '1',
137                                                     "accessinfo": {"tenant": '2'}
138                                                  }
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")
144         data = inst_req_data
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")
147
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
159             "tenants": [
160                 {
161                     "id": "1",
162                     "name": "tenantname_1"
163                 }
164             ]
165         }
166         c2_data = {
167             "returnCode": 0,
168             "vimId": "11111",
169             "vimName": "11111",
170             "status": "ACTIVE",
171             "id": "3c9eebdbbfd345658269340b9ea6fb73",
172             "name": "net1",
173             "tenantId": "tenant1",
174             "networkName": "ommnet",
175             "shared": 1,
176             "vlanTransparent": 1,
177             "networkType": "vlan",
178             "segmentationId": 202,
179             "physicalNetwork": "ctrl",
180             "routerExternal": 0
181         }
182         c3_data = {  # get_volume
183             "status": "available11",
184             "name": "wangsong",
185             "attachments": [
186                 {
187                     "device": "/dev/vdc",
188                     "serverId": "3030e666-528e-4954-88f5-cc21dab1262b",
189                     "volumeId": "4bd3e9eb-cd8b-456a-8589-910836a0ab31",
190                     "hostName": None,
191                     "id": "4bd3e9eb-cd8b-456a-8589-910836a0ab31"
192                 }
193             ],
194             "createTime": "2015-12-02T06:39:40.000000",
195             "type": None,
196             "id": "4bd3e9eb-cd8b-456a-8589-910836a0ab31",
197             "size": 40
198         }
199         mock_call.side_effect = [c1_data, c2_data, c3_data]
200
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")
204         data = inst_req_data
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")
207
208
209
210
211
212
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         mock_call_req.side_effect = [r1, r2, r3]
225         mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
226                                  c4_data_create_network, c5_data_create_subnet, c6_data_create_port]
227
228         self.nf_inst_id = '1111'
229         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
230         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
231         data = inst_req_data
232         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
233         self.assert_job_result(self.job_id, 255, "unexpected exception")
234
235
236
237
238     # @mock.patch.object(restcall, 'call_req')
239     # # @mock.patch.object(adaptor, 'create_vim_res')
240     # def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
241     #     NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
242     #                                     nfvouser='root', nfvopassword='root123')
243     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
244     #     r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
245     #     r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
246     #     # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
247     #     mock_call_req.side_effect = [r1, r2, r3]
248     #     # mock_create_vim_res.re.return_value = None
249     #     create_data = {
250     #         "vnfdId": "111",
251     #         "vnfInstanceName": "vFW_01",
252     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
253     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
254     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
255     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
256     #     data = inst_req_data
257     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
258     #     self.assert_job_result(self.job_id, 255, "Create resource failed")
259
260     # @mock.patch.object(restcall, 'call_req')
261     # # @mock.patch.object(adaptor, 'create_vim_res')
262     # def test_instantiate_vnf_success(self, mock_call_req):
263     #     NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
264     #                                     nfvouser='root', nfvopassword='root123')
265     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
266     #     r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
267     #     r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
268     #     r4 = [0, json.JSONEncoder().encode('None'), '200']
269     #     mock_call_req.side_effect = [r1, r2, r3, r4]
270     #     # mock_create_vim_res.re.return_value = None
271     #     create_data = {
272     #         "vnfdId": "111",
273     #         "vnfInstanceName": "vFW_01",
274     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
275     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
276     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
277     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
278     #     data = inst_req_data
279     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
280     #     self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
281
282 inst_req_data = {
283     "flavourId": "flavour_1",
284     "instantiationLevelId": "instantiationLevel_1",
285     "extVirtualLinks": [
286         {
287             "vlInstanceId": "1",
288             "vim": {
289                 "vimInfoId": "1",
290                 "vimId": "1",
291                 "interfaceInfo": {
292                     "vimType": "vim",
293                     "apiVersion": "v2",
294                     "protocolType": "http"
295                 },
296                 "accessInfo": {
297                     "tenant": "tenant_vCPE",
298                     "username": "vCPE",
299                     "password": "vCPE_321"
300                 },
301                 "interfaceEndpoint": "http://10.43.21.105:80/"
302             },
303             "resourceId": "1246",
304             "extCps": [
305                 {
306                     "cpdId": "11",
307                     "addresses": [
308                         {
309                             "addressType": "MAC",
310                             "l2AddressData": "00:f3:43:20:a2:a3"
311                         },
312                         {
313                             "addressType": "IP",
314                             "l3AddressData": {
315                                 "iPAddressType": "IPv4",
316                                 "iPAddress": "192.168.104.2"
317                             }
318                         }
319                     ],
320                     "numDynamicAddresses": 0
321                 }
322             ]
323         }
324     ],
325     "localizationLanguage": "en_US",
326     "additionalParams": {}
327 }