Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 15 | from xos.exceptions import XOSValidationError |
| 16 | |
| 17 | from models_decl import ProgranService_decl |
| 18 | from models_decl import ENodeB_decl |
| 19 | from models_decl import Handover_decl |
| 20 | from models_decl import ProgranServiceInstance_decl |
| 21 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 22 | class ProgranService(ProgranService_decl): |
| 23 | class Meta: |
| 24 | proxy = True |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 25 | def save(self, *args, **kwargs): |
| 26 | |
| 27 | existing_services = ProgranService.objects.all() |
| 28 | |
| 29 | if len(existing_services) > 0 and not self.delete: |
| 30 | raise XOSValidationError("A ProgranService already exists, you should not have more than one") |
| 31 | |
| 32 | super(ProgranService, self).save(*args, **kwargs) |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 33 | |
| 34 | |
| 35 | class ENodeB(ENodeB_decl): |
| 36 | class Meta: |
| 37 | proxy = True |
| 38 | |
Matteo Scandolo | cfa9e8f | 2018-01-31 18:29:06 -0800 | [diff] [blame] | 39 | def save(self, *args, **kwargs): |
| 40 | |
| 41 | # remove all the profiles related to this enodeb (clearing the relation, not the models) |
| 42 | if self.deleted: |
| 43 | self.profiles.clear() |
| 44 | |
| 45 | # prevent enbId duplicates |
| 46 | try: |
| 47 | instance_with_same_id = ENodeB.objects.get(enbId=self.enbId) |
| 48 | |
| 49 | if (not self.pk and instance_with_same_id) or (self.pk and self.pk != instance_with_same_id.pk): |
| 50 | raise XOSValidationError("A ENodeB with enbId '%s' already exists" % self.enbId) |
| 51 | except self.DoesNotExist: |
| 52 | pass |
| 53 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 54 | if self.is_new and not self.created_by: |
| 55 | # NOTE if created_by is null it has been created by XOS |
| 56 | self.created_by = "XOS" |
| 57 | |
Matteo Scandolo | cfa9e8f | 2018-01-31 18:29:06 -0800 | [diff] [blame] | 58 | super(ENodeB, self).save(*args, **kwargs) |
Matteo Scandolo | 6b607c8 | 2018-01-30 09:12:26 -0800 | [diff] [blame] | 59 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 60 | |
| 61 | class Handover(Handover_decl): |
| 62 | class Meta: |
| 63 | proxy = True |
| 64 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 65 | def save(self, *args, **kwargs): |
| 66 | if self.is_new and not self.created_by: |
| 67 | # NOTE if created_by is null it has been created by XOS |
| 68 | self.created_by = "XOS" |
| 69 | super(Handover, self).save(*args, **kwargs) |
| 70 | |
| 71 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 72 | |
| 73 | class ProgranServiceInstance(ProgranServiceInstance_decl): |
| 74 | class Meta: |
| 75 | proxy = True |
| 76 | |
| 77 | def save(self, *args, **kwargs): |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame] | 78 | |
| 79 | # TODO we should not allow name changes as name is the mapping with the backend |
| 80 | |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame] | 81 | # name is mandatory |
| 82 | if not self.name: |
| 83 | raise XOSValidationError("name is mandatory for ProgranServiceInstances") |
| 84 | |
| 85 | # NOTE this check is disabled as when Progran create a profile it fails |
| 86 | # if self.DlUeAllocRbRate > self.DlAllocRBRate: |
| 87 | # raise XOSValidationError("DlUeAllocRbRate (%s) cannot be bigger than DlAllocRBRate (%s)" % (self.DlUeAllocRbRate, self.DlAllocRBRate)) |
| 88 | |
Matteo Scandolo | e463b06 | 2018-02-01 10:14:03 -0800 | [diff] [blame] | 89 | # prevent name duplicates |
| 90 | try: |
| 91 | instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name) |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 92 | |
Matteo Scandolo | e463b06 | 2018-02-01 10:14:03 -0800 | [diff] [blame] | 93 | if (not self.pk and instances_with_same_name) or (self.pk and self.pk != instances_with_same_name.pk): |
| 94 | raise XOSValidationError("A ProgranServiceInstance with name '%s' already exists" % self.name) |
| 95 | except self.DoesNotExist: |
| 96 | pass |
Matteo Scandolo | d52da97 | 2018-01-31 14:37:43 -0800 | [diff] [blame] | 97 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 98 | if self.is_new and not self.created_by: |
| 99 | # NOTE if created_by is null it has been created by XOS |
| 100 | self.created_by = "XOS" |
| 101 | |
| 102 | |
Matteo Scandolo | e624d34 | 2018-02-01 15:51:57 -0800 | [diff] [blame] | 103 | # check that the sum of upload and download rate for a single enodeb is not greater than 95 |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 104 | if not self.deleted: |
| 105 | limit = 95 |
| 106 | same_enodeb = ProgranServiceInstance.objects.filter(enodeb_id=self.enodeb_id) |
Matteo Scandolo | e624d34 | 2018-02-01 15:51:57 -0800 | [diff] [blame] | 107 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 108 | total_up = self.UlAllocRBRate |
| 109 | total_down = self.DlAllocRBRate |
Matteo Scandolo | e624d34 | 2018-02-01 15:51:57 -0800 | [diff] [blame] | 110 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 111 | for p in same_enodeb: |
| 112 | if p.pk != self.pk: |
| 113 | total_up = total_up + p.UlAllocRBRate |
| 114 | total_down = total_down + p.DlAllocRBRate |
Matteo Scandolo | e624d34 | 2018-02-01 15:51:57 -0800 | [diff] [blame] | 115 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 116 | if total_up > limit: |
| 117 | raise XOSValidationError("UlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit) |
Matteo Scandolo | e624d34 | 2018-02-01 15:51:57 -0800 | [diff] [blame] | 118 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 119 | if total_down > limit: |
| 120 | raise XOSValidationError("DlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit) |
Matteo Scandolo | e624d34 | 2018-02-01 15:51:57 -0800 | [diff] [blame] | 121 | |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame] | 122 | caller_kind = "xos" |
| 123 | |
| 124 | if "caller_kind" in kwargs: |
| 125 | caller_kind = kwargs.pop("caller_kind") |
| 126 | |
| 127 | print "Profile %s has been saved by %s" % (self.name, caller_kind) |
| 128 | |
| 129 | if caller_kind == "xos": |
| 130 | print "Setting no_sync to false for profile %s" % self.name |
| 131 | self.no_sync = False |
| 132 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 133 | super(ProgranServiceInstance, self).save(*args, **kwargs) |
| 134 | |
| 135 | |