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