cefb86d6cda8db21405c21b0aa93965db8986de8
[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.views import InstantiateVnf
23 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
24 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
25 from lcm.pub.database.models import NfInstModel, JobStatusModel
26 from lcm.pub.utils import restcall
27 from lcm.pub.utils.jobutil import JobUtil
28
29 inst_req_data = {
30     "flavourId": "flavour_1",
31     "instantiationLevelId": "instantiationLevel_1",
32     "extVirtualLinks": [
33         {
34             "vlInstanceId": "1",
35             "vim": {
36                 "vimInfoId": "1",
37                 "vimId": "1",
38                 "interfaceInfo": {
39                     "vimType": "vim",
40                     "apiVersion": "v2",
41                     "protocolType": "http"
42                 },
43                 "accessInfo": {
44                     "tenant": "tenant_vCPE",
45                     "username": "vCPE",
46                     "password": "vCPE_321"
47                 },
48                 "interfaceEndpoint": "http://10.43.21.105:80/"
49             },
50             "resourceId": "1246",
51             "extCps": [
52                 {
53                     "cpdId": "11",
54                     "addresses": [
55                         {
56                             "addressType": "MAC",
57                             "l2AddressData": "00:f3:43:20:a2:a3"
58                         },
59                         {
60                             "addressType": "IP",
61                             "l3AddressData": {
62                                 "iPAddressType": "IPv4",
63                                 "iPAddress": "192.168.104.2"
64                             }
65                         }
66                     ],
67                     "numDynamicAddresses": 0
68                 }
69             ]
70         }
71     ],
72     "localizationLanguage": "en_US",
73     "additionalParams": {}
74 }
75
76
77 class TestNsInstantiate(TestCase):
78     def setUp(self):
79         self.client = Client()
80         self.nsd_id = str(uuid.uuid4())
81
82     def tearDown(self):
83         pass
84
85     def assert_job_result(self, job_id, job_progress, job_detail):
86         jobs = JobStatusModel.objects.filter(
87             jobid=job_id,
88             progress=job_progress,
89             descp=job_detail)
90         self.assertEqual(1, len(jobs))
91
92     @mock.patch.object(restcall, 'call_req')
93     def test_create_vnf_identifier(self, mock_call_req):
94         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
95         mock_call_req.side_effect = [r1]
96         data = {
97             "vnfdId": "111",
98             "vnfInstanceName": "vFW_01",
99             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
100         response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
101         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
102         context = json.loads(response.content)
103         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
104
105     def test_instantiate_vnf(self):
106         response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
107         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
108
109     def test_instantiate_vnf_when_instid_not_exist(self):
110         self.nf_inst_id = str(uuid.uuid4())
111         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
112         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
113         data = inst_req_data
114         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
115         self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
116
117
118 vnfd_model_dict = {
119     'local_storages': [],
120     'vdus': [
121         {
122             'volumn_storages': [],
123             'nfv_compute': {
124                 'mem_size': '',
125                 'num_cpus': u'2'},
126             'local_storages': [],
127             'vdu_id': u'vdu_omm.001',
128             'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
129             'dependencies': [],
130             'vls': [],
131             'cps': [],
132             'properties': {
133                 'key_vdu': '',
134                 'support_scaling': False,
135                 'vdu_type': '',
136                 'name': '',
137                 'storage_policy': '',
138                 'location_info': {
139                     'vimId': '',
140                     'availability_zone': '',
141                     'region': '',
142                     'dc': '',
143                     'host': '',
144                     'tenant': ''},
145                 'inject_data_list': [],
146                 'watchdog': {
147                     'action': '',
148                     'enabledelay': ''},
149                 'local_affinity_antiaffinity_rule': {},
150                 'template_id': u'omm.001',
151                 'manual_scale_select_vim': False},
152             'description': u'singleommvm'},
153         {
154             'volumn_storages': [],
155             'nfv_compute': {
156                 'mem_size': '',
157                 'num_cpus': u'4'},
158             'local_storages': [],
159             'vdu_id': u'vdu_1',
160             'image_file': u'sss',
161             'dependencies': [],
162             'vls': [],
163             'cps': [],
164             'properties': {
165                 'key_vdu': '',
166                 'support_scaling': False,
167                 'vdu_type': '',
168                 'name': '',
169                 'storage_policy': '',
170                 'location_info': {
171                     'vimId': '',
172                     'availability_zone': '',
173                     'region': '',
174                     'dc': '',
175                     'host': '',
176                     'tenant': ''},
177                 'inject_data_list': [],
178                 'watchdog': {
179                     'action': '',
180                     'enabledelay': ''},
181                 'local_affinity_antiaffinity_rule': {},
182                 'template_id': u'1',
183                 'manual_scale_select_vim': False},
184             'description': u'ompvm'},
185         {
186             'volumn_storages': [],
187             'nfv_compute': {
188                 'mem_size': '',
189                 'num_cpus': u'14'},
190             'local_storages': [],
191             'vdu_id': u'vdu_2',
192             'image_file': u'sss',
193             'dependencies': [],
194             'vls': [],
195             'cps': [],
196             'properties': {
197                 'key_vdu': '',
198                 'support_scaling': False,
199                 'vdu_type': '',
200                 'name': '',
201                 'storage_policy': '',
202                 'location_info': {
203                     'vimId': '',
204                     'availability_zone': '',
205                     'region': '',
206                     'dc': '',
207                     'host': '',
208                     'tenant': ''},
209                 'inject_data_list': [],
210                 'watchdog': {
211                     'action': '',
212                     'enabledelay': ''},
213                 'local_affinity_antiaffinity_rule': {},
214                 'template_id': u'2',
215                 'manual_scale_select_vim': False},
216             'description': u'ompvm'},
217         {
218             'volumn_storages': [],
219             'nfv_compute': {
220                 'mem_size': '',
221                 'num_cpus': u'14'},
222             'local_storages': [],
223             'vdu_id': u'vdu_3',
224             'image_file': u'sss',
225             'dependencies': [],
226             'vls': [],
227             'cps': [],
228             'properties': {
229                 'key_vdu': '',
230                 'support_scaling': False,
231                 'vdu_type': '',
232                 'name': '',
233                 'storage_policy': '',
234                 'location_info': {
235                     'vimId': '',
236                     'availability_zone': '',
237                     'region': '',
238                     'dc': '',
239                     'host': '',
240                     'tenant': ''},
241                 'inject_data_list': [],
242                 'watchdog': {
243                     'action': '',
244                     'enabledelay': ''},
245                 'local_affinity_antiaffinity_rule': {},
246                 'template_id': u'3',
247                 'manual_scale_select_vim': False},
248             'description': u'ompvm'},
249         {
250             'volumn_storages': [],
251             'nfv_compute': {
252                 'mem_size': '',
253                 'num_cpus': u'4'},
254             'local_storages': [],
255             'vdu_id': u'vdu_10',
256             'image_file': u'sss',
257             'dependencies': [],
258             'vls': [],
259             'cps': [],
260             'properties': {
261                 'key_vdu': '',
262                 'support_scaling': False,
263                 'vdu_type': '',
264                 'name': '',
265                 'storage_policy': '',
266                 'location_info': {
267                     'vimId': '',
268                     'availability_zone': '',
269                     'region': '',
270                     'dc': '',
271                     'host': '',
272                     'tenant': ''},
273                 'inject_data_list': [],
274                 'watchdog': {
275                     'action': '',
276                     'enabledelay': ''},
277                 'local_affinity_antiaffinity_rule': {},
278                 'template_id': u'10',
279                 'manual_scale_select_vim': False},
280             'description': u'ppvm'},
281         {
282             'volumn_storages': [],
283             'nfv_compute': {
284                 'mem_size': '',
285                 'num_cpus': u'14'},
286             'local_storages': [],
287             'vdu_id': u'vdu_11',
288             'image_file': u'sss',
289             'dependencies': [],
290             'vls': [],
291             'cps': [],
292             'properties': {
293                 'key_vdu': '',
294                 'support_scaling': False,
295                 'vdu_type': '',
296                 'name': '',
297                 'storage_policy': '',
298                 'location_info': {
299                     'vimId': '',
300                     'availability_zone': '',
301                     'region': '',
302                     'dc': '',
303                     'host': '',
304                     'tenant': ''},
305                 'inject_data_list': [],
306                 'watchdog': {
307                     'action': '',
308                     'enabledelay': ''},
309                 'local_affinity_antiaffinity_rule': {},
310                 'template_id': u'11',
311                 'manual_scale_select_vim': False},
312             'description': u'ppvm'},
313         {
314             'volumn_storages': [],
315             'nfv_compute': {
316                 'mem_size': '',
317                 'num_cpus': u'14'},
318             'local_storages': [],
319             'vdu_id': u'vdu_12',
320             'image_file': u'sss',
321             'dependencies': [],
322             'vls': [],
323             'cps': [],
324             'properties': {
325                 'key_vdu': '',
326                 'support_scaling': False,
327                 'vdu_type': '',
328                 'name': '',
329                 'storage_policy': '',
330                 'location_info': {
331                     'vimId': '',
332                     'availability_zone': '',
333                     'region': '',
334                     'dc': '',
335                     'host': '',
336                     'tenant': ''},
337                 'inject_data_list': [],
338                 'watchdog': {
339                     'action': '',
340                     'enabledelay': ''},
341                 'local_affinity_antiaffinity_rule': {},
342                 'template_id': u'12',
343                 'manual_scale_select_vim': False},
344             'description': u'ppvm'}],
345     'volumn_storages': [],
346     'policies': {
347         'scaling': {
348             'targets': {},
349             'policy_id': u'policy_scale_sss-vnf-template',
350             'properties': {
351                 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
352             'description': ''}},
353     'image_files': [
354         {
355             'description': '',
356             'properties': {
357                 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
358                 'checksum': '',
359                 'disk_format': u'VMDK',
360                 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
361                 'container_type': 'vm',
362                 'version': '',
363                 'hypervisor_type': 'kvm'},
364             'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
365         {
366             'description': '',
367             'properties': {
368                 'name': u'sss.vmdk',
369                 'checksum': '',
370                 'disk_format': u'VMDK',
371                 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
372                 'container_type': 'vm',
373                 'version': '',
374                 'hypervisor_type': 'kvm'},
375             'image_file_id': u'sss'}],
376     'vls': [],
377     'cps': [],
378     'metadata': {
379         'vendor': u'zte',
380         'is_shared': False,
381         'description': '',
382         'domain_type': u'CN',
383         'version': u'v4.14.10',
384         'vmnumber_overquota_alarm': False,
385         'cross_dc': False,
386         'vnf_type': u'SSS',
387         'vnfd_version': u'V00000001',
388         'id': u'sss-vnf-template',
389         'name': u'sss-vnf-template'}}