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