Scott Baker | 7a32759 | 2016-06-20 17:34:06 -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.onos.models import ONOSService |
| 10 | from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied |
| 11 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
| 12 | |
| 13 | class ONOSServiceSerializer(PlusModelSerializer): |
| 14 | id = ReadOnlyField() |
| 15 | rest_hostname = serializers.CharField(required=False) |
| 16 | rest_port = serializers.CharField(default="8181") |
| 17 | no_container = serializers.BooleanField(default=False) |
| 18 | node_key = serializers.CharField(required=False) |
| 19 | |
| 20 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 21 | class Meta: |
| 22 | model = ONOSService |
| 23 | fields = ('humanReadableName', 'id', 'rest_hostname', 'rest_port', 'no_container', 'node_key') |
| 24 | |
| 25 | def getHumanReadableName(self, obj): |
| 26 | return obj.__unicode__() |
| 27 | |
| 28 | class ServiceAttributeSerializer(serializers.Serializer): |
| 29 | id = ReadOnlyField() |
| 30 | name = serializers.CharField(required=False) |
| 31 | value = serializers.CharField(required=False) |
| 32 | |
| 33 | class ONOSServiceViewSet(XOSViewSet): |
| 34 | base_name = "onos" |
| 35 | method_name = "onos" |
| 36 | method_kind = "viewset" |
| 37 | queryset = ONOSService.get_service_objects().all() |
| 38 | serializer_class = ONOSServiceSerializer |
| 39 | |
| 40 | custom_serializers = {"set_attribute": ServiceAttributeSerializer} |
| 41 | |
| 42 | @classmethod |
| 43 | def get_urlpatterns(self, api_path="^"): |
| 44 | patterns = super(ONOSServiceViewSet, self).get_urlpatterns(api_path=api_path) |
| 45 | |
| 46 | patterns.append( self.detail_url("attributes/$", {"get": "get_attributes", "post": "add_attribute"}, "attributes") ) |
| 47 | patterns.append( self.detail_url("attributes/(?P<attribute>[0-9]+)/$", {"get": "get_attribute", "put": "set_attribute", "delete": "delete_attribute"}, "attribute") ) |
| 48 | |
| 49 | return patterns |
| 50 | |
| 51 | def get_attributes(self, request, pk=None): |
| 52 | svc = self.get_object() |
| 53 | return Response(ServiceAttributeSerializer(svc.serviceattributes.all(), many=True).data) |
| 54 | |
| 55 | def add_attribute(self, request, pk=None): |
| 56 | svc = self.get_object() |
| 57 | ser = ServiceAttributeSerializer(data=request.data) |
| 58 | ser.is_valid(raise_exception = True) |
| 59 | att = ServiceAttribute(service=svc, **ser.validated_data) |
| 60 | att.save() |
| 61 | return Response(ServiceAttributeSerializer(att).data) |
| 62 | |
| 63 | def get_attribute(self, request, pk=None, attribute=None): |
| 64 | svc = self.get_object() |
| 65 | att = ServiceAttribute.objects.get(pk=attribute) |
| 66 | return Response(ServiceAttributeSerializer(att).data) |
| 67 | |
| 68 | def set_attribute(self, request, pk=None, attribute=None): |
| 69 | svc = self.get_object() |
| 70 | att = ServiceAttribute.objects.get(pk=attribute) |
| 71 | ser = ServicettributeSerializer(att, data=request.data) |
| 72 | ser.is_valid(raise_exception = True) |
| 73 | att.name = ser.validated_data.get("name", att.name) |
| 74 | att.value = ser.validated_data.get("value", att.value) |
| 75 | att.save() |
| 76 | return Response(ServiceAttributeSerializer(att).data) |
| 77 | |
| 78 | def delete_attribute(self, request, pk=None, attribute=None): |
| 79 | att = ServiceAttribute.objects.get(pk=attribute) |
| 80 | att.delete() |
| 81 | return Response(status=status.HTTP_204_NO_CONTENT) |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |