Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 1 | from django.http import HttpResponseBadRequest |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 2 | from rest_framework.response import Response |
Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 3 | from rest_framework.views import APIView |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 4 | from rest_framework import serializers, filters, status |
| 5 | from api.xosapi_helpers import PlusModelSerializer, XOSViewSet, ReadOnlyField |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 6 | from services.metronetwork.models import * |
Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 7 | from random import randint |
| 8 | import json |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 9 | from django.core.exceptions import ObjectDoesNotExist |
| 10 | from django.core import serializers as jsonserializer |
| 11 | |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 12 | class MetroNetworkSystemSerializer(PlusModelSerializer): |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 13 | id = ReadOnlyField() |
| 14 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 15 | |
| 16 | class Meta: |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 17 | model = MetroNetworkSystem |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 18 | fields = ('humanReadableName', |
| 19 | 'id', |
| 20 | 'restUrl', |
| 21 | 'administrativeState', |
| 22 | 'operationalState') |
| 23 | |
| 24 | def getHumanReadableName(self, obj): |
Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 25 | return obj.name |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 26 | |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 27 | class MetroNetworkSystemViewSet(XOSViewSet): |
| 28 | base_name = "metronetworksystem" |
| 29 | method_name = "metronetworksystem" |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 30 | method_kind = "viewset" |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 31 | queryset = MetroNetworkSystem.objects.all() |
| 32 | serializer_class = MetroNetworkSystemSerializer |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 33 | |
| 34 | @classmethod |
| 35 | def get_urlpatterns(self, api_path="^"): |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 36 | patterns = super(MetroNetworkSystemViewSet, self).get_urlpatterns(api_path=api_path) |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 37 | |
| 38 | return patterns |
| 39 | |
| 40 | def list(self, request): |
| 41 | object_list = self.filter_queryset(self.get_queryset()) |
| 42 | |
| 43 | serializer = self.get_serializer(object_list, many=True) |
| 44 | |
| 45 | return Response(serializer.data) |
| 46 | |
| 47 | |
| 48 | class NetworkEdgePortSerializer(PlusModelSerializer): |
| 49 | id = ReadOnlyField() |
| 50 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 51 | |
| 52 | class Meta: |
| 53 | model = NetworkEdgePort |
| 54 | fields = ('humanReadableName', |
| 55 | 'pid', |
| 56 | 'id', |
| 57 | 'element', |
| 58 | 'bwpCfgCbs', |
| 59 | 'bwpCfgEbs', |
| 60 | 'bwpCfgCir', |
| 61 | 'bwpCfgEir', |
| 62 | 'name', |
| 63 | 'location', |
| 64 | 'latlng') |
| 65 | |
| 66 | |
| 67 | def getHumanReadableName(self, obj): |
| 68 | return obj.id |
| 69 | |
| 70 | class NetworkEdgePortViewSet(XOSViewSet): |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 71 | base_name = "UNI" |
| 72 | method_name = "UNI" |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 73 | method_kind = "viewset" |
| 74 | queryset = NetworkEdgePort.objects.all() |
| 75 | serializer_class = NetworkEdgePortSerializer |
| 76 | |
| 77 | @classmethod |
| 78 | def get_urlpatterns(self, api_path="^"): |
| 79 | patterns = super(NetworkEdgePortViewSet, self).get_urlpatterns(api_path=api_path) |
| 80 | |
| 81 | return patterns |
| 82 | |
| 83 | def list(self, request): |
| 84 | object_list = self.filter_queryset(self.get_queryset()) |
| 85 | |
| 86 | serializer = self.get_serializer(object_list, many=True) |
| 87 | |
| 88 | return Response(serializer.data) |
| 89 | |
| 90 | class NetworkEdgeToEdgePointConnectionSerializer(PlusModelSerializer): |
| 91 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 92 | uni1 = NetworkEdgePortSerializer(required=True, read_only=False) |
| 93 | uni2 = NetworkEdgePortSerializer(required=True, read_only=False) |
| 94 | |
| 95 | class Meta: |
| 96 | model = NetworkEdgeToEdgePointConnection |
| 97 | |
| 98 | fields = ('humanReadableName', |
| 99 | 'sid', |
| 100 | 'id', |
| 101 | 'type', |
| 102 | 'uni1', |
| 103 | 'uni2', |
| 104 | 'operstate', |
| 105 | 'adminstate' |
| 106 | ) |
| 107 | |
| 108 | def getHumanReadableName(self, obj): |
| 109 | return obj.id |
| 110 | |
| 111 | class NetworkEdgeToEdgePointConnectionViewSet(XOSViewSet): |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 112 | base_name = "ELINE" |
| 113 | method_name = "ELINE" |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 114 | method_kind = "viewset" |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 115 | queryset = NetworkEdgeToEdgePointConnection.get_service_objects().all() |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 116 | serializer_class = NetworkEdgeToEdgePointConnectionSerializer |
| 117 | |
| 118 | @classmethod |
| 119 | def get_urlpatterns(self, api_path="^"): |
| 120 | patterns = super(NetworkEdgeToEdgePointConnectionViewSet, self).get_urlpatterns(api_path=api_path) |
| 121 | |
| 122 | return patterns |
| 123 | |
| 124 | def list(self, request): |
| 125 | |
| 126 | object_list = self.filter_queryset(self.get_queryset()) |
| 127 | |
| 128 | serializer = self.get_serializer(object_list, many=True) |
| 129 | |
| 130 | return Response(serializer.data) |
| 131 | |
| 132 | def destroy(self, request, pk=None): |
| 133 | ELineConnectionToDelete = NetworkEdgeToEdgePointConnection.objects.get(pk=pk) |
| 134 | |
| 135 | if (ELineConnectionToDelete): |
| 136 | ELineConnectionToDelete.adminstate = 'deactivationrequested' |
| 137 | ELineConnectionToDelete.save() |
| 138 | else: |
| 139 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 140 | |
| 141 | return Response(status=status.HTTP_200_OK) |
| 142 | |
| 143 | |
| 144 | def create(self, validated_data): |
| 145 | |
| 146 | ELineConnection = NetworkEdgeToEdgePointConnection() |
| 147 | ELineConnection.sid = validated_data.data.get('sid') |
| 148 | ELineConnection.adminstate = validated_data.data.get('adminstate') |
| 149 | ELineConnection.operstate = validated_data.data.get('operstate') |
Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 150 | ELineConnection.sid = validated_data.data.get('sid') |
| 151 | ELineConnection.type = 'Point_To_Point' |
Rizwan Haider | 30b3379 | 2016-08-18 02:11:18 -0400 | [diff] [blame] | 152 | |
| 153 | uni1 = validated_data.data.get('uni1') |
| 154 | uni2 = validated_data.data.get('uni2') |
| 155 | |
| 156 | uni1 = NetworkEdgePort.objects.get(pk=uni1['id']) |
| 157 | uni2 = NetworkEdgePort.objects.get(pk=uni2['id']) |
| 158 | |
| 159 | ELineConnection.uni1 = uni1 |
| 160 | ELineConnection.uni2 = uni2 |
| 161 | ELineConnection.save() |
| 162 | |
| 163 | response_data = {} |
| 164 | response_data['sid'] = ELineConnection.sid |
| 165 | response_data['adminstate'] = ELineConnection.adminstate |
| 166 | response_data['operstate'] = ELineConnection.operstate |
| 167 | response_data['type'] = ELineConnection.type |
| 168 | |
| 169 | response_data['uni1'] = {} |
| 170 | response_data['uni1']['id'] = uni1.id |
| 171 | response_data['uni1']['pid'] = uni1.pid |
| 172 | response_data['uni1']['bwpCfgCbs'] = uni1.bwpCfgCbs |
| 173 | response_data['uni1']['bwpCfgEbs'] = uni1.bwpCfgEbs |
| 174 | response_data['uni1']['bwpCfgCir'] = uni1.bwpCfgCir |
| 175 | response_data['uni1']['bwpCfgEir'] = uni1.bwpCfgEir |
| 176 | response_data['uni1']['name'] = uni1.name |
| 177 | response_data['uni1']['location'] = uni1.location |
| 178 | response_data['uni1']['latlng'] = uni1.latlng |
| 179 | |
| 180 | response_data['uni2'] = {} |
| 181 | response_data['uni2']['id'] = uni2.id |
| 182 | response_data['uni2']['pid'] = uni2.pid |
| 183 | response_data['uni2']['bwpCfgCbs'] = uni2.bwpCfgCbs |
| 184 | response_data['uni2']['bwpCfgEbs'] = uni2.bwpCfgEbs |
| 185 | response_data['uni2']['bwpCfgCir'] = uni2.bwpCfgCir |
| 186 | response_data['uni2']['bwpCfgEir'] = uni2.bwpCfgEir |
| 187 | response_data['uni2']['name'] = uni1.name |
| 188 | response_data['uni2']['location'] = uni1.location |
| 189 | response_data['uni2']['latlng'] = uni1.latlng |
| 190 | |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 191 | return Response(response_data) |
| 192 | |
| 193 | class NetworkEdgeToMultipointConnectionSerializer(PlusModelSerializer): |
| 194 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 195 | root = NetworkEdgePortSerializer(required=True, read_only=False) |
| 196 | eps = NetworkEdgePortSerializer(required=True, read_only=False, many=True) |
| 197 | |
| 198 | class Meta: |
| 199 | model = NetworkEdgeToMultipointConnection |
| 200 | |
| 201 | fields = ('humanReadableName', |
| 202 | 'sid', |
| 203 | 'id', |
| 204 | 'type', |
| 205 | 'root', |
| 206 | 'eps', |
| 207 | 'operstate', |
| 208 | 'adminstate' |
| 209 | ) |
| 210 | |
| 211 | def getHumanReadableName(self, obj): |
| 212 | return obj.id |
| 213 | |
| 214 | class NetworkEdgeToMultipointConnectionViewSet(XOSViewSet): |
| 215 | base_name = "ETREE" |
| 216 | method_name = "ETREE" |
| 217 | method_kind = "viewset" |
| 218 | queryset = NetworkEdgeToMultipointConnection.get_service_objects().all() |
| 219 | serializer_class = NetworkEdgeToMultipointConnectionSerializer |
| 220 | |
| 221 | @classmethod |
| 222 | def get_urlpatterns(self, api_path="^"): |
| 223 | patterns = super(NetworkEdgeToMultipointConnectionViewSet, self).get_urlpatterns(api_path=api_path) |
| 224 | |
| 225 | return patterns |
| 226 | |
| 227 | def list(self, request): |
| 228 | |
| 229 | object_list = self.filter_queryset(self.get_queryset()) |
| 230 | |
| 231 | serializer = self.get_serializer(object_list, many=True) |
| 232 | |
| 233 | return Response(serializer.data) |
| 234 | |
| 235 | def destroy(self, request, pk=None): |
| 236 | ETreeConnectionToDelete = NetworkEdgeToMultipointConnection.objects.get(pk=pk) |
| 237 | |
| 238 | if (ETreeConnectionToDelete): |
| 239 | ETreeConnectionToDelete.adminstate = 'deactivationrequested' |
| 240 | ETreeConnectionToDelete.save() |
| 241 | else: |
| 242 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 243 | |
| 244 | return Response(status=status.HTTP_200_OK) |
| 245 | |
| 246 | def create(self, validated_data): |
| 247 | |
| 248 | ETreeConnection = NetworkEdgeToMultipointConnection() |
| 249 | ETreeConnection.sid = validated_data.data.get('sid') |
| 250 | ETreeConnection.adminstate = validated_data.data.get('adminstate') |
| 251 | ETreeConnection.operstate = validated_data.data.get('operstate') |
Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 252 | ETreeConnection.sid = validated_data.data.get('sid') |
| 253 | ETreeConnection.type = 'Root_Multipoint' |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 254 | |
| 255 | root_id = validated_data.data.get('root') |
| 256 | eps_list = validated_data.data.get('eps') |
| 257 | |
| 258 | root = NetworkEdgePort.objects.get(pk=root_id) |
| 259 | ETreeConnection.root = root |
| 260 | ETreeConnection.save() |
| 261 | |
| 262 | for ep in eps_list: |
| 263 | port = NetworkEdgePort.objects.get(pk=ep['id']) |
| 264 | ETreeConnection.eps.add(port) |
| 265 | |
| 266 | response_data = {} |
| 267 | response_data['sid'] = ETreeConnection.sid |
| 268 | response_data['adminstate'] = ETreeConnection.adminstate |
| 269 | response_data['operstate'] = ETreeConnection.operstate |
| 270 | response_data['type'] = ETreeConnection.type |
| 271 | |
| 272 | response_data['root'] = {} |
| 273 | response_data['root']['id'] = root.id |
| 274 | response_data['root']['pid'] = root.pid |
| 275 | response_data['root']['bwpCfgCbs'] = root.bwpCfgCbs |
| 276 | response_data['root']['bwpCfgEbs'] = root.bwpCfgEbs |
| 277 | response_data['root']['bwpCfgCir'] = root.bwpCfgCir |
| 278 | response_data['root']['bwpCfgEir'] = root.bwpCfgEir |
| 279 | response_data['root']['name'] = root.name |
| 280 | response_data['root']['location'] = root.location |
| 281 | response_data['root']['latlng'] = root.latlng |
| 282 | |
| 283 | eps_data = [] |
| 284 | for ep in ETreeConnection.eps.all(): |
| 285 | port = {} |
| 286 | port['id'] = ep.id |
| 287 | port['pid'] = ep.pid |
| 288 | port['bwpCfgCbs'] = ep.bwpCfgCbs |
| 289 | port['bwpCfgEbs'] = ep.bwpCfgEbs |
| 290 | port['bwpCfgCir'] = ep.bwpCfgCir |
| 291 | port['bwpCfgEir'] = ep.bwpCfgEir |
| 292 | port['name'] = ep.name |
| 293 | port['location'] = ep.location |
| 294 | port['latlng'] = ep.latlng |
| 295 | eps_data.append(port) |
| 296 | |
| 297 | response_data['eps'] = eps_data |
| 298 | |
| 299 | return Response(response_data) |
| 300 | |
| 301 | class NetworkMultipointToMultipointConnectionSerializer(PlusModelSerializer): |
| 302 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 303 | eps = NetworkEdgePortSerializer(required=True, read_only=False, many=True) |
| 304 | |
| 305 | class Meta: |
| 306 | model = NetworkMultipointToMultipointConnection |
| 307 | |
| 308 | fields = ('humanReadableName', |
| 309 | 'sid', |
| 310 | 'id', |
| 311 | 'type', |
| 312 | 'eps', |
| 313 | 'operstate', |
| 314 | 'adminstate' |
| 315 | ) |
| 316 | |
| 317 | def getHumanReadableName(self, obj): |
| 318 | return obj.id |
| 319 | |
| 320 | class NetworkMultipointToMultipointConnectionViewSet(XOSViewSet): |
| 321 | base_name = "ELAN" |
| 322 | method_name = "ELAN" |
| 323 | method_kind = "viewset" |
| 324 | queryset = NetworkMultipointToMultipointConnection.get_service_objects().all() |
| 325 | serializer_class = NetworkMultipointToMultipointConnectionSerializer |
| 326 | |
| 327 | @classmethod |
| 328 | def get_urlpatterns(self, api_path="^"): |
| 329 | patterns = super(NetworkMultipointToMultipointConnectionViewSet, self).get_urlpatterns(api_path=api_path) |
| 330 | |
| 331 | return patterns |
| 332 | |
| 333 | def list(self, request): |
| 334 | |
| 335 | object_list = self.filter_queryset(self.get_queryset()) |
| 336 | |
| 337 | serializer = self.get_serializer(object_list, many=True) |
| 338 | |
| 339 | return Response(serializer.data) |
| 340 | |
| 341 | def destroy(self, request, pk=None): |
| 342 | ETreeConnectionToDelete = NetworkMultipointToMultipointConnection.objects.get(pk=pk) |
| 343 | |
| 344 | if (ETreeConnectionToDelete): |
| 345 | ETreeConnectionToDelete.adminstate = 'deactivationrequested' |
| 346 | ETreeConnectionToDelete.save() |
| 347 | else: |
| 348 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 349 | |
| 350 | return Response(status=status.HTTP_200_OK) |
| 351 | |
| 352 | def create(self, validated_data): |
| 353 | |
| 354 | ELanConnection = NetworkMultipointToMultipointConnection() |
| 355 | ELanConnection.sid = validated_data.data.get('sid') |
| 356 | ELanConnection.adminstate = validated_data.data.get('adminstate') |
| 357 | ELanConnection.operstate = validated_data.data.get('operstate') |
Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 358 | ELanConnection.sid = validated_data.data.get('sid') |
| 359 | ELanConnection.type = 'Multipoint_To_Multipoint' |
Rizwan Haider | 65baf55 | 2016-09-28 16:47:28 -0400 | [diff] [blame] | 360 | |
| 361 | eps_list = validated_data.data.get('eps') |
| 362 | ELanConnection.save() |
| 363 | |
| 364 | for ep in eps_list: |
| 365 | port = NetworkEdgePort.objects.get(pk=ep['id']) |
| 366 | ELanConnection.eps.add(port) |
| 367 | |
| 368 | response_data = {} |
| 369 | response_data['sid'] = ELanConnection.sid |
| 370 | response_data['adminstate'] = ELanConnection.adminstate |
| 371 | response_data['operstate'] = ELanConnection.operstate |
| 372 | response_data['type'] = ELanConnection.type |
| 373 | |
| 374 | eps_data = [] |
| 375 | for ep in ELanConnection.eps.all(): |
| 376 | port = {} |
| 377 | port['id'] = ep.id |
| 378 | port['pid'] = ep.pid |
| 379 | port['bwpCfgCbs'] = ep.bwpCfgCbs |
| 380 | port['bwpCfgEbs'] = ep.bwpCfgEbs |
| 381 | port['bwpCfgCir'] = ep.bwpCfgCir |
| 382 | port['bwpCfgEir'] = ep.bwpCfgEir |
| 383 | port['name'] = ep.name |
| 384 | port['location'] = ep.location |
| 385 | port['latlng'] = ep.latlng |
| 386 | eps_data.append(port) |
| 387 | |
| 388 | response_data['eps'] = eps_data |
| 389 | |
| 390 | return Response(response_data) |
Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 391 | |
| 392 | ############################ |
| 393 | |
| 394 | class BandwidthProfileSerializer(PlusModelSerializer): |
| 395 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 396 | |
| 397 | class Meta: |
| 398 | model = BandwidthProfile |
| 399 | |
| 400 | fields = ('humanReadableName', |
| 401 | 'id', |
| 402 | 'bwpcfgcbs', |
| 403 | 'bwpcfgebs', |
| 404 | 'bwpcfgcir', |
| 405 | 'bwpcfgeir', |
| 406 | 'name' |
| 407 | ) |
| 408 | |
| 409 | def getHumanReadableName(self, obj): |
| 410 | return obj.name |
| 411 | |
| 412 | class BandwidthProfileViewSet(XOSViewSet): |
| 413 | base_name = "BANDWIDTH_PROFILE" |
| 414 | method_name = "BANDWIDTH_PROFILE" |
| 415 | method_kind = "viewset" |
| 416 | queryset = BandwidthProfile.objects.all() |
| 417 | serializer_class = BandwidthProfileSerializer |
| 418 | |
| 419 | @classmethod |
| 420 | def get_urlpatterns(self, api_path="^"): |
| 421 | patterns = super(BandwidthProfileViewSet, self).get_urlpatterns(api_path=api_path) |
| 422 | |
| 423 | return patterns |
| 424 | |
| 425 | def list(self, request): |
| 426 | |
| 427 | object_list = self.filter_queryset(self.get_queryset()) |
| 428 | |
| 429 | serializer = self.get_serializer(object_list, many=True) |
| 430 | |
| 431 | return Response(serializer.data) |
| 432 | |
| 433 | def create(self, validated_data): |
| 434 | |
| 435 | bandwidthProfile = BandwidthProfile() |
| 436 | bandwidthProfile.name = validated_data.data.get('name') |
| 437 | bandwidthProfile.bwpcfgcbs = validated_data.data.get('bwpcfgcbs') |
| 438 | bandwidthProfile.bwpcfgebs = validated_data.data.get('bwpcfgebs') |
| 439 | bandwidthProfile.bwpcfgcir = validated_data.data.get('bwpcfgcir') |
| 440 | bandwidthProfile.bwpcfgeir = validated_data.data.get('bwpcfgeir') |
| 441 | |
| 442 | bandwidthProfile.save() |
| 443 | |
| 444 | response_data = {} |
| 445 | response_data['name'] = bandwidthProfile.name |
| 446 | response_data['bwpcfgcbs'] = bandwidthProfile.bwpcfgcbs |
| 447 | response_data['bwpcfgebs'] = bandwidthProfile.bwpcfgebs |
| 448 | response_data['bwpcfgcir'] = bandwidthProfile.bwpcfgeir |
| 449 | response_data['bwpcfgcir'] = bandwidthProfile.bwpcfgcir |
| 450 | response_data['id'] = bandwidthProfile.id |
| 451 | |
| 452 | return Response(response_data) |
| 453 | |
| 454 | class VnodSiteSerializer(PlusModelSerializer): |
| 455 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 456 | |
| 457 | class Meta: |
| 458 | model = Site |
| 459 | |
| 460 | fields = ('humanReadableName', |
| 461 | 'site_url', |
| 462 | 'enabled', |
| 463 | 'longitude', |
| 464 | 'latitude', |
| 465 | 'name' |
| 466 | ) |
| 467 | |
| 468 | def getHumanReadableName(self, obj): |
| 469 | return obj.name |
| 470 | |
| 471 | class RemotePortSerializer(PlusModelSerializer): |
| 472 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 473 | edgeport = NetworkEdgePortSerializer(read_only=True) |
| 474 | remoteportsite = VnodSiteSerializer(read_only=True) |
| 475 | |
| 476 | class Meta: |
| 477 | model = RemotePort |
| 478 | |
| 479 | fields = ('humanReadableName', |
| 480 | 'name', |
| 481 | 'edgeport', |
| 482 | 'id', |
| 483 | 'remoteportsite' |
| 484 | ) |
| 485 | |
| 486 | def getHumanReadableName(self, obj): |
| 487 | return obj.name |
| 488 | |
| 489 | class ServiceSpokeSerializer(PlusModelSerializer): |
| 490 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 491 | vnodlocalsite = VnodSiteSerializer(read_only=True) |
| 492 | vnodlocalport = RemotePortSerializer(read_only=True) |
| 493 | |
| 494 | class Meta: |
| 495 | model = ServiceSpoke |
| 496 | |
| 497 | fields = ('humanReadableName', |
| 498 | 'id', |
| 499 | 'name', |
| 500 | 'remotesubscriber', |
| 501 | 'remotevnodid', |
| 502 | 'autoattached', |
| 503 | 'operstate', |
| 504 | 'vnodlocalsite', |
| 505 | 'vnodlocalport' |
| 506 | ) |
| 507 | |
| 508 | def getHumanReadableName(self, obj): |
| 509 | return obj.name |
| 510 | |
| 511 | class VnodGlobalServiceSerializer(PlusModelSerializer): |
| 512 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 513 | metronetworkroottomultipoint = NetworkEdgeToMultipointConnectionSerializer(read_only=True) |
| 514 | metronetworkmultipoint = NetworkMultipointToMultipointConnectionSerializer(read_only=True) |
| 515 | metronetworkpointtopoint = NetworkEdgeToEdgePointConnectionSerializer(read_only=True) |
| 516 | spokes = ServiceSpokeSerializer(read_only=True, many=True) |
| 517 | bandwidthProfile = BandwidthProfileSerializer(read_only=True) |
| 518 | |
| 519 | class Meta: |
| 520 | model = VnodGlobalService |
| 521 | |
| 522 | fields = ('humanReadableName', |
| 523 | 'servicehandle', |
| 524 | 'vlanid', |
| 525 | 'id', |
| 526 | 'type', |
| 527 | 'operstate', |
| 528 | 'adminstate', |
| 529 | 'metronetworkroottomultipoint', |
| 530 | 'metronetworkmultipoint', |
| 531 | 'metronetworkpointtopoint', |
| 532 | 'spokes', |
| 533 | 'bandwidthProfile', |
| 534 | 'name' |
| 535 | ) |
| 536 | |
| 537 | def getHumanReadableName(self, obj): |
| 538 | return obj.name |
| 539 | |
| 540 | class VnodGlobalServiceViewSet(XOSViewSet): |
| 541 | base_name = "VNOD_GLOBAL_SERVICE" |
| 542 | method_name = "VNOD_GLOBAL_SERVICE" |
| 543 | method_kind = "viewset" |
| 544 | queryset = VnodGlobalService.get_service_objects().all() |
| 545 | serializer_class = VnodGlobalServiceSerializer |
| 546 | |
| 547 | @classmethod |
| 548 | def get_urlpatterns(self, api_path="^"): |
| 549 | patterns = super(VnodGlobalServiceViewSet, self).get_urlpatterns(api_path=api_path) |
| 550 | |
| 551 | return patterns |
| 552 | |
| 553 | def list(self, request): |
| 554 | |
| 555 | object_list = self.filter_queryset(self.get_queryset()) |
| 556 | |
| 557 | serializer = self.get_serializer(object_list, many=True) |
| 558 | |
| 559 | return Response(serializer.data) |
| 560 | |
| 561 | def destroy(self, request, pk=None): |
| 562 | VnodGlobalServiceToDelete = VnodGlobalService.objects.get(pk=pk) |
| 563 | |
| 564 | if (VnodGlobalServiceToDelete): |
| 565 | VnodGlobalServiceToDelete.adminstate = 'deactivationrequested' |
| 566 | VnodGlobalServiceToDelete.save() |
| 567 | else: |
| 568 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 569 | |
| 570 | return Response(status=status.HTTP_200_OK) |
| 571 | |
| 572 | def create(self, validated_data): |
| 573 | |
| 574 | vnodGlobalService = VnodGlobalService() |
| 575 | |
| 576 | vnodGlobalService.name = validated_data.data.get('name') |
| 577 | |
| 578 | if VnodGlobalService.objects.filter( |
| 579 | name=vnodGlobalService.name).exists(): |
| 580 | return HttpResponseBadRequest('Error: VnodGlobalService name \'' + vnodGlobalService.name |
| 581 | + '\' already exists.') |
| 582 | |
| 583 | vnodGlobalService.servicehandle = validated_data.data.get('servicehandle') |
| 584 | vnodGlobalService.adminstate = 'enabled' |
| 585 | vnodGlobalService.operstate = 'inactive' |
| 586 | vnodGlobalService.type = validated_data.data.get('type') |
| 587 | |
| 588 | vnodGlobalService.vlanid = self.getUniqueVlandId() |
| 589 | |
| 590 | bandwidth_profile = validated_data.data.get('bandwidthProfile') |
| 591 | bandwidthprofile = BandwidthProfile.objects.get(pk=bandwidth_profile['id']) |
| 592 | if (bandwidth_profile): |
| 593 | vnodGlobalService.bandwidthProfile = bandwidthprofile |
| 594 | |
| 595 | vnodGlobalService.save() |
| 596 | |
| 597 | spokes_list = validated_data.data.get('spokes') |
| 598 | if (spokes_list is None): |
| 599 | vnodGlobalService.delete() |
| 600 | return HttpResponseBadRequest('Error: No spokes found in request.') |
| 601 | |
| 602 | for spoke in spokes_list: |
| 603 | vnodlocalsite = Site.objects.get(pk=spoke['vnodlocalsite']['id']) |
| 604 | servicespoke = ServiceSpoke() |
| 605 | servicespoke.vnodlocalsite = vnodlocalsite |
| 606 | servicespoke.vnodlocalport = self.getRandomRemotePort(vnodlocalsite) |
| 607 | servicespoke.name = spoke['name'] |
| 608 | if (spoke.get('autoattached')): |
| 609 | servicespoke.autoattached = spoke['autoattached'] |
| 610 | servicespoke.operstate = 'inactive' |
| 611 | servicespoke.adminstate = 'disabled' |
| 612 | servicespoke.save() |
| 613 | vnodGlobalService.spokes.add(servicespoke) |
| 614 | |
| 615 | serializer = self.get_serializer(vnodGlobalService) |
| 616 | return Response(serializer.data) |
| 617 | |
| 618 | def getUniqueVlandId(self): |
| 619 | unique = False |
| 620 | while not unique: |
| 621 | vlanid = randint(1, 4095) |
| 622 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(vlanid=vlanid) |
| 623 | if (not vnodglobalservice): |
| 624 | unique = True |
| 625 | return vlanid |
| 626 | |
| 627 | def getRandomRemotePort(self, site): |
| 628 | remotePort = RemotePort.objects.get(remoteportsite__name=site.name) |
| 629 | if (remotePort): |
| 630 | return remotePort |
| 631 | return None |
| 632 | |
| 633 | @classmethod |
| 634 | def calculateVnodGlobalOperState(self, servicehandle): |
| 635 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 636 | if (not vnodglobalservice): |
| 637 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 638 | |
| 639 | vnodglobalservice = vnodglobalservice[0] |
| 640 | all_spokes_active_and_enabled = True |
| 641 | |
| 642 | for spoke in vnodglobalservice.spokes.all(): |
| 643 | if (spoke.operstate != 'active' or spoke.adminstate != 'enabled'): |
| 644 | all_spokes_active_and_enabled = False |
| 645 | break; |
| 646 | |
| 647 | if (all_spokes_active_and_enabled): |
| 648 | vnodglobalservice.operstate = 'active' |
| 649 | else: |
| 650 | vnodglobalservice.operstate = 'inactive' |
| 651 | |
| 652 | vnodglobalservice.save() |
| 653 | return all_spokes_active_and_enabled |
| 654 | |
| 655 | @classmethod |
| 656 | def createService(self, servicehandle): |
| 657 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 658 | if (not vnodglobalservice): |
| 659 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 660 | |
| 661 | vnodglobalservice = vnodglobalservice[0] |
| 662 | if (vnodglobalservice.type == 'eline'): |
| 663 | |
| 664 | spokes = vnodglobalservice.spokes.all() |
| 665 | uni1 = spokes[0].vnodlocalport.edgeport |
| 666 | uni2 = spokes[1].vnodlocalport.edgeport |
| 667 | name = 'ELine-' + str(vnodglobalservice.id) |
| 668 | type = 'Point_To_Point' |
| 669 | operstate = 'active' |
| 670 | adminstate = 'activationrequested' |
| 671 | |
| 672 | eline = NetworkEdgeToEdgePointConnection() |
| 673 | eline.name = name |
| 674 | eline.type = type |
| 675 | eline.operstate = operstate |
| 676 | eline.adminstate = adminstate |
| 677 | eline.vlanid = vnodglobalservice.vlanid |
| 678 | eline.sid = name |
| 679 | eline.uni1 = NetworkEdgePort.objects.get(pid=uni1.pid) |
| 680 | eline.uni2 = NetworkEdgePort.objects.get(pid=uni2.pid) |
| 681 | |
| 682 | eline.save() |
| 683 | vnodglobalservice.metronetworkpointtopoint = eline |
| 684 | vnodglobalservice.save() |
| 685 | |
| 686 | elif (vnodglobalservice.type == 'elan'): |
| 687 | |
| 688 | spokes = vnodglobalservice.spokes.all() |
| 689 | |
| 690 | name = 'ELAN-' + str(vnodglobalservice.id) |
| 691 | type = 'Multipoint_To_Multipoint' |
| 692 | operstate = 'active' |
| 693 | adminstate = 'activationrequested' |
| 694 | |
| 695 | elan = NetworkMultipointToMultipointConnection() |
| 696 | elan.name = name |
| 697 | elan.type = type |
| 698 | elan.operstate = 'inactive' |
| 699 | elan.adminstate = 'disabled' |
| 700 | elan.vlanid = vnodglobalservice.vlanid |
| 701 | elan.save() |
| 702 | |
| 703 | for spoke in spokes: |
| 704 | uni = NetworkEdgePort.objects.get(pid=spoke.vnodlocalport.edgeport.pid) |
| 705 | elan.eps.add(uni) |
| 706 | |
| 707 | elan.operstate = operstate |
| 708 | elan.adminstate = adminstate |
| 709 | elan.save() |
| 710 | |
| 711 | vnodglobalservice.metronetworkmultipoint = elan |
| 712 | vnodglobalservice.save() |
| 713 | |
| 714 | # TODO: elif (vnodglobalservice.type == 'etree'): |
| 715 | |
| 716 | class VnodGlobalServiceAutoAttachmentView(APIView): |
| 717 | method_kind = "list" |
| 718 | method_name = "vnodglobal_api_autoattach" |
| 719 | |
| 720 | def get(self, request, format=None): |
| 721 | params = request.query_params |
| 722 | sitename = params.get('sitename') |
| 723 | |
| 724 | if ( sitename is None): |
| 725 | HttpResponseBadRequest("Error: Request requires] 'sitename' as a query param.") |
| 726 | |
| 727 | vnodglobalservices = VnodGlobalService.get_service_objects().filter(spokes__autoattached=True, |
| 728 | spokes__operstate='inactive', |
| 729 | spokes__vnodlocalsite__name=sitename) |
| 730 | |
| 731 | if (not vnodglobalservices): |
| 732 | HttpResponseBadRequest({"handles" : []}) |
| 733 | |
| 734 | handles = [] |
| 735 | for vnodglobalservice in vnodglobalservices: |
| 736 | if (vnodglobalservice.adminstate != 'disabled'): |
| 737 | handles.append(vnodglobalservice.servicehandle) |
| 738 | |
| 739 | response_data = {'servicehandles' : handles} |
| 740 | return Response(response_data) |
| 741 | |
| 742 | class VnodGlobalServiceConfigurationView(APIView): |
| 743 | method_kind = "list" |
| 744 | method_name = "vnodglobal_api_configuration" |
| 745 | |
| 746 | def get(self, request, format=None): |
| 747 | params = request.query_params |
| 748 | servicehandle = params.get('servicehandle') |
| 749 | sitename = params.get('sitename') |
| 750 | |
| 751 | if (servicehandle is None or sitename is None): |
| 752 | HttpResponseBadRequest("Error: Request requires 'servicehandle' and 'sitename' as query params.") |
| 753 | |
| 754 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 755 | if (not vnodglobalservice): |
| 756 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 757 | |
| 758 | vnodglobalservice = vnodglobalservice[0] |
| 759 | response_data = {} |
| 760 | response_data['vlanid'] = vnodglobalservice.vlanid |
| 761 | |
| 762 | for spoke in vnodglobalservice.spokes.all(): |
| 763 | if (spoke.vnodlocalsite.name == sitename and spoke.adminstate != 'configured'): |
| 764 | response_data['port'] = {} |
| 765 | response_data['port']['name'] = spoke.vnodlocalport.name |
| 766 | break; |
| 767 | |
| 768 | return Response(response_data) |
| 769 | |
| 770 | class VnodGlobalServiceActivationView(APIView): |
| 771 | method_kind = "list" |
| 772 | method_name = "vnodglobal_api_activation" |
| 773 | |
| 774 | def post(self, request, format=None): |
| 775 | body_json = request.body |
| 776 | body = json.loads(body_json) |
| 777 | |
| 778 | servicehandle=body['servicehandle'] |
| 779 | sitename=body['sitename'] |
| 780 | activate = body['activate'] |
| 781 | vnodlocalid = body['vnodlocalid'] |
| 782 | portid = body.get('portid') |
| 783 | |
| 784 | if (activate == 'true' or activate == 'True'): |
| 785 | isActivate = True |
| 786 | else: |
| 787 | isActivate = False |
| 788 | |
| 789 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 790 | if (not vnodglobalservice): |
| 791 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 792 | |
| 793 | vnodglobalservice = vnodglobalservice[0] |
| 794 | |
| 795 | for spoke in vnodglobalservice.spokes.all(): |
| 796 | if (spoke.vnodlocalsite.name == sitename and spoke.vnodlocalport.name == portid): |
| 797 | spoke_id = spoke.id |
| 798 | servicespoke = ServiceSpoke.objects.get(id=spoke_id) |
| 799 | servicespoke.remotevnodid = vnodlocalid |
| 800 | servicespoke.save() |
| 801 | break; |
| 802 | |
| 803 | return Response() |
| 804 | |
| 805 | class VnodGlobalServiceAdminOperationalStateView(APIView): |
| 806 | method_kind = "list" |
| 807 | method_name = "vnodglobal_api_status" |
| 808 | |
| 809 | def post(self, request, format=None): |
| 810 | body_json = request.body |
| 811 | body = json.loads(body_json) |
| 812 | |
| 813 | servicehandle = body['servicehandle'] |
| 814 | sitename = body['sitename'] |
| 815 | operstate = body.get('operstate') |
| 816 | adminstate = body.get('adminstate') |
| 817 | portid = body.get('portid') |
| 818 | |
| 819 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 820 | if (not vnodglobalservice): |
| 821 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 822 | |
| 823 | vnodglobalservice = vnodglobalservice[0] |
| 824 | |
| 825 | for spoke in vnodglobalservice.spokes.all(): |
| 826 | if (spoke.vnodlocalsite.name == sitename and spoke.vnodlocalport.name == portid): |
| 827 | spoke_id = spoke.id |
| 828 | servicespoke = ServiceSpoke.objects.get(id=spoke_id) |
| 829 | if (operstate): |
| 830 | servicespoke.operstate = operstate |
| 831 | if (adminstate): |
| 832 | servicespoke.adminstate = adminstate |
| 833 | servicespoke.save() |
| 834 | break; |
| 835 | |
| 836 | isGlobalActive = VnodGlobalServiceViewSet.calculateVnodGlobalOperState(servicehandle=servicehandle) |
| 837 | |
| 838 | if (vnodglobalservice.metronetworkmultipoint is None and vnodglobalservice.metronetworkpointtopoint is None and |
| 839 | vnodglobalservice.metronetworkroottomultipoint is None and isGlobalActive): |
| 840 | VnodGlobalServiceViewSet.createService(servicehandle=servicehandle) |
| 841 | |
| 842 | return Response() |