1 # Copyright 2018 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
24 class VnfGrantViewTest(unittest.TestCase):
26 self.client = APIClient()
31 @mock.patch.object(restcall, 'call_req')
32 def test_grant_vnf_normal(self, mock_call_req):
38 "operation": "INSTANTIATE",
39 "isAutomaticInvocation": True,
40 "instantiationLevelId": "5",
46 "resourceTemplateId": "3",
48 "vimConnectionId": "4",
49 "resourceProviderId": "5",
51 "vimLevelResourceType": "7"
55 "placementConstraints": [
57 "affinityOrAntiAffinity": "AFFINITY",
63 "vimConnectionId": "2",
64 "resourceProviderId": "3"
71 "sameResourceGroup": True,
76 "vimConnectionId": "2",
77 "resourceProviderId": "3"
82 "additionalParams": {},
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)
101 "id": resp_data.get("id"),
102 "vnfInstanceId": "1",
103 "vnfLcmOpOccId": "2",
111 self.assertEqual(expect_resp_data, resp_data)
113 def test_grant_vnf_when_vnfinst_not_exist(self):
115 "vnfInstanceId": "1",
116 "vnfLcmOpOccId": "2",
119 "operation": "INSTANTIATE",
120 "isAutomaticInvocation": True,
121 "instantiationLevelId": "5",
127 "resourceTemplateId": "3",
130 "placementConstraints": [
132 "affinityOrAntiAffinity": "AFFINITY",
136 "idType": "RES_MGMT",
138 "vimConnectionId": "2",
139 "resourceProviderId": "3"
146 "sameResourceGroup": True,
149 "idType": "RES_MGMT",
151 "vimConnectionId": "2",
152 "resourceProviderId": "3"
157 "additionalParams": {},
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)
170 @mock.patch.object(restcall, 'call_req')
171 def test_grant_vnf(self, mock_call_req):
173 "vnfInstanceId": "1",
174 "vnfLcmOpOccId": "2",
177 "operation": "INSTANTIATE",
178 "isAutomaticInvocation": True,
179 "instantiationLevelId": "5",
185 "resourceTemplateId": "3",
189 "additionalParams": {"vnfmid": "3"},
200 "volume_storages": [],
206 "local_storages": "2",
209 "num_virtual_cpu": "111"
212 "virtual_mem_size": "3 B"
218 "local_storages": [{"local_storage_id": "1"}],
227 "name": "underlayervpn",
228 "csarVersion": "1.0",
230 "csarProvider": "huawei",
233 "id": "ns_underlayervpn_1_0"
239 "csarId": "vOpenNAT",
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"
255 NfInstModel.objects.create(nfinstid='1',
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)
265 "id": resp_data.get("id"),
266 "vnfInstanceId": "1",
267 "vnfLcmOpOccId": "2",
275 self.assertEqual(expect_resp_data, resp_data)
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)
281 def test_notify_vnf_normal(self):
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",
296 "changeType": "ADDED",
298 "vimConnectionId": "string",
299 "resourceProviderId": "string",
300 "resourceId": "string",
301 "vimLevelResourceType": "string"
304 "affectedVnfcCpIds": [],
305 "addedStorageResourceIds": [],
306 "removedStorageResourceIds": [],
308 "affectedVirtualLinks": [{
310 "virtualLinkDescId": "string",
311 "changeType": "ADDED",
313 "vimConnectionId": "string",
314 "resourceProviderId": "string",
315 "resourceId": "string",
316 "vimLevelResourceType": "network",
319 "affectedVirtualStorages": [{
321 "virtualStorageDescId": "string",
322 "changeType": "ADDED",
324 "vimConnectionId": "string",
325 "resourceProviderId": "string",
326 "resourceId": "string",
327 "vimLevelResourceType": "network",
332 "vnfInstanceName": "string",
333 "vnfInstanceDescription": "string",
334 "vnfConfigurableProperties": {
335 "additionalProp1": "string",
336 "additionalProp2": "string",
337 "additionalProp3": "string"
340 "additionalProp1": "string",
341 "additionalProp2": "string",
342 "additionalProp3": "string"
345 "additionalProp1": "string",
346 "additionalProp2": "string",
347 "additionalProp3": "string"
349 "vimConnectionInfo": [{
354 "additionalProp1": "string",
355 "additionalProp2": "string",
356 "additionalProp3": "string"
359 "additionalProp1": "string",
360 "additionalProp2": "string",
361 "additionalProp3": "string"
364 "additionalProp1": "string",
365 "additionalProp2": "string",
366 "additionalProp3": "string"
369 "vnfPkgId": "string",
371 "vnfProvider": "string",
372 "vnfProductName": "string",
373 "vnfSoftwareVersion": "string",
374 "vnfdVersion": "string"
376 "changedExtConnectivity": [{
379 "vimConnectionId": "string",
380 "resourceProviderId": "string",
381 "resourceId": "string",
382 "vimLevelResourceType": "string"
387 "vimConnectionId": "string",
388 "resourceProviderId": "string",
389 "resourceId": "string",
390 "vimLevelResourceType": "string"
392 "cpInstanceId": "string"
414 NfInstModel.objects.create(nfinstid='22',
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)