blob: 778c14532fee6a6389e9a30d60fc3cf71447690a [file] [log] [blame]
Scott Bakerc3ce3e72016-06-20 17:35:19 -07001from rest_framework.decorators import api_view
2from rest_framework.response import Response
3from rest_framework.reverse import reverse
4from rest_framework import serializers
5from rest_framework import generics
6from rest_framework import viewsets
7from rest_framework.decorators import detail_route, list_route
8from rest_framework.views import APIView
Scott Bakerc3800fa2016-08-25 15:30:43 -07009from rest_framework import status
Scott Bakerc3ce3e72016-06-20 17:35:19 -070010from core.models import *
11from services.vtn.models import VTNService
Scott Baker23429032016-09-06 12:02:39 -070012from services.vtn.vtnnetport import VTNNetwork, VTNPort
Scott Bakerc3ce3e72016-06-20 17:35:19 -070013from django.forms import widgets
14from django.conf.urls import patterns, url
15from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
16from django.shortcuts import get_object_or_404
17from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
18from xos.exceptions import *
19import json
20import subprocess
21
22class VTNServiceSerializer(PlusModelSerializer):
23 id = ReadOnlyField()
24
25 privateGatewayMac = serializers.CharField(required=False)
26 localManagementIp = serializers.CharField(required=False)
27 ovsdbPort = serializers.IntegerField(required=False)
28 sshPort = serializers.IntegerField(required=False)
29 sshUser = serializers.CharField(required=False)
30 sshKeyFile = serializers.CharField(required=False)
31 mgmtSubnetBits = serializers.IntegerField(required=False)
32 xosEndpoint = serializers.CharField(required=False)
33 xosUser = serializers.CharField(required=False)
34 xosPassword = serializers.CharField(required=False)
35
Scott Bakerc3ce3e72016-06-20 17:35:19 -070036 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
37 class Meta:
38 model = VTNService
39 fields = ('humanReadableName', 'id', 'privateGatewayMac', 'localManagementIp', 'ovsdbPort', 'sshPort', 'sshUser', 'sshKeyFile',
40 'mgmtSubnetBits', 'xosEndpoint', 'xosUser', 'xosPassword')
41
42 def getHumanReadableName(self, obj):
43 return obj.__unicode__()
44
Scott Bakerc3800fa2016-08-25 15:30:43 -070045class VTNNetworkSerializer(serializers.Serializer):
46 id = ReadOnlyField()
47 name = serializers.CharField(required=False)
48 subnet = serializers.CharField(required=False)
49 gateway = serializers.CharField(required=False)
50 segmentation_id = serializers.CharField(required=False)
51
52 class Meta:
53 fields = ('id', 'name', 'subnet', 'gateway', 'segmentation_id')
54
55class VTNServiceNetworkSerializer(serializers.Serializer):
56 id = ReadOnlyField()
57 type = serializers.CharField(required=False)
58 subscriberNetworks = serializers.SerializerMethodField("getSubscriberNetworks")
59 providerNetworks = serializers.SerializerMethodField("getProviderNetworks")
60 ownerSliceName = ReadOnlyField()
61 ownerServiceName = ReadOnlyField()
62
63 class Meta:
64 fields = ('id', 'type', "subscriberNetworks", "providerNetworks", "ownerSliceName", "ownerServiceName")
65
66 def getSubscriberNetworks(self, obj):
67 return obj.subscriberNetworks
68
69 def getProviderNetworks(self, obj):
70 return obj.providerNetworks
71
Scott Bakerc3800fa2016-08-25 15:30:43 -070072class VTNPortSerializer(serializers.Serializer):
73 id = ReadOnlyField()
74 name = serializers.CharField(required=False)
75 network_id = serializers.CharField(required=False)
Scott Baker9837fb82016-09-26 13:43:57 -070076 network_name = serializers.CharField(required=False)
Scott Bakerc3800fa2016-08-25 15:30:43 -070077 mac_address = serializers.CharField(required=False)
78 ip_address = serializers.CharField(required=False)
79
80 class Meta:
Scott Baker9837fb82016-09-26 13:43:57 -070081 fields = ('id', 'name', 'network_id', 'network_name', 'mac_address', 'ip_address')
Scott Bakerc3800fa2016-08-25 15:30:43 -070082
83class FloatingAddressPairSerializer(serializers.Serializer):
84 ip_address = ReadOnlyField()
85 mac_address = ReadOnlyField()
86
87class VTNServicePortSerializer(serializers.Serializer):
88 id = ReadOnlyField()
89 vlan_id = serializers.IntegerField(required=False)
Scott Baker9837fb82016-09-26 13:43:57 -070090 network_name = serializers.CharField(required=False)
Scott Bakerc3800fa2016-08-25 15:30:43 -070091
92 # TODO: structure this better
93 floating_address_pairs = serializers.SerializerMethodField("getFloatingAddressPairs")
94
95 class Meta:
Scott Baker9837fb82016-09-26 13:43:57 -070096 fields = ('id', 'vlan_id', 'network_name')
Scott Bakerc3800fa2016-08-25 15:30:43 -070097
98 def getFloatingAddressPairs(self, obj):
99 return obj.floating_address_pairs
100
Scott Bakerc3ce3e72016-06-20 17:35:19 -0700101class VTNViewSet(XOSViewSet):
102 base_name = "vtn"
103 method_name = "vtn"
104 method_kind = "viewset"
105
106 # these are just because ViewSet needs some queryset and model, even if we don't use the
107 # default endpoints
108 queryset = VTNService.get_service_objects().all()
109 model = VTNService
110 serializer_class = VTNServiceSerializer
111
Scott Bakerc3800fa2016-08-25 15:30:43 -0700112 custom_serializers = {"get_port": VTNPortSerializer,
113 "get_ports": VTNPortSerializer,
114 "get_network": VTNNetworkSerializer,
115 "get_networks": VTNNetworkSerializer}
116
Scott Bakerc3ce3e72016-06-20 17:35:19 -0700117 @classmethod
118 def get_urlpatterns(self, api_path="^"):
119 patterns = []
120
121 patterns.append( self.detail_url("services/$", {"get": "get_services"}, "services") )
122 patterns.append( self.detail_url("services_names/$", {"get": "get_services_names"}, "services") )
123 patterns.append( self.detail_url("services/(?P<service>[a-zA-Z0-9\-_]+)/$", {"get": "get_service"}, "get_service") )
124
125 # Not as RESTful as it could be, but maintain these endpoints for compability
126 patterns.append( self.list_url("services/$", {"get": "get_services"}, "rootvtn_services") )
127 patterns.append( self.list_url("services_names/$", {"get": "get_services_names"}, "rootvtn_services") )
128 patterns.append( self.list_url("services/(?P<service>[a-zA-Z0-9\-_]+)/$", {"get": "get_service"}, "rootvtn_get_service") )
129
Scott Bakerc3800fa2016-08-25 15:30:43 -0700130 # Neutron-replacement APIs
131 patterns.append( self.list_url("ports/$", {"get": "get_ports"}, "vtn_ports") )
132 patterns.append( self.list_url("ports/(?P<port_id>[a-zA-Z0-9\-_]+)/$", {"get": "get_port"}, "vtn_port") )
133 patterns.append( self.list_url("networks/$", {"get": "get_networks"}, "vtn_networks") )
134 patterns.append( self.list_url("networks/(?P<network_id>[a-zA-Z0-9\-_]+)/$", {"get": "get_network"}, "vtn_network") )
135
136 patterns.append( self.list_url("servicePorts/$", {"get": "get_service_ports"}, "vtn_service_ports") )
137 patterns.append( self.list_url("servicePorts/(?P<port_id>[a-zA-Z0-9\-_]+)/$", {"get": "get_service_port"}, "vtn_service_port") )
138 patterns.append( self.list_url("serviceNetworks/$", {"get": "get_service_networks"}, "vtn_service_networks") )
139 patterns.append( self.list_url("serviceNetworks/(?P<network_id>[a-zA-Z0-9\-_]+)/$", {"get": "get_service_network"}, "vtn_service_network") )
140
Scott Bakerc3ce3e72016-06-20 17:35:19 -0700141 patterns = patterns + super(VTNViewSet,self).get_urlpatterns(api_path)
142
143 return patterns
144
145 def get_services_names(self, request, pk=None):
146 result = {}
147 for service in Service.objects.all():
148 for id in service.get_vtn_src_names():
149 dependencies = service.get_vtn_dependencies_names()
150 if dependencies:
151 result[id] = dependencies
152 return Response(result)
153
154 def get_services(self, request, pk=None):
155 result = {}
156 for service in Service.objects.all():
157 for id in service.get_vtn_src_ids():
158 dependencies = service.get_vtn_dependencies_ids()
159 if dependencies:
160 result[id] = dependencies
161 return Response(result)
162
163 def get_service(self, request, pk=None, service=None):
164 for xos_service in Service.objects.all():
165 if service in xos_service.get_vtn_src_ids():
166 return Response(xos_service.get_vtn_dependencies_ids())
167 return Response([])
168
Scott Bakerc3800fa2016-08-25 15:30:43 -0700169 def get_ports(self, request):
170 result=[]
171 for port in Port.objects.all():
172 result.append(VTNPortSerializer(VTNPort(port)).data)
Scott Bakercb25cd52016-09-09 12:28:10 -0700173 return Response({"ports": result})
Scott Bakerc3800fa2016-08-25 15:30:43 -0700174
175 def get_port(self, request, port_id=None):
176 port = Port.objects.filter(port_id = port_id)
177 if port:
178 port=port[0]
179 else:
180 return Response("Failed to find port %s" % port_id, status=status.HTTP_404_NOT_FOUND)
Scott Bakercb25cd52016-09-09 12:28:10 -0700181 return Response({"port": VTNPortSerializer(VTNPort(port)).data})
Scott Bakerc3800fa2016-08-25 15:30:43 -0700182
183 def get_service_ports(self, request):
184 result=[]
185 for port in Port.objects.all():
186 result.append(VTNServicePortSerializer(VTNPort(port)).data)
Scott Bakercb25cd52016-09-09 12:28:10 -0700187 return Response({"servicePorts": result})
Scott Bakerc3800fa2016-08-25 15:30:43 -0700188
189 def get_service_port(self, request, port_id=None):
190 port = Port.objects.filter(port_id = port_id)
191 if port:
192 port=port[0]
193 else:
194 return Response("Failed to find port %s" % port_id, status=status.HTTP_404_NOT_FOUND)
Scott Bakercb25cd52016-09-09 12:28:10 -0700195 return Response({"servicePort": VTNServicePortSerializer(VTNPort(port)).data})
Scott Bakerc3800fa2016-08-25 15:30:43 -0700196
197 def get_networks(self, request):
198 result=[]
199 for network in Network.objects.all():
200 result.append(VTNNetworkSerializer(VTNNetwork(network)).data)
Scott Bakercb25cd52016-09-09 12:28:10 -0700201 return Response({"networks": result})
Scott Bakerc3800fa2016-08-25 15:30:43 -0700202
203 def get_network(self, request, network_id=None):
Scott Bakercb25cd52016-09-09 12:28:10 -0700204 #network = Network.objects.filter(network_id = network_id)
205 network = [x for x in Network.objects.all() if VTNNetwork(x).id == network_id]
Scott Bakerc3800fa2016-08-25 15:30:43 -0700206 if network:
207 network=network[0]
208 else:
209 return Response("Failed to find network %s" % network_id, status=status.HTTP_404_NOT_FOUND)
Scott Bakercb25cd52016-09-09 12:28:10 -0700210 return Response({"network": VTNNetworkSerializer(VTNNetwork(network)).data})
Scott Bakerc3800fa2016-08-25 15:30:43 -0700211
212 def get_service_networks(self, request):
213 result=[]
214 for network in Network.objects.all():
215 result.append(VTNServiceNetworkSerializer(VTNNetwork(network)).data)
Scott Bakercb25cd52016-09-09 12:28:10 -0700216 return Response({"serviceNetworks": result})
Scott Bakerc3800fa2016-08-25 15:30:43 -0700217
218 def get_service_network(self, request, network_id=None):
Scott Bakercb25cd52016-09-09 12:28:10 -0700219 #network = Network.objects.filter(network_id = network_id)
220 network = [x for x in Network.objects.all() if VTNNetwork(x).id == network_id]
Scott Bakerc3800fa2016-08-25 15:30:43 -0700221 if network:
222 network=network[0]
223 else:
224 return Response("Failed to find network %s" % network_id, status=status.HTTP_404_NOT_FOUND)
Scott Bakercb25cd52016-09-09 12:28:10 -0700225 return Response({"serviceNetwork": VTNServiceNetworkSerializer(VTNNetwork(network)).data})
Scott Bakerc3800fa2016-08-25 15:30:43 -0700226
227
Scott Bakerc3ce3e72016-06-20 17:35:19 -0700228