2d9ae5417868132e38b80a3d3978e0fd91108b02
[vfc/nfvo/lcm.git] / lcm / packages / tests / test_sdc_nf.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 import json
15
16 import mock
17 import random
18 from django.test import Client
19 from django.test import TestCase
20 from rest_framework import status
21
22 from lcm.packages.sdc_nf_package import SdcNfDistributeThread, SdcNfPkgDeleteThread
23 from lcm.pub.database.models import JobStatusModel, JobModel
24 from lcm.pub.database.models import NfPackageModel, NfInstModel
25 from lcm.pub.msapi import sdc
26 from lcm.pub.utils import restcall, toscaparser, idutil
27
28
29 class TestNfPackage(TestCase):
30     def setUp(self):
31         self.client = Client()
32         NfPackageModel.objects.filter().delete()
33         NfInstModel.objects.filter().delete()
34         JobModel.objects.filter().delete()
35         idutil.get_auto_id = mock.Mock()
36         idutil.get_auto_id.return_value = random.random()
37         JobStatusModel.objects.filter().delete()
38         self.vnfd_data = {
39             "volume_storages": [
40                 {
41                     "properties": {
42                         "size_of_storage": {
43                             "factor": 10,
44                             "value": 10000000000,
45                             "unit": "GB",
46                             "unit_size": 1000000000
47                         },
48                         "type_of_storage": "volume",
49                         "rdma_enabled": False,
50                         "size": "10 GB"
51                     },
52                     "volume_storage_id": "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7",
53                     "description": ""
54                 }
55             ],
56             "inputs": {},
57             "vdus": [
58                 {
59                     "volume_storages": [
60                         "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7"
61                     ],
62                     "description": "",
63                     "dependencies": [],
64                     "vls": [],
65                     "properties": {
66                         "name": "vNat",
67                         "configurable_properties": {
68                             "test": {
69                                 "additional_vnfc_configurable_properties": {
70                                     "aaa": "1",
71                                     "bbb": "2",
72                                     "ccc": "3"
73                                 }
74                             }
75                         },
76                         "description": "the virtual machine of vNat",
77                         "nfvi_constraints": [
78                             "test"
79                         ],
80                         "boot_order": [
81                             "vNAT_Storage"
82                         ]
83                     },
84                     "vdu_id": "vdu_vNat",
85                     "artifacts": [
86                         {
87                             "artifact_name": "vNatVNFImage",
88                             "type": "tosca.artifacts.nfv.SwImage",
89                             "properties": {
90                                 "operating_system": "linux",
91                                 "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
92                                 "name": "vNatVNFImage",
93                                 "container_format": "bare",
94                                 "min_ram": "1 GB",
95                                 "disk_format": "qcow2",
96                                 "supported_virtualisation_environments": [
97                                     "test_0"
98                                 ],
99                                 "version": "1.0",
100                                 "checksum": "5000",
101                                 "min_disk": "10 GB",
102                                 "size": "10 GB"
103                             },
104                             "file": "/swimages/vRouterVNF_ControlPlane.qcow2"
105                         }
106                     ],
107                     "nfv_compute": {
108                         "flavor_extra_specs": {
109                             "hw:cpu_sockets": "2",
110                             "sw:ovs_dpdk": "true",
111                             "hw:cpu_threads": "2",
112                             "hw:numa_mem.1": "3072",
113                             "hw:numa_mem.0": "1024",
114                             "hw:numa_nodes": "2",
115                             "hw:numa_cpus.0": "0,1",
116                             "hw:numa_cpus.1": "2,3,4,5",
117                             "hw:cpu_cores": "2",
118                             "hw:cpu_threads_policy": "isolate"
119                         },
120                         "cpu_frequency": "2.4 GHz",
121                         "num_cpus": 2,
122                         "mem_size": "10 GB"
123                     },
124                     "local_storages": [],
125                     "image_file": "vNatVNFImage",
126                     "cps": []
127                 }
128             ],
129             "image_files": [
130                 {
131                     "properties": {
132                         "operating_system": "linux",
133                         "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
134                         "name": "vNatVNFImage",
135                         "container_format": "bare",
136                         "min_ram": "1 GB",
137                         "disk_format": "qcow2",
138                         "supported_virtualisation_environments": [
139                             "test_0"
140                         ],
141                         "version": "1.0",
142                         "checksum": "5000",
143                         "min_disk": "10 GB",
144                         "size": "10 GB"
145                     },
146                     "image_file_id": "vNatVNFImage",
147                     "description": ""
148                 }
149             ],
150             "routers": [],
151             "local_storages": [],
152             "vnf_exposed": {
153                 "external_cps": [
154                     {
155                         "key_name": "sriov_plane",
156                         "cp_id": "SRIOV_Port"
157                     }
158                 ],
159                 "forward_cps": []
160             },
161             "vls": [
162                 {
163                     "route_id": "",
164                     "vl_id": "sriov_link",
165                     "route_external": False,
166                     "description": "",
167                     "properties": {
168                         "vl_flavours": {
169                             "vl_id": "aaaa"
170                         },
171                         "connectivity_type": {
172                             "layer_protocol": "ipv4",
173                             "flow_pattern": "flat"
174                         },
175                         "description": "sriov_link",
176                         "test_access": [
177                             "test"
178                         ]
179                     }
180                 }
181             ],
182             "cps": [
183                 {
184                     "vl_id": "sriov_link",
185                     "vdu_id": "vdu_vNat",
186                     "description": "",
187                     "cp_id": "SRIOV_Port",
188                     "properties": {
189                         "address_data": [
190                             {
191                                 "address_type": "ip_address",
192                                 "l3_address_data": {
193                                     "ip_address_type": "ipv4",
194                                     "floating_ip_activated": False,
195                                     "number_of_ip_address": 1,
196                                     "ip_address_assignment": True
197                                 }
198                             }
199                         ],
200                         "description": "sriov port",
201                         "layer_protocol": "ipv4",
202                         "virtual_network_interface_requirements": [
203                             {
204                                 "requirement": {
205                                     "SRIOV": "true"
206                                 },
207                                 "support_mandatory": False,
208                                 "name": "sriov",
209                                 "description": "sriov"
210                             },
211                             {
212                                 "requirement": {
213                                     "SRIOV": "False"
214                                 },
215                                 "support_mandatory": False,
216                                 "name": "normal",
217                                 "description": "normal"
218                             }
219                         ],
220                         "role": "root",
221                         "bitrate_requirement": 10
222                     }
223                 }
224             ],
225             "metadata": {
226                 "vnfSoftwareVersion": "1.0.0",
227                 "vnfProductName": "zte",
228                 "localizationLanguage": [
229                     "english",
230                     "chinese"
231                 ],
232                 "vnfProvider": "zte",
233                 "vnfmInfo": "zte",
234                 "defaultLocalizationLanguage": "english",
235                 "vnfdId": "zte-hss-1.0",
236                 "id": "zte-hss-1.0",
237                 "vnfProductInfoDescription": "hss",
238                 "vnfdVersion": "1.0.0",
239                 "vnfProductInfoName": "hss"
240             }
241         }
242
243     def tearDown(self):
244         pass
245
246     def assert_job_result(self, job_id, job_progress, job_detail):
247         jobs = JobStatusModel.objects.filter(
248             jobid=job_id,
249             progress=job_progress,
250             descp=job_detail)
251         self.assertEqual(1, len(jobs))
252
253     @mock.patch.object(SdcNfDistributeThread, 'run')
254     def test_nf_pkg_distribute_normal(self, mock_run):
255         resp = self.client.post("/api/nslcm/v1/vnfpackage", {"csarId": "1", "vimIds": ["1"]}, format='json')
256         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
257
258     def test_nf_pkg_distribute_when_csar_already_exist(self):
259         NfPackageModel(uuid="1", nfpackageid="1", vnfdid="vcpe_vfw_zte_1_0").save()
260         SdcNfDistributeThread(csar_id="1", vim_ids=["1"], lab_vim_id="", job_id="2").run()
261         self.assert_job_result("2", 255, "NF CSAR(1) already exists.")
262
263     @mock.patch.object(restcall, 'call_req')
264     @mock.patch.object(sdc, 'download_artifacts')
265     @mock.patch.object(toscaparser, 'parse_vnfd')
266     def test_nf_pkg_distribute_when_vnfd_already_exist(self, mock_parse_vnfd, mock_download_artifacts,
267                                                        mock_call_req):
268         mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data)
269         mock_download_artifacts.return_value = "/home/hss.csar"
270         mock_call_req.return_value = [0, json.JSONEncoder().encode([{
271             "uuid": "1",
272             "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/hss.csar"
273         }]), '200']
274         NfPackageModel(uuid="2", nfpackageid="2", vnfdid="zte-hss-1.0").save()
275         SdcNfDistributeThread(csar_id="1", vim_ids=["1"], lab_vim_id="", job_id="2").run()
276         self.assert_job_result("2", 255, "NFD(zte-hss-1.0) already exists.")
277
278     @mock.patch.object(restcall, 'call_req')
279     @mock.patch.object(sdc, 'download_artifacts')
280     @mock.patch.object(toscaparser, 'parse_vnfd')
281     def test_nf_pkg_distribute_successfully(self, mock_parse_vnfd, mock_download_artifacts, mock_call_req):
282         mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data)
283         mock_download_artifacts.return_value = "/home/hss.csar"
284         mock_call_req.return_value = [0, json.JSONEncoder().encode([{
285             "uuid": "1",
286             "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/hss.csar"
287         }]), '200']
288         SdcNfDistributeThread(csar_id="1", vim_ids=["1"], lab_vim_id="", job_id="4").run()
289         self.assert_job_result("4", 100, "CSAR(1) distribute successfully.")
290
291     ###############################################################################################################
292
293     @mock.patch.object(SdcNfPkgDeleteThread, 'run')
294     def test_nf_pkg_delete_normal(self, mock_run):
295         resp = self.client.delete("/api/nslcm/v1/vnfpackage/1")
296         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
297
298     def test_nf_pkg_normal_delete(self):
299         NfPackageModel(uuid="2", nfpackageid="2", vnfdid="vcpe_vfw_zte_1_0").save()
300         SdcNfPkgDeleteThread(csar_id="2", job_id="2", force_delete=False).run()
301         self.assert_job_result("2", 100, "Delete CSAR(2) successfully.")
302
303     def test_nf_pkg_force_delete(self):
304         NfPackageModel(uuid="1", nfpackageid="1", vnfdid="vcpe_vfw_zte_1_0").save()
305         NfInstModel(nfinstid="1", package_id="1").save()
306         SdcNfPkgDeleteThread(csar_id="1", job_id="2", force_delete=True).run()
307         self.assert_job_result("2", 100, "Delete CSAR(1) successfully.")
308
309     def test_nf_pkg_delete_when_pkg_in_using(self):
310         NfPackageModel(uuid="3", nfpackageid="3", vnfdid="vcpe_vfw_zte_1_0").save()
311         NfInstModel(nfinstid="3", package_id="3").save()
312         SdcNfPkgDeleteThread(csar_id="3", job_id="2", force_delete=False).run()
313         self.assert_job_result("2", 255, "NfInst by csar(3) exists, cannot delete.")
314
315     def test_nf_pkg_get_all(self):
316         NfPackageModel(uuid="3", nfpackageid="3", vnfdid="4").save()
317
318         resp = self.client.get("/api/nslcm/v1/vnfpackage")
319         self.assertEqual(resp.status_code, status.HTTP_200_OK)
320         self.assertEqual({"csars": [{"csarId": "3", "vnfdId": "4"}]}, resp.data)
321
322     @mock.patch.object(restcall, 'call_req')
323     def test_nf_pkg_get_one(self, mock_call_req):
324         NfPackageModel(uuid="4", nfpackageid="4", vnfdid="5", vendor="6", vnfdversion="7", vnfversion="8").save()
325         NfInstModel(nfinstid="1", package_id="4", nf_name="3").save()
326         mock_call_req.return_value = [0, json.JSONEncoder().encode({
327             "packageInfo": {
328                 "vnfdId": "5",
329                 "vnfdProvider": "6",
330                 "vnfdVersion": "7",
331                 "vnfVersion": "8"
332             }
333         }), '200']
334
335         resp = self.client.get("/api/nslcm/v1/vnfpackage/4")
336         self.assertEqual(resp.status_code, status.HTTP_200_OK)
337         expect_data = {
338             "csarId": "4",
339             "packageInfo": {
340                 "vnfdId": "5",
341                 "vnfdProvider": "6",
342                 "vnfdVersion": "7",
343                 "vnfVersion": "8"
344             },
345             "imageInfo": [],
346             "vnfInstanceInfo": [{
347                 "vnfInstanceId": "1", "vnfInstanceName": "3"
348             }]
349         }
350         self.assertEqual(expect_data, resp.data)