blob: 481057d633d7d91f4c04f5cca83ace3417df1089 [file] [log] [blame]
Scott Baker7a327592016-06-20 17:34:06 -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 status
7from core.models import *
8from django.forms import widgets
9from services.onos.models import ONOSService, ONOSApp
10from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
11from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
12
13def get_default_onos_service():
14 onos_services = ONOSService.get_service_objects().all()
15 if onos_services:
16 return onos_services[0].id
17 return None
18
19class ONOSAppSerializer(PlusModelSerializer):
20 id = ReadOnlyField()
21 name = serializers.CharField()
22 dependencies = serializers.CharField()
23
24 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
25 class Meta:
26 model = ONOSApp
27 fields = ('humanReadableName', 'id', 'name', 'dependencies')
28
29 def getHumanReadableName(self, obj):
30 return obj.__unicode__()
31
32class TenantAttributeSerializer(serializers.Serializer):
33 id = ReadOnlyField()
34 name = serializers.CharField(required=False)
35 value = serializers.CharField(required=False)
36
37class ONOSAppViewSet(XOSViewSet):
38 base_name = "app"
39 method_name = "app"
40 method_kind = "viewset"
41 queryset = ONOSApp.get_tenant_objects().all()
42 serializer_class = ONOSAppSerializer
43
44 custom_serializers = {"set_attribute": TenantAttributeSerializer}
45
46 @classmethod
47 def get_urlpatterns(self, api_path="^"):
48 patterns = super(ONOSAppViewSet, self).get_urlpatterns(api_path=api_path)
49
50 patterns.append( self.detail_url("attributes/$", {"get": "get_attributes", "post": "add_attribute"}, "attributes") )
51 patterns.append( self.detail_url("attributes/(?P<attribute>[0-9]+)/$", {"get": "get_attribute", "put": "set_attribute", "delete": "delete_attribute"}, "attribute") )
52
53 return patterns
54
55 def get_attributes(self, request, pk=None):
56 app = self.get_object()
57 return Response(TenantAttributeSerializer(app.tenantattributes.all(), many=True).data)
58
59 def add_attribute(self, request, pk=None):
60 app = self.get_object()
61 ser = TenantAttributeSerializer(data=request.data)
62 ser.is_valid(raise_exception = True)
63 att = TenantAttribute(tenant=app, **ser.validated_data)
64 att.save()
65 return Response(TenantAttributeSerializer(att).data)
66
67 def get_attribute(self, request, pk=None, attribute=None):
68 app = self.get_object()
69 att = TenantAttribute.objects.get(pk=attribute)
70 return Response(TenantAttributeSerializer(att).data)
71
72 def set_attribute(self, request, pk=None, attribute=None):
73 app = self.get_object()
74 att = TenantAttribute.objects.get(pk=attribute)
75 ser = TenantAttributeSerializer(att, data=request.data)
76 ser.is_valid(raise_exception = True)
77 att.name = ser.validated_data.get("name", att.name)
78 att.value = ser.validated_data.get("value", att.value)
79 att.save()
80 return Response(TenantAttributeSerializer(att).data)
81
82 def delete_attribute(self, request, pk=None, attribute=None):
83 att = TenantAttribute.objects.get(pk=attribute)
84 att.delete()
85 return Response(status=status.HTTP_204_NO_CONTENT)
86
87
88
89
90
91