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 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 19 | from xos.exceptions import XOSValidationError, XOSProgrammingError, XOSPermissionDenied |
| 20 | from models_decl import RCORDService_decl, RCORDSubscriber_decl |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 21 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 22 | class RCORDService(RCORDService_decl): |
| 23 | class Meta: |
| 24 | proxy = True |
| 25 | |
| 26 | class RCORDSubscriber(RCORDSubscriber_decl): |
Matteo Scandolo | 520217f | 2018-05-16 14:15:56 -0700 | [diff] [blame] | 27 | |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 28 | class Meta: |
| 29 | proxy = True |
| 30 | |
| 31 | def invalidate_related_objects(self): |
| 32 | # Dirty all vSGs related to this subscriber, so the vSG synchronizer |
| 33 | # will run. |
| 34 | |
| 35 | # FIXME: This should be reimplemented when multiple-objects-per-synchronizer is implemented. |
| 36 | |
| 37 | for link in self.subscribed_links.all(): |
| 38 | outer_service_instance = link.provider_service_instance |
| 39 | # TODO: We may need to invalide the vOLT too... |
| 40 | for link in outer_service_instance.subscribed_links.all(): |
| 41 | inner_service_instance = link.provider_service_instance |
| 42 | inner_service_instance.save(update_fields=["updated"]) |
| 43 | |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 44 | def generate_s_tag(self): |
| 45 | # NOTE what's the right way to generate an s_tag? |
| 46 | tag = random.randint(16, 4096) |
| 47 | return tag |
| 48 | |
| 49 | def generate_c_tag(self): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 50 | # NOTE this method will loop if available c_tags are ended |
| 51 | tag = random.randint(16, 4096) |
| 52 | if tag in self.get_used_c_tags(): |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 53 | return self.generate_c_tag() |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 54 | return tag |
| 55 | |
| 56 | def get_used_c_tags(self): |
| 57 | same_onu_subscribers = RCORDSubscriber.objects.filter(onu_device=self.onu_device) |
| 58 | return [s.c_tag for s in same_onu_subscribers] |
| 59 | |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 60 | def save(self, *args, **kwargs): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 61 | |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 62 | self.validate_unique_service_specific_id(none_okay=True) |
| 63 | |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 64 | # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its |
| 65 | # Instance. |
| 66 | if not self.creator: |
| 67 | # If we weren't passed an explicit creator, then we will assume the caller is the creator. |
| 68 | if not getattr(self, "caller", None): |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 69 | raise XOSProgrammingError("RCORDSubscriber's self.caller was not set") |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 70 | self.creator = self.caller |
| 71 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 72 | # validate IP Address |
| 73 | if hasattr(self, 'ip_address') and self.ip_address is not None: |
| 74 | try: |
| 75 | socket.inet_aton(self.ip_address) |
| 76 | except socket.error: |
| 77 | raise XOSValidationError("The ip_address you specified (%s) is not valid" % self.ip_address) |
| 78 | |
| 79 | # validate MAC Address |
| 80 | if hasattr(self, 'mac_address') and self.mac_address is not None: |
| 81 | if not re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", self.mac_address.lower()): |
| 82 | raise XOSValidationError("The mac_address you specified (%s) is not valid" % self.mac_address) |
| 83 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 84 | # validate c_tag |
| 85 | if hasattr(self, 'c_tag') and self.c_tag is not None: |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 86 | is_update_with_same_tag = False |
| 87 | |
| 88 | if not self.is_new: |
| 89 | # if it is an update, but the tag is the same, skip validation |
| 90 | existing = RCORDSubscriber.objects.filter(c_tag=self.c_tag) |
| 91 | |
| 92 | if len(existing) > 0 and existing[0].c_tag == self.c_tag and existing[0].id == self.id: |
| 93 | is_update_with_same_tag = True |
| 94 | |
| 95 | if self.c_tag in self.get_used_c_tags() and not is_update_with_same_tag: |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 96 | raise XOSValidationError("The c_tag you specified (%s) has already been used on device %s" % (self.c_tag, self.onu_device)) |
| 97 | |
| 98 | if not hasattr(self, "c_tag") or self.c_tag is None: |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 99 | self.c_tag = self.generate_c_tag() |
| 100 | |
| 101 | if not hasattr(self, "s_tag") or self.s_tag is None: |
| 102 | self.s_tag = self.generate_s_tag() |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 103 | |
Matteo Scandolo | 520217f | 2018-05-16 14:15:56 -0700 | [diff] [blame] | 104 | self.set_owner() |
| 105 | |
Matteo Scandolo | 3ecc167 | 2018-06-25 16:58:12 -0700 | [diff] [blame] | 106 | if self.status != "pre-provisioned" and hasattr(self.owner.leaf_model, "access") and self.owner.leaf_model.access == "voltha": |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 107 | |
Matteo Scandolo | b6d67fd | 2018-05-18 16:28:51 -0700 | [diff] [blame] | 108 | # if the access network is managed by voltha, validate that onu_device actually exist |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 109 | volt_service = self.owner.provider_services[0].leaf_model # we assume RCORDService is connected only to the vOLTService |
| 110 | |
Matteo Scandolo | b6d67fd | 2018-05-18 16:28:51 -0700 | [diff] [blame] | 111 | if not volt_service.has_access_device(self.onu_device): |
| 112 | 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] | 113 | |
| 114 | super(RCORDSubscriber, self).save(*args, **kwargs) |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 115 | self.invalidate_related_objects() |
| 116 | return |