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