blob: 3544d25e671675bb42a6d39af5a137ac8bece626 [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 Scandolof6b6ed22018-02-13 15:27:21 -080081 # 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 Scandoloe463b062018-02-01 10:14:03 -080089 # prevent name duplicates
90 try:
91 instances_with_same_name = ProgranServiceInstance.objects.get(name=self.name)
Matteo Scandolo1c049b02018-01-18 11:32:46 -080092
Matteo Scandoloe463b062018-02-01 10:14:03 -080093 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 Scandolod52da972018-01-31 14:37:43 -080097
Matteo Scandolo830403a2018-02-05 10:51:59 -080098 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 Scandoloe624d342018-02-01 15:51:57 -0800103 # 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 -0800104 if not self.deleted:
105 limit = 95
106 same_enodeb = ProgranServiceInstance.objects.filter(enodeb_id=self.enodeb_id)
Matteo Scandoloe624d342018-02-01 15:51:57 -0800107
Matteo Scandolo830403a2018-02-05 10:51:59 -0800108 total_up = self.UlAllocRBRate
109 total_down = self.DlAllocRBRate
Matteo Scandoloe624d342018-02-01 15:51:57 -0800110
Matteo Scandolo830403a2018-02-05 10:51:59 -0800111 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 Scandoloe624d342018-02-01 15:51:57 -0800115
Matteo Scandolo830403a2018-02-05 10:51:59 -0800116 if total_up > limit:
117 raise XOSValidationError("UlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
Matteo Scandoloe624d342018-02-01 15:51:57 -0800118
Matteo Scandolo830403a2018-02-05 10:51:59 -0800119 if total_down > limit:
120 raise XOSValidationError("DlAllocRBRate for the enodeb associated with this profile is greater than %s" % limit)
Matteo Scandoloe624d342018-02-01 15:51:57 -0800121
Matteo Scandolof6b6ed22018-02-13 15:27:21 -0800122 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 Scandolo1c049b02018-01-18 11:32:46 -0800133 super(ProgranServiceInstance, self).save(*args, **kwargs)
134
135