Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -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 status |
| 7 | from core.models import * |
| 8 | from django.forms import widgets |
| 9 | from services.vrouter.models import * |
| 10 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 11 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 12 | import json |
| 13 | |
| 14 | BASE_NAME = 'vrouter' |
| 15 | |
| 16 | |
| 17 | class VRouterServiceSerializer(PlusModelSerializer): |
| 18 | id = ReadOnlyField() |
| 19 | name = serializers.CharField(required=False) |
| 20 | kind = serializers.CharField(required=False) |
| 21 | service_specific_attribute = serializers.CharField(required=False) |
| 22 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 23 | |
| 24 | class Meta: |
| 25 | model = VRouterService |
| 26 | fields = ('humanReadableName', 'id', 'name', 'kind', 'service_specific_attribute') |
| 27 | |
| 28 | def getHumanReadableName(self, obj): |
| 29 | return obj.__unicode__() |
| 30 | |
| 31 | |
| 32 | class VRouterDeviceSerializer(PlusModelSerializer): |
| 33 | id = ReadOnlyField() |
| 34 | name = serializers.CharField(required=False) |
| 35 | openflow_id = serializers.CharField(required=False) |
| 36 | config_key = serializers.CharField(required=False) |
| 37 | driver = serializers.CharField(required=False) |
| 38 | vrouter_service = serializers.PrimaryKeyRelatedField(read_only=True) |
| 39 | |
| 40 | ports = serializers.SerializerMethodField("getDevicePorts") |
| 41 | |
| 42 | def getDevicePorts(self, device): |
| 43 | ports = VRouterPort.objects.filter(vrouter_device=device.id) |
| 44 | return VRouterPortSerializer(ports, many=True).data |
| 45 | |
| 46 | class Meta: |
| 47 | model = VRouterDevice |
| 48 | fields = ('id', 'name', 'openflow_id', 'config_key', 'driver', 'vrouter_service', 'ports') |
| 49 | |
| 50 | |
| 51 | class VRouterPortSerializer(PlusModelSerializer): |
| 52 | id = ReadOnlyField() |
| 53 | name = serializers.CharField(required=False) |
| 54 | openflow_id = serializers.CharField(required=False) |
| 55 | interfaces = serializers.SerializerMethodField("getPortInterfaces") |
| 56 | |
| 57 | def getPortInterfaces(self, port): |
| 58 | interfaces = VRouterInterface.objects.filter(vrouter_port=port.id) |
| 59 | return VRouterInterfaceSerializer(interfaces, many=True).data |
| 60 | |
| 61 | class Meta: |
| 62 | model = VRouterPort |
| 63 | fields = ('id', 'name', 'openflow_id', 'interfaces') |
| 64 | |
| 65 | |
| 66 | class VRouterInterfaceSerializer(PlusModelSerializer): |
| 67 | id = ReadOnlyField() |
| 68 | name = serializers.CharField(required=False) |
| 69 | mac = serializers.CharField(required=False) |
| 70 | vlan = serializers.CharField(required=False) |
| 71 | ips = serializers.SerializerMethodField("getInterfaceIps") |
| 72 | |
| 73 | def getInterfaceIps(self, interface): |
| 74 | interfaces = VRouterIp.objects.filter(vrouter_interface=interface.id) |
| 75 | return VRouterIpSerializer(interfaces, many=True).data |
| 76 | |
| 77 | class Meta: |
| 78 | model = VRouterPort |
| 79 | fields = ('id', 'name', 'mac', 'vlan', 'ips') |
| 80 | |
| 81 | |
| 82 | class VRouterIpSerializer(PlusModelSerializer): |
| 83 | id = ReadOnlyField() |
| 84 | ip = serializers.CharField(required=False) |
| 85 | name = serializers.CharField(required=False) |
| 86 | |
| 87 | class Meta: |
| 88 | model = VRouterIp |
| 89 | fields = ('id', 'ip', 'name') |
| 90 | |
| 91 | |
| 92 | class VRouterAppSerializer(PlusModelSerializer): |
| 93 | id = ReadOnlyField() |
| 94 | name = serializers.CharField(required=False) |
| 95 | control_plane_connect_point = serializers.CharField(required=False) |
| 96 | ospf_enabled = serializers.BooleanField(required=False) |
| 97 | interfaces = serializers.SerializerMethodField("dumpInterfaces") |
| 98 | |
| 99 | def dumpInterfaces(self, app): |
| 100 | return json.dumps(app.interfaces) |
| 101 | |
| 102 | class Meta: |
| 103 | model = VRouterApp |
| 104 | fields = ('id', 'name', 'control_plane_connect_point', 'ospf_enabled', 'interfaces') |
| 105 | |
| 106 | |
| 107 | class VRouterServiceViewSet(XOSViewSet): |
| 108 | base_name = BASE_NAME |
| 109 | method_name = "vrouter" |
| 110 | method_kind = "viewset" |
| 111 | queryset = VRouterService.objects.filter(kind='vROUTER') |
| 112 | serializer_class = VRouterServiceSerializer |
| 113 | |
| 114 | @classmethod |
| 115 | def get_urlpatterns(self, api_path="^"): |
| 116 | patterns = super(VRouterServiceViewSet, self).get_urlpatterns(api_path=api_path) |
| 117 | |
| 118 | patterns.append(self.detail_url("devices/$", { |
| 119 | "get": "get_devices" |
| 120 | }, "vrouter_devices")) |
| 121 | |
| 122 | patterns.append(self.detail_url("apps/$", { |
| 123 | "get": "get_apps" |
| 124 | }, "vrouter_apps")) |
| 125 | |
| 126 | return patterns |
| 127 | |
| 128 | def get_devices(self, request, pk=None): |
| 129 | if (not request.user.is_authenticated()): |
| 130 | raise XOSPermissionDenied("You must be authenticated in order to use this API") |
| 131 | else: |
| 132 | if(pk is not None): |
| 133 | devices = VRouterDevice.objects.filter(vrouter_service=pk) |
| 134 | else: |
| 135 | devices = VRouterDevice.objects.all() |
| 136 | return Response(VRouterDeviceSerializer(devices, many=True).data) |
| 137 | |
| 138 | def get_apps(self, request, pk=None): |
| 139 | if (not request.user.is_authenticated()): |
| 140 | raise XOSPermissionDenied("You must be authenticated in order to use this API") |
| 141 | else: |
| 142 | if(pk is not None): |
| 143 | apps = VRouterApp.objects.filter(vrouter_service=pk) |
| 144 | else: |
| 145 | apps = VRouterApp.objects.all() |
| 146 | return Response(VRouterAppSerializer(apps, many=True).data) |