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