SOL003 API Align
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / tests / grant_vnf_test.py
1 # Copyright 2018 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 unittest
16 import json
17 import mock
18 from rest_framework.test import APIClient
19 from rest_framework import status
20 from lcm.pub.database.models import NfInstModel
21 from lcm.pub.utils import restcall
22
23
24 class VnfGrantViewTest(unittest.TestCase):
25     def setUp(self):
26         self.client = APIClient()
27
28     def tearDown(self):
29         pass
30
31     @mock.patch.object(restcall, 'call_req')
32     def test_grant_vnf_normal(self, mock_call_req):
33         data = {
34             "vnfInstanceId": "1",
35             "vnfLcmOpOccId": "2",
36             "vnfdId": "3",
37             "flavourId": "4",
38             "operation": "INSTANTIATE",
39             "isAutomaticInvocation": True,
40             "instantiationLevelId": "5",
41             "addResources": [
42                 {
43                     "id": "1",
44                     "type": "COMPUTE",
45                     "vduId": "2",
46                     "resourceTemplateId": "3",
47                     "resourceTemplate": {
48                         "vimConnectionId": "4",
49                         "resourceProviderId": "5",
50                         "resourceId": "6",
51                         "vimLevelResourceType": "7"
52                     }
53                 }
54             ],
55             "placementConstraints": [
56                 {
57                     "affinityOrAntiAffinity": "AFFINITY",
58                     "scope": "NFVI_POP",
59                     "resource": [
60                         {
61                             "idType": "RES_MGMT",
62                             "resourceId": "1",
63                             "vimConnectionId": "2",
64                             "resourceProviderId": "3"
65                         }
66                     ]
67                 }
68             ],
69             "vimConstraints": [
70                 {
71                     "sameResourceGroup": True,
72                     "resource": [
73                         {
74                             "idType": "RES_MGMT",
75                             "resourceId": "1",
76                             "vimConnectionId": "2",
77                             "resourceProviderId": "3"
78                         }
79                     ]
80                 }
81             ],
82             "additionalParams": {},
83             "_links": {
84                 "vnfLcmOpOcc": {
85                     "href": "1"
86                 },
87                 "vnfInstance": {
88                     "href": "2"
89                 }
90             }
91         }
92         vimConnections = {
93             "id": "1",
94             "vimId": "1",
95         }
96         mock_call_req.return_value = [0, json.JSONEncoder().encode(vimConnections), '200']
97         response = self.client.post("/api/nslcm/v2/grants", data=data, format='json')
98         self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
99         resp_data = json.loads(response.content)
100         expect_resp_data = {
101             "id": resp_data.get("id"),
102             "vnfInstanceId": "1",
103             "vnfLcmOpOccId": "2",
104             "vimConnections": [
105                 {
106                     "id": "1",
107                     "vimId": "1"
108                 }
109             ]
110         }
111         self.assertEqual(expect_resp_data, resp_data)
112
113     def test_grant_vnf_when_vnfinst_not_exist(self):
114         data = {
115             "vnfInstanceId": "1",
116             "vnfLcmOpOccId": "2",
117             "vnfdId": "3",
118             "flavourId": "4",
119             "operation": "INSTANTIATE",
120             "isAutomaticInvocation": True,
121             "instantiationLevelId": "5",
122             "removeResources": [
123                 {
124                     "id": "1",
125                     "type": "COMPUTE",
126                     "vduId": "2",
127                     "resourceTemplateId": "3",
128                 }
129             ],
130             "placementConstraints": [
131                 {
132                     "affinityOrAntiAffinity": "AFFINITY",
133                     "scope": "NFVI_POP",
134                     "resource": [
135                         {
136                             "idType": "RES_MGMT",
137                             "resourceId": "1",
138                             "vimConnectionId": "2",
139                             "resourceProviderId": "3"
140                         }
141                     ]
142                 }
143             ],
144             "vimConstraints": [
145                 {
146                     "sameResourceGroup": True,
147                     "resource": [
148                         {
149                             "idType": "RES_MGMT",
150                             "resourceId": "1",
151                             "vimConnectionId": "2",
152                             "resourceProviderId": "3"
153                         }
154                     ]
155                 }
156             ],
157             "additionalParams": {},
158             "_links": {
159                 "vnfLcmOpOcc": {
160                     "href": "1"
161                 },
162                 "vnfInstance": {
163                     "href": "2"
164                 }
165             }
166         }
167         response = self.client.post("/api/nslcm/v2/grants", data=data, format='json')
168         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
169
170     @mock.patch.object(restcall, 'call_req')
171     def test_grant_vnf(self, mock_call_req):
172         data = {
173             "vnfInstanceId": "1",
174             "vnfLcmOpOccId": "2",
175             "vnfdId": "3",
176             "flavourId": "4",
177             "operation": "INSTANTIATE",
178             "isAutomaticInvocation": True,
179             "instantiationLevelId": "5",
180             "addResources": [
181                 {
182                     "id": "1",
183                     "type": "COMPUTE",
184                     "vduId": "2",
185                     "resourceTemplateId": "3",
186                     "vdu": "1"
187                 }
188             ],
189             "additionalParams": {"vnfmid": "3"},
190             "_links": {
191                 "vnfLcmOpOcc": {
192                     "href": "1"
193                 },
194                 "vnfInstance": {
195                     "href": "2"
196                 }
197             }
198         }
199         vnfdModel = {
200             "volume_storages": [],
201             "vdus": [{
202                 "vdu_id": "1",
203                 "properties": {
204                     "name": "1"
205                 },
206                 "local_storages": "2",
207                 "virtual_compute": {
208                     "virtual_cpu": {
209                         "num_virtual_cpu": "111"
210                     },
211                     "virtual_memory": {
212                         "virtual_mem_size": "3 B"
213                     }
214                 },
215             }],
216             "image_files": [],
217             "routers": [],
218             "local_storages": [{"local_storage_id": "1"}],
219             "vnf_exposed": {
220                 "external_cps": [],
221                 "forward_cps": []
222             },
223             "ns_vls": [],
224             "cps": [],
225             "metadata": {
226                 "designer": "sdno",
227                 "name": "underlayervpn",
228                 "csarVersion": "1.0",
229                 "csarType": "SSAR",
230                 "csarProvider": "huawei",
231                 "version": "1.0",
232                 "type": "SSAR",
233                 "id": "ns_underlayervpn_1_0"
234             }
235         }
236
237         vnfpackage_info = {
238             "imageInfo": [],
239             "csarId": "vOpenNAT",
240             "packageInfo": {
241                 "csarName": "vOpenNAT.csar",
242                 "vnfdModel": json.dumps(vnfdModel),
243                 "vnfdProvider": "Intel",
244                 "vnfdId": "openNAT_1.0",
245                 "downloadUrl": "http://10.96.33.39:8806/static/catalog/vOpenNAT/vOpenNAT.csar",
246                 "vnfVersion": "v1.0",
247                 "vnfdVersion": "v1.0",
248                 "vnfPackageId": "vOpenNAT"
249             }
250         }
251         vimConnections = {
252             "id": "1",
253             "vimId": "1",
254         }
255         NfInstModel.objects.create(nfinstid='1',
256                                    package_id="2",
257                                    vnfm_inst_id='3')
258         get_vnfpackage = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
259         get_vimConnections = [0, json.JSONEncoder().encode(vimConnections), '200']
260         mock_call_req.side_effect = [get_vnfpackage, get_vimConnections]
261         response = self.client.post("/api/nslcm/v2/grants", data=data, format='json')
262         self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
263         resp_data = json.loads(response.content)
264         expect_resp_data = {
265             "id": resp_data.get("id"),
266             "vnfInstanceId": "1",
267             "vnfLcmOpOccId": "2",
268             "vimConnections": [
269                 {
270                     "id": "1",
271                     "vimId": "1"
272                 }
273             ]
274         }
275         self.assertEqual(expect_resp_data, resp_data)
276
277     def test_get_notify_vnf_normal(self):
278         response = self.client.get("/api/nslcm/v2/ns/1/ns_vnfs/1/Notify")
279         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
280
281     def test_notify_vnf_normal(self):
282         data = {
283             "id": "string",
284             "notificationType": "string",
285             "subscriptionId": "string",
286             "timeStamp": "string",
287             "notificationStatus": "START",
288             "operationState": "STARTING",
289             "vnfInstanceId": "string",
290             "operation": "INSTANTIATE",
291             "isAutomaticInvocation": True,
292             "vnfLcmOpOccId": "string",
293             "affectedVnfcs": [{
294                 "id": "string",
295                 "vduId": "string",
296                 "changeType": "ADDED",
297                 "computeResource": {
298                     "vimConnectionId": "string",
299                     "resourceProviderId": "string",
300                     "resourceId": "string",
301                     "vimLevelResourceType": "string"
302                 },
303                 "metadata": {},
304                 "affectedVnfcCpIds": [],
305                 "addedStorageResourceIds": [],
306                 "removedStorageResourceIds": [],
307             }],
308             "affectedVirtualLinks": [{
309                 "id": "string",
310                 "virtualLinkDescId": "string",
311                 "changeType": "ADDED",
312                 "networkResource": {
313                     "vimConnectionId": "string",
314                     "resourceProviderId": "string",
315                     "resourceId": "string",
316                     "vimLevelResourceType": "network",
317                 }
318             }],
319             "affectedVirtualStorages": [{
320                 "id": "string",
321                 "virtualStorageDescId": "string",
322                 "changeType": "ADDED",
323                 "storageResource": {
324                     "vimConnectionId": "string",
325                     "resourceProviderId": "string",
326                     "resourceId": "string",
327                     "vimLevelResourceType": "network",
328                 },
329                 "metadata": {}
330             }],
331             "changedInfo": {
332                 "vnfInstanceName": "string",
333                 "vnfInstanceDescription": "string",
334                 "vnfConfigurableProperties": {
335                     "additionalProp1": "string",
336                     "additionalProp2": "string",
337                     "additionalProp3": "string"
338                 },
339                 "metadata": {
340                     "additionalProp1": "string",
341                     "additionalProp2": "string",
342                     "additionalProp3": "string"
343                 },
344                 "extensions": {
345                     "additionalProp1": "string",
346                     "additionalProp2": "string",
347                     "additionalProp3": "string"
348                 },
349                 "vimConnectionInfo": [{
350                     "id": "string",
351                     "vimId": "string",
352                     "vimType": "string",
353                     "interfaceInfo": {
354                         "additionalProp1": "string",
355                         "additionalProp2": "string",
356                         "additionalProp3": "string"
357                     },
358                     "accessInfo": {
359                         "additionalProp1": "string",
360                         "additionalProp2": "string",
361                         "additionalProp3": "string"
362                     },
363                     "extra": {
364                         "additionalProp1": "string",
365                         "additionalProp2": "string",
366                         "additionalProp3": "string"
367                     }
368                 }],
369                 "vnfPkgId": "string",
370                 "vnfdId": "string",
371                 "vnfProvider": "string",
372                 "vnfProductName": "string",
373                 "vnfSoftwareVersion": "string",
374                 "vnfdVersion": "string"
375             },
376             "changedExtConnectivity": [{
377                 "id": "string",
378                 "resourceHandle": {
379                     "vimConnectionId": "string",
380                     "resourceProviderId": "string",
381                     "resourceId": "string",
382                     "vimLevelResourceType": "string"
383                 },
384                 "extLinkPorts": [{
385                     "id": "string",
386                     "resourceHandle": {
387                         "vimConnectionId": "string",
388                         "resourceProviderId": "string",
389                         "resourceId": "string",
390                         "vimLevelResourceType": "string"
391                     },
392                     "cpInstanceId": "string"
393                 }]
394             }],
395             "error": {
396                 "type": "string",
397                 "title": "string",
398                 "status": 0,
399                 "detail": "string",
400                 "instance": "string"
401             },
402             "_links": {
403                 "vnfInstance": {
404                     "href": "string"
405                 },
406                 "subscription": {
407                     "href": "string"
408                 },
409                 "vnfLcmOpOcc": {
410                     "href": "string"
411                 }
412             }
413         }
414         NfInstModel.objects.create(nfinstid='22',
415                                    mnfinstid='2',
416                                    vnfm_inst_id='1')
417         response = self.client.post("/api/nslcm/v2/ns/1/ns_vnfs/2/Notify", data=data, format='json')
418         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)