blob: 25a37df2837e75a71d92160577fe40c45b22fe87 [file] [log] [blame]
Matteo Scandolo830403a2018-02-05 10:51:59 -08001# 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 Scandolo1c049b02018-01-18 11:32:46 -080015from xos.exceptions import XOSValidationError
16
17from models_decl import ProgranService_decl
18from models_decl import ENodeB_decl
19from models_decl import Handover_decl
20from models_decl import ProgranServiceInstance_decl
21
Matteo Scandolo1c049b02018-01-18 11:32:46 -080022class ProgranService(ProgranService_decl):
23 class Meta:
24 proxy = True
Matteo Scandolo0a207b52018-01-29 13:39:43 -080025 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 Scandolo1c049b02018-01-18 11:32:46 -080033
34
35class ENodeB(ENodeB_decl):
36 class Meta:
37 proxy = True
38
Matteo Scandolocfa9e8f2018-01-31 18:29:06 -080039 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 Scandolo830403a2018-02-05 10:51:59 -080054 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 Scandolocfa9e8f2018-01-31 18:29:06 -080058 super(ENodeB, self).save(*args, **kwargs)
Matteo Scandolo6b607c82018-01-30 09:12:26 -080059
Matteo Scandolo1c049b02018-01-18 11:32:46 -080060
61class Handover(Handover_decl):
62 class Meta:
63 proxy = True
64
Matteo Scandolo830403a2018-02-05 10:51:59 -080065 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 Scandolo1c049b02018-01-18 11:32:46 -080072
73class ProgranServiceInstance(ProgranServiceInstance_decl):
74 class Meta:
75 proxy = True
76
77 def save(self, *args, **kwargs):
78 # NOTE someone is setting owner_id, so just override it for now
Matteo Scandolo1c049b02018-01-18 11:32:46 -080079 try:
Matteo Scandolo0a207b52018-01-29 13:39:43 -080080 # NOTE we allow just one ProgranService
81 progran_service = ProgranService.objects.all()[0]
Matteo Scandolo1c049b02018-01-18 11:32:46 -080082 self.owner_id = progran_service.id
83 except IndexError:
84 raise XOSValidationError("Service Progran cannot be found, please make sure that the model exists.")
85
Matteo Scandoloe463b062018-02-01 10:14:03 -080086 # prevent name duplicates
87 try:
88 instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name)
Matteo Scandolo1c049b02018-01-18 11:32:46 -080089
Matteo Scandoloe463b062018-02-01 10:14:03 -080090 if (not self.pk and instances_with_same_name) or (self.pk and self.pk != instances_with_same_name.pk):
91 raise XOSValidationError("A ProgranServiceInstance with name '%s' already exists" % self.name)
92 except self.DoesNotExist:
93 pass
Matteo Scandolod52da972018-01-31 14:37:43 -080094
Matteo Scandolo830403a2018-02-05 10:51:59 -080095 if self.is_new and not self.created_by:
96 # NOTE if created_by is null it has been created by XOS
97 self.created_by = "XOS"
98
99
Matteo Scandoloe624d342018-02-01 15:51:57 -0800100 # check that the sum of upload and download rate for a single enodeb is not greater than 95
Matteo Scandolo830403a2018-02-05 10:51:59 -0800101 if not self.deleted:
102 limit = 95
103 same_enodeb = ProgranServiceInstance.objects.filter(enodeb_id=self.enodeb_id)
Matteo Scandoloe624d342018-02-01 15:51:57 -0800104
Matteo Scandolo830403a2018-02-05 10:51:59 -0800105 total_up = self.UlAllocRBRate
106 total_down = self.DlAllocRBRate
Matteo Scandoloe624d342018-02-01 15:51:57 -0800107
Matteo Scandolo830403a2018-02-05 10:51:59 -0800108 for p in same_enodeb:
109 if p.pk != self.pk:
110 total_up = total_up + p.UlAllocRBRate
111 total_down = total_down + p.DlAllocRBRate
Matteo Scandoloe624d342018-02-01 15:51:57 -0800112
Matteo Scandolo830403a2018-02-05 10:51:59 -0800113 if total_up > limit:
114 raise XOSValidationError("UlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
Matteo Scandoloe624d342018-02-01 15:51:57 -0800115
Matteo Scandolo830403a2018-02-05 10:51:59 -0800116 if total_down > limit:
117 raise XOSValidationError("DlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
Matteo Scandoloe624d342018-02-01 15:51:57 -0800118
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800119 super(ProgranServiceInstance, self).save(*args, **kwargs)
120
121