blob: 3cc1f5966ba8f305b4c81f3dbe396f3519d7e178 [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -07001
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.
15
16
Matteo Scandoloea529092018-09-11 16:36:39 -070017
Matteo Scandolo74f63302018-11-01 14:05:01 -070018from synchronizers.new_base.modelaccessor import RCORDSubscriber, RCORDIpAddress, ONUDevice, model_accessor
Matteo Scandoload0c1752018-08-09 15:47:16 -070019from synchronizers.new_base.policy import Policy
20
Matteo Scandoloea529092018-09-11 16:36:39 -070021import os
22import sys
23
24sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
25sys.path.append(sync_path)
26
27from helpers import AttHelpers
28
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070029class DeferredException(Exception):
30 pass
31
Matteo Scandoload0c1752018-08-09 15:47:16 -070032class AttWorkflowDriverServiceInstancePolicy(Policy):
33 model_name = "AttWorkflowDriverServiceInstance"
34
35 def handle_create(self, si):
36 self.logger.debug("MODEL_POLICY: handle_create for AttWorkflowDriverServiceInstance %s " % si.id)
37 self.handle_update(si)
38
Matteo Scandoload0c1752018-08-09 15:47:16 -070039 def handle_update(self, si):
Matteo Scandolob8da43d2018-09-12 15:52:16 -070040 self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s " % (si.id), onu_state=si.onu_state, authentication_state=si.authentication_state)
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070041
Matteo Scandoloea529092018-09-11 16:36:39 -070042 # validating ONU
43 if si.onu_state == "AWAITING" or si.onu_state == "ENABLED":
44 # we validate the ONU state only if it is enabled or awaiting,
45 # if it's disabled it means someone has disabled it
46 self.validate_onu_state(si)
Matteo Scandolo744effb2018-10-02 14:36:19 -070047 else:
Matteo Scandoloc5c558f2018-11-02 10:33:40 -070048 # clean the status and verify that the ONU is actually disabled
Matteo Scandolode8cfa82018-10-16 13:49:05 -070049 si.status_message = "ONU has been disabled"
Matteo Scandoloc5c558f2018-11-02 10:33:40 -070050 self.update_onu(si.serial_number, "DISABLED")
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070051
Matteo Scandoloea529092018-09-11 16:36:39 -070052 # handling the subscriber status
53 subscriber = self.get_subscriber(si.serial_number)
Matteo Scandoload0c1752018-08-09 15:47:16 -070054
Matteo Scandoloea529092018-09-11 16:36:39 -070055 if subscriber:
56 self.update_subscriber(subscriber, si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070057
Andy Bavier8ed30c92018-12-11 13:46:25 -070058 if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]:
59 si.ip_address = ""
60 si.mac_address = ""
61 si.dhcp_state = ""
62
Andy Bavier0d631eb2018-10-17 18:05:04 -070063 si.save_changed_fields()
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070064
Matteo Scandoloea529092018-09-11 16:36:39 -070065 def validate_onu_state(self, si):
Scott Baker0b4ad932018-11-26 15:02:13 -080066 [valid, message] = AttHelpers.validate_onu(self.logger, si)
Matteo Scandolo200dab42018-09-12 13:52:09 -070067 si.status_message = message
Matteo Scandoloea529092018-09-11 16:36:39 -070068 if valid:
69 si.onu_state = "ENABLED"
70 self.update_onu(si.serial_number, "ENABLED")
71 else:
72 si.onu_state = "DISABLED"
73 self.update_onu(si.serial_number, "DISABLED")
Matteo Scandoload0c1752018-08-09 15:47:16 -070074
Matteo Scandoloea529092018-09-11 16:36:39 -070075 def update_onu(self, serial_number, admin_state):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -070076 onu = [onu for onu in ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0]
77 if onu.admin_state == admin_state:
78 self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state))
79 else:
80 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
81 onu.admin_state = admin_state
Andy Bavier0d631eb2018-10-17 18:05:04 -070082 onu.save_changed_fields(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -070083
Matteo Scandoloea529092018-09-11 16:36:39 -070084 def get_subscriber(self, serial_number):
85 try:
86 return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
87 except IndexError:
88 # If the subscriber doesn't exist we don't do anything
89 self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
90 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -070091
Matteo Scandolo74f63302018-11-01 14:05:01 -070092 def update_subscriber_ip(self, subscriber, ip):
93 # TODO check if the subscriber has an IP and update it,
94 # or create a new one
95 try:
96 ip = RCORDIpAddress.objects.filter(
97 subscriber_id=subscriber.id,
98 ip=ip
99 )[0]
100 self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
101 ip.save_changed_fields()
102 except IndexError:
103 self.logger.debug("MODEL_POLICY: Creating new RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
104 ip = RCORDIpAddress(
105 subscriber_id=subscriber.id,
106 ip=ip,
107 description="DHCP Assigned IP Address"
108 )
109 ip.save()
110
Andy Bavier8ed30c92018-12-11 13:46:25 -0700111 def delete_subscriber_ip(self, subscriber, ip):
112 try:
113 ip = RCORDIpAddress.objects.filter(
114 subscriber_id=subscriber.id,
115 ip=ip
116 )[0]
117 self.logger.debug("MODEL_POLICY: delete RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
118 ip.delete()
119 except:
120 self.logger.warning("MODEL_POLICY: no RCORDIpAddress object found, cannot delete", ip=ip)
121
Matteo Scandoloea529092018-09-11 16:36:39 -0700122 def update_subscriber(self, subscriber, si):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700123 cur_status = subscriber.status
Matteo Scandoloea529092018-09-11 16:36:39 -0700124 if si.authentication_state == "AWAITING":
125 subscriber.status = "awaiting-auth"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700126 si.status_message += " - Awaiting Authentication"
Matteo Scandoloea529092018-09-11 16:36:39 -0700127 elif si.authentication_state == "REQUESTED":
128 subscriber.status = "awaiting-auth"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700129 si.status_message += " - Authentication requested"
Matteo Scandoloea529092018-09-11 16:36:39 -0700130 elif si.authentication_state == "STARTED":
131 subscriber.status = "awaiting-auth"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700132 si.status_message += " - Authentication started"
Matteo Scandoloea529092018-09-11 16:36:39 -0700133 elif si.authentication_state == "APPROVED":
134 subscriber.status = "enabled"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700135 si.status_message += " - Authentication succeded"
Matteo Scandoloea529092018-09-11 16:36:39 -0700136 elif si.authentication_state == "DENIED":
137 subscriber.status = "auth-failed"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700138 si.status_message += " - Authentication denied"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700139
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700140 # NOTE we save the subscriber only if:
141 # - the status has changed
142 # - we get a DHCPACK event
143 if cur_status != subscriber.status or si.dhcp_state == "DHCPACK":
144 self.logger.debug("MODEL_POLICY: updating subscriber", onu_device=subscriber.onu_device, authentication_state=si.authentication_state, subscriber_status=subscriber.status)
Andy Bavier8ed30c92018-12-11 13:46:25 -0700145 if subscriber.status == "awaiting-auth":
146 self.delete_subscriber_ip(subscriber, si.ip_address)
147 subscriber.mac_address = ""
148 elif si.ip_address and si.mac_address:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700149 self.update_subscriber_ip(subscriber, si.ip_address)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700150 subscriber.mac_address = si.mac_address
Andy Bavier0d631eb2018-10-17 18:05:04 -0700151 subscriber.save_changed_fields(always_update_timestamp=True)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700152 else:
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700153 self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device,
154 authentication_state=si.authentication_state, subscriber_status=subscriber.status)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700155
156 def handle_delete(self, si):
157 pass