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