blob: d4d3ab0c8a0615be9736ada8958b766a2a9ca933 [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"
Scott Baker7a327592016-06-20 17:34:06 -070041 serializer_class = ONOSAppSerializer
42
43 custom_serializers = {"set_attribute": TenantAttributeSerializer}
44
45 @classmethod
46 def get_urlpatterns(self, api_path="^"):
47 patterns = super(ONOSAppViewSet, self).get_urlpatterns(api_path=api_path)
48
49 patterns.append( self.detail_url("attributes/$", {"get": "get_attributes", "post": "add_attribute"}, "attributes") )
50 patterns.append( self.detail_url("attributes/(?P<attribute>[0-9]+)/$", {"get": "get_attribute", "put": "set_attribute", "delete": "delete_attribute"}, "attribute") )
51
52 return patterns
53
Scott Baker9507fc92016-07-27 09:49:14 -070054 def get_queryset(self):
55 queryset = ONOSApp.get_tenant_objects().all()
56
57 # Since name isn't a real database field of the Tenant object, we have
58 # to go through some extra work...
59 name = self.request.query_params.get('name', None)
60 if name is not None:
61 ids = [x.id for x in queryset.all() if x.name == name]
62 return queryset.filter(id__in=ids)
63
64 return queryset
65
Scott Baker7a327592016-06-20 17:34:06 -070066 def get_attributes(self, request, pk=None):
67 app = self.get_object()
Scott Baker9507fc92016-07-27 09:49:14 -070068 qs = app.tenantattributes.all()
69 name = self.request.query_params.get('attribute_name', None)
70 if name is not None:
71 qs = qs.filter(name=name)
72 return Response(TenantAttributeSerializer(qs, many=True).data)
Scott Baker7a327592016-06-20 17:34:06 -070073
74 def add_attribute(self, request, pk=None):
75 app = self.get_object()
76 ser = TenantAttributeSerializer(data=request.data)
77 ser.is_valid(raise_exception = True)
78 att = TenantAttribute(tenant=app, **ser.validated_data)
79 att.save()
80 return Response(TenantAttributeSerializer(att).data)
81
82 def get_attribute(self, request, pk=None, attribute=None):
83 app = self.get_object()
84 att = TenantAttribute.objects.get(pk=attribute)
85 return Response(TenantAttributeSerializer(att).data)
86
87 def set_attribute(self, request, pk=None, attribute=None):
88 app = self.get_object()
89 att = TenantAttribute.objects.get(pk=attribute)
90 ser = TenantAttributeSerializer(att, data=request.data)
91 ser.is_valid(raise_exception = True)
92 att.name = ser.validated_data.get("name", att.name)
93 att.value = ser.validated_data.get("value", att.value)
94 att.save()
95 return Response(TenantAttributeSerializer(att).data)
96
97 def delete_attribute(self, request, pk=None, attribute=None):
98 att = TenantAttribute.objects.get(pk=attribute)
99 att.delete()
100 return Response(status=status.HTTP_204_NO_CONTENT)
101
102
103
104
105
106