blob: fef3fb1ac0e63873f9b71601aaaaaa109ee7dff4 [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):
Matteo Scandolof6b6ed22018-02-13 15:27:21 -080078
79 # TODO we should not allow name changes as name is the mapping with the backend
80
Matteo Scandolo1c049b02018-01-18 11:32:46 -080081 # NOTE someone is setting owner_id, so just override it for now
Matteo Scandolo1c049b02018-01-18 11:32:46 -080082 try:
Matteo Scandolo0a207b52018-01-29 13:39:43 -080083 # NOTE we allow just one ProgranService
84 progran_service = ProgranService.objects.all()[0]
Matteo Scandolo1c049b02018-01-18 11:32:46 -080085 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 Scandolof6b6ed22018-02-13 15:27:21 -080089 # 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 Scandoloe463b062018-02-01 10:14:03 -080097 # prevent name duplicates
98 try:
99 instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name)
Matteo Scandolo1c049b02018-01-18 11:32:46 -0800100
Matteo Scandoloe463b062018-02-01 10:14:03 -0800101 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 Scandolod52da972018-01-31 14:37:43 -0800105
Matteo Scandolo830403a2018-02-05 10:51:59 -0800106 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 Scandoloe624d342018-02-01 15:51:57 -0800111 # 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 -0800112 if not self.deleted:
113 limit = 95
114 same_enodeb = ProgranServiceInstance.objects.filter(enodeb_id=self.enodeb_id)
Matteo Scandoloe624d342018-02-01 15:51:57 -0800115
Matteo Scandolo830403a2018-02-05 10:51:59 -0800116 total_up = self.UlAllocRBRate
117 total_down = self.DlAllocRBRate
Matteo Scandoloe624d342018-02-01 15:51:57 -0800118
Matteo Scandolo830403a2018-02-05 10:51:59 -0800119 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 Scandoloe624d342018-02-01 15:51:57 -0800123
Matteo Scandolo830403a2018-02-05 10:51:59 -0800124 if total_up > limit:
125 raise XOSValidationError("UlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
Matteo Scandoloe624d342018-02-01 15:51:57 -0800126
Matteo Scandolo830403a2018-02-05 10:51:59 -0800127 if total_down > limit:
128 raise XOSValidationError("DlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
Matteo Scandoloe624d342018-02-01 15:51:57 -0800129
Matteo Scandolof6b6ed22018-02-13 15:27:21 -0800130 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 Scandolo1c049b02018-01-18 11:32:46 -0800141 super(ProgranServiceInstance, self).save(*args, **kwargs)
142
143