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 | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 17 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 18 | from xos.exceptions import XOSValidationError, XOSProgrammingError, XOSPermissionDenied |
| 19 | from models_decl import RCORDService_decl, RCORDSubscriber_decl |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 20 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 21 | class RCORDService(RCORDService_decl): |
| 22 | class Meta: |
| 23 | proxy = True |
| 24 | |
| 25 | class RCORDSubscriber(RCORDSubscriber_decl): |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 26 | class Meta: |
| 27 | proxy = True |
| 28 | |
| 29 | def invalidate_related_objects(self): |
| 30 | # Dirty all vSGs related to this subscriber, so the vSG synchronizer |
| 31 | # will run. |
| 32 | |
| 33 | # FIXME: This should be reimplemented when multiple-objects-per-synchronizer is implemented. |
| 34 | |
| 35 | for link in self.subscribed_links.all(): |
| 36 | outer_service_instance = link.provider_service_instance |
| 37 | # TODO: We may need to invalide the vOLT too... |
| 38 | for link in outer_service_instance.subscribed_links.all(): |
| 39 | inner_service_instance = link.provider_service_instance |
| 40 | inner_service_instance.save(update_fields=["updated"]) |
| 41 | |
| 42 | def save(self, *args, **kwargs): |
| 43 | self.validate_unique_service_specific_id(none_okay=True) |
| 44 | |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 45 | # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its |
| 46 | # Instance. |
| 47 | if not self.creator: |
| 48 | # If we weren't passed an explicit creator, then we will assume the caller is the creator. |
| 49 | if not getattr(self, "caller", None): |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 50 | raise XOSProgrammingError("RCORDSubscriber's self.caller was not set") |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 51 | self.creator = self.caller |
| 52 | |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 53 | if (not hasattr(self, 'caller') or not self.caller.is_admin): |
| 54 | if (self.has_field_changed("service_specific_id")): |
| 55 | raise XOSPermissionDenied("You do not have permission to change service_specific_id") |
| 56 | |
Matteo Scandolo | d1707b3 | 2018-05-04 12:42:53 -0700 | [diff] [blame] | 57 | # validate IP Address |
| 58 | if hasattr(self, 'ip_address') and self.ip_address is not None: |
| 59 | try: |
| 60 | socket.inet_aton(self.ip_address) |
| 61 | except socket.error: |
| 62 | raise XOSValidationError("The ip_address you specified (%s) is not valid" % self.ip_address) |
| 63 | |
| 64 | # validate MAC Address |
| 65 | if hasattr(self, 'mac_address') and self.mac_address is not None: |
| 66 | if not re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", self.mac_address.lower()): |
| 67 | raise XOSValidationError("The mac_address you specified (%s) is not valid" % self.mac_address) |
| 68 | |
| 69 | if self.owner.leaf_model.access == "voltha": |
| 70 | # if the access network is managed by voltha, validate that olt_device and olt_port actually exists |
| 71 | volt_service = self.owner.provider_services[0].leaf_model # we assume RCORDService is connected only to the vOLTService |
| 72 | |
| 73 | try: |
| 74 | olt_device = [d for d in volt_service.volt_devices.all() if d.name == self.olt_device][0] |
| 75 | except IndexError, e: |
| 76 | raise XOSValidationError("The olt_device you specified (%s) does not exists" % self.olt_device) |
| 77 | |
| 78 | try: |
| 79 | olt_port = [p for p in olt_device.ports.all() if p.name == self.olt_port][0] |
| 80 | except IndexError, e: |
| 81 | raise XOSValidationError("The olt_port you specified (%s) does not exists on OLTDevice %s" % (self.olt_port, olt_device.name)) |
| 82 | |
| 83 | |
| 84 | |
| 85 | super(RCORDSubscriber, self).save(*args, **kwargs) |
Matteo Scandolo | 62a83f0 | 2018-03-01 15:59:18 -0800 | [diff] [blame] | 86 | self.invalidate_related_objects() |
| 87 | return |