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