blob: 7227ab97590ba3de2f8c4f956cdeb39e1dde1e00 [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
Matteo Scandoloa4a279a2019-03-14 15:56:22 -070020from models_decl import RCORDService_decl, RCORDSubscriber_decl, RCORDIpAddress_decl, BandwidthProfile_decl
21
22class BandwidthProfile(BandwidthProfile_decl):
23 class Meta:
24 proxy = True
Matteo Scandolo62a83f02018-03-01 15:59:18 -080025
Matteo Scandolod1707b32018-05-04 12:42:53 -070026class RCORDService(RCORDService_decl):
27 class Meta:
28 proxy = True
29
Luca Preted6700ba2018-09-12 16:40:49 -070030class 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 Scandolod1707b32018-05-04 12:42:53 -070047class RCORDSubscriber(RCORDSubscriber_decl):
Matteo Scandolo520217f2018-05-16 14:15:56 -070048
Matteo Scandolo62a83f02018-03-01 15:59:18 -080049 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 Scandoloa1875602018-10-16 07:35:04 -070060 # TODO: We may need to invalidate the vOLT too...
Matteo Scandolo62a83f02018-03-01 15:59:18 -080061 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 Scandoloc348b3f2018-07-29 09:35:11 -070065 def generate_s_tag(self):
66 # NOTE what's the right way to generate an s_tag?
67 tag = random.randint(16, 4096)
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +053068
69 # Check that combination(c_tag,s_tag) is unique.
70 # If the combination is not unique it will keep on calling this function recursively till it gets a unique combination.
71
72 self.s_tag = tag
73 if None != self.get_used_s_c_tag_subscriber_id():
74 return self.generate_s_tag()
75 else:
76 return tag
77
Matteo Scandoloc348b3f2018-07-29 09:35:11 -070078
79 def generate_c_tag(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -070080 # NOTE this method will loop if available c_tags are ended
81 tag = random.randint(16, 4096)
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +053082
83 # Check that this random generated value is valid across given ONU-first check.
84 # If the value is valid it will assign c_tag wth it and do second check.
85 # If the value is not valid below function is recursively called till it gets a unique value.
86
Matteo Scandolocc94e902018-05-22 15:25:25 -070087 if tag in self.get_used_c_tags():
Matteo Scandoloc348b3f2018-07-29 09:35:11 -070088 return self.generate_c_tag()
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +053089 else:
90 self.c_tag = tag
91
92 # Scenario if we don't have a s_tag.
93 # Second check-verify that combination is unique across.
94
95 if not self.s_tag:
96 self.s_tag = self.generate_s_tag()
97 return tag
98 elif None != self.get_used_s_c_tag_subscriber_id():
99 return self.generate_c_tag()
100 else:
101 return tag
102
103 def get_same_onu_subscribers(self):
104 return RCORDSubscriber.objects.filter(onu_device=self.onu_device)
105
106 def get_same_s_c_tag_subscribers(self):
107 return RCORDSubscriber.objects.filter(c_tag=self.c_tag, s_tag=self.s_tag)
Matteo Scandolocc94e902018-05-22 15:25:25 -0700108
109 def get_used_c_tags(self):
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530110 same_onu_subscribers = self.get_same_onu_subscribers()
Matteo Scandoloa1875602018-10-16 07:35:04 -0700111 same_onu_subscribers = [s for s in same_onu_subscribers if s.id != self.id]
112 used_tags = [s.c_tag for s in same_onu_subscribers]
113 return used_tags
Matteo Scandolocc94e902018-05-22 15:25:25 -0700114
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530115 def get_used_s_c_tag_subscriber_id(self):
116 # Function to check c_tag and s_tag combination are unique across.
117 same_s_c_tag_subscribers = self.get_same_s_c_tag_subscribers()
118 same_s_c_tag_subscribers = [s for s in same_s_c_tag_subscribers if s.id != self.id]
119 if len(same_s_c_tag_subscribers) > 0:
120 return same_s_c_tag_subscribers[0].id
121 else:
122 return None
123
Matteo Scandolo62a83f02018-03-01 15:59:18 -0800124 def save(self, *args, **kwargs):
125 self.validate_unique_service_specific_id(none_okay=True)
126
Scott Baker9d9ddf62018-03-20 20:44:27 -0700127 # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its
128 # Instance.
129 if not self.creator:
130 # If we weren't passed an explicit creator, then we will assume the caller is the creator.
131 if not getattr(self, "caller", None):
Matteo Scandolod1707b32018-05-04 12:42:53 -0700132 raise XOSProgrammingError("RCORDSubscriber's self.caller was not set")
Scott Baker9d9ddf62018-03-20 20:44:27 -0700133 self.creator = self.caller
134
Matteo Scandolod1707b32018-05-04 12:42:53 -0700135 # validate MAC Address
Andy Bavier618f0ed2018-12-11 13:45:23 -0700136 if hasattr(self, 'mac_address') and self.mac_address:
Matteo Scandolod1707b32018-05-04 12:42:53 -0700137 if not re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", self.mac_address.lower()):
Luca Preted6700ba2018-09-12 16:40:49 -0700138 raise XOSValidationError("The MAC address specified is not valid: %s" % self.mac_address)
Matteo Scandolod1707b32018-05-04 12:42:53 -0700139
Matteo Scandolocc94e902018-05-22 15:25:25 -0700140 # validate c_tag
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530141 if self.c_tag:
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700142 is_update_with_same_tag = False
143
144 if not self.is_new:
145 # if it is an update, but the tag is the same, skip validation
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530146 existing = RCORDSubscriber.objects.filter(id=self.id)
Matteo Scandoloaa37b422018-05-23 15:36:56 -0700147
148 if len(existing) > 0 and existing[0].c_tag == self.c_tag and existing[0].id == self.id:
149 is_update_with_same_tag = True
150
151 if self.c_tag in self.get_used_c_tags() and not is_update_with_same_tag:
Matteo Scandolocc94e902018-05-22 15:25:25 -0700152 raise XOSValidationError("The c_tag you specified (%s) has already been used on device %s" % (self.c_tag, self.onu_device))
153
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530154 # validate s_tag and c_tag combination
155 if self.c_tag and self.s_tag:
156 is_update_with_same_tag = False
157
158 if not self.is_new:
159 # if it is an update, but the tags are the same, skip validation
160 existing = RCORDSubscriber.objects.filter(id=self.id)
161 if len(existing) > 0 and existing[0].c_tag == self.c_tag and existing[0].s_tag == self.s_tag and existing[0].id == self.id:
162 is_update_with_same_tag = True
163
164 id = self.get_used_s_c_tag_subscriber_id()
165 if None != id and not is_update_with_same_tag:
166 raise XOSValidationError("The c_tag(%s) and s_tag(%s) pair you specified,has already been used by Subscriber with Id (%s)" % (self.c_tag,self.s_tag,id))
167
168 if not self.c_tag:
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700169 self.c_tag = self.generate_c_tag()
170
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530171 elif not self.s_tag:
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700172 self.s_tag = self.generate_s_tag()
Matteo Scandolocc94e902018-05-22 15:25:25 -0700173
Matteo Scandolo520217f2018-05-16 14:15:56 -0700174 self.set_owner()
175
Matteo Scandolo10d94512019-03-04 15:54:43 -0800176 if self.status != "pre-provisioned" and hasattr(self.owner.leaf_model, "access") and self.owner.leaf_model.access == "voltha" and not self.deleted:
Matteo Scandolocc94e902018-05-22 15:25:25 -0700177
Matteo Scandolob6d67fd2018-05-18 16:28:51 -0700178 # if the access network is managed by voltha, validate that onu_device actually exist
Matteo Scandolod1707b32018-05-04 12:42:53 -0700179 volt_service = self.owner.provider_services[0].leaf_model # we assume RCORDService is connected only to the vOLTService
180
Matteo Scandolob6d67fd2018-05-18 16:28:51 -0700181 if not volt_service.has_access_device(self.onu_device):
182 raise XOSValidationError("The onu_device you specified (%s) does not exists" % self.onu_device)
Matteo Scandolod1707b32018-05-04 12:42:53 -0700183
184 super(RCORDSubscriber, self).save(*args, **kwargs)
Matteo Scandolo62a83f02018-03-01 15:59:18 -0800185 self.invalidate_related_objects()
HARDIK WINDLASSabe3a9c2019-03-18 16:17:28 +0530186 return