blob: dd6c57f66ac2c2b2401764190bf04dee4c6f131e [file] [log] [blame]
Matteo Scandolo62a83f02018-03-01 15:59:18 -08001
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 Scandolod1707b32018-05-04 12:42:53 -070015import re
16import socket
Matteo Scandolo62a83f02018-03-01 15:59:18 -080017
Matteo Scandolod1707b32018-05-04 12:42:53 -070018from xos.exceptions import XOSValidationError, XOSProgrammingError, XOSPermissionDenied
19from models_decl import RCORDService_decl, RCORDSubscriber_decl
Matteo Scandolo62a83f02018-03-01 15:59:18 -080020
Matteo Scandolod1707b32018-05-04 12:42:53 -070021class RCORDService(RCORDService_decl):
22 class Meta:
23 proxy = True
24
25class RCORDSubscriber(RCORDSubscriber_decl):
Matteo Scandolo520217f2018-05-16 14:15:56 -070026
Matteo Scandolo62a83f02018-03-01 15:59:18 -080027 class Meta:
28 proxy = True
29
30 def invalidate_related_objects(self):
31 # Dirty all vSGs related to this subscriber, so the vSG synchronizer
32 # will run.
33
34 # FIXME: This should be reimplemented when multiple-objects-per-synchronizer is implemented.
35
36 for link in self.subscribed_links.all():
37 outer_service_instance = link.provider_service_instance
38 # TODO: We may need to invalide the vOLT too...
39 for link in outer_service_instance.subscribed_links.all():
40 inner_service_instance = link.provider_service_instance
41 inner_service_instance.save(update_fields=["updated"])
42
43 def save(self, *args, **kwargs):
44 self.validate_unique_service_specific_id(none_okay=True)
45
Scott Baker9d9ddf62018-03-20 20:44:27 -070046 # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its
47 # Instance.
48 if not self.creator:
49 # If we weren't passed an explicit creator, then we will assume the caller is the creator.
50 if not getattr(self, "caller", None):
Matteo Scandolod1707b32018-05-04 12:42:53 -070051 raise XOSProgrammingError("RCORDSubscriber's self.caller was not set")
Scott Baker9d9ddf62018-03-20 20:44:27 -070052 self.creator = self.caller
53
Matteo Scandolo62a83f02018-03-01 15:59:18 -080054 if (not hasattr(self, 'caller') or not self.caller.is_admin):
55 if (self.has_field_changed("service_specific_id")):
56 raise XOSPermissionDenied("You do not have permission to change service_specific_id")
57
Matteo Scandolod1707b32018-05-04 12:42:53 -070058 # validate IP Address
59 if hasattr(self, 'ip_address') and self.ip_address is not None:
60 try:
61 socket.inet_aton(self.ip_address)
62 except socket.error:
63 raise XOSValidationError("The ip_address you specified (%s) is not valid" % self.ip_address)
64
65 # validate MAC Address
66 if hasattr(self, 'mac_address') and self.mac_address is not None:
67 if not re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", self.mac_address.lower()):
68 raise XOSValidationError("The mac_address you specified (%s) is not valid" % self.mac_address)
69
Matteo Scandolo520217f2018-05-16 14:15:56 -070070 self.set_owner()
71
Matteo Scandolof9698702018-05-17 13:26:19 -070072 if hasattr(self.owner.leaf_model, "access") and self.owner.leaf_model.access == "voltha":
Matteo Scandolob6d67fd2018-05-18 16:28:51 -070073 # if the access network is managed by voltha, validate that onu_device actually exist
Matteo Scandolod1707b32018-05-04 12:42:53 -070074 volt_service = self.owner.provider_services[0].leaf_model # we assume RCORDService is connected only to the vOLTService
75
Matteo Scandolob6d67fd2018-05-18 16:28:51 -070076 if not volt_service.has_access_device(self.onu_device):
77 raise XOSValidationError("The onu_device you specified (%s) does not exists" % self.onu_device)
Matteo Scandolod1707b32018-05-04 12:42:53 -070078
79 super(RCORDSubscriber, self).save(*args, **kwargs)
Matteo Scandolo62a83f02018-03-01 15:59:18 -080080 self.invalidate_related_objects()
81 return