Merge "Ns descriptor related stuffs."
[vfc/nfvo/catalog.git] / catalog / packages / tests / test_vnf_package.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 os
16 import json
17 import mock
18 import urllib2
19
20 from rest_framework.test import APIClient
21 from django.test import TestCase
22 from rest_framework import status
23 from catalog.pub.config.config import CATALOG_ROOT_PATH
24 from catalog.packages.biz.vnf_package import VnfPkgUploadThread
25 from catalog.pub.database.models import VnfPackageModel
26 from catalog.pub.utils import toscaparser
27
28
29 class MockReq():
30     def read(self):
31         return "1"
32
33     def close(self):
34         pass
35
36
37 class TestVnfPackage(TestCase):
38     def setUp(self):
39         self.client = APIClient()
40         self.vnfd_data = {
41             "volume_storages": [
42                 {
43                     "properties": {
44                         "size_of_storage": {
45                             "factor": 10,
46                             "value": 10000000000,
47                             "unit": "GB",
48                             "unit_size": 1000000000
49                         },
50                         "type_of_storage": "volume",
51                         "rdma_enabled": False,
52                         "size": "10 GB"
53                     },
54                     "volume_storage_id": "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7",
55                     "description": ""
56                 }
57             ],
58             "inputs": {},
59             "vdus": [
60                 {
61                     "volume_storages": [
62                         "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7"
63                     ],
64                     "description": "",
65                     "dependencies": [],
66                     "vls": [],
67                     "properties": {
68                         "name": "vNat",
69                         "configurable_properties": {
70                             "test": {
71                                 "additional_vnfc_configurable_properties": {
72                                     "aaa": "1",
73                                     "bbb": "2",
74                                     "ccc": "3"
75                                 }
76                             }
77                         },
78                         "description": "the virtual machine of vNat",
79                         "nfvi_constraints": [
80                             "test"
81                         ],
82                         "boot_order": [
83                             "vNAT_Storage"
84                         ]
85                     },
86                     "vdu_id": "vdu_vNat",
87                     "artifacts": [
88                         {
89                             "artifact_name": "vNatVNFImage",
90                             "type": "tosca.artifacts.nfv.SwImage",
91                             "properties": {
92                                 "operating_system": "linux",
93                                 "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
94                                 "name": "vNatVNFImage",
95                                 "container_format": "bare",
96                                 "min_ram": "1 GB",
97                                 "disk_format": "qcow2",
98                                 "supported_virtualisation_environments": [
99                                     "test_0"
100                                 ],
101                                 "version": "1.0",
102                                 "checksum": "5000",
103                                 "min_disk": "10 GB",
104                                 "size": "10 GB"
105                             },
106                             "file": "/swimages/vRouterVNF_ControlPlane.qcow2"
107                         }
108                     ],
109                     "nfv_compute": {
110                         "flavor_extra_specs": {
111                             "hw:cpu_sockets": "2",
112                             "sw:ovs_dpdk": "true",
113                             "hw:cpu_threads": "2",
114                             "hw:numa_mem.1": "3072",
115                             "hw:numa_mem.0": "1024",
116                             "hw:numa_nodes": "2",
117                             "hw:numa_cpus.0": "0,1",
118                             "hw:numa_cpus.1": "2,3,4,5",
119                             "hw:cpu_cores": "2",
120                             "hw:cpu_threads_policy": "isolate"
121                         },
122                         "cpu_frequency": "2.4 GHz",
123                         "num_cpus": 2,
124                         "mem_size": "10 GB"
125                     },
126                     "local_storages": [],
127                     "image_file": "vNatVNFImage",
128                     "cps": []
129                 }
130             ],
131             "image_files": [
132                 {
133                     "properties": {
134                         "operating_system": "linux",
135                         "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
136                         "name": "vNatVNFImage",
137                         "container_format": "bare",
138                         "min_ram": "1 GB",
139                         "disk_format": "qcow2",
140                         "supported_virtualisation_environments": [
141                             "test_0"
142                         ],
143                         "version": "1.0",
144                         "checksum": "5000",
145                         "min_disk": "10 GB",
146                         "size": "10 GB"
147                     },
148                     "image_file_id": "vNatVNFImage",
149                     "description": ""
150                 }
151             ],
152             "routers": [],
153             "local_storages": [],
154             "vnf_exposed": {
155                 "external_cps": [
156                     {
157                         "key_name": "sriov_plane",
158                         "cp_id": "SRIOV_Port"
159                     }
160                 ],
161                 "forward_cps": []
162             },
163             "vls": [
164                 {
165                     "route_id": "",
166                     "vl_id": "sriov_link",
167                     "route_external": False,
168                     "description": "",
169                     "properties": {
170                         "vl_flavours": {
171                             "vl_id": "aaaa"
172                         },
173                         "connectivity_type": {
174                             "layer_protocol": "ipv4",
175                             "flow_pattern": "flat"
176                         },
177                         "description": "sriov_link",
178                         "test_access": [
179                             "test"
180                         ]
181                     }
182                 }
183             ],
184             "cps": [
185                 {
186                     "vl_id": "sriov_link",
187                     "vdu_id": "vdu_vNat",
188                     "description": "",
189                     "cp_id": "SRIOV_Port",
190                     "properties": {
191                         "address_data": [
192                             {
193                                 "address_type": "ip_address",
194                                 "l3_address_data": {
195                                     "ip_address_type": "ipv4",
196                                     "floating_ip_activated": False,
197                                     "number_of_ip_address": 1,
198                                     "ip_address_assignment": True
199                                 }
200                             }
201                         ],
202                         "description": "sriov port",
203                         "layer_protocol": "ipv4",
204                         "virtual_network_interface_requirements": [
205                             {
206                                 "requirement": {
207                                     "SRIOV": "true"
208                                 },
209                                 "support_mandatory": False,
210                                 "name": "sriov",
211                                 "description": "sriov"
212                             },
213                             {
214                                 "requirement": {
215                                     "SRIOV": "False"
216                                 },
217                                 "support_mandatory": False,
218                                 "name": "normal",
219                                 "description": "normal"
220                             }
221                         ],
222                         "role": "root",
223                         "bitrate_requirement": 10
224                     }
225                 }
226             ],
227             "metadata": {
228                 "vnfSoftwareVersion": "1.0.0",
229                 "vnfProductName": "zte",
230                 "localizationLanguage": [
231                     "english",
232                     "chinese"
233                 ],
234                 "vnfProvider": "zte",
235                 "vnfmInfo": "zte",
236                 "defaultLocalizationLanguage": "english",
237                 "vnfdId": "zte-hss-1.0",
238                 "id": "zte-hss-1.0",
239                 "vnfProductInfoDescription": "hss",
240                 "vnfdVersion": "1.0.0",
241                 "vnfProductInfoName": "hss"
242             }
243         }
244
245     def tearDown(self):
246         pass
247
248     @mock.patch.object(toscaparser, 'parse_vnfd')
249     def test_upload_vnf_pkg(self, mock_parse_vnfd):
250         data = {'file': open(os.path.join(CATALOG_ROOT_PATH, "empty.txt"), "rb")}
251         VnfPackageModel.objects.create(
252             vnfPackageId="222",
253             onboardingState="CREATED"
254         )
255         mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data)
256         response = self.client.put("/api/vnfpkgm/v1/vnf_packages/222/package_content", data=data)
257         vnf_pkg1 = VnfPackageModel.objects.filter(vnfPackageId="222")
258         self.assertEqual("zte-hss-1.0", vnf_pkg1[0].vnfdId)
259         self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
260
261         os.remove(vnf_pkg1[0].localFilePath)
262         os.removedirs(os.path.join(CATALOG_ROOT_PATH, vnf_pkg1[0].vnfPackageId))
263
264     @mock.patch.object(toscaparser, 'parse_vnfd')
265     @mock.patch.object(urllib2, 'urlopen')
266     def test_upload_nf_pkg_from_uri(self, mock_urlopen, mock_parse_vnfd):
267         vnf_pkg = VnfPackageModel.objects.create(
268             vnfPackageId="222",
269             onboardingState="CREATED"
270         )
271         mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data)
272         req_data = {"addressInformation": "https://127.0.0.1:1234/sdc/v1/hss.csar"}
273         mock_urlopen.return_value = MockReq()
274         vnf_pkg_id = vnf_pkg.vnfPackageId
275         VnfPkgUploadThread(req_data, vnf_pkg_id).run()
276         vnf_pkg1 = VnfPackageModel.objects.filter(vnfPackageId="222")
277         self.assertEqual("zte-hss-1.0", vnf_pkg1[0].vnfdId)
278
279         os.remove(vnf_pkg1[0].localFilePath)
280         os.removedirs(os.path.join(CATALOG_ROOT_PATH, vnf_pkg1[0].vnfPackageId))
281
282     def test_create_vnf_pkg(self):
283         req_data = {
284             "userDefinedData": {"a": "A"}
285         }
286         response = self.client.post("/api/vnfpkgm/v1/vnf_packages", data=req_data, format="json")
287         resp_data = json.loads(response.content)
288         expect_resp_data = {
289             "id": resp_data.get("id"),
290             "onboardingState": "CREATED",
291             "operationalState": "DISABLED",
292             "usageState": "NOT_IN_USE",
293             "userDefinedData": {"a": "A"},
294             "_links": None  # TODO
295         }
296         self.assertEqual(expect_resp_data, resp_data)
297         self.assertEqual(response.status_code, status.HTTP_201_CREATED)