Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 1 | from rest_framework.response import Response |
| 2 | from rest_framework import serializers, filters, status |
| 3 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 4 | from services.metronetwork.models import * |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 5 | from django.core.exceptions import ObjectDoesNotExist |
| 6 | from django.core import serializers as jsonserializer |
| 7 | |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 8 | class MetroNetworkSystemSerializer(PlusModelSerializer): |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 9 | id = ReadOnlyField() |
| 10 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 11 | |
| 12 | class Meta: |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 13 | model = MetroNetworkSystem |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 14 | fields = ('humanReadableName', |
| 15 | 'id', |
| 16 | 'restUrl', |
| 17 | 'administrativeState', |
| 18 | 'operationalState') |
| 19 | |
| 20 | def getHumanReadableName(self, obj): |
| 21 | return obj.__unicode__() |
| 22 | |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 23 | class MetroNetworkSystemViewSet(XOSViewSet): |
| 24 | base_name = "metronetworksystem" |
| 25 | method_name = "metronetworksystem" |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 26 | method_kind = "viewset" |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 27 | queryset = MetroNetworkSystem.objects.all() |
| 28 | serializer_class = MetroNetworkSystemSerializer |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 29 | |
| 30 | @classmethod |
| 31 | def get_urlpatterns(self, api_path="^"): |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 32 | patterns = super(MetroNetworkSystemViewSet, self).get_urlpatterns(api_path=api_path) |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 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 | |
| 44 | class 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 | |
| 66 | class NetworkEdgePortViewSet(XOSViewSet): |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 67 | base_name = "UNI" |
| 68 | method_name = "UNI" |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 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 | |
| 86 | class 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 | |
| 107 | class NetworkEdgeToEdgePointConnectionViewSet(XOSViewSet): |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 108 | base_name = "ELINE" |
| 109 | method_name = "ELINE" |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 110 | method_kind = "viewset" |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 111 | queryset = NetworkEdgeToEdgePointConnection.get_service_objects().all() |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 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 | |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame^] | 186 | return Response(response_data) |
| 187 | |
| 188 | class NetworkEdgeToMultipointConnectionSerializer(PlusModelSerializer): |
| 189 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 190 | root = NetworkEdgePortSerializer(required=True, read_only=False) |
| 191 | eps = NetworkEdgePortSerializer(required=True, read_only=False, many=True) |
| 192 | |
| 193 | class Meta: |
| 194 | model = NetworkEdgeToMultipointConnection |
| 195 | |
| 196 | fields = ('humanReadableName', |
| 197 | 'sid', |
| 198 | 'id', |
| 199 | 'type', |
| 200 | 'root', |
| 201 | 'eps', |
| 202 | 'operstate', |
| 203 | 'adminstate' |
| 204 | ) |
| 205 | |
| 206 | def getHumanReadableName(self, obj): |
| 207 | return obj.id |
| 208 | |
| 209 | class NetworkEdgeToMultipointConnectionViewSet(XOSViewSet): |
| 210 | base_name = "ETREE" |
| 211 | method_name = "ETREE" |
| 212 | method_kind = "viewset" |
| 213 | queryset = NetworkEdgeToMultipointConnection.get_service_objects().all() |
| 214 | serializer_class = NetworkEdgeToMultipointConnectionSerializer |
| 215 | |
| 216 | @classmethod |
| 217 | def get_urlpatterns(self, api_path="^"): |
| 218 | patterns = super(NetworkEdgeToMultipointConnectionViewSet, self).get_urlpatterns(api_path=api_path) |
| 219 | |
| 220 | return patterns |
| 221 | |
| 222 | def list(self, request): |
| 223 | |
| 224 | object_list = self.filter_queryset(self.get_queryset()) |
| 225 | |
| 226 | serializer = self.get_serializer(object_list, many=True) |
| 227 | |
| 228 | return Response(serializer.data) |
| 229 | |
| 230 | def destroy(self, request, pk=None): |
| 231 | ETreeConnectionToDelete = NetworkEdgeToMultipointConnection.objects.get(pk=pk) |
| 232 | |
| 233 | if (ETreeConnectionToDelete): |
| 234 | ETreeConnectionToDelete.adminstate = 'deactivationrequested' |
| 235 | ETreeConnectionToDelete.save() |
| 236 | else: |
| 237 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 238 | |
| 239 | return Response(status=status.HTTP_200_OK) |
| 240 | |
| 241 | def create(self, validated_data): |
| 242 | |
| 243 | ETreeConnection = NetworkEdgeToMultipointConnection() |
| 244 | ETreeConnection.sid = validated_data.data.get('sid') |
| 245 | ETreeConnection.adminstate = validated_data.data.get('adminstate') |
| 246 | ETreeConnection.operstate = validated_data.data.get('operstate') |
| 247 | ETreeConnection.type = validated_data.data.get('type') |
| 248 | |
| 249 | root_id = validated_data.data.get('root') |
| 250 | eps_list = validated_data.data.get('eps') |
| 251 | |
| 252 | root = NetworkEdgePort.objects.get(pk=root_id) |
| 253 | ETreeConnection.root = root |
| 254 | ETreeConnection.save() |
| 255 | |
| 256 | for ep in eps_list: |
| 257 | port = NetworkEdgePort.objects.get(pk=ep['id']) |
| 258 | ETreeConnection.eps.add(port) |
| 259 | |
| 260 | response_data = {} |
| 261 | response_data['sid'] = ETreeConnection.sid |
| 262 | response_data['adminstate'] = ETreeConnection.adminstate |
| 263 | response_data['operstate'] = ETreeConnection.operstate |
| 264 | response_data['type'] = ETreeConnection.type |
| 265 | |
| 266 | response_data['root'] = {} |
| 267 | response_data['root']['id'] = root.id |
| 268 | response_data['root']['pid'] = root.pid |
| 269 | response_data['root']['bwpCfgCbs'] = root.bwpCfgCbs |
| 270 | response_data['root']['bwpCfgEbs'] = root.bwpCfgEbs |
| 271 | response_data['root']['bwpCfgCir'] = root.bwpCfgCir |
| 272 | response_data['root']['bwpCfgEir'] = root.bwpCfgEir |
| 273 | response_data['root']['name'] = root.name |
| 274 | response_data['root']['location'] = root.location |
| 275 | response_data['root']['latlng'] = root.latlng |
| 276 | |
| 277 | eps_data = [] |
| 278 | for ep in ETreeConnection.eps.all(): |
| 279 | port = {} |
| 280 | port['id'] = ep.id |
| 281 | port['pid'] = ep.pid |
| 282 | port['bwpCfgCbs'] = ep.bwpCfgCbs |
| 283 | port['bwpCfgEbs'] = ep.bwpCfgEbs |
| 284 | port['bwpCfgCir'] = ep.bwpCfgCir |
| 285 | port['bwpCfgEir'] = ep.bwpCfgEir |
| 286 | port['name'] = ep.name |
| 287 | port['location'] = ep.location |
| 288 | port['latlng'] = ep.latlng |
| 289 | eps_data.append(port) |
| 290 | |
| 291 | response_data['eps'] = eps_data |
| 292 | |
| 293 | return Response(response_data) |
| 294 | |
| 295 | class NetworkMultipointToMultipointConnectionSerializer(PlusModelSerializer): |
| 296 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 297 | eps = NetworkEdgePortSerializer(required=True, read_only=False, many=True) |
| 298 | |
| 299 | class Meta: |
| 300 | model = NetworkMultipointToMultipointConnection |
| 301 | |
| 302 | fields = ('humanReadableName', |
| 303 | 'sid', |
| 304 | 'id', |
| 305 | 'type', |
| 306 | 'eps', |
| 307 | 'operstate', |
| 308 | 'adminstate' |
| 309 | ) |
| 310 | |
| 311 | def getHumanReadableName(self, obj): |
| 312 | return obj.id |
| 313 | |
| 314 | class NetworkMultipointToMultipointConnectionViewSet(XOSViewSet): |
| 315 | base_name = "ELAN" |
| 316 | method_name = "ELAN" |
| 317 | method_kind = "viewset" |
| 318 | queryset = NetworkMultipointToMultipointConnection.get_service_objects().all() |
| 319 | serializer_class = NetworkMultipointToMultipointConnectionSerializer |
| 320 | |
| 321 | @classmethod |
| 322 | def get_urlpatterns(self, api_path="^"): |
| 323 | patterns = super(NetworkMultipointToMultipointConnectionViewSet, self).get_urlpatterns(api_path=api_path) |
| 324 | |
| 325 | return patterns |
| 326 | |
| 327 | def list(self, request): |
| 328 | |
| 329 | object_list = self.filter_queryset(self.get_queryset()) |
| 330 | |
| 331 | serializer = self.get_serializer(object_list, many=True) |
| 332 | |
| 333 | return Response(serializer.data) |
| 334 | |
| 335 | def destroy(self, request, pk=None): |
| 336 | ETreeConnectionToDelete = NetworkMultipointToMultipointConnection.objects.get(pk=pk) |
| 337 | |
| 338 | if (ETreeConnectionToDelete): |
| 339 | ETreeConnectionToDelete.adminstate = 'deactivationrequested' |
| 340 | ETreeConnectionToDelete.save() |
| 341 | else: |
| 342 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 343 | |
| 344 | return Response(status=status.HTTP_200_OK) |
| 345 | |
| 346 | def create(self, validated_data): |
| 347 | |
| 348 | ELanConnection = NetworkMultipointToMultipointConnection() |
| 349 | ELanConnection.sid = validated_data.data.get('sid') |
| 350 | ELanConnection.adminstate = validated_data.data.get('adminstate') |
| 351 | ELanConnection.operstate = validated_data.data.get('operstate') |
| 352 | ELanConnection.type = validated_data.data.get('type') |
| 353 | |
| 354 | eps_list = validated_data.data.get('eps') |
| 355 | ELanConnection.save() |
| 356 | |
| 357 | for ep in eps_list: |
| 358 | port = NetworkEdgePort.objects.get(pk=ep['id']) |
| 359 | ELanConnection.eps.add(port) |
| 360 | |
| 361 | response_data = {} |
| 362 | response_data['sid'] = ELanConnection.sid |
| 363 | response_data['adminstate'] = ELanConnection.adminstate |
| 364 | response_data['operstate'] = ELanConnection.operstate |
| 365 | response_data['type'] = ELanConnection.type |
| 366 | |
| 367 | eps_data = [] |
| 368 | for ep in ELanConnection.eps.all(): |
| 369 | port = {} |
| 370 | port['id'] = ep.id |
| 371 | port['pid'] = ep.pid |
| 372 | port['bwpCfgCbs'] = ep.bwpCfgCbs |
| 373 | port['bwpCfgEbs'] = ep.bwpCfgEbs |
| 374 | port['bwpCfgCir'] = ep.bwpCfgCir |
| 375 | port['bwpCfgEir'] = ep.bwpCfgEir |
| 376 | port['name'] = ep.name |
| 377 | port['location'] = ep.location |
| 378 | port['latlng'] = ep.latlng |
| 379 | eps_data.append(port) |
| 380 | |
| 381 | response_data['eps'] = eps_data |
| 382 | |
| 383 | return Response(response_data) |