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 | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 81 | # NOTE someone is setting owner_id, so just override it for now |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 82 | try: |
Matteo Scandolo | 0a207b5 | 2018-01-29 13:39:43 -0800 | [diff] [blame] | 83 | # NOTE we allow just one ProgranService |
| 84 | progran_service = ProgranService.objects.all()[0] |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 85 | self.owner_id = progran_service.id |
| 86 | except IndexError: |
| 87 | raise XOSValidationError("Service Progran cannot be found, please make sure that the model exists.") |
| 88 | |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame^] | 89 | # name is mandatory |
| 90 | if not self.name: |
| 91 | raise XOSValidationError("name is mandatory for ProgranServiceInstances") |
| 92 | |
| 93 | # NOTE this check is disabled as when Progran create a profile it fails |
| 94 | # if self.DlUeAllocRbRate > self.DlAllocRBRate: |
| 95 | # raise XOSValidationError("DlUeAllocRbRate (%s) cannot be bigger than DlAllocRBRate (%s)" % (self.DlUeAllocRbRate, self.DlAllocRBRate)) |
| 96 | |
Matteo Scandolo | e463b06 | 2018-02-01 10:14:03 -0800 | [diff] [blame] | 97 | # prevent name duplicates |
| 98 | try: |
| 99 | instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name) |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 100 | |
Matteo Scandolo | e463b06 | 2018-02-01 10:14:03 -0800 | [diff] [blame] | 101 | if (not self.pk and instances_with_same_name) or (self.pk and self.pk != instances_with_same_name.pk): |
| 102 | raise XOSValidationError("A ProgranServiceInstance with name '%s' already exists" % self.name) |
| 103 | except self.DoesNotExist: |
| 104 | pass |
Matteo Scandolo | d52da97 | 2018-01-31 14:37:43 -0800 | [diff] [blame] | 105 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 106 | if self.is_new and not self.created_by: |
| 107 | # NOTE if created_by is null it has been created by XOS |
| 108 | self.created_by = "XOS" |
| 109 | |
| 110 | |
Matteo Scandolo | e624d34 | 2018-02-01 15:51:57 -0800 | [diff] [blame] | 111 | # 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] | 112 | if not self.deleted: |
| 113 | limit = 95 |
| 114 | same_enodeb = ProgranServiceInstance.objects.filter(enodeb_id=self.enodeb_id) |
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 | total_up = self.UlAllocRBRate |
| 117 | total_down = self.DlAllocRBRate |
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 | for p in same_enodeb: |
| 120 | if p.pk != self.pk: |
| 121 | total_up = total_up + p.UlAllocRBRate |
| 122 | total_down = total_down + p.DlAllocRBRate |
Matteo Scandolo | e624d34 | 2018-02-01 15:51:57 -0800 | [diff] [blame] | 123 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 124 | if total_up > limit: |
| 125 | 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] | 126 | |
Matteo Scandolo | 830403a | 2018-02-05 10:51:59 -0800 | [diff] [blame] | 127 | if total_down > limit: |
| 128 | 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] | 129 | |
Matteo Scandolo | f6b6ed2 | 2018-02-13 15:27:21 -0800 | [diff] [blame^] | 130 | caller_kind = "xos" |
| 131 | |
| 132 | if "caller_kind" in kwargs: |
| 133 | caller_kind = kwargs.pop("caller_kind") |
| 134 | |
| 135 | print "Profile %s has been saved by %s" % (self.name, caller_kind) |
| 136 | |
| 137 | if caller_kind == "xos": |
| 138 | print "Setting no_sync to false for profile %s" % self.name |
| 139 | self.no_sync = False |
| 140 | |
Matteo Scandolo | 1c049b0 | 2018-01-18 11:32:46 -0800 | [diff] [blame] | 141 | super(ProgranServiceInstance, self).save(*args, **kwargs) |
| 142 | |
| 143 | |