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