78514cea9a9406681a609b3356d2150bf481edd9
[vfc/nfvo/lcm.git] / lcm / ns / tests / vls / tests.py
1 # Copyright 2016 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 json
16 import uuid
17
18 import mock
19 from django.test import TestCase, Client
20 from rest_framework import status
21
22 from lcm.pub.database.models import VLInstModel, NSInstModel, VNFFGInstModel
23 from lcm.pub.nfvi.vim import vimadaptor
24 from lcm.pub.utils import restcall
25
26
27 class TestVlViews(TestCase):
28     def setUp(self):
29         self.client = Client()
30         self.ns_inst_id = str(uuid.uuid4())
31         self.vnffg_id = str(uuid.uuid4())
32         self.vl_id_1 = 1
33         self.vl_id_2 = 1
34         self.vim_id = str(uuid.uuid4())
35         self.tenant = "tenantname"
36         properties = {"network_type": "vlan", "name": "externalMNetworkName", "dhcp_enabled": False,
37                       "location_info": {"host": True, "vimid": self.vim_id, "region": True, "tenant": self.tenant},
38                       "end_ip": "190.168.100.100", "gateway_ip": "190.168.100.1", "start_ip": "190.168.100.2",
39                       "cidr": "190.168.100.0/24", "mtu": 1500, "network_name": "sub_mnet", "ip_version": 4}
40         self.context = {
41             "vls": [{"vl_id": self.vl_id_1, "description": "", "properties": properties, "route_external": False},
42                     {"vl_id": self.vl_id_2, "description": "", "properties": properties, "route_external": False}],
43             "vnffgs": [{"vnffg_id": self.vnffg_id, "description": "",
44                         "properties": {"vendor": "zte", "version": "1.1.2", "number_of_endpoints": 7,
45                                        "dependent_virtual_link": [self.vl_id_2, self.vl_id_1],
46                                        "connection_point": ["CP01", "CP02"],
47                                        "constituent_vnfs": ["VNF1", "VNF2", "VNF3"],
48                                        "constituent_pnfs": ["pnf1", "pnf2"]},
49                         "members": ["forwarding_path1", "forwarding_path2"]}]}
50         NSInstModel(id=self.ns_inst_id, name="ns_name").save()
51         VNFFGInstModel(vnffgdid=self.vnffg_id, vnffginstid="", nsinstid=self.ns_inst_id, endpointnumber=0, vllist="",
52                        cplist="", vnflist="", fplist="", status="").save()
53
54     def tearDown(self):
55         VLInstModel.objects.all().delete()
56         NSInstModel.objects.all().delete()
57         VNFFGInstModel.objects.all().delete()
58
59     @mock.patch.object(restcall, "call_req")
60     @mock.patch.object(vimadaptor.VimAdaptor, "create_network")
61     def test_create_vl(self, mock_create_network, mock_req_by_rest):
62         network_id = str(uuid.uuid4())
63         subnetwork_id = str(uuid.uuid4())
64         mock_create_network.return_value = [0,
65                                             {"status": "ACTIVE", "id": network_id, "name": "net1",
66                                              "provider:segmentation_id": 204, "provider:network_type": "vlan",
67                                              "res_type": 1,
68                                              "subnet_list": [
69                                                  {"id": subnetwork_id, "name": "subnet1", "res_type": 1}]}]
70         mock_req_by_rest.return_value = [0,
71                                          '{"test":"test_name","name":"vim_name","type":"type_name","url":"url_add"'
72                                          ',"userName":"user_name","password":"password","tenant":"tenant"}']
73
74         self.create_vl(self.vl_id_1)
75         self.create_vl(self.vl_id_2)
76         vl_from_vl_1 = VLInstModel.objects.filter(vldid=self.vl_id_1, ownerid=self.ns_inst_id)
77         self.assertEqual(network_id, vl_from_vl_1[0].relatednetworkid)
78         self.assertEqual(subnetwork_id, vl_from_vl_1[0].relatedsubnetworkid)
79         #self.assertEqual(self.vim_id, vl_from_vl_1[0].vimid)
80         self.assertEqual(self.tenant, vl_from_vl_1[0].tenant)
81         vl_from_vl_2 = VLInstModel.objects.filter(vldid=self.vl_id_2, ownerid=self.ns_inst_id)
82         self.assertEqual(VNFFGInstModel.objects.filter(vnffgdid=self.vnffg_id, nsinstid=self.ns_inst_id)[0].vllist,
83                          vl_from_vl_2[0].vlinstanceid + "," + vl_from_vl_1[0].vlinstanceid)
84
85     def create_vl(self, vl_id):
86         req_data = {
87             "nsInstanceId": self.ns_inst_id,
88             "context": json.JSONEncoder().encode(self.context),
89             "vlindex": vl_id}
90         response = self.client.post("/api/nslcm/v1/ns/vls", data=req_data)
91         self.assertEqual(status.HTTP_201_CREATED, response.status_code)
92         self.assertEqual(0, response.data["result"])
93
94     @mock.patch.object(restcall, "call_req")
95     @mock.patch.object(vimadaptor.VimAdaptor, "create_network")
96     @mock.patch.object(uuid, "uuid4")
97     def test_create_network_fail_when_send_to_vim(self, mock_uuid4, mock_create_network, mock_req_by_rest):
98         req_data = {
99             "nsInstanceId": self.ns_inst_id,
100             "context": json.JSONEncoder().encode(self.context),
101             "vlindex": self.vl_id_1}
102         mock_uuid4.return_value = '999'
103         mock_req_by_rest.return_value = [0,
104                                          '{"test":"test_name","name":"vim_name","type":"type_name","url":"url_add"'
105                                          ',"userName":"user_name","password":"password","tenant":"tenant"}']
106         mock_create_network.return_value = [1, (1)]
107         response = self.client.post("/api/nslcm/v1/ns/vls", data=req_data)
108         retinfo = {"detail": "vl instantiation failed, detail message: Send post vl request to vim failed."}
109         self.assertEqual(retinfo["detail"], response.data["detail"])
110
111
112 class TestVlDetailViews(TestCase):
113     def setUp(self):
114         self.client = Client()
115         self.vl_inst_id = str(uuid.uuid4())
116         self.vl_name = str(uuid.uuid4())
117         self.ns_inst_id = str(uuid.uuid4())
118         VLInstModel(vlinstanceid=self.vl_inst_id, vldid="", vlinstancename=self.vl_name, ownertype=1,
119                     ownerid=self.ns_inst_id, relatednetworkid="network1", relatedsubnetworkid="subnet1,subnet2",
120                     vimid="",
121                     tenant="").save()
122         VNFFGInstModel(vnffgdid="", vnffginstid="", nsinstid=self.ns_inst_id,
123                        vllist="test1," + self.vl_inst_id + ",test2,test3", endpointnumber=0, cplist="", vnflist="",
124                        fplist="", status="").save()
125
126     def tearDown(self):
127         VLInstModel.objects.all().delete()
128         VNFFGInstModel.objects.all().delete()
129
130     @mock.patch.object(restcall, "call_req")
131     @mock.patch.object(vimadaptor.VimAdaptor, "delete_network")
132     @mock.patch.object(vimadaptor.VimAdaptor, "delete_subnet")
133     def test_delete_vl(self, mock_delete_subnet, mock_delete_network, mock_req_by_rest):
134         mock_req_by_rest.return_value = [0,
135                                          '{"test":"test_name","name":"vim_name","type":"type_name","url":"url_add"'
136                                          ',"userName":"user_name","password":"password","tenant":"tenant"}']
137         response = self.client.delete("/api/nslcm/v1/ns/vls/%s" % self.vl_inst_id)
138         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)
139         expect_resp_data = {"result": 0, "detail": "delete vl success"}
140         self.assertEqual(expect_resp_data, response.data)
141
142         for vnffg_info in VNFFGInstModel.objects.filter(nsinstid=self.ns_inst_id):
143             self.assertEqual(vnffg_info.vllist, "test1,test2,test3")
144         if VLInstModel.objects.filter(vlinstanceid=self.vl_inst_id):
145             self.fail()
146
147         response = self.client.delete("/api/nslcm/v1/ns/vls/%s" % "notExist")
148         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)
149         expect_resp_data = {"result": 0, "detail": "vl is not exist or has been already deleted"}
150         self.assertEqual(expect_resp_data, response.data)
151
152     def test_query_vl(self):
153         response = self.client.get("/api/nslcm/v1/ns/vls/%s" % self.vl_inst_id)
154         self.assertEqual(status.HTTP_200_OK, response.status_code)
155         expect_resp_data = {'vlId': self.vl_inst_id, 'vlName': self.vl_name, 'vlStatus': "active"}
156         self.assertEqual(expect_resp_data, response.data)
157
158         response = self.client.get("/api/nslcm/v1/ns/vls/%s" % "notExist")
159         self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code)