blob: ccf52116686ae1e29058ca1f3297f28bae03b90b [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 Scandolocc94e902018-05-22 15:25:25 -070017import random
Matteo Scandolo62a83f02018-03-01 15:59:18 -080018
Matteo Scandolod1707b32018-05-04 12:42:53 -070019from xos.exceptions import XOSValidationError, XOSProgrammingError, XOSPermissionDenied
20from models_decl import RCORDService_decl, RCORDSubscriber_decl
Matteo Scandolo62a83f02018-03-01 15:59:18 -080021
Matteo Scandolod1707b32018-05-04 12:42:53 -070022class RCORDService(RCORDService_decl):
23 class Meta:
24 proxy = True
25
26class RCORDSubscriber(RCORDSubscriber_decl):
Matteo Scandolo520217f2018-05-16 14:15:56 -070027
Matteo Scandolo62a83f02018-03-01 15:59:18 -080028 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
Matteo Scandoloa1875602018-10-16 07:35:04 -070039 # TODO: We may need to invalidate the vOLT too...
Matteo Scandolo62a83f02018-03-01 15:59:18 -080040 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 Scandoloc348b3f2018-07-29 09:35:11 -070044 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 Scandolocc94e902018-05-22 15:25:25 -070050 # 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 Scandoloc348b3f2018-07-29 09:35:11 -070053 return self.generate_c_tag()
Matteo Scandolocc94e902018-05-22 15:25:25 -070054 return tag
55
56 def get_used_c_tags(self):
Matteo Scandoloa1875602018-10-16 07:35:04 -070057 # TODO add validation for no duplicate c_tag on the same s_tag
Matteo Scandolocc94e902018-05-22 15:25:25 -070058 same_onu_subscribers = RCORDSubscriber.objects.filter(onu_device=self.onu_device)
Matteo Scandoloa1875602018-10-16 07:35:04 -070059 same_onu_subscribers = [s for s in same_onu_subscribers if s.id != self.id]
60 used_tags = [s.c_tag for s in same_onu_subscribers]
61 return used_tags
Matteo Scandolocc94e902018-05-22 15:25:25 -070062
Matteo Scandolo62a83f02018-03-01 15:59:18 -080063 def save(self, *args, **kwargs):
Matteo Scandolocc94e902018-05-22 15:25:25 -070064
Matteo Scandolo62a83f02018-03-01 15:59:18 -080065 self.validate_unique_service_specific_id(none_okay=True)
66
Scott Baker9d9ddf62018-03-20 20:44:27 -070067 # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its
68 # Instance.
69 if not self.creator:
70 # If we weren't passed an explicit creator, then we will assume the caller is the creator.
71 if not getattr(self, "caller", None):
Matteo Scandolod1707b32018-05-04 12:42:53 -070072 raise XOSProgrammingError("RCORDSubscriber's self.caller was not set")
Scott Baker9d9ddf62018-03-20 20:44:27 -070073 self.creator = self.caller
74
Matteo Scandolod1707b32018-05-04 12:42:53 -070075 # validate IP Address
76 if hasattr(self, 'ip_address') and self.ip_address is not None:
77 try:
78 socket.inet_aton(self.ip_address)
79 except socket.error:
80 raise XOSValidationError("The ip_address you specified (%s) is not valid" % self.ip_address)
81
82 # validate MAC Address
83 if hasattr(self, 'mac_address') and self.mac_address is not None:
84 if not re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", self.mac_address.lower()):
85 raise XOSValidationError("The mac_address you specified (%s) is not valid" % self.mac_address)
86
Matteo Scandolocc94e902018-05-22 15:25:25 -070087 # validate c_tag
88 if hasattr(self, 'c_tag') and self.c_tag is not None:
Matteo Scandoloaa37b422018-05-23 15:36:56 -070089 is_update_with_same_tag = False
90
91 if not self.is_new:
92 # if it is an update, but the tag is the same, skip validation
93 existing = RCORDSubscriber.objects.filter(c_tag=self.c_tag)
94
95 if len(existing) > 0 and existing[0].c_tag == self.c_tag and existing[0].id == self.id:
96 is_update_with_same_tag = True
97
98 if self.c_tag in self.get_used_c_tags() and not is_update_with_same_tag:
Matteo Scandolocc94e902018-05-22 15:25:25 -070099 raise XOSValidationError("The c_tag you specified (%s) has already been used on device %s" % (self.c_tag, self.onu_device))
100
101 if not hasattr(self, "c_tag") or self.c_tag is None:
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700102 self.c_tag = self.generate_c_tag()
103
104 if not hasattr(self, "s_tag") or self.s_tag is None:
105 self.s_tag = self.generate_s_tag()
Matteo Scandolocc94e902018-05-22 15:25:25 -0700106
Matteo Scandolo520217f2018-05-16 14:15:56 -0700107 self.set_owner()
108
Matteo Scandolo3ecc1672018-06-25 16:58:12 -0700109 if self.status != "pre-provisioned" and hasattr(self.owner.leaf_model, "access") and self.owner.leaf_model.access == "voltha":
Matteo Scandolocc94e902018-05-22 15:25:25 -0700110
Matteo Scandolob6d67fd2018-05-18 16:28:51 -0700111 # if the access network is managed by voltha, validate that onu_device actually exist
Matteo Scandolod1707b32018-05-04 12:42:53 -0700112 volt_service = self.owner.provider_services[0].leaf_model # we assume RCORDService is connected only to the vOLTService
113
Matteo Scandolob6d67fd2018-05-18 16:28:51 -0700114 if not volt_service.has_access_device(self.onu_device):
115 raise XOSValidationError("The onu_device you specified (%s) does not exists" % self.onu_device)
Matteo Scandolod1707b32018-05-04 12:42:53 -0700116
117 super(RCORDSubscriber, self).save(*args, **kwargs)
Matteo Scandolo62a83f02018-03-01 15:59:18 -0800118 self.invalidate_related_objects()
119 return