f14c6f79d944ad62dcffc16d7b1d50cf8d0b3034
[vfc/nfvo/lcm.git] / lcm / packages / tests / test_sdc_ns.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.database.models import NSDModel, NSInstModel, NfPackageModel
22 from lcm.pub.msapi import sdc
23
24
25 class TestSdcNsPackage(TestCase):
26     def setUp(self):
27         self.client = Client()
28         NSDModel.objects.filter().delete()
29         NSInstModel.objects.filter().delete()
30         NfPackageModel.objects.filter().delete()
31         self.nsd_data = {
32             "vnffgs": [
33                 {
34                     "vnffg_id": "vnffg1",
35                     "description": "",
36                     "members": [
37                         "path1",
38                         "path2"
39                     ],
40                     "properties": {
41                         "vendor": "zte",
42                         "connection_point": [
43                             "m6000_data_in",
44                             "m600_tunnel_cp",
45                             "m6000_data_out"
46                         ],
47                         "version": "1.0",
48                         "constituent_vnfs": [
49                             "VFW",
50                             "VNAT"
51                         ],
52                         "number_of_endpoints": 3,
53                         "dependent_virtual_link": [
54                             "sfc_data_network",
55                             "ext_datanet_net",
56                             "ext_mnet_net"
57                         ]
58                     }
59                 }
60             ],
61             "inputs": {
62                 "sfc_data_network": {
63                     "type": "string",
64                     "value": "sfc_data_network"
65                 },
66                 "externalDataNetworkName": {
67                     "type": "string",
68                     "value": "vlan_4004_tunnel_net"
69                 },
70                 "externalManageNetworkName": {
71                     "type": "string",
72                     "value": "vlan_4008_mng_net"
73                 },
74                 "NatIpRange": {
75                     "type": "string",
76                     "value": "192.167.0.10-192.168.0.20"
77                 },
78                 "externalPluginManageNetworkName": {
79                     "type": "string",
80                     "value": "vlan_4007_plugin_net"
81                 }
82             },
83             "pnfs": [
84                 {
85                     "pnf_id": "m6000_s",
86                     "cps": [],
87                     "description": "",
88                     "properties": {
89                         "vendor": "zte",
90                         "request_reclassification": False ,
91                         "pnf_type": "m6000s",
92                         "version": "1.0",
93                         "management_address": "111111",
94                         "id": "m6000_s",
95                         "nsh_aware": False 
96                     }
97                 }
98             ],
99             "fps": [
100                 {
101                     "properties": {
102                         "symmetric": False ,
103                         "policy": {
104                             "type": "ACL",
105                             "criteria": {
106                                 "dest_port_range": "1-100",
107                                 "ip_protocol": "tcp",
108                                 "source_ip_range": [
109                                     "119.1.1.1-119.1.1.10"
110                                 ],
111                                 "dest_ip_range": [
112                                     {
113                                         "get_input": "NatIpRange"
114                                     }
115                                 ],
116                                 "dscp": 0,
117                                 "source_port_range": "1-100"
118                             }
119                         }
120                     },
121                     "forwarder_list": [
122                         {
123                             "capability": "",
124                             "type": "cp",
125                             "node_name": "m6000_data_out"
126                         },
127                         {
128                             "capability": "",
129                             "type": "cp",
130                             "node_name": "m600_tunnel_cp"
131                         },
132                         {
133                             "capability": "vnat_fw_inout",
134                             "type": "vnf",
135                             "node_name": "VNAT"
136                         }
137                     ],
138                     "description": "",
139                     "fp_id": "path2"
140                 },
141                 {
142                     "properties": {
143                         "symmetric": True,
144                         "policy": {
145                             "type": "ACL",
146                             "criteria": {
147                                 "dest_port_range": "1-100",
148                                 "ip_protocol": "tcp",
149                                 "source_ip_range": [
150                                     "1-100"
151                                 ],
152                                 "dest_ip_range": [
153                                     "1-100"
154                                 ],
155                                 "dscp": 4,
156                                 "source_port_range": "1-100"
157                             }
158                         }
159                     },
160                     "forwarder_list": [
161                         {
162                             "capability": "",
163                             "type": "cp",
164                             "node_name": "m6000_data_in"
165                         },
166                         {
167                             "capability": "",
168                             "type": "cp",
169                             "node_name": "m600_tunnel_cp"
170                         },
171                         {
172                             "capability": "vfw_fw_inout",
173                             "type": "vnf",
174                             "node_name": "VFW"
175                         },
176                         {
177                             "capability": "vnat_fw_inout",
178                             "type": "vnf",
179                             "node_name": "VNAT"
180                         },
181                         {
182                             "capability": "",
183                             "type": "cp",
184                             "node_name": "m600_tunnel_cp"
185                         },
186                         {
187                             "capability": "",
188                             "type": "cp",
189                             "node_name": "m6000_data_out"
190                         }
191                     ],
192                     "description": "",
193                     "fp_id": "path1"
194                 }
195             ],
196             "routers": [],
197             "vnfs": [
198                 {
199                     "vnf_id": "VFW",
200                     "description": "",
201                     "properties": {
202                         "plugin_info": "vbrasplugin_1.0",
203                         "vendor": "zte",
204                         "is_shared": False ,
205                         "adjust_vnf_capacity": True,
206                         "name": "VFW",
207                         "vnf_extend_type": "driver",
208                         "csarVersion": "v1.0",
209                         "csarType": "NFAR",
210                         "csarProvider": "ZTE",
211                         "version": "1.0",
212                         "nsh_aware": True,
213                         "cross_dc": False ,
214                         "vnf_type": "VFW",
215                         "vmnumber_overquota_alarm": True,
216                         "vnfd_version": "1.0.0",
217                         "externalPluginManageNetworkName": "vlan_4007_plugin_net",
218                         "id": "vcpe_vfw_zte_1_0",
219                         "request_reclassification": False 
220                     },
221                     "dependencies": [
222                         {
223                             "key_name": "vfw_ctrl_by_manager_cp",
224                             "vl_id": "ext_mnet_net"
225                         },
226                         {
227                             "key_name": "vfw_data_cp",
228                             "vl_id": "sfc_data_network"
229                         }
230                     ],
231                     "type": "tosca.nodes.nfv.ext.zte.VNF.VFW",
232                     "networks": []
233                 }
234             ],
235             "ns_exposed": {
236                 "external_cps": [],
237                 "forward_cps": []
238             },
239             "policies": [
240                 {
241                     "file_url": "policies/abc.drl",
242                     "name": "aaa"
243                 }
244             ],
245             "vls": [
246                 {
247                     "route_id": "",
248                     "vl_id": "ext_mnet_net",
249                     "route_external": False ,
250                     "description": "",
251                     "properties": {
252                         "name": "vlan_4008_mng_net",
253                         "mtu": 1500,
254                         "location_info": {
255                             "tenant": "admin",
256                             "vimid": 2,
257                             "availability_zone": "nova"
258                         },
259                         "ip_version": 4,
260                         "dhcp_enabled": True,
261                         "network_name": "vlan_4008_mng_net",
262                         "network_type": "vlan"
263                     }
264                 },
265                 {
266                     "route_id": "",
267                     "vl_id": "ext_datanet_net",
268                     "route_external": False ,
269                     "description": "",
270                     "properties": {
271                         "name": "vlan_4004_tunnel_net",
272                         "mtu": 1500,
273                         "location_info": {
274                             "tenant": "admin",
275                             "vimid": 2,
276                             "availability_zone": "nova"
277                         },
278                         "ip_version": 4,
279                         "dhcp_enabled": True,
280                         "network_name": "vlan_4004_tunnel_net",
281                         "network_type": "vlan"
282                     }
283                 },
284                 {
285                     "route_id": "",
286                     "vl_id": "sfc_data_network",
287                     "route_external": False ,
288                     "description": "",
289                     "properties": {
290                         "name": "sfc_data_network",
291                         "dhcp_enabled": True,
292                         "is_predefined": False ,
293                         "location_info": {
294                             "tenant": "admin",
295                             "vimid": 2,
296                             "availability_zone": "nova"
297                         },
298                         "ip_version": 4,
299                         "mtu": 1500,
300                         "network_name": "sfc_data_network",
301                         "network_type": "vlan"
302                     }
303                 }
304             ],
305             "cps": [
306                 {
307                     "pnf_id": "m6000_s",
308                     "vl_id": "path2",
309                     "description": "",
310                     "cp_id": "m6000_data_out",
311                     "properties": {
312                         "direction": "bidirectional",
313                         "vnic_type": "normal",
314                         "bandwidth": 0,
315                         "mac_address": "11-22-33-22-11-44",
316                         "interface_name": "xgei-0/4/1/5",
317                         "ip_address": "176.1.1.2",
318                         "order": 0,
319                         "sfc_encapsulation": "mac"
320                     }
321                 },
322                 {
323                     "pnf_id": "m6000_s",
324                     "vl_id": "ext_datanet_net",
325                     "description": "",
326                     "cp_id": "m600_tunnel_cp",
327                     "properties": {
328                         "direction": "bidirectional",
329                         "vnic_type": "normal",
330                         "bandwidth": 0,
331                         "mac_address": "00-11-00-22-33-00",
332                         "interface_name": "gei-0/4/0/13",
333                         "ip_address": "191.167.100.5",
334                         "order": 0,
335                         "sfc_encapsulation": "mac"
336                     }
337                 },
338                 {
339                     "pnf_id": "m6000_s",
340                     "vl_id": "path2",
341                     "description": "",
342                     "cp_id": "m6000_data_in",
343                     "properties": {
344                         "direction": "bidirectional",
345                         "vnic_type": "normal",
346                         "bandwidth": 0,
347                         "mac_address": "11-22-33-22-11-41",
348                         "interface_name": "gei-0/4/0/7",
349                         "ip_address": "1.1.1.1",
350                         "order": 0,
351                         "sfc_encapsulation": "mac",
352                         "bond": "none"
353                     }
354                 },
355                 {
356                     "pnf_id": "m6000_s",
357                     "vl_id": "ext_mnet_net",
358                     "description": "",
359                     "cp_id": "m600_mnt_cp",
360                     "properties": {
361                         "direction": "bidirectional",
362                         "vnic_type": "normal",
363                         "bandwidth": 0,
364                         "mac_address": "00-11-00-22-33-11",
365                         "interface_name": "gei-0/4/0/1",
366                         "ip_address": "10.46.244.51",
367                         "order": 0,
368                         "sfc_encapsulation": "mac",
369                         "bond": "none"
370                     }
371                 }
372             ],
373             "metadata": {
374                 "invariant_id": "vcpe_ns_sff_1",
375                 "name": "VCPE_NS",
376                 "csarVersion": "v1.0",
377                 "csarType": "NSAR",
378                 "csarProvider": "ZTE",
379                 "version": 1,
380                 "vendor": "ZTE",
381                 "id": "VCPE_NS",
382                 "description": "vcpe_ns"
383             }
384         }
385
386     def tearDown(self):
387         pass
388
389     def test_ns_pkg_distribute_when_ns_exists(self):
390         NSDModel(id="1", nsd_id="2").save()
391         resp = self.client.post("/api/nslcm/v1/nspackage", {"csarId": "1"}, format='json')
392         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
393         self.assertEqual("failed", resp.data["status"])
394         self.assertEqual("NS CSAR(1) already exists.", resp.data["statusDescription"])
395
396     @mock.patch.object(restcall, 'call_req')
397     def test_ns_pkg_distribute_when_csar_not_exist(self, mock_call_req):
398         mock_call_req.return_value = [0, "[]", '200']
399         resp = self.client.post("/api/nslcm/v1/nspackage", {"csarId": "1"}, format='json')
400         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
401         self.assertEqual("failed", resp.data["status"])
402         self.assertEqual("Failed to query artifact(services,1) from sdc.", resp.data["statusDescription"])
403
404     @mock.patch.object(restcall, 'call_req')
405     @mock.patch.object(sdc, 'download_artifacts')
406     @mock.patch.object(toscaparser, 'parse_nsd')
407     def test_ns_pkg_distribute_when_nsd_already_exists(self, 
408         mock_parse_nsd, mock_download_artifacts, mock_call_req):
409         mock_parse_nsd.return_value = json.JSONEncoder().encode(self.nsd_data)
410         mock_download_artifacts.return_value = "/home/vcpe.csar"
411         mock_call_req.return_value = [0, json.JSONEncoder().encode([{
412             "uuid": "1",
413             "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/vcpe.csar"
414             }]), '200']
415         NSDModel(id="2", nsd_id="VCPE_NS").save()
416         resp = self.client.post("/api/nslcm/v1/nspackage", {"csarId": "1"}, format='json')
417         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
418         self.assertEqual("failed", resp.data["status"])
419         self.assertEqual("NSD(VCPE_NS) already exists.", resp.data["statusDescription"])
420
421     @mock.patch.object(restcall, 'call_req')
422     @mock.patch.object(sdc, 'download_artifacts')
423     @mock.patch.object(toscaparser, 'parse_nsd')
424     def test_ns_pkg_distribute_when_nf_not_distributed(self, 
425         mock_parse_nsd, mock_download_artifacts, mock_call_req):
426         mock_parse_nsd.return_value = json.JSONEncoder().encode(self.nsd_data)
427         mock_download_artifacts.return_value = "/home/vcpe.csar"
428         mock_call_req.return_value = [0, json.JSONEncoder().encode([{
429             "uuid": "1",
430             "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/vcpe.csar"
431             }]), '200']
432         resp = self.client.post("/api/nslcm/v1/nspackage", {"csarId": "1"}, format='json')
433         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
434         self.assertEqual("failed", resp.data["status"])
435         self.assertEqual("VNF package(vcpe_vfw_zte_1_0) is not distributed.", resp.data["statusDescription"])
436
437     @mock.patch.object(restcall, 'call_req')
438     @mock.patch.object(sdc, 'download_artifacts')
439     @mock.patch.object(toscaparser, 'parse_nsd')
440     def test_ns_pkg_distribute_when_successfully(self, 
441         mock_parse_nsd, mock_download_artifacts, mock_call_req):
442         mock_parse_nsd.return_value = json.JSONEncoder().encode(self.nsd_data)
443         mock_download_artifacts.return_value = "/home/vcpe.csar"
444         mock_call_req.return_value = [0, json.JSONEncoder().encode([{
445             "uuid": "1",
446             "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/vcpe.csar"
447             }]), '200']
448         NfPackageModel(uuid="1", nfpackageid="1", vnfdid="vcpe_vfw_zte_1_0").save()
449         resp = self.client.post("/api/nslcm/v1/nspackage", {"csarId": "1"}, format='json')
450         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
451         self.assertEqual("success", resp.data["status"])
452         self.assertEqual("CSAR(1) distributed successfully.", resp.data["statusDescription"])
453
454     ###############################################################################################################
455
456     def test_ns_pkg_normal_delete(self):
457         NSDModel(id="8", nsd_id="2").save()
458         resp = self.client.delete("/api/nslcm/v1/nspackage/8")
459         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
460         self.assertEqual("success", resp.data["status"])
461         self.assertEqual("Delete CSAR(8) successfully.", resp.data["statusDescription"])
462
463     def test_ns_pkg_force_delete(self):
464         NSInstModel(id="1", nspackage_id="8").save()
465         NSDModel(id="8", nsd_id="2").save()
466         resp = self.client.delete("/api/nslcm/v1/nspackage/8force")
467         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
468         self.assertEqual("success", resp.data["status"])
469         self.assertEqual("Delete CSAR(8) successfully.", resp.data["statusDescription"])
470
471     def test_ns_pkg_delete_when_pkg_in_using(self):
472         NSInstModel(id="1", nspackage_id="8").save()
473         NSDModel(id="8", nsd_id="2").save()
474         resp = self.client.delete("/api/nslcm/v1/nspackage/8")
475         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
476         self.assertEqual("failed", resp.data["status"])
477         self.assertEqual("CSAR(8) is in using, cannot be deleted.", resp.data["statusDescription"])
478
479     def test_ns_pkg_get_all(self):
480         NSDModel(id="13", nsd_id="2", vendor="3", version="4").save()
481
482         resp = self.client.get("/api/nslcm/v1/nspackage")
483         self.assertEqual(resp.status_code, status.HTTP_200_OK)
484         self.assertEqual({"csars": [{"csarId":"13", "nsdId": "2"}]}, resp.data)
485
486     def test_ns_pkg_get_one(self):
487         NSDModel(id="14", nsd_id="2", vendor="3", version="4").save()
488         NSInstModel(id="1", nspackage_id="14", name="11").save()
489
490         resp = self.client.get("/api/nslcm/v1/nspackage/14")
491         self.assertEqual(resp.status_code, status.HTTP_200_OK)
492         self.assertEqual({"csarId": "14", 
493             "packageInfo": {
494                 "nsdId": "2",
495                 "nsdProvider": "3",
496                 "nsdVersion": "4"
497             }, 
498             "nsInstanceInfo": [{
499                 "nsInstanceId": "1", "nsInstanceName": "11"
500             }]}, resp.data)
501
502
503         
504
505
506
507
508
509
510
511
512