blob: fef3fb1ac0e63873f9b71601aaaaaa109ee7dff4 [file] [log] [blame]
# Copyright 2017-present Open Networking Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from xos.exceptions import XOSValidationError
from models_decl import ProgranService_decl
from models_decl import ENodeB_decl
from models_decl import Handover_decl
from models_decl import ProgranServiceInstance_decl
class ProgranService(ProgranService_decl):
class Meta:
proxy = True
def save(self, *args, **kwargs):
existing_services = ProgranService.objects.all()
if len(existing_services) > 0 and not self.delete:
raise XOSValidationError("A ProgranService already exists, you should not have more than one")
super(ProgranService, self).save(*args, **kwargs)
class ENodeB(ENodeB_decl):
class Meta:
proxy = True
def save(self, *args, **kwargs):
# remove all the profiles related to this enodeb (clearing the relation, not the models)
if self.deleted:
self.profiles.clear()
# prevent enbId duplicates
try:
instance_with_same_id = ENodeB.objects.get(enbId=self.enbId)
if (not self.pk and instance_with_same_id) or (self.pk and self.pk != instance_with_same_id.pk):
raise XOSValidationError("A ENodeB with enbId '%s' already exists" % self.enbId)
except self.DoesNotExist:
pass
if self.is_new and not self.created_by:
# NOTE if created_by is null it has been created by XOS
self.created_by = "XOS"
super(ENodeB, self).save(*args, **kwargs)
class Handover(Handover_decl):
class Meta:
proxy = True
def save(self, *args, **kwargs):
if self.is_new and not self.created_by:
# NOTE if created_by is null it has been created by XOS
self.created_by = "XOS"
super(Handover, self).save(*args, **kwargs)
class ProgranServiceInstance(ProgranServiceInstance_decl):
class Meta:
proxy = True
def save(self, *args, **kwargs):
# TODO we should not allow name changes as name is the mapping with the backend
# NOTE someone is setting owner_id, so just override it for now
try:
# NOTE we allow just one ProgranService
progran_service = ProgranService.objects.all()[0]
self.owner_id = progran_service.id
except IndexError:
raise XOSValidationError("Service Progran cannot be found, please make sure that the model exists.")
# name is mandatory
if not self.name:
raise XOSValidationError("name is mandatory for ProgranServiceInstances")
# NOTE this check is disabled as when Progran create a profile it fails
# if self.DlUeAllocRbRate > self.DlAllocRBRate:
# raise XOSValidationError("DlUeAllocRbRate (%s) cannot be bigger than DlAllocRBRate (%s)" % (self.DlUeAllocRbRate, self.DlAllocRBRate))
# prevent name duplicates
try:
instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name)
if (not self.pk and instances_with_same_name) or (self.pk and self.pk != instances_with_same_name.pk):
raise XOSValidationError("A ProgranServiceInstance with name '%s' already exists" % self.name)
except self.DoesNotExist:
pass
if self.is_new and not self.created_by:
# NOTE if created_by is null it has been created by XOS
self.created_by = "XOS"
# check that the sum of upload and download rate for a single enodeb is not greater than 95
if not self.deleted:
limit = 95
same_enodeb = ProgranServiceInstance.objects.filter(enodeb_id=self.enodeb_id)
total_up = self.UlAllocRBRate
total_down = self.DlAllocRBRate
for p in same_enodeb:
if p.pk != self.pk:
total_up = total_up + p.UlAllocRBRate
total_down = total_down + p.DlAllocRBRate
if total_up > limit:
raise XOSValidationError("UlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
if total_down > limit:
raise XOSValidationError("DlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
caller_kind = "xos"
if "caller_kind" in kwargs:
caller_kind = kwargs.pop("caller_kind")
print "Profile %s has been saved by %s" % (self.name, caller_kind)
if caller_kind == "xos":
print "Setting no_sync to false for profile %s" % self.name
self.no_sync = False
super(ProgranServiceInstance, self).save(*args, **kwargs)