Scott Baker | c3ce3e7 | 2016-06-20 17:35:19 -0700 | [diff] [blame] | 1 | from rest_framework.decorators import api_view |
| 2 | from rest_framework.response import Response |
| 3 | from rest_framework.reverse import reverse |
| 4 | from rest_framework import serializers |
| 5 | from rest_framework import generics |
| 6 | from rest_framework import viewsets |
| 7 | from rest_framework.decorators import detail_route, list_route |
| 8 | from rest_framework.views import APIView |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 9 | from rest_framework import status |
Scott Baker | c3ce3e7 | 2016-06-20 17:35:19 -0700 | [diff] [blame] | 10 | from core.models import * |
| 11 | from services.vtn.models import VTNService |
Scott Baker | 2342903 | 2016-09-06 12:02:39 -0700 | [diff] [blame] | 12 | from services.vtn.vtnnetport import VTNNetwork, VTNPort |
Scott Baker | c3ce3e7 | 2016-06-20 17:35:19 -0700 | [diff] [blame] | 13 | from django.forms import widgets |
| 14 | from django.conf.urls import patterns, url |
| 15 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 16 | from django.shortcuts import get_object_or_404 |
| 17 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 18 | from xos.exceptions import * |
| 19 | import json |
| 20 | import subprocess |
| 21 | |
| 22 | class 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 Baker | c3ce3e7 | 2016-06-20 17:35:19 -0700 | [diff] [blame] | 36 | 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 Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 45 | class 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 | |
| 55 | class 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 Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 72 | class VTNPortSerializer(serializers.Serializer): |
| 73 | id = ReadOnlyField() |
| 74 | name = serializers.CharField(required=False) |
| 75 | network_id = serializers.CharField(required=False) |
Scott Baker | 9837fb8 | 2016-09-26 13:43:57 -0700 | [diff] [blame] | 76 | network_name = serializers.CharField(required=False) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 77 | mac_address = serializers.CharField(required=False) |
| 78 | ip_address = serializers.CharField(required=False) |
| 79 | |
| 80 | class Meta: |
Scott Baker | 9837fb8 | 2016-09-26 13:43:57 -0700 | [diff] [blame] | 81 | fields = ('id', 'name', 'network_id', 'network_name', 'mac_address', 'ip_address') |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 82 | |
| 83 | class FloatingAddressPairSerializer(serializers.Serializer): |
| 84 | ip_address = ReadOnlyField() |
| 85 | mac_address = ReadOnlyField() |
| 86 | |
| 87 | class VTNServicePortSerializer(serializers.Serializer): |
| 88 | id = ReadOnlyField() |
| 89 | vlan_id = serializers.IntegerField(required=False) |
Scott Baker | 9837fb8 | 2016-09-26 13:43:57 -0700 | [diff] [blame] | 90 | network_name = serializers.CharField(required=False) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 91 | |
| 92 | # TODO: structure this better |
| 93 | floating_address_pairs = serializers.SerializerMethodField("getFloatingAddressPairs") |
| 94 | |
| 95 | class Meta: |
Scott Baker | 9837fb8 | 2016-09-26 13:43:57 -0700 | [diff] [blame] | 96 | fields = ('id', 'vlan_id', 'network_name') |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 97 | |
| 98 | def getFloatingAddressPairs(self, obj): |
| 99 | return obj.floating_address_pairs |
| 100 | |
Scott Baker | c3ce3e7 | 2016-06-20 17:35:19 -0700 | [diff] [blame] | 101 | class 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 Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 112 | custom_serializers = {"get_port": VTNPortSerializer, |
| 113 | "get_ports": VTNPortSerializer, |
| 114 | "get_network": VTNNetworkSerializer, |
| 115 | "get_networks": VTNNetworkSerializer} |
| 116 | |
Scott Baker | c3ce3e7 | 2016-06-20 17:35:19 -0700 | [diff] [blame] | 117 | @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 Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 130 | # 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 Baker | c3ce3e7 | 2016-06-20 17:35:19 -0700 | [diff] [blame] | 141 | 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 Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 169 | def get_ports(self, request): |
| 170 | result=[] |
| 171 | for port in Port.objects.all(): |
| 172 | result.append(VTNPortSerializer(VTNPort(port)).data) |
Scott Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 173 | return Response({"ports": result}) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 174 | |
| 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 Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 181 | return Response({"port": VTNPortSerializer(VTNPort(port)).data}) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 182 | |
| 183 | def get_service_ports(self, request): |
| 184 | result=[] |
| 185 | for port in Port.objects.all(): |
| 186 | result.append(VTNServicePortSerializer(VTNPort(port)).data) |
Scott Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 187 | return Response({"servicePorts": result}) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 188 | |
| 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 Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 195 | return Response({"servicePort": VTNServicePortSerializer(VTNPort(port)).data}) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 196 | |
| 197 | def get_networks(self, request): |
| 198 | result=[] |
| 199 | for network in Network.objects.all(): |
Scott Baker | f489374 | 2016-11-23 12:31:20 -0800 | [diff] [blame] | 200 | network = VTNNetwork(network) |
| 201 | if network.id is not None: |
| 202 | result.append(VTNNetworkSerializer(network).data) |
Scott Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 203 | return Response({"networks": result}) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 204 | |
| 205 | def get_network(self, request, network_id=None): |
Scott Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 206 | network = [x for x in Network.objects.all() if VTNNetwork(x).id == network_id] |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 207 | if network: |
| 208 | network=network[0] |
| 209 | else: |
| 210 | return Response("Failed to find network %s" % network_id, status=status.HTTP_404_NOT_FOUND) |
Scott Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 211 | return Response({"network": VTNNetworkSerializer(VTNNetwork(network)).data}) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 212 | |
| 213 | def get_service_networks(self, request): |
| 214 | result=[] |
| 215 | for network in Network.objects.all(): |
Scott Baker | f489374 | 2016-11-23 12:31:20 -0800 | [diff] [blame] | 216 | network = VTNNetwork(network) |
| 217 | if network.id is not None: |
| 218 | result.append(VTNServiceNetworkSerializer(network).data) |
Scott Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 219 | return Response({"serviceNetworks": result}) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 220 | |
| 221 | def get_service_network(self, request, network_id=None): |
Scott Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 222 | network = [x for x in Network.objects.all() if VTNNetwork(x).id == network_id] |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 223 | if network: |
| 224 | network=network[0] |
| 225 | else: |
| 226 | return Response("Failed to find network %s" % network_id, status=status.HTTP_404_NOT_FOUND) |
Scott Baker | cb25cd5 | 2016-09-09 12:28:10 -0700 | [diff] [blame] | 227 | return Response({"serviceNetwork": VTNServiceNetworkSerializer(VTNNetwork(network)).data}) |
Scott Baker | c3800fa | 2016-08-25 15:30:43 -0700 | [diff] [blame] | 228 | |
| 229 | |
Scott Baker | c3ce3e7 | 2016-06-20 17:35:19 -0700 | [diff] [blame] | 230 | |