blob: 25559a09171438a87704478e7d237d6e18f459c1 [file] [log] [blame]
Scott Baker286ca5d2015-04-20 16:37:35 -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
Scott Baker99f3b172015-06-05 12:44:22 -07006from rest_framework import status
Scott Baker286ca5d2015-04-20 16:37:35 -07007from core.models import *
8from django.forms import widgets
Scott Bakera72b4542016-03-08 11:37:54 -08009from services.cord.models import VOLTTenant, VOLTService, CordSubscriberRoot
Scott Baker286ca5d2015-04-20 16:37:35 -070010from plus import PlusSerializerMixin
11from xos.apibase import XOSListCreateAPIView, XOSRetrieveUpdateDestroyAPIView, XOSPermissionDenied
12
13if hasattr(serializers, "ReadOnlyField"):
14 # rest_framework 3.x
15 ReadOnlyField = serializers.ReadOnlyField
16else:
17 # rest_framework 2.x
18 ReadOnlyField = serializers.Field
19
20def get_default_volt_service():
21 volt_services = VOLTService.get_service_objects().all()
22 if volt_services:
23 return volt_services[0].id
24 return None
25
26class VOLTTenantIdSerializer(serializers.ModelSerializer, PlusSerializerMixin):
27 id = ReadOnlyField()
28 service_specific_id = serializers.CharField()
Scott Bakerf1527cd2015-11-09 20:25:21 -080029 s_tag = serializers.CharField()
30 c_tag = serializers.CharField()
Scott Bakera2f9ecc2015-04-20 16:57:53 -070031 provider_service = serializers.PrimaryKeyRelatedField(queryset=VOLTService.get_service_objects().all(), default=get_default_volt_service)
Scott Bakera72b4542016-03-08 11:37:54 -080032 subscriber_root = serializers.PrimaryKeyRelatedField(queryset=CordSubscriberRoot.get_tenant_objects().all(), required=False)
Scott Baker286ca5d2015-04-20 16:37:35 -070033
34 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
35
Scott Baker12075a72015-06-01 14:11:48 -070036 computeNodeName = serializers.SerializerMethodField("getComputeNodeName")
37
Scott Baker286ca5d2015-04-20 16:37:35 -070038 class Meta:
39 model = VOLTTenant
Scott Bakera72b4542016-03-08 11:37:54 -080040 fields = ('humanReadableName', 'id', 'provider_service', 'service_specific_id', 's_tag', 'c_tag', 'computeNodeName', 'subscriber_root' )
Scott Baker286ca5d2015-04-20 16:37:35 -070041
42 def getHumanReadableName(self, obj):
43 return obj.__unicode__()
44
Scott Baker12075a72015-06-01 14:11:48 -070045 def getComputeNodeName(self, obj):
46 vcpe = obj.vcpe
47 if not vcpe:
48 return None
Tony Mackd8515472015-08-19 11:58:18 -040049 instance = vcpe.instance
50 if not instance:
Scott Baker12075a72015-06-01 14:11:48 -070051 return None
Tony Mackd8515472015-08-19 11:58:18 -040052 return instance.node.name
Scott Baker12075a72015-06-01 14:11:48 -070053
Scott Baker286ca5d2015-04-20 16:37:35 -070054class VOLTTenantList(XOSListCreateAPIView):
Scott Baker286ca5d2015-04-20 16:37:35 -070055 serializer_class = VOLTTenantIdSerializer
56
57 method_kind = "list"
58 method_name = "volttenant"
59
Scott Baker8d29e6b2015-04-20 20:22:50 -070060 def get_queryset(self):
61 queryset = VOLTTenant.get_tenant_objects().select_related().all()
62
Scott Baker52c578a2016-03-30 09:37:52 -070063 service_specific_id = self.request.query_params.get('service_specific_id', None)
Scott Baker8d29e6b2015-04-20 20:22:50 -070064 if service_specific_id is not None:
65 queryset = queryset.filter(service_specific_id=service_specific_id)
66
Scott Baker52c578a2016-03-30 09:37:52 -070067 c_tag = self.request.query_params.get('c_tag', None)
Scott Bakerf1527cd2015-11-09 20:25:21 -080068 if c_tag is not None:
69 ids = [x.id for x in queryset if x.get_attribute("c_tag", None)==c_tag]
70 queryset = queryset.filter(id__in=ids)
71
Scott Baker52c578a2016-03-30 09:37:52 -070072 s_tag = self.request.query_params.get('s_tag', None)
Scott Bakerf1527cd2015-11-09 20:25:21 -080073 if s_tag is not None:
74 ids = [x.id for x in queryset if x.get_attribute("s_tag", None)==s_tag]
Scott Baker8d29e6b2015-04-20 20:22:50 -070075 queryset = queryset.filter(id__in=ids)
76
77 return queryset
78
Scott Baker99f3b172015-06-05 12:44:22 -070079 def post(self, request, format=None):
80 data = request.DATA
81
82 existing_obj = None
83 for obj in VOLTTenant.get_tenant_objects().all():
Scott Bakerf1527cd2015-11-09 20:25:21 -080084 if (obj.c_tag == data.get("c_tag", None)) and (obj.s_tag == data.get("s_tag", None)) and (obj.service_specific_id == data.get("service_specific_id",None)):
Scott Baker99f3b172015-06-05 12:44:22 -070085 existing_obj = obj
86
87 if existing_obj:
88 serializer = VOLTTenantIdSerializer(existing_obj)
89 headers = self.get_success_headers(serializer.data)
90 return Response( serializer.data, status=status.HTTP_200_OK )
91
92 return super(VOLTTenantList, self).post(request, format)
93
Scott Baker286ca5d2015-04-20 16:37:35 -070094class VOLTTenantDetail(XOSRetrieveUpdateDestroyAPIView):
Scott Baker286ca5d2015-04-20 16:37:35 -070095 serializer_class = VOLTTenantIdSerializer
Scott Baker8d29e6b2015-04-20 20:22:50 -070096 queryset = VOLTTenant.get_tenant_objects().select_related().all()
Scott Baker286ca5d2015-04-20 16:37:35 -070097
98 method_kind = "detail"
99 method_name = "volttenant"
100
101
Scott Baker8d29e6b2015-04-20 20:22:50 -0700102
103
104