blob: 0a800344a46a049ae558fb9ad1f0c933177d5e81 [file] [log] [blame]
Rizwan Haider30b33792016-08-18 02:11:18 -04001from rest_framework.response import Response
2from rest_framework import serializers, filters, status
3from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField
4from services.metronetwork.models import MetroNetworkService
5from core.models.netw import NetworkEdgePort, NetworkEdgeToEdgePointConnection
6from django.core.exceptions import ObjectDoesNotExist
7from django.core import serializers as jsonserializer
8
9class MetroNetworkServiceSerializer(PlusModelSerializer):
10 id = ReadOnlyField()
11 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
12
13 class Meta:
14 model = MetroNetworkService
15 fields = ('humanReadableName',
16 'id',
17 'restUrl',
18 'administrativeState',
19 'operationalState')
20
21 def getHumanReadableName(self, obj):
22 return obj.__unicode__()
23
24class MetroNetworkServiceViewSet(XOSViewSet):
25 base_name = "metronetworkservice"
26 method_name = "metronetworkservice/metronetwork"
27 method_kind = "viewset"
28 queryset = MetroNetworkService.get_service_objects().all()
29 serializer_class = MetroNetworkServiceSerializer
30
31 @classmethod
32 def get_urlpatterns(self, api_path="^"):
33 patterns = super(MetroNetworkServiceViewSet, self).get_urlpatterns(api_path=api_path)
34
35 return patterns
36
37 def list(self, request):
38 object_list = self.filter_queryset(self.get_queryset())
39
40 serializer = self.get_serializer(object_list, many=True)
41
42 return Response(serializer.data)
43
44
45class NetworkEdgePortSerializer(PlusModelSerializer):
46 id = ReadOnlyField()
47 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
48
49 class Meta:
50 model = NetworkEdgePort
51 fields = ('humanReadableName',
52 'pid',
53 'id',
54 'element',
55 'bwpCfgCbs',
56 'bwpCfgEbs',
57 'bwpCfgCir',
58 'bwpCfgEir',
59 'name',
60 'location',
61 'latlng')
62
63
64 def getHumanReadableName(self, obj):
65 return obj.id
66
67class NetworkEdgePortViewSet(XOSViewSet):
68 base_name = "SCA_ETH_FPP_UNI_N"
69 method_name = "metronetworkservice/SCA_ETH_FPP_UNI_N"
70 method_kind = "viewset"
71 queryset = NetworkEdgePort.objects.all()
72 serializer_class = NetworkEdgePortSerializer
73
74 @classmethod
75 def get_urlpatterns(self, api_path="^"):
76 patterns = super(NetworkEdgePortViewSet, self).get_urlpatterns(api_path=api_path)
77
78 return patterns
79
80 def list(self, request):
81 object_list = self.filter_queryset(self.get_queryset())
82
83 serializer = self.get_serializer(object_list, many=True)
84
85 return Response(serializer.data)
86
87class NetworkEdgeToEdgePointConnectionSerializer(PlusModelSerializer):
88 humanReadableName = serializers.SerializerMethodField("getHumanReadableName")
89 uni1 = NetworkEdgePortSerializer(required=True, read_only=False)
90 uni2 = NetworkEdgePortSerializer(required=True, read_only=False)
91
92 class Meta:
93 model = NetworkEdgeToEdgePointConnection
94
95 fields = ('humanReadableName',
96 'sid',
97 'id',
98 'type',
99 'uni1',
100 'uni2',
101 'operstate',
102 'adminstate'
103 )
104
105 def getHumanReadableName(self, obj):
106 return obj.id
107
108class NetworkEdgeToEdgePointConnectionViewSet(XOSViewSet):
109 base_name = "SCA_ETH_FDFr_EC"
110 method_name = "metronetworkservice/SCA_ETH_FDFr_EC"
111 method_kind = "viewset"
112 queryset = NetworkEdgeToEdgePointConnection.objects.all()
113 serializer_class = NetworkEdgeToEdgePointConnectionSerializer
114
115 @classmethod
116 def get_urlpatterns(self, api_path="^"):
117 patterns = super(NetworkEdgeToEdgePointConnectionViewSet, self).get_urlpatterns(api_path=api_path)
118
119 return patterns
120
121 def list(self, request):
122
123 object_list = self.filter_queryset(self.get_queryset())
124
125 serializer = self.get_serializer(object_list, many=True)
126
127 return Response(serializer.data)
128
129 def destroy(self, request, pk=None):
130 ELineConnectionToDelete = NetworkEdgeToEdgePointConnection.objects.get(pk=pk)
131
132 if (ELineConnectionToDelete):
133 ELineConnectionToDelete.adminstate = 'deactivationrequested'
134 ELineConnectionToDelete.save()
135 else:
136 return Response(status=status.HTTP_400_BAD_REQUEST)
137
138 return Response(status=status.HTTP_200_OK)
139
140
141 def create(self, validated_data):
142
143 ELineConnection = NetworkEdgeToEdgePointConnection()
144 ELineConnection.sid = validated_data.data.get('sid')
145 ELineConnection.adminstate = validated_data.data.get('adminstate')
146 ELineConnection.operstate = validated_data.data.get('operstate')
147 ELineConnection.type = validated_data.data.get('type')
148
149 uni1 = validated_data.data.get('uni1')
150 uni2 = validated_data.data.get('uni2')
151
152 uni1 = NetworkEdgePort.objects.get(pk=uni1['id'])
153 uni2 = NetworkEdgePort.objects.get(pk=uni2['id'])
154
155 ELineConnection.uni1 = uni1
156 ELineConnection.uni2 = uni2
157 ELineConnection.save()
158
159 response_data = {}
160 response_data['sid'] = ELineConnection.sid
161 response_data['adminstate'] = ELineConnection.adminstate
162 response_data['operstate'] = ELineConnection.operstate
163 response_data['type'] = ELineConnection.type
164
165 response_data['uni1'] = {}
166 response_data['uni1']['id'] = uni1.id
167 response_data['uni1']['pid'] = uni1.pid
168 response_data['uni1']['bwpCfgCbs'] = uni1.bwpCfgCbs
169 response_data['uni1']['bwpCfgEbs'] = uni1.bwpCfgEbs
170 response_data['uni1']['bwpCfgCir'] = uni1.bwpCfgCir
171 response_data['uni1']['bwpCfgEir'] = uni1.bwpCfgEir
172 response_data['uni1']['name'] = uni1.name
173 response_data['uni1']['location'] = uni1.location
174 response_data['uni1']['latlng'] = uni1.latlng
175
176 response_data['uni2'] = {}
177 response_data['uni2']['id'] = uni2.id
178 response_data['uni2']['pid'] = uni2.pid
179 response_data['uni2']['bwpCfgCbs'] = uni2.bwpCfgCbs
180 response_data['uni2']['bwpCfgEbs'] = uni2.bwpCfgEbs
181 response_data['uni2']['bwpCfgCir'] = uni2.bwpCfgCir
182 response_data['uni2']['bwpCfgEir'] = uni2.bwpCfgEir
183 response_data['uni2']['name'] = uni1.name
184 response_data['uni2']['location'] = uni1.location
185 response_data['uni2']['latlng'] = uni1.latlng
186
187 return Response(response_data)