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 | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 18 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame^] | 19 | from xos.exceptions import XOSValidationError, XOSProgrammingError |
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 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame^] | 22 | |
Matteo Scandolo | a4a279a | 2019-03-14 15:56:22 -0700 | [diff] [blame] | 23 | class BandwidthProfile(BandwidthProfile_decl): |
| 24 | class Meta: |
| 25 | proxy = True |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 26 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame^] | 27 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 28 | class RCORDService(RCORDService_decl): |
| 29 | class Meta: |
| 30 | proxy = True |
| 31 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame^] | 32 | |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame] | 33 | class RCORDIpAddress(RCORDIpAddress_decl): |
| 34 | class Meta: |
| 35 | proxy = True |
| 36 | |
| 37 | def save(self, *args, **kwargs): |
| 38 | try: |
| 39 | if ":" in self.ip: |
| 40 | # it's an IPv6 address |
| 41 | socket.inet_pton(socket.AF_INET6, self.ip) |
| 42 | else: |
| 43 | # it's an IPv4 address |
| 44 | socket.inet_pton(socket.AF_INET, self.ip) |
| 45 | except socket.error: |
| 46 | raise XOSValidationError("The IP specified is not valid: %s" % self.ip) |
| 47 | super(RCORDIpAddress, self).save(*args, **kwargs) |
| 48 | return |
| 49 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame^] | 50 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 51 | class RCORDSubscriber(RCORDSubscriber_decl): |
Matteo Scandolo | 520217f | 2018-05-16 14:15:56 -0700 | [diff] [blame] | 52 | |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 53 | class Meta: |
| 54 | proxy = True |
| 55 | |
| 56 | def invalidate_related_objects(self): |
| 57 | # Dirty all vSGs related to this subscriber, so the vSG synchronizer |
| 58 | # will run. |
| 59 | |
| 60 | # FIXME: This should be reimplemented when multiple-objects-per-synchronizer is implemented. |
| 61 | |
| 62 | for link in self.subscribed_links.all(): |
| 63 | outer_service_instance = link.provider_service_instance |
Matteo Scandolo | a187560 | 2018-10-16 07:35:04 -0700 | [diff] [blame] | 64 | # TODO: We may need to invalidate the vOLT too... |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 65 | for link in outer_service_instance.subscribed_links.all(): |
| 66 | inner_service_instance = link.provider_service_instance |
| 67 | inner_service_instance.save(update_fields=["updated"]) |
| 68 | |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 69 | def generate_s_tag(self): |
| 70 | # NOTE what's the right way to generate an s_tag? |
| 71 | tag = random.randint(16, 4096) |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 72 | |
| 73 | # Check that combination(c_tag,s_tag) is unique. |
| 74 | # If the combination is not unique it will keep on calling this function recursively till it gets a unique combination. |
| 75 | |
| 76 | self.s_tag = tag |
| 77 | if None != self.get_used_s_c_tag_subscriber_id(): |
| 78 | return self.generate_s_tag() |
| 79 | else: |
| 80 | return tag |
| 81 | |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 82 | |
| 83 | def generate_c_tag(self): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 84 | # NOTE this method will loop if available c_tags are ended |
| 85 | tag = random.randint(16, 4096) |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 86 | |
| 87 | # Check that this random generated value is valid across given ONU-first check. |
| 88 | # If the value is valid it will assign c_tag wth it and do second check. |
| 89 | # If the value is not valid below function is recursively called till it gets a unique value. |
| 90 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 91 | if tag in self.get_used_c_tags(): |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 92 | return self.generate_c_tag() |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 93 | else: |
| 94 | self.c_tag = tag |
| 95 | |
| 96 | # Scenario if we don't have a s_tag. |
| 97 | # Second check-verify that combination is unique across. |
| 98 | |
| 99 | if not self.s_tag: |
| 100 | self.s_tag = self.generate_s_tag() |
| 101 | return tag |
| 102 | elif None != self.get_used_s_c_tag_subscriber_id(): |
| 103 | return self.generate_c_tag() |
| 104 | else: |
| 105 | return tag |
| 106 | |
| 107 | def get_same_onu_subscribers(self): |
| 108 | return RCORDSubscriber.objects.filter(onu_device=self.onu_device) |
| 109 | |
| 110 | def get_same_s_c_tag_subscribers(self): |
| 111 | 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] | 112 | |
| 113 | def get_used_c_tags(self): |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 114 | same_onu_subscribers = self.get_same_onu_subscribers() |
Matteo Scandolo | a187560 | 2018-10-16 07:35:04 -0700 | [diff] [blame] | 115 | same_onu_subscribers = [s for s in same_onu_subscribers if s.id != self.id] |
| 116 | used_tags = [s.c_tag for s in same_onu_subscribers] |
| 117 | return used_tags |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 118 | |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 119 | def get_used_s_c_tag_subscriber_id(self): |
| 120 | # Function to check c_tag and s_tag combination are unique across. |
| 121 | same_s_c_tag_subscribers = self.get_same_s_c_tag_subscribers() |
| 122 | same_s_c_tag_subscribers = [s for s in same_s_c_tag_subscribers if s.id != self.id] |
| 123 | if len(same_s_c_tag_subscribers) > 0: |
| 124 | return same_s_c_tag_subscribers[0].id |
| 125 | else: |
| 126 | return None |
| 127 | |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 128 | def save(self, *args, **kwargs): |
| 129 | self.validate_unique_service_specific_id(none_okay=True) |
| 130 | |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 131 | # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its |
| 132 | # Instance. |
| 133 | if not self.creator: |
| 134 | # If we weren't passed an explicit creator, then we will assume the caller is the creator. |
| 135 | if not getattr(self, "caller", None): |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 136 | raise XOSProgrammingError("RCORDSubscriber's self.caller was not set") |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 137 | self.creator = self.caller |
| 138 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 139 | # validate MAC Address |
Andy Bavier | 618f0ed | 2018-12-11 13:45:23 -0700 | [diff] [blame] | 140 | if hasattr(self, 'mac_address') and self.mac_address: |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 141 | 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] | 142 | 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] | 143 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 144 | # validate c_tag |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 145 | if self.c_tag: |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 146 | is_update_with_same_tag = False |
| 147 | |
| 148 | if not self.is_new: |
| 149 | # 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] | 150 | existing = RCORDSubscriber.objects.filter(id=self.id) |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 151 | |
| 152 | if len(existing) > 0 and existing[0].c_tag == self.c_tag and existing[0].id == self.id: |
| 153 | is_update_with_same_tag = True |
| 154 | |
| 155 | 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^] | 156 | raise XOSValidationError( |
| 157 | "The c_tag you specified (%s) has already been used on device %s" % |
| 158 | (self.c_tag, self.onu_device)) |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 159 | |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 160 | # validate s_tag and c_tag combination |
| 161 | if self.c_tag and self.s_tag: |
| 162 | is_update_with_same_tag = False |
| 163 | |
| 164 | if not self.is_new: |
| 165 | # if it is an update, but the tags are the same, skip validation |
| 166 | existing = RCORDSubscriber.objects.filter(id=self.id) |
| 167 | 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: |
| 168 | is_update_with_same_tag = True |
| 169 | |
| 170 | id = self.get_used_s_c_tag_subscriber_id() |
| 171 | if None != id and not is_update_with_same_tag: |
| 172 | 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)) |
| 173 | |
| 174 | if not self.c_tag: |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 175 | self.c_tag = self.generate_c_tag() |
| 176 | |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 177 | elif not self.s_tag: |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 178 | self.s_tag = self.generate_s_tag() |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 179 | |
Matteo Scandolo | 520217f | 2018-05-16 14:15:56 -0700 | [diff] [blame] | 180 | self.set_owner() |
| 181 | |
Scott Baker | b167151 | 2019-04-01 19:16:54 -0700 | [diff] [blame^] | 182 | if self.status != "pre-provisioned" and \ |
| 183 | hasattr(self.owner.leaf_model, "access") and \ |
| 184 | self.owner.leaf_model.access == "voltha" and \ |
| 185 | not self.deleted: |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 186 | |
Matteo Scandolo | b6d67fd | 2018-05-18 16:28:51 -0700 | [diff] [blame] | 187 | # 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^] | 188 | # we assume RCORDService is connected only to the vOLTService |
| 189 | volt_service = self.owner.provider_services[0].leaf_model |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 190 | |
Matteo Scandolo | b6d67fd | 2018-05-18 16:28:51 -0700 | [diff] [blame] | 191 | if not volt_service.has_access_device(self.onu_device): |
| 192 | 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] | 193 | |
| 194 | super(RCORDSubscriber, self).save(*args, **kwargs) |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 195 | self.invalidate_related_objects() |
HARDIK WINDLASS | abe3a9c | 2019-03-18 16:17:28 +0530 | [diff] [blame] | 196 | return |