Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 1 | |
| 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 Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 15 | import re |
| 16 | import socket |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 17 | import random |
Matteo Scandolo | a2b2e22 | 2019-06-18 18:32:43 -0700 | [diff] [blame] | 18 | from django.core.exceptions import ObjectDoesNotExist |
vigneshethiraj | 4d3e597 | 2019-04-04 19:22:14 +0530 | [diff] [blame] | 19 | from xos.exceptions import XOSValidationError, XOSProgrammingError, XOSPermissionDenied, XOSConfigurationError |
Matteo Scandolo | a4a279a | 2019-03-14 15:56:22 -0700 | [diff] [blame] | 20 | from models_decl import RCORDService_decl, RCORDSubscriber_decl, RCORDIpAddress_decl, BandwidthProfile_decl |
| 21 | |
vigneshethiraj | 4d3e597 | 2019-04-04 19:22:14 +0530 | [diff] [blame] | 22 | range_of_tags = list(range(16,4096)) |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame] | 23 | |
Matteo Scandolo | a4a279a | 2019-03-14 15:56:22 -0700 | [diff] [blame] | 24 | class BandwidthProfile(BandwidthProfile_decl): |
| 25 | class Meta: |
| 26 | proxy = True |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 27 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame] | 28 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 29 | class RCORDService(RCORDService_decl): |
| 30 | class Meta: |
| 31 | proxy = True |
| 32 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame] | 33 | |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame] | 34 | class 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 Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame] | 51 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 52 | class RCORDSubscriber(RCORDSubscriber_decl): |
Matteo Scandolo | 520217f | 2018-05-16 14:15:56 -0700 | [diff] [blame] | 53 | |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 54 | 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 Scandolo | a187560 | 2018-10-16 07:35:04 -0700 | [diff] [blame] | 65 | # TODO: We may need to invalidate the vOLT too... |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 66 | 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 Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 70 | def generate_s_tag(self): |
vigneshethiraj | 4d3e597 | 2019-04-04 19:22:14 +0530 | [diff] [blame] | 71 | |
| 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 WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 75 | |
| 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 Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 85 | def generate_c_tag(self): |
vigneshethiraj | 4d3e597 | 2019-04-04 19:22:14 +0530 | [diff] [blame] | 86 | |
| 87 | # unused_c_tags_for_s_tag() this function will return a list of unused c_tags for the given s_tag |
| 88 | |
| 89 | tag = random.choice(self.unused_c_tags_for_s_tag()) |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 90 | |
| 91 | # Check that this random generated value is valid across given ONU-first check. |
| 92 | # If the value is valid it will assign c_tag wth it and do second check. |
| 93 | # If the value is not valid below function is recursively called till it gets a unique value. |
| 94 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 95 | if tag in self.get_used_c_tags(): |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 96 | return self.generate_c_tag() |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 97 | else: |
| 98 | self.c_tag = tag |
| 99 | |
| 100 | # Scenario if we don't have a s_tag. |
| 101 | # Second check-verify that combination is unique across. |
| 102 | |
| 103 | if not self.s_tag: |
| 104 | self.s_tag = self.generate_s_tag() |
| 105 | return tag |
| 106 | elif None != self.get_used_s_c_tag_subscriber_id(): |
| 107 | return self.generate_c_tag() |
| 108 | else: |
| 109 | return tag |
| 110 | |
vigneshethiraj | 4d3e597 | 2019-04-04 19:22:14 +0530 | [diff] [blame] | 111 | def unused_c_tags_for_s_tag(self): |
| 112 | |
| 113 | #This function will return a list of unused c_tags |
| 114 | |
| 115 | #If there is no s_tag |
| 116 | global range_of_tags |
| 117 | |
| 118 | #If there's a s_tag, then return unused c_tags for that s_tag |
| 119 | subscribers_with_same_s_tag = RCORDSubscriber.objects.filter(s_tag = self.s_tag) |
| 120 | used_c_tags = [s.c_tag for s in subscribers_with_same_s_tag] |
| 121 | if len(used_c_tags) == 0: |
| 122 | return range_of_tags |
| 123 | else: |
| 124 | list_of_unused_c_tags_for_s_tag = list(set(range_of_tags) - set(used_c_tags)) |
| 125 | if len(list_of_unused_c_tags_for_s_tag) == 0: |
| 126 | raise XOSConfigurationError("All the c_tags are exhausted for this s_tag: %s"% self.s_tag) |
| 127 | else: |
| 128 | return list_of_unused_c_tags_for_s_tag |
| 129 | |
| 130 | def unused_s_tags_for_c_tag(self): |
| 131 | |
| 132 | #This function will return a list of unused s_tags |
| 133 | |
| 134 | global range_of_tags |
| 135 | #If there's a c_tag, then return unused s_tags for that c_tag |
| 136 | subscribers_with_same_c_tag = RCORDSubscriber.objects.filter(c_tag = self.c_tag) |
| 137 | used_s_tags = [s.s_tag for s in subscribers_with_same_c_tag] |
| 138 | if len(used_s_tags) == 0: |
| 139 | return range_of_tags |
| 140 | else: |
| 141 | list_of_unused_s_tags_for_c_tag = list(set(range_of_tags) - set(used_s_tags)) |
| 142 | if len(list_of_unused_s_tags_for_c_tag) == 0: |
| 143 | raise XOSConfigurationError("All the s_tags are exhausted for this c_tag: %s"% self.c_tag) |
| 144 | else: |
| 145 | return list_of_unused_s_tags_for_c_tag |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 146 | def get_same_onu_subscribers(self): |
| 147 | return RCORDSubscriber.objects.filter(onu_device=self.onu_device) |
| 148 | |
| 149 | def get_same_s_c_tag_subscribers(self): |
| 150 | return RCORDSubscriber.objects.filter(c_tag=self.c_tag, s_tag=self.s_tag) |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 151 | |
| 152 | def get_used_c_tags(self): |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 153 | same_onu_subscribers = self.get_same_onu_subscribers() |
Matteo Scandolo | a187560 | 2018-10-16 07:35:04 -0700 | [diff] [blame] | 154 | same_onu_subscribers = [s for s in same_onu_subscribers if s.id != self.id] |
| 155 | used_tags = [s.c_tag for s in same_onu_subscribers] |
| 156 | return used_tags |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 157 | |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 158 | def get_used_s_c_tag_subscriber_id(self): |
| 159 | # Function to check c_tag and s_tag combination are unique across. |
| 160 | same_s_c_tag_subscribers = self.get_same_s_c_tag_subscribers() |
| 161 | same_s_c_tag_subscribers = [s for s in same_s_c_tag_subscribers if s.id != self.id] |
| 162 | if len(same_s_c_tag_subscribers) > 0: |
| 163 | return same_s_c_tag_subscribers[0].id |
| 164 | else: |
| 165 | return None |
| 166 | |
Matteo Scandolo | a2b2e22 | 2019-06-18 18:32:43 -0700 | [diff] [blame] | 167 | def validate_tech_profile_id(self): |
| 168 | if self.owner.leaf_model.access != "voltha": |
| 169 | # if we're not using VOLTHA we don't need to validate this |
| 170 | return True |
| 171 | |
| 172 | volt = None |
| 173 | for ps in self.owner.provider_services: |
| 174 | provider_service = ps.leaf_model |
| 175 | if provider_service.name.lower() == "volt": |
| 176 | volt = provider_service |
| 177 | |
| 178 | technology = volt.get_olt_technology_from_unu_sn(self.onu_device) |
| 179 | |
| 180 | try: |
| 181 | tp = volt.get_tech_profile(technology, self.tech_profile_id) |
| 182 | return True |
| 183 | except ObjectDoesNotExist: |
| 184 | return False |
| 185 | |
| 186 | |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 187 | def save(self, *args, **kwargs): |
| 188 | self.validate_unique_service_specific_id(none_okay=True) |
| 189 | |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 190 | # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its |
| 191 | # Instance. |
| 192 | if not self.creator: |
| 193 | # If we weren't passed an explicit creator, then we will assume the caller is the creator. |
| 194 | if not getattr(self, "caller", None): |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 195 | raise XOSProgrammingError("RCORDSubscriber's self.caller was not set") |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 196 | self.creator = self.caller |
| 197 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 198 | # validate MAC Address |
Andy Bavier | 618f0ed | 2018-12-11 13:45:23 -0700 | [diff] [blame] | 199 | if hasattr(self, 'mac_address') and self.mac_address: |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 200 | if not re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", self.mac_address.lower()): |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame] | 201 | raise XOSValidationError("The MAC address specified is not valid: %s" % self.mac_address) |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 202 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 203 | # validate c_tag |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 204 | if self.c_tag: |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 205 | is_update_with_same_tag = False |
| 206 | |
| 207 | if not self.is_new: |
| 208 | # if it is an update, but the tag is the same, skip validation |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 209 | existing = RCORDSubscriber.objects.filter(id=self.id) |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 210 | |
| 211 | if len(existing) > 0 and existing[0].c_tag == self.c_tag and existing[0].id == self.id: |
| 212 | is_update_with_same_tag = True |
| 213 | |
| 214 | if self.c_tag in self.get_used_c_tags() and not is_update_with_same_tag: |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame] | 215 | raise XOSValidationError( |
| 216 | "The c_tag you specified (%s) has already been used on device %s" % |
| 217 | (self.c_tag, self.onu_device)) |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 218 | |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 219 | # validate s_tag and c_tag combination |
| 220 | if self.c_tag and self.s_tag: |
| 221 | is_update_with_same_tag = False |
| 222 | |
| 223 | if not self.is_new: |
| 224 | # if it is an update, but the tags are the same, skip validation |
| 225 | existing = RCORDSubscriber.objects.filter(id=self.id) |
| 226 | 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: |
| 227 | is_update_with_same_tag = True |
| 228 | |
| 229 | id = self.get_used_s_c_tag_subscriber_id() |
| 230 | if None != id and not is_update_with_same_tag: |
| 231 | 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)) |
vigneshethiraj | 4d3e597 | 2019-04-04 19:22:14 +0530 | [diff] [blame] | 232 | |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 233 | |
| 234 | if not self.c_tag: |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 235 | self.c_tag = self.generate_c_tag() |
| 236 | |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 237 | elif not self.s_tag: |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 238 | self.s_tag = self.generate_s_tag() |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 239 | |
Matteo Scandolo | 520217f | 2018-05-16 14:15:56 -0700 | [diff] [blame] | 240 | self.set_owner() |
| 241 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame] | 242 | if self.status != "pre-provisioned" and \ |
| 243 | hasattr(self.owner.leaf_model, "access") and \ |
| 244 | self.owner.leaf_model.access == "voltha" and \ |
| 245 | not self.deleted: |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 246 | |
Matteo Scandolo | b6d67fd | 2018-05-18 16:28:51 -0700 | [diff] [blame] | 247 | # if the access network is managed by voltha, validate that onu_device actually exist |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame] | 248 | # we assume RCORDService is connected only to the vOLTService |
| 249 | volt_service = self.owner.provider_services[0].leaf_model |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 250 | |
Matteo Scandolo | b6d67fd | 2018-05-18 16:28:51 -0700 | [diff] [blame] | 251 | if not volt_service.has_access_device(self.onu_device): |
| 252 | raise XOSValidationError("The onu_device you specified (%s) does not exists" % self.onu_device) |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 253 | |
Matteo Scandolo | 17962fc | 2019-06-21 10:17:47 -0700 | [diff] [blame] | 254 | # if the access network is managed by voltha, validate that the tech_profile_id actually exists |
| 255 | if not self.validate_tech_profile_id(): |
| 256 | raise XOSValidationError("The technology profile you specified [%s] does not exist" % self.tech_profile_id) |
Matteo Scandolo | a2b2e22 | 2019-06-18 18:32:43 -0700 | [diff] [blame] | 257 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 258 | super(RCORDSubscriber, self).save(*args, **kwargs) |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 259 | self.invalidate_related_objects() |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 260 | return |
vigneshethiraj | 4d3e597 | 2019-04-04 19:22:14 +0530 | [diff] [blame] | 261 | |