blob: 67e25c4b3393ca4710608cfe68387599490a08ef [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
Andy Bavierafaf1762019-01-16 09:41:43 -070042 # Changing ONU state can change auth state
43 # Changing auth state can change DHCP state
44 # So need to process in this order
45 self.process_onu_state(si)
46 self.process_auth_state(si)
47 self.process_dhcp_state(si)
48
49 self.validate_states(si)
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070050
Matteo Scandoloea529092018-09-11 16:36:39 -070051 # handling the subscriber status
Andy Bavierafaf1762019-01-16 09:41:43 -070052 # It's a combination of all the other states
Matteo Scandoloea529092018-09-11 16:36:39 -070053 subscriber = self.get_subscriber(si.serial_number)
Matteo Scandoloea529092018-09-11 16:36:39 -070054 if subscriber:
55 self.update_subscriber(subscriber, si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070056
Andy Bavierafaf1762019-01-16 09:41:43 -070057 si.save_changed_fields()
58
59 def process_onu_state(self, si):
Andy Bavier7fdb12a2019-01-31 13:01:42 -070060 [valid, message] = AttHelpers.validate_onu(self.logger, si)
Andy Bavierafaf1762019-01-16 09:41:43 -070061 if si.onu_state == "AWAITING" or si.onu_state == "ENABLED":
Andy Bavierafaf1762019-01-16 09:41:43 -070062 si.status_message = message
63 if valid:
64 si.onu_state = "ENABLED"
65 self.update_onu(si.serial_number, "ENABLED")
66 else:
67 si.onu_state = "DISABLED"
68 self.update_onu(si.serial_number, "DISABLED")
Andy Bavier7fdb12a2019-01-31 13:01:42 -070069 else: # DISABLED
70 if not valid:
71 si.status_message = message
72 else:
73 si.status_message = "ONU has been disabled"
Andy Bavierafaf1762019-01-16 09:41:43 -070074 self.update_onu(si.serial_number, "DISABLED")
75
76 def process_auth_state(self, si):
77 auth_msgs = {
78 "AWAITING": " - Awaiting Authentication",
79 "REQUESTED": " - Authentication requested",
80 "STARTED": " - Authentication started",
81 "APPROVED": " - Authentication succeeded",
82 "DENIED": " - Authentication denied"
83 }
84 if si.onu_state == "DISABLED":
85 si.authentication_state = "AWAITING"
86 else:
87 si.status_message += auth_msgs[si.authentication_state]
88
89 def process_dhcp_state(self, si):
Andy Bavier8ed30c92018-12-11 13:46:25 -070090 if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]:
91 si.ip_address = ""
92 si.mac_address = ""
Andy Bavier15310cf2018-12-13 14:16:49 -070093 si.dhcp_state = "AWAITING"
Andy Bavier8ed30c92018-12-11 13:46:25 -070094
Andy Bavierafaf1762019-01-16 09:41:43 -070095 # Make sure the object is in a legitimate state
96 # It should be after the above processing steps
97 # However this might still fail if an event has fired in the meantime
98 # Valid states:
99 # ONU | Auth | DHCP
100 # ===============================
101 # AWAITING | AWAITING | AWAITING
102 # ENABLED | * | AWAITING
103 # ENABLED | APPROVED | *
104 # DISABLED | AWAITING | AWAITING
105 def validate_states(self, si):
106 if (si.onu_state == "AWAITING" or si.onu_state == "DISABLED") and si.authentication_state == "AWAITING" and si.dhcp_state == "AWAITING":
107 return
108 if si.onu_state == "ENABLED" and (si.authentication_state == "APPROVED" or si.dhcp_state == "AWAITING"):
109 return
110 self.logger.warning("MODEL_POLICY (validate_states): invalid state combination", onu_state=si.onu_state, auth_state=si.authentication_state, dhcp_state=si.dhcp_state)
Matteo Scandoloe8c33d62018-08-16 14:37:24 -0700111
Matteo Scandoload0c1752018-08-09 15:47:16 -0700112
Matteo Scandoloea529092018-09-11 16:36:39 -0700113 def update_onu(self, serial_number, admin_state):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700114 onu = [onu for onu in ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0]
115 if onu.admin_state == admin_state:
116 self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state))
117 else:
118 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
119 onu.admin_state = admin_state
Andy Bavier0d631eb2018-10-17 18:05:04 -0700120 onu.save_changed_fields(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700121
Matteo Scandoloea529092018-09-11 16:36:39 -0700122 def get_subscriber(self, serial_number):
123 try:
124 return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
125 except IndexError:
126 # If the subscriber doesn't exist we don't do anything
127 self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
128 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -0700129
Matteo Scandolo74f63302018-11-01 14:05:01 -0700130 def update_subscriber_ip(self, subscriber, ip):
131 # TODO check if the subscriber has an IP and update it,
132 # or create a new one
133 try:
134 ip = RCORDIpAddress.objects.filter(
135 subscriber_id=subscriber.id,
136 ip=ip
137 )[0]
138 self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
139 ip.save_changed_fields()
140 except IndexError:
141 self.logger.debug("MODEL_POLICY: Creating new RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
142 ip = RCORDIpAddress(
143 subscriber_id=subscriber.id,
144 ip=ip,
145 description="DHCP Assigned IP Address"
146 )
147 ip.save()
148
Andy Bavier8ed30c92018-12-11 13:46:25 -0700149 def delete_subscriber_ip(self, subscriber, ip):
150 try:
151 ip = RCORDIpAddress.objects.filter(
152 subscriber_id=subscriber.id,
153 ip=ip
154 )[0]
155 self.logger.debug("MODEL_POLICY: delete RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
156 ip.delete()
157 except:
158 self.logger.warning("MODEL_POLICY: no RCORDIpAddress object found, cannot delete", ip=ip)
159
Matteo Scandoloea529092018-09-11 16:36:39 -0700160 def update_subscriber(self, subscriber, si):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700161 cur_status = subscriber.status
Andy Bavierafaf1762019-01-16 09:41:43 -0700162 # Don't change state if someone has disabled the subscriber
163 if subscriber.status != "disabled":
164 if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]:
165 subscriber.status = "awaiting-auth"
166 elif si.authentication_state == "APPROVED":
167 subscriber.status = "enabled"
168 elif si.authentication_state == "DENIED":
169 subscriber.status = "auth-failed"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700170
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700171 # NOTE we save the subscriber only if:
172 # - the status has changed
173 # - we get a DHCPACK event
174 if cur_status != subscriber.status or si.dhcp_state == "DHCPACK":
175 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 -0700176 if subscriber.status == "awaiting-auth":
177 self.delete_subscriber_ip(subscriber, si.ip_address)
178 subscriber.mac_address = ""
179 elif si.ip_address and si.mac_address:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700180 self.update_subscriber_ip(subscriber, si.ip_address)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700181 subscriber.mac_address = si.mac_address
Andy Bavier0d631eb2018-10-17 18:05:04 -0700182 subscriber.save_changed_fields(always_update_timestamp=True)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700183 else:
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700184 self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device,
185 authentication_state=si.authentication_state, subscriber_status=subscriber.status)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700186
187 def handle_delete(self, si):
188 pass