blob: 9713ab4539c3ba60d7bfe3f03bcf7e24d45c5258 [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
Luca Preted6700ba2018-09-12 16:40:49 -070020from models_decl import RCORDService_decl, RCORDSubscriber_decl, RCORDIpAddress_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
Luca Preted6700ba2018-09-12 16:40:49 -070026class RCORDIpAddress(RCORDIpAddress_decl):
27 class Meta:
28 proxy = True
29
30 def save(self, *args, **kwargs):
31 try:
32 if ":" in self.ip:
33 # it's an IPv6 address
34 socket.inet_pton(socket.AF_INET6, self.ip)
35 else:
36 # it's an IPv4 address
37 socket.inet_pton(socket.AF_INET, self.ip)
38 except socket.error:
39 raise XOSValidationError("The IP specified is not valid: %s" % self.ip)
40 super(RCORDIpAddress, self).save(*args, **kwargs)
41 return
42
Matteo Scandolod1707b32018-05-04 12:42:53 -070043class RCORDSubscriber(RCORDSubscriber_decl):
Matteo Scandolo520217f2018-05-16 14:15:56 -070044
Matteo Scandolo62a83f02018-03-01 15:59:18 -080045 class Meta:
46 proxy = True
47
48 def invalidate_related_objects(self):
49 # Dirty all vSGs related to this subscriber, so the vSG synchronizer
50 # will run.
51
52 # FIXME: This should be reimplemented when multiple-objects-per-synchronizer is implemented.
53
54 for link in self.subscribed_links.all():
55 outer_service_instance = link.provider_service_instance
Matteo Scandoloa1875602018-10-16 07:35:04 -070056 # TODO: We may need to invalidate the vOLT too...
Matteo Scandolo62a83f02018-03-01 15:59:18 -080057 for link in outer_service_instance.subscribed_links.all():
58 inner_service_instance = link.provider_service_instance
59 inner_service_instance.save(update_fields=["updated"])
60
Matteo Scandoloc348b3f2018-07-29 09:35:11 -070061 def generate_s_tag(self):
62 # NOTE what's the right way to generate an s_tag?
63 tag = random.randint(16, 4096)
64 return tag
65
66 def generate_c_tag(self):
Matteo Scandolocc94e902018-05-22 15:25:25 -070067 # NOTE this method will loop if available c_tags are ended
68 tag = random.randint(16, 4096)
69 if tag in self.get_used_c_tags():
Matteo Scandoloc348b3f2018-07-29 09:35:11 -070070 return self.generate_c_tag()
Matteo Scandolocc94e902018-05-22 15:25:25 -070071 return tag
72
73 def get_used_c_tags(self):
Matteo Scandoloa1875602018-10-16 07:35:04 -070074 # TODO add validation for no duplicate c_tag on the same s_tag
Matteo Scandolocc94e902018-05-22 15:25:25 -070075 same_onu_subscribers = RCORDSubscriber.objects.filter(onu_device=self.onu_device)
Matteo Scandoloa1875602018-10-16 07:35:04 -070076 same_onu_subscribers = [s for s in same_onu_subscribers if s.id != self.id]
77 used_tags = [s.c_tag for s in same_onu_subscribers]
78 return used_tags
Matteo Scandolocc94e902018-05-22 15:25:25 -070079
Matteo Scandolo62a83f02018-03-01 15:59:18 -080080 def save(self, *args, **kwargs):
81 self.validate_unique_service_specific_id(none_okay=True)
82
Scott Baker9d9ddf62018-03-20 20:44:27 -070083 # VSGServiceInstance will extract the creator from the Subscriber, as it needs a creator to create its
84 # Instance.
85 if not self.creator:
86 # If we weren't passed an explicit creator, then we will assume the caller is the creator.
87 if not getattr(self, "caller", None):
Matteo Scandolod1707b32018-05-04 12:42:53 -070088 raise XOSProgrammingError("RCORDSubscriber's self.caller was not set")
Scott Baker9d9ddf62018-03-20 20:44:27 -070089 self.creator = self.caller
90
Matteo Scandolod1707b32018-05-04 12:42:53 -070091 # validate MAC Address
Andy Bavier618f0ed2018-12-11 13:45:23 -070092 if hasattr(self, 'mac_address') and self.mac_address:
Matteo Scandolod1707b32018-05-04 12:42:53 -070093 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 -070094 raise XOSValidationError("The MAC address specified is not valid: %s" % self.mac_address)
Matteo Scandolod1707b32018-05-04 12:42:53 -070095
Matteo Scandolocc94e902018-05-22 15:25:25 -070096 # validate c_tag
Andy Bavier618f0ed2018-12-11 13:45:23 -070097 if hasattr(self, 'c_tag') and self.c_tag:
Matteo Scandoloaa37b422018-05-23 15:36:56 -070098 is_update_with_same_tag = False
99
100 if not self.is_new:
101 # if it is an update, but the tag is the same, skip validation
102 existing = RCORDSubscriber.objects.filter(c_tag=self.c_tag)
103
104 if len(existing) > 0 and existing[0].c_tag == self.c_tag and existing[0].id == self.id:
105 is_update_with_same_tag = True
106
107 if self.c_tag in self.get_used_c_tags() and not is_update_with_same_tag:
Matteo Scandolocc94e902018-05-22 15:25:25 -0700108 raise XOSValidationError("The c_tag you specified (%s) has already been used on device %s" % (self.c_tag, self.onu_device))
109
Andy Bavier618f0ed2018-12-11 13:45:23 -0700110 if not hasattr(self, "c_tag") or not self.c_tag:
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700111 self.c_tag = self.generate_c_tag()
112
Andy Bavier618f0ed2018-12-11 13:45:23 -0700113 if not hasattr(self, "s_tag") or not self.s_tag:
Matteo Scandoloc348b3f2018-07-29 09:35:11 -0700114 self.s_tag = self.generate_s_tag()
Matteo Scandolocc94e902018-05-22 15:25:25 -0700115
Matteo Scandolo520217f2018-05-16 14:15:56 -0700116 self.set_owner()
117
Matteo Scandolo3ecc1672018-06-25 16:58:12 -0700118 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 -0700119
Matteo Scandolob6d67fd2018-05-18 16:28:51 -0700120 # if the access network is managed by voltha, validate that onu_device actually exist
Matteo Scandolod1707b32018-05-04 12:42:53 -0700121 volt_service = self.owner.provider_services[0].leaf_model # we assume RCORDService is connected only to the vOLTService
122
Matteo Scandolob6d67fd2018-05-18 16:28:51 -0700123 if not volt_service.has_access_device(self.onu_device):
124 raise XOSValidationError("The onu_device you specified (%s) does not exists" % self.onu_device)
Matteo Scandolod1707b32018-05-04 12:42:53 -0700125
126 super(RCORDSubscriber, self).save(*args, **kwargs)
Matteo Scandolo62a83f02018-03-01 15:59:18 -0800127 self.invalidate_related_objects()
128 return