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 | |
Andrea Campanella | 1123755 | 2017-03-21 17:21:20 +0100 | [diff] [blame^] | 400 | |
Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 401 | fields = ('humanReadableName', |
| 402 | 'id', |
| 403 | 'bwpcfgcbs', |
| 404 | 'bwpcfgebs', |
| 405 | 'bwpcfgcir', |
| 406 | 'bwpcfgeir', |
| 407 | 'name' |
| 408 | ) |
| 409 | |
| 410 | def getHumanReadableName(self, obj): |
| 411 | return obj.name |
| 412 | |
| 413 | class BandwidthProfileViewSet(XOSViewSet): |
| 414 | base_name = "BANDWIDTH_PROFILE" |
| 415 | method_name = "BANDWIDTH_PROFILE" |
| 416 | method_kind = "viewset" |
| 417 | queryset = BandwidthProfile.objects.all() |
| 418 | serializer_class = BandwidthProfileSerializer |
| 419 | |
| 420 | @classmethod |
| 421 | def get_urlpatterns(self, api_path="^"): |
| 422 | patterns = super(BandwidthProfileViewSet, self).get_urlpatterns(api_path=api_path) |
| 423 | |
| 424 | return patterns |
| 425 | |
| 426 | def list(self, request): |
| 427 | |
| 428 | object_list = self.filter_queryset(self.get_queryset()) |
| 429 | |
| 430 | serializer = self.get_serializer(object_list, many=True) |
| 431 | |
| 432 | return Response(serializer.data) |
| 433 | |
| 434 | def create(self, validated_data): |
| 435 | |
| 436 | bandwidthProfile = BandwidthProfile() |
| 437 | bandwidthProfile.name = validated_data.data.get('name') |
| 438 | bandwidthProfile.bwpcfgcbs = validated_data.data.get('bwpcfgcbs') |
| 439 | bandwidthProfile.bwpcfgebs = validated_data.data.get('bwpcfgebs') |
| 440 | bandwidthProfile.bwpcfgcir = validated_data.data.get('bwpcfgcir') |
| 441 | bandwidthProfile.bwpcfgeir = validated_data.data.get('bwpcfgeir') |
| 442 | |
| 443 | bandwidthProfile.save() |
| 444 | |
| 445 | response_data = {} |
| 446 | response_data['name'] = bandwidthProfile.name |
| 447 | response_data['bwpcfgcbs'] = bandwidthProfile.bwpcfgcbs |
| 448 | response_data['bwpcfgebs'] = bandwidthProfile.bwpcfgebs |
| 449 | response_data['bwpcfgcir'] = bandwidthProfile.bwpcfgeir |
| 450 | response_data['bwpcfgcir'] = bandwidthProfile.bwpcfgcir |
| 451 | response_data['id'] = bandwidthProfile.id |
| 452 | |
| 453 | return Response(response_data) |
| 454 | |
Andrea Campanella | 1123755 | 2017-03-21 17:21:20 +0100 | [diff] [blame^] | 455 | class UserNetworkInterfaceSerializer(PlusModelSerializer): |
| 456 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 457 | |
| 458 | class Meta: |
| 459 | model = UserNetworkInterface |
| 460 | |
| 461 | fields = ('humanReadableName', |
| 462 | 'id', |
| 463 | 'bwpcfgcbs', |
| 464 | 'bwpcfgebs', |
| 465 | 'bwpcfgcir', |
| 466 | 'bwpcfgeir', |
| 467 | 'name' |
| 468 | ) |
| 469 | |
| 470 | def getHumanReadableName(self, obj): |
| 471 | return obj.name |
| 472 | |
| 473 | class UserNetworkInterfaceViewSet(XOSViewSet): |
| 474 | base_name = "USER_NETWORK_INTERFACE" |
| 475 | method_name = "USER_NETWORK_INTERFACE" |
| 476 | method_kind = "viewset" |
| 477 | queryset = UserNetworkInterface.objects.all() |
| 478 | serializer_class = UserNetworkInterfaceSerializer |
| 479 | |
| 480 | @classmethod |
| 481 | def get_urlpatterns(self, api_path="^"): |
| 482 | patterns = super(UserNetworkInterfaceViewSet, self).get_urlpatterns(api_path=api_path) |
| 483 | |
| 484 | return patterns |
| 485 | |
| 486 | def list(self, request): |
| 487 | |
| 488 | object_list = self.filter_queryset(self.get_queryset()) |
| 489 | |
| 490 | serializer = self.get_serializer(object_list, many=True) |
| 491 | |
| 492 | return Response(serializer.data) |
| 493 | |
| 494 | def create(self, validated_data): |
| 495 | |
| 496 | userNetworkInterface = UserNetworkInterface() |
| 497 | |
| 498 | userNetworkInterface.name = validated_data.data.get('name') |
| 499 | userNetworkInterface.capacity = validated_data.data.get('capacity') |
| 500 | userNetworkInterface.bw_used = validated_data.data.get('bw_used') |
| 501 | userNetworkInterface.vlanIds = validated_data.data.get('vlanIds') |
| 502 | userNetworkInterface.location = validated_data.data.get('location') |
| 503 | userNetworkInterface.latlng = validated_data.data.get('latlng') |
| 504 | |
| 505 | copyin_props = ['enabled', 'capacity', 'bw_used', 'vlanIds', 'location', 'latlng', 'name'] |
| 506 | |
| 507 | userNetworkInterface.save() |
| 508 | |
| 509 | response_data = {} |
| 510 | response_data['name'] = userNetworkInterface.name |
| 511 | response_data['capacity'] = userNetworkInterface.capacity |
| 512 | response_data['bw_used'] = userNetworkInterface.bw_used |
| 513 | response_data['vlanIds'] = userNetworkInterface.vlanIds |
| 514 | response_data['location'] = userNetworkInterface.location |
| 515 | response_data['latlng'] = userNetworkInterface.latlng |
| 516 | response_data['id'] = userNetworkInterface.id |
| 517 | |
| 518 | return Response(response_data) |
| 519 | |
Rizwan Haider | e6ffdc0 | 2016-11-08 13:43:48 -0500 | [diff] [blame] | 520 | class VnodSiteSerializer(PlusModelSerializer): |
| 521 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 522 | |
| 523 | class Meta: |
| 524 | model = Site |
| 525 | |
| 526 | fields = ('humanReadableName', |
| 527 | 'site_url', |
| 528 | 'enabled', |
| 529 | 'longitude', |
| 530 | 'latitude', |
| 531 | 'name' |
| 532 | ) |
| 533 | |
| 534 | def getHumanReadableName(self, obj): |
| 535 | return obj.name |
| 536 | |
| 537 | class RemotePortSerializer(PlusModelSerializer): |
| 538 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 539 | edgeport = NetworkEdgePortSerializer(read_only=True) |
| 540 | remoteportsite = VnodSiteSerializer(read_only=True) |
| 541 | |
| 542 | class Meta: |
| 543 | model = RemotePort |
| 544 | |
| 545 | fields = ('humanReadableName', |
| 546 | 'name', |
| 547 | 'edgeport', |
| 548 | 'id', |
| 549 | 'remoteportsite' |
| 550 | ) |
| 551 | |
| 552 | def getHumanReadableName(self, obj): |
| 553 | return obj.name |
| 554 | |
| 555 | class ServiceSpokeSerializer(PlusModelSerializer): |
| 556 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 557 | vnodlocalsite = VnodSiteSerializer(read_only=True) |
| 558 | vnodlocalport = RemotePortSerializer(read_only=True) |
| 559 | |
| 560 | class Meta: |
| 561 | model = ServiceSpoke |
| 562 | |
| 563 | fields = ('humanReadableName', |
| 564 | 'id', |
| 565 | 'name', |
| 566 | 'remotesubscriber', |
| 567 | 'remotevnodid', |
| 568 | 'autoattached', |
| 569 | 'operstate', |
| 570 | 'vnodlocalsite', |
| 571 | 'vnodlocalport' |
| 572 | ) |
| 573 | |
| 574 | def getHumanReadableName(self, obj): |
| 575 | return obj.name |
| 576 | |
| 577 | class VnodGlobalServiceSerializer(PlusModelSerializer): |
| 578 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
| 579 | metronetworkroottomultipoint = NetworkEdgeToMultipointConnectionSerializer(read_only=True) |
| 580 | metronetworkmultipoint = NetworkMultipointToMultipointConnectionSerializer(read_only=True) |
| 581 | metronetworkpointtopoint = NetworkEdgeToEdgePointConnectionSerializer(read_only=True) |
| 582 | spokes = ServiceSpokeSerializer(read_only=True, many=True) |
| 583 | bandwidthProfile = BandwidthProfileSerializer(read_only=True) |
| 584 | |
| 585 | class Meta: |
| 586 | model = VnodGlobalService |
| 587 | |
| 588 | fields = ('humanReadableName', |
| 589 | 'servicehandle', |
| 590 | 'vlanid', |
| 591 | 'id', |
| 592 | 'type', |
| 593 | 'operstate', |
| 594 | 'adminstate', |
| 595 | 'metronetworkroottomultipoint', |
| 596 | 'metronetworkmultipoint', |
| 597 | 'metronetworkpointtopoint', |
| 598 | 'spokes', |
| 599 | 'bandwidthProfile', |
| 600 | 'name' |
| 601 | ) |
| 602 | |
| 603 | def getHumanReadableName(self, obj): |
| 604 | return obj.name |
| 605 | |
| 606 | class VnodGlobalServiceViewSet(XOSViewSet): |
| 607 | base_name = "VNOD_GLOBAL_SERVICE" |
| 608 | method_name = "VNOD_GLOBAL_SERVICE" |
| 609 | method_kind = "viewset" |
| 610 | queryset = VnodGlobalService.get_service_objects().all() |
| 611 | serializer_class = VnodGlobalServiceSerializer |
| 612 | |
| 613 | @classmethod |
| 614 | def get_urlpatterns(self, api_path="^"): |
| 615 | patterns = super(VnodGlobalServiceViewSet, self).get_urlpatterns(api_path=api_path) |
| 616 | |
| 617 | return patterns |
| 618 | |
| 619 | def list(self, request): |
| 620 | |
| 621 | object_list = self.filter_queryset(self.get_queryset()) |
| 622 | |
| 623 | serializer = self.get_serializer(object_list, many=True) |
| 624 | |
| 625 | return Response(serializer.data) |
| 626 | |
| 627 | def destroy(self, request, pk=None): |
| 628 | VnodGlobalServiceToDelete = VnodGlobalService.objects.get(pk=pk) |
| 629 | |
| 630 | if (VnodGlobalServiceToDelete): |
| 631 | VnodGlobalServiceToDelete.adminstate = 'deactivationrequested' |
| 632 | VnodGlobalServiceToDelete.save() |
| 633 | else: |
| 634 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 635 | |
| 636 | return Response(status=status.HTTP_200_OK) |
| 637 | |
| 638 | def create(self, validated_data): |
| 639 | |
| 640 | vnodGlobalService = VnodGlobalService() |
| 641 | |
| 642 | vnodGlobalService.name = validated_data.data.get('name') |
| 643 | |
| 644 | if VnodGlobalService.objects.filter( |
| 645 | name=vnodGlobalService.name).exists(): |
| 646 | return HttpResponseBadRequest('Error: VnodGlobalService name \'' + vnodGlobalService.name |
| 647 | + '\' already exists.') |
| 648 | |
| 649 | vnodGlobalService.servicehandle = validated_data.data.get('servicehandle') |
| 650 | vnodGlobalService.adminstate = 'enabled' |
| 651 | vnodGlobalService.operstate = 'inactive' |
| 652 | vnodGlobalService.type = validated_data.data.get('type') |
| 653 | |
| 654 | vnodGlobalService.vlanid = self.getUniqueVlandId() |
| 655 | |
| 656 | bandwidth_profile = validated_data.data.get('bandwidthProfile') |
| 657 | bandwidthprofile = BandwidthProfile.objects.get(pk=bandwidth_profile['id']) |
| 658 | if (bandwidth_profile): |
| 659 | vnodGlobalService.bandwidthProfile = bandwidthprofile |
| 660 | |
| 661 | vnodGlobalService.save() |
| 662 | |
| 663 | spokes_list = validated_data.data.get('spokes') |
| 664 | if (spokes_list is None): |
| 665 | vnodGlobalService.delete() |
| 666 | return HttpResponseBadRequest('Error: No spokes found in request.') |
| 667 | |
| 668 | for spoke in spokes_list: |
| 669 | vnodlocalsite = Site.objects.get(pk=spoke['vnodlocalsite']['id']) |
| 670 | servicespoke = ServiceSpoke() |
| 671 | servicespoke.vnodlocalsite = vnodlocalsite |
| 672 | servicespoke.vnodlocalport = self.getRandomRemotePort(vnodlocalsite) |
| 673 | servicespoke.name = spoke['name'] |
| 674 | if (spoke.get('autoattached')): |
| 675 | servicespoke.autoattached = spoke['autoattached'] |
| 676 | servicespoke.operstate = 'inactive' |
| 677 | servicespoke.adminstate = 'disabled' |
| 678 | servicespoke.save() |
| 679 | vnodGlobalService.spokes.add(servicespoke) |
| 680 | |
| 681 | serializer = self.get_serializer(vnodGlobalService) |
| 682 | return Response(serializer.data) |
| 683 | |
| 684 | def getUniqueVlandId(self): |
| 685 | unique = False |
| 686 | while not unique: |
| 687 | vlanid = randint(1, 4095) |
| 688 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(vlanid=vlanid) |
| 689 | if (not vnodglobalservice): |
| 690 | unique = True |
| 691 | return vlanid |
| 692 | |
| 693 | def getRandomRemotePort(self, site): |
| 694 | remotePort = RemotePort.objects.get(remoteportsite__name=site.name) |
| 695 | if (remotePort): |
| 696 | return remotePort |
| 697 | return None |
| 698 | |
| 699 | @classmethod |
| 700 | def calculateVnodGlobalOperState(self, servicehandle): |
| 701 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 702 | if (not vnodglobalservice): |
| 703 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 704 | |
| 705 | vnodglobalservice = vnodglobalservice[0] |
| 706 | all_spokes_active_and_enabled = True |
| 707 | |
| 708 | for spoke in vnodglobalservice.spokes.all(): |
| 709 | if (spoke.operstate != 'active' or spoke.adminstate != 'enabled'): |
| 710 | all_spokes_active_and_enabled = False |
| 711 | break; |
| 712 | |
| 713 | if (all_spokes_active_and_enabled): |
| 714 | vnodglobalservice.operstate = 'active' |
| 715 | else: |
| 716 | vnodglobalservice.operstate = 'inactive' |
| 717 | |
| 718 | vnodglobalservice.save() |
| 719 | return all_spokes_active_and_enabled |
| 720 | |
| 721 | @classmethod |
| 722 | def createService(self, servicehandle): |
| 723 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 724 | if (not vnodglobalservice): |
| 725 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 726 | |
| 727 | vnodglobalservice = vnodglobalservice[0] |
| 728 | if (vnodglobalservice.type == 'eline'): |
| 729 | |
| 730 | spokes = vnodglobalservice.spokes.all() |
| 731 | uni1 = spokes[0].vnodlocalport.edgeport |
| 732 | uni2 = spokes[1].vnodlocalport.edgeport |
| 733 | name = 'ELine-' + str(vnodglobalservice.id) |
| 734 | type = 'Point_To_Point' |
| 735 | operstate = 'active' |
| 736 | adminstate = 'activationrequested' |
| 737 | |
| 738 | eline = NetworkEdgeToEdgePointConnection() |
| 739 | eline.name = name |
| 740 | eline.type = type |
| 741 | eline.operstate = operstate |
| 742 | eline.adminstate = adminstate |
| 743 | eline.vlanid = vnodglobalservice.vlanid |
| 744 | eline.sid = name |
| 745 | eline.uni1 = NetworkEdgePort.objects.get(pid=uni1.pid) |
| 746 | eline.uni2 = NetworkEdgePort.objects.get(pid=uni2.pid) |
| 747 | |
| 748 | eline.save() |
| 749 | vnodglobalservice.metronetworkpointtopoint = eline |
| 750 | vnodglobalservice.save() |
| 751 | |
| 752 | elif (vnodglobalservice.type == 'elan'): |
| 753 | |
| 754 | spokes = vnodglobalservice.spokes.all() |
| 755 | |
| 756 | name = 'ELAN-' + str(vnodglobalservice.id) |
| 757 | type = 'Multipoint_To_Multipoint' |
| 758 | operstate = 'active' |
| 759 | adminstate = 'activationrequested' |
| 760 | |
| 761 | elan = NetworkMultipointToMultipointConnection() |
| 762 | elan.name = name |
| 763 | elan.type = type |
| 764 | elan.operstate = 'inactive' |
| 765 | elan.adminstate = 'disabled' |
| 766 | elan.vlanid = vnodglobalservice.vlanid |
| 767 | elan.save() |
| 768 | |
| 769 | for spoke in spokes: |
| 770 | uni = NetworkEdgePort.objects.get(pid=spoke.vnodlocalport.edgeport.pid) |
| 771 | elan.eps.add(uni) |
| 772 | |
| 773 | elan.operstate = operstate |
| 774 | elan.adminstate = adminstate |
| 775 | elan.save() |
| 776 | |
| 777 | vnodglobalservice.metronetworkmultipoint = elan |
| 778 | vnodglobalservice.save() |
| 779 | |
| 780 | # TODO: elif (vnodglobalservice.type == 'etree'): |
| 781 | |
| 782 | class VnodGlobalServiceAutoAttachmentView(APIView): |
| 783 | method_kind = "list" |
| 784 | method_name = "vnodglobal_api_autoattach" |
| 785 | |
| 786 | def get(self, request, format=None): |
| 787 | params = request.query_params |
| 788 | sitename = params.get('sitename') |
| 789 | |
| 790 | if ( sitename is None): |
| 791 | HttpResponseBadRequest("Error: Request requires] 'sitename' as a query param.") |
| 792 | |
| 793 | vnodglobalservices = VnodGlobalService.get_service_objects().filter(spokes__autoattached=True, |
| 794 | spokes__operstate='inactive', |
| 795 | spokes__vnodlocalsite__name=sitename) |
| 796 | |
| 797 | if (not vnodglobalservices): |
| 798 | HttpResponseBadRequest({"handles" : []}) |
| 799 | |
| 800 | handles = [] |
| 801 | for vnodglobalservice in vnodglobalservices: |
| 802 | if (vnodglobalservice.adminstate != 'disabled'): |
| 803 | handles.append(vnodglobalservice.servicehandle) |
| 804 | |
| 805 | response_data = {'servicehandles' : handles} |
| 806 | return Response(response_data) |
| 807 | |
| 808 | class VnodGlobalServiceConfigurationView(APIView): |
| 809 | method_kind = "list" |
| 810 | method_name = "vnodglobal_api_configuration" |
| 811 | |
| 812 | def get(self, request, format=None): |
| 813 | params = request.query_params |
| 814 | servicehandle = params.get('servicehandle') |
| 815 | sitename = params.get('sitename') |
| 816 | |
| 817 | if (servicehandle is None or sitename is None): |
| 818 | HttpResponseBadRequest("Error: Request requires 'servicehandle' and 'sitename' as query params.") |
| 819 | |
| 820 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 821 | if (not vnodglobalservice): |
| 822 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 823 | |
| 824 | vnodglobalservice = vnodglobalservice[0] |
| 825 | response_data = {} |
| 826 | response_data['vlanid'] = vnodglobalservice.vlanid |
| 827 | |
| 828 | for spoke in vnodglobalservice.spokes.all(): |
| 829 | if (spoke.vnodlocalsite.name == sitename and spoke.adminstate != 'configured'): |
| 830 | response_data['port'] = {} |
| 831 | response_data['port']['name'] = spoke.vnodlocalport.name |
| 832 | break; |
| 833 | |
| 834 | return Response(response_data) |
| 835 | |
| 836 | class VnodGlobalServiceActivationView(APIView): |
| 837 | method_kind = "list" |
| 838 | method_name = "vnodglobal_api_activation" |
| 839 | |
| 840 | def post(self, request, format=None): |
| 841 | body_json = request.body |
| 842 | body = json.loads(body_json) |
| 843 | |
| 844 | servicehandle=body['servicehandle'] |
| 845 | sitename=body['sitename'] |
| 846 | activate = body['activate'] |
| 847 | vnodlocalid = body['vnodlocalid'] |
| 848 | portid = body.get('portid') |
| 849 | |
| 850 | if (activate == 'true' or activate == 'True'): |
| 851 | isActivate = True |
| 852 | else: |
| 853 | isActivate = False |
| 854 | |
| 855 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 856 | if (not vnodglobalservice): |
| 857 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 858 | |
| 859 | vnodglobalservice = vnodglobalservice[0] |
| 860 | |
| 861 | for spoke in vnodglobalservice.spokes.all(): |
| 862 | if (spoke.vnodlocalsite.name == sitename and spoke.vnodlocalport.name == portid): |
| 863 | spoke_id = spoke.id |
| 864 | servicespoke = ServiceSpoke.objects.get(id=spoke_id) |
| 865 | servicespoke.remotevnodid = vnodlocalid |
| 866 | servicespoke.save() |
| 867 | break; |
| 868 | |
| 869 | return Response() |
| 870 | |
| 871 | class VnodGlobalServiceAdminOperationalStateView(APIView): |
| 872 | method_kind = "list" |
| 873 | method_name = "vnodglobal_api_status" |
| 874 | |
| 875 | def post(self, request, format=None): |
| 876 | body_json = request.body |
| 877 | body = json.loads(body_json) |
| 878 | |
| 879 | servicehandle = body['servicehandle'] |
| 880 | sitename = body['sitename'] |
| 881 | operstate = body.get('operstate') |
| 882 | adminstate = body.get('adminstate') |
| 883 | portid = body.get('portid') |
| 884 | |
| 885 | vnodglobalservice = VnodGlobalService.get_service_objects().filter(servicehandle=servicehandle) |
| 886 | if (not vnodglobalservice): |
| 887 | HttpResponseBadRequest('Error: Could not find VnodGlobalObject with servicehandle=' + servicehandle) |
| 888 | |
| 889 | vnodglobalservice = vnodglobalservice[0] |
| 890 | |
| 891 | for spoke in vnodglobalservice.spokes.all(): |
| 892 | if (spoke.vnodlocalsite.name == sitename and spoke.vnodlocalport.name == portid): |
| 893 | spoke_id = spoke.id |
| 894 | servicespoke = ServiceSpoke.objects.get(id=spoke_id) |
| 895 | if (operstate): |
| 896 | servicespoke.operstate = operstate |
| 897 | if (adminstate): |
| 898 | servicespoke.adminstate = adminstate |
| 899 | servicespoke.save() |
| 900 | break; |
| 901 | |
| 902 | isGlobalActive = VnodGlobalServiceViewSet.calculateVnodGlobalOperState(servicehandle=servicehandle) |
| 903 | |
| 904 | if (vnodglobalservice.metronetworkmultipoint is None and vnodglobalservice.metronetworkpointtopoint is None and |
| 905 | vnodglobalservice.metronetworkroottomultipoint is None and isGlobalActive): |
| 906 | VnodGlobalServiceViewSet.createService(servicehandle=servicehandle) |
| 907 | |
| 908 | return Response() |