blob: 5c9634a2cdfa79ff5f114833618a6103b1a7b087 [file] [log] [blame]
Scott Baker8e6647a2016-06-20 17:16:20 -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.volt.models import VOLTTenant, VOLTService, CordSubscriberRoot
10from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
11from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
12
13def get_default_volt_service():
14 volt_services = VOLTService.get_service_objects().all()
15 if volt_services:
16 return volt_services[0].id
17 return None
18
19class VOLTTenantForAPI(VOLTTenant):
20 class Meta:
21 proxy = True
22 app_label = "cord"
23
24 @property
25 def subscriber(self):
26 return self.subscriber_root.id
27
28 @subscriber.setter
29 def subscriber(self, value):
30 self.subscriber_root = value # CordSubscriberRoot.get_tenant_objects().get(id=value)
31
32 @property
33 def related(self):
34 related = {}
35 if self.vcpe:
36 related["vsg_id"] = self.vcpe.id
37 if self.vcpe.instance:
38 related["instance_id"] = self.vcpe.instance.id
39 related["instance_name"] = self.vcpe.instance.name
40 related["wan_container_ip"] = self.vcpe.wan_container_ip
41 if self.vcpe.instance.node:
42 related["compute_node_name"] = self.vcpe.instance.node.name
43 return related
44
45class VOLTTenantSerializer(PlusModelSerializer):
46 id = ReadOnlyField()
47 service_specific_id = serializers.CharField(required=False)
48 s_tag = serializers.CharField()
49 c_tag = serializers.CharField()
50 subscriber = serializers.PrimaryKeyRelatedField(queryset=CordSubscriberRoot.get_tenant_objects().all(), required=False)
51 related = serializers.DictField(required=False)
52
53 property_fields=["subscriber"]
54
55 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
56 class Meta:
57 model = VOLTTenantForAPI
58 fields = ('humanReadableName', 'id', 'service_specific_id', 's_tag', 'c_tag', 'subscriber', 'related' )
59
60 def getHumanReadableName(self, obj):
61 return obj.__unicode__()
62
63class VOLTTenantViewSet(XOSViewSet):
64 base_name = "volt"
65 method_name = "volt"
66 method_kind = "viewset"
67 queryset = VOLTTenantForAPI.get_tenant_objects().all() # select_related().all()
68 serializer_class = VOLTTenantSerializer
69
70 @classmethod
71 def get_urlpatterns(self, api_path="^"):
72 patterns = super(VOLTTenantViewSet, self).get_urlpatterns(api_path=api_path)
73
74 return patterns
75
76 def list(self, request):
77 queryset = self.filter_queryset(self.get_queryset())
78
79 c_tag = self.request.query_params.get('c_tag', None)
80 if c_tag is not None:
81 ids = [x.id for x in queryset if x.get_attribute("c_tag", None)==c_tag]
82 queryset = queryset.filter(id__in=ids)
83
84 s_tag = self.request.query_params.get('s_tag', None)
85 if s_tag is not None:
86 ids = [x.id for x in queryset if x.get_attribute("s_tag", None)==s_tag]
87 queryset = queryset.filter(id__in=ids)
88
89 serializer = self.get_serializer(queryset, many=True)
90
91 return Response(serializer.data)
92
93
94
95
96