blob: 5a42ce53e8f278c276490f1d7e37b40e6037e0bd [file] [log] [blame]
Matteo Scandolo62a83f02018-03-01 15:59:18 -08001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Matteo Scandolod1707b32018-05-04 12:42:53 -070015import re
16import socket
Matteo Scandolocc94e902018-05-22 15:25:25 -070017import random
Matteo Scandolo62a83f02018-03-01 15:59:18 -080018
vigneshethiraj4d3e5972019-04-04 19:22:14 +053019from xos.exceptions import XOSValidationError, XOSProgrammingError, XOSPermissionDenied, XOSConfigurationError
Matteo Scandoloa4a279a2019-03-14 15:56:22 -070020from models_decl import RCORDService_decl, RCORDSubscriber_decl, RCORDIpAddress_decl, BandwidthProfile_decl
21
vigneshethiraj4d3e5972019-04-04 19:22:14 +053022range_of_tags = list(range(16,4096))
Scott Bakerb1671512019-04-01 19:16:54 -070023
Matteo Scandoloa4a279a2019-03-14 15:56:22 -070024class BandwidthProfile(BandwidthProfile_decl):
25 class Meta:
26 proxy = True
Matteo Scandolo62a83f02018-03-01 15:59:18 -080027
Scott Bakerb1671512019-04-01 19:16:54 -070028
Matteo Scandolod1707b32018-05-04 12:42:53 -070029class RCORDService(RCORDService_decl):
30 class Meta:
31 proxy = True
32
Scott Bakerb1671512019-04-01 19:16:54 -070033
Luca Preted6700ba2018-09-12 16:40:49 -070034class RCORDIpAddress(RCORDIpAddress_decl):
35 class Meta:
36 proxy = True
37
38 def save(self, *args, **kwargs):
39 try:
40 if ":" in self.ip:
41 # it's an IPv6 address
42 socket.inet_pton(socket.AF_INET6, self.ip)
43 else:
44 # it's an IPv4 address
45 socket.inet_pton(socket.AF_INET, self.ip)
46 except socket.error:
47 raise XOSValidationError("The IP specified is not valid: %s" % self.ip)
48 super(RCORDIpAddress, self).save(*args, **kwargs)
49 return
50
Scott Bakerb1671512019-04-01 19:16:54 -070051
Matteo Scandolod1707b32018-05-04 12:42:53 -070052class RCORDSubscriber(RCORDSubscriber_decl):
Matteo Scandolo520217f2018-05-16 14:15:56 -070053
Matteo Scandolo62a83f02018-03-01 15:59:18 -080054 class Meta:
55 proxy = True
56
57 def invalidate_related_objects(self):
58 # Dirty all vSGs related to this subscriber, so the vSG synchronizer
59 # will run.
60
61 # FIXME: This should be reimplemented when multiple-objects-per-synchronizer is implemented.
62
63 for link in self.subscribed_links.all():
64 outer_service_instance = link.provider_service_instance
Matteo Scandoloa1875602018-10-16 07:35:04 -070065 # TODO: We may need to invalidate the vOLT too...
Matteo Scandolo62a83f02018-03-01 15:59:18 -080066 for link in outer_service_instance.subscribed_links.all():
67 inner_service_instance = link.provider_service_instance
68 inner_service_instance.save(update_fields=["updated"])
69
Matteo Scandoloc348b3f2018-07-29 09:35:11 -070070 def generate_s_tag(self):
vigneshethiraj4d3e5972019-04-04 19:22:14 +053071
72 # unused_s_tags_for_c_tag() this function will return a list of unused s_tags for that c_tag
73
74 tag = random.choice(self.unused_s_tags_for_c_tag())
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +053075
76 # Check that combination(c_tag,s_tag) is unique.
77 # If the combination is not unique it will keep on calling this function recursively till it gets a unique combination.
78
79 self.s_tag = tag
80 if None != self.get_used_s_c_tag_subscriber_id():
81 return self.generate_s_tag()
82 else:
83 return tag
84
Matteo Scandoloc348b3f2018-07-29 09:35:11 -070085
86 def generate_c_tag(self):
vigneshethiraj4d3e5972019-04-04 19:22:14 +053087
88 # unused_c_tags_for_s_tag() this function will return a list of unused c_tags for the given s_tag
89
90 tag = random.choice(self.unused_c_tags_for_s_tag())
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +053091
92 # Check that this random generated value is valid across given ONU-first check.
93 # If the value is valid it will assign c_tag wth it and do second check.
94 # If the value is not valid below function is recursively called till it gets a unique value.
95
Matteo Scandolocc94e902018-05-22 15:25:25 -070096 if tag in self.get_used_c_tags():
Matteo Scandoloc348b3f2018-07-29 09:35:11 -070097 return self.generate_c_tag()
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +053098 else:
99 self.c_tag = tag
100
101 # Scenario if we don't have a s_tag.
102 # Second check-verify that combination is unique across.
103
104 if not self.s_tag:
105 self.s_tag = self.generate_s_tag()
106 return tag
107 elif None != self.get_used_s_c_tag_subscriber_id():
108 return self.generate_c_tag()
109 else:
110 return tag
111
vigneshethiraj4d3e5972019-04-04 19:22:14 +0530112 def unused_c_tags_for_s_tag(self):
113
114 #This function will return a list of unused c_tags
115
116 #If there is no s_tag
117 global range_of_tags
118
119 #If there's a s_tag, then return unused c_tags for that s_tag
120 subscribers_with_same_s_tag = RCORDSubscriber.objects.filter(s_tag = self.s_tag)
121 used_c_tags = [s.c_tag for s in subscribers_with_same_s_tag]
122 if len(used_c_tags) == 0:
123 return range_of_tags
124 else:
125 list_of_unused_c_tags_for_s_tag = list(set(range_of_tags) - set(used_c_tags))
126 if len(list_of_unused_c_tags_for_s_tag) == 0:
127 raise XOSConfigurationError("All the c_tags are exhausted for this s_tag: %s"% self.s_tag)
128 else:
129 return list_of_unused_c_tags_for_s_tag
130
131 def unused_s_tags_for_c_tag(self):
132
133 #This function will return a list of unused s_tags
134
135 global range_of_tags
136 #If there's a c_tag, then return unused s_tags for that c_tag
137 subscribers_with_same_c_tag = RCORDSubscriber.objects.filter(c_tag = self.c_tag)
138 used_s_tags = [s.s_tag for s in subscribers_with_same_c_tag]
139 if len(used_s_tags) == 0:
140 return range_of_tags
141 else:
142 list_of_unused_s_tags_for_c_tag = list(set(range_of_tags) - set(used_s_tags))
143 if len(list_of_unused_s_tags_for_c_tag) == 0:
144 raise XOSConfigurationError("All the s_tags are exhausted for this c_tag: %s"% self.c_tag)
145 else:
146 return list_of_unused_s_tags_for_c_tag
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530147 def get_same_onu_subscribers(self):
148 return RCORDSubscriber.objects.filter(onu_device=self.onu_device)
149
150 def get_same_s_c_tag_subscribers(self):
151 return RCORDSubscriber.objects.filter(c_tag=self.c_tag, s_tag=self.s_tag)
Matteo Scandolocc94e902018-05-22 15:25:25 -0700152
153 def get_used_c_tags(self):
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530154 same_onu_subscribers = self.get_same_onu_subscribers()
Matteo Scandoloa1875602018-10-16 07:35:04 -0700155 same_onu_subscribers = [s for s in same_onu_subscribers if s.id != self.id]
156 used_tags = [s.c_tag for s in same_onu_subscribers]
157 return used_tags
Matteo Scandolocc94e902018-05-22 15:25:25 -0700158
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530159 def get_used_s_c_tag_subscriber_id(self):
160 # Function to check c_tag and s_tag combination are unique across.
161 same_s_c_tag_subscribers = self.get_same_s_c_tag_subscribers()
162 same_s_c_tag_subscribers = [s for s in same_s_c_tag_subscribers if s.id != self.id]
163 if len(same_s_c_tag_subscribers) > 0:
164 return same_s_c_tag_subscribers[0].id
165 else:
166 return None
167
Matteo Scandolo62a83f02018-03-01 15:59:18 -0800168 def save(self, *args, **kwargs):
169 self.validate_unique_service_specific_id(none_okay=True)
170
Scott Baker9d9ddf62018-03-20 20:44:27 -0700171 # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its
172 # Instance.
173 if not self.creator:
174 # If we weren't passed an explicit creator, then we will assume the caller is the creator.
175 if not getattr(self, "caller", None):
Matteo Scandolod1707b32018-05-04 12:42:53 -0700176 raise XOSProgrammingError("RCORDSubscriber's self.caller was not set")
Scott Baker9d9ddf62018-03-20 20:44:27 -0700177 self.creator = self.caller
178
Matteo Scandolod1707b32018-05-04 12:42:53 -0700179 # validate MAC Address
Andy Bavier618f0ed2018-12-11 13:45:23 -0700180 if hasattr(self, 'mac_address') and self.mac_address:
Matteo Scandolod1707b32018-05-04 12:42:53 -0700181 if not re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", self.mac_address.lower()):
Luca Preted6700ba2018-09-12 16:40:49 -0700182 raise XOSValidationError("The MAC address specified is not valid: %s" % self.mac_address)
Matteo Scandolod1707b32018-05-04 12:42:53 -0700183
Matteo Scandolocc94e902018-05-22 15:25:25 -0700184 # validate c_tag
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530185 if self.c_tag:
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700186 is_update_with_same_tag = False
187
188 if not self.is_new:
189 # if it is an update, but the tag is the same, skip validation
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530190 existing = RCORDSubscriber.objects.filter(id=self.id)
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700191
192 if len(existing) > 0 and existing[0].c_tag == self.c_tag and existing[0].id == self.id:
193 is_update_with_same_tag = True
194
195 if self.c_tag in self.get_used_c_tags() and not is_update_with_same_tag:
Scott Bakerb1671512019-04-01 19:16:54 -0700196 raise XOSValidationError(
197 "The c_tag you specified (%s) has already been used on device %s" %
198 (self.c_tag, self.onu_device))
Matteo Scandolocc94e902018-05-22 15:25:25 -0700199
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530200 # validate s_tag and c_tag combination
201 if self.c_tag and self.s_tag:
202 is_update_with_same_tag = False
203
204 if not self.is_new:
205 # if it is an update, but the tags are the same, skip validation
206 existing = RCORDSubscriber.objects.filter(id=self.id)
207 if len(existing) > 0 and existing[0].c_tag == self.c_tag and existing[0].s_tag == self.s_tag and existing[0].id == self.id:
208 is_update_with_same_tag = True
209
210 id = self.get_used_s_c_tag_subscriber_id()
211 if None != id and not is_update_with_same_tag:
212 raise XOSValidationError("The c_tag(%s) and s_tag(%s) pair you specified,has already been used by Subscriber with Id (%s)" % (self.c_tag,self.s_tag,id))
vigneshethiraj4d3e5972019-04-04 19:22:14 +0530213
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530214
215 if not self.c_tag:
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700216 self.c_tag = self.generate_c_tag()
217
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530218 elif not self.s_tag:
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700219 self.s_tag = self.generate_s_tag()
Matteo Scandolocc94e902018-05-22 15:25:25 -0700220
Matteo Scandolo520217f2018-05-16 14:15:56 -0700221 self.set_owner()
222
Scott Bakerb1671512019-04-01 19:16:54 -0700223 if self.status != "pre-provisioned" and \
224 hasattr(self.owner.leaf_model, "access") and \
225 self.owner.leaf_model.access == "voltha" and \
226 not self.deleted:
Matteo Scandolocc94e902018-05-22 15:25:25 -0700227
Matteo Scandolob6d67fd2018-05-18 16:28:51 -0700228 # if the access network is managed by voltha, validate that onu_device actually exist
Scott Bakerb1671512019-04-01 19:16:54 -0700229 # we assume RCORDService is connected only to the vOLTService
230 volt_service = self.owner.provider_services[0].leaf_model
Matteo Scandolod1707b32018-05-04 12:42:53 -0700231
Matteo Scandolob6d67fd2018-05-18 16:28:51 -0700232 if not volt_service.has_access_device(self.onu_device):
233 raise XOSValidationError("The onu_device you specified (%s) does not exists" % self.onu_device)
Matteo Scandolod1707b32018-05-04 12:42:53 -0700234
235 super(RCORDSubscriber, self).save(*args, **kwargs)
Matteo Scandolo62a83f02018-03-01 15:59:18 -0800236 self.invalidate_related_objects()
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530237 return
vigneshethiraj4d3e5972019-04-04 19:22:14 +0530238