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