blob: 01c32cbed3c07f377c4253eba2bd274c8fa96327 [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)
76 mac_address = serializers.CharField(required=False)
77 ip_address = serializers.CharField(required=False)
78
79 class Meta:
80 fields = ('id', 'name', 'network_id', 'mac_address', 'ip_address')
81
82class FloatingAddressPairSerializer(serializers.Serializer):
83 ip_address = ReadOnlyField()
84 mac_address = ReadOnlyField()
85
86class VTNServicePortSerializer(serializers.Serializer):
87 id = ReadOnlyField()
88 vlan_id = serializers.IntegerField(required=False)
89
90 # TODO: structure this better
91 floating_address_pairs = serializers.SerializerMethodField("getFloatingAddressPairs")
92
93 class Meta:
94 fields = ('id', 'vlan_id')
95
96 def getFloatingAddressPairs(self, obj):
97 return obj.floating_address_pairs
98
Scott Bakerc3ce3e72016-06-20 17:35:19 -070099class VTNViewSet(XOSViewSet):
100 base_name = "vtn"
101 method_name = "vtn"
102 method_kind = "viewset"
103
104 # these are just because ViewSet needs some queryset and model, even if we don't use the
105 # default endpoints
106 queryset = VTNService.get_service_objects().all()
107 model = VTNService
108 serializer_class = VTNServiceSerializer
109
Scott Bakerc3800fa2016-08-25 15:30:43 -0700110 custom_serializers = {"get_port": VTNPortSerializer,
111 "get_ports": VTNPortSerializer,
112 "get_network": VTNNetworkSerializer,
113 "get_networks": VTNNetworkSerializer}
114
Scott Bakerc3ce3e72016-06-20 17:35:19 -0700115 @classmethod
116 def get_urlpatterns(self, api_path="^"):
117 patterns = []
118
119 patterns.append( self.detail_url("services/$", {"get": "get_services"}, "services") )
120 patterns.append( self.detail_url("services_names/$", {"get": "get_services_names"}, "services") )
121 patterns.append( self.detail_url("services/(?P<service>[a-zA-Z0-9\-_]+)/$", {"get": "get_service"}, "get_service") )
122
123 # Not as RESTful as it could be, but maintain these endpoints for compability
124 patterns.append( self.list_url("services/$", {"get": "get_services"}, "rootvtn_services") )
125 patterns.append( self.list_url("services_names/$", {"get": "get_services_names"}, "rootvtn_services") )
126 patterns.append( self.list_url("services/(?P<service>[a-zA-Z0-9\-_]+)/$", {"get": "get_service"}, "rootvtn_get_service") )
127
Scott Bakerc3800fa2016-08-25 15:30:43 -0700128 # Neutron-replacement APIs
129 patterns.append( self.list_url("ports/$", {"get": "get_ports"}, "vtn_ports") )
130 patterns.append( self.list_url("ports/(?P<port_id>[a-zA-Z0-9\-_]+)/$", {"get": "get_port"}, "vtn_port") )
131 patterns.append( self.list_url("networks/$", {"get": "get_networks"}, "vtn_networks") )
132 patterns.append( self.list_url("networks/(?P<network_id>[a-zA-Z0-9\-_]+)/$", {"get": "get_network"}, "vtn_network") )
133
134 patterns.append( self.list_url("servicePorts/$", {"get": "get_service_ports"}, "vtn_service_ports") )
135 patterns.append( self.list_url("servicePorts/(?P<port_id>[a-zA-Z0-9\-_]+)/$", {"get": "get_service_port"}, "vtn_service_port") )
136 patterns.append( self.list_url("serviceNetworks/$", {"get": "get_service_networks"}, "vtn_service_networks") )
137 patterns.append( self.list_url("serviceNetworks/(?P<network_id>[a-zA-Z0-9\-_]+)/$", {"get": "get_service_network"}, "vtn_service_network") )
138
Scott Bakerc3ce3e72016-06-20 17:35:19 -0700139 patterns = patterns + super(VTNViewSet,self).get_urlpatterns(api_path)
140
141 return patterns
142
143 def get_services_names(self, request, pk=None):
144 result = {}
145 for service in Service.objects.all():
146 for id in service.get_vtn_src_names():
147 dependencies = service.get_vtn_dependencies_names()
148 if dependencies:
149 result[id] = dependencies
150 return Response(result)
151
152 def get_services(self, request, pk=None):
153 result = {}
154 for service in Service.objects.all():
155 for id in service.get_vtn_src_ids():
156 dependencies = service.get_vtn_dependencies_ids()
157 if dependencies:
158 result[id] = dependencies
159 return Response(result)
160
161 def get_service(self, request, pk=None, service=None):
162 for xos_service in Service.objects.all():
163 if service in xos_service.get_vtn_src_ids():
164 return Response(xos_service.get_vtn_dependencies_ids())
165 return Response([])
166
Scott Bakerc3800fa2016-08-25 15:30:43 -0700167 def get_ports(self, request):
168 result=[]
169 for port in Port.objects.all():
170 result.append(VTNPortSerializer(VTNPort(port)).data)
171 return Response(result)
172
173 def get_port(self, request, port_id=None):
174 port = Port.objects.filter(port_id = port_id)
175 if port:
176 port=port[0]
177 else:
178 return Response("Failed to find port %s" % port_id, status=status.HTTP_404_NOT_FOUND)
179 return Response(VTNPortSerializer(VTNPort(port)).data)
180
181 def get_service_ports(self, request):
182 result=[]
183 for port in Port.objects.all():
184 result.append(VTNServicePortSerializer(VTNPort(port)).data)
185 return Response(result)
186
187 def get_service_port(self, request, port_id=None):
188 port = Port.objects.filter(port_id = port_id)
189 if port:
190 port=port[0]
191 else:
192 return Response("Failed to find port %s" % port_id, status=status.HTTP_404_NOT_FOUND)
193 return Response(VTNServicePortSerializer(VTNPort(port)).data)
194
195 def get_networks(self, request):
196 result=[]
197 for network in Network.objects.all():
198 result.append(VTNNetworkSerializer(VTNNetwork(network)).data)
199 return Response(result)
200
201 def get_network(self, request, network_id=None):
202 network = Network.objects.filter(network_id = network_id)
203 if network:
204 network=network[0]
205 else:
206 return Response("Failed to find network %s" % network_id, status=status.HTTP_404_NOT_FOUND)
207 return Response(VTNNetworkSerializer(VTNNetwork(network)).data)
208
209 def get_service_networks(self, request):
210 result=[]
211 for network in Network.objects.all():
212 result.append(VTNServiceNetworkSerializer(VTNNetwork(network)).data)
213 return Response(result)
214
215 def get_service_network(self, request, network_id=None):
216 network = Network.objects.filter(network_id = network_id)
217 if network:
218 network=network[0]
219 else:
220 return Response("Failed to find network %s" % network_id, status=status.HTTP_404_NOT_FOUND)
221 return Response(VTNServiceNetworkSerializer(VTNNetwork(network)).data)
222
223
Scott Bakerc3ce3e72016-06-20 17:35:19 -0700224