blob: 51728301e505ca0aa55959f595f7fee1e649d2f1 [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
Scott Baker71d20472019-02-01 12:05:35 -080018from xossynchronizer.model_policies.policy import Policy
Matteo Scandoload0c1752018-08-09 15:47:16 -070019
Matteo Scandoloea529092018-09-11 16:36:39 -070020import os
21import sys
22
23sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
24sys.path.append(sync_path)
25
26from helpers import AttHelpers
27
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070028class DeferredException(Exception):
29 pass
30
Matteo Scandoload0c1752018-08-09 15:47:16 -070031class AttWorkflowDriverServiceInstancePolicy(Policy):
32 model_name = "AttWorkflowDriverServiceInstance"
33
34 def handle_create(self, si):
35 self.logger.debug("MODEL_POLICY: handle_create for AttWorkflowDriverServiceInstance %s " % si.id)
36 self.handle_update(si)
37
Matteo Scandoload0c1752018-08-09 15:47:16 -070038 def handle_update(self, si):
Matteo Scandolob8da43d2018-09-12 15:52:16 -070039 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 -070040
Andy Bavierafaf1762019-01-16 09:41:43 -070041 # Changing ONU state can change auth state
42 # Changing auth state can change DHCP state
43 # So need to process in this order
44 self.process_onu_state(si)
45 self.process_auth_state(si)
46 self.process_dhcp_state(si)
47
48 self.validate_states(si)
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070049
Matteo Scandoloea529092018-09-11 16:36:39 -070050 # handling the subscriber status
Andy Bavierafaf1762019-01-16 09:41:43 -070051 # It's a combination of all the other states
Matteo Scandoloea529092018-09-11 16:36:39 -070052 subscriber = self.get_subscriber(si.serial_number)
Matteo Scandoloea529092018-09-11 16:36:39 -070053 if subscriber:
54 self.update_subscriber(subscriber, si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070055
Andy Bavierafaf1762019-01-16 09:41:43 -070056 si.save_changed_fields()
57
58 def process_onu_state(self, si):
Scott Baker71d20472019-02-01 12:05:35 -080059 [valid, message] = AttHelpers.validate_onu(self.model_accessor, self.logger, si)
Andy Bavierafaf1762019-01-16 09:41:43 -070060 if si.onu_state == "AWAITING" or si.onu_state == "ENABLED":
Andy Bavierafaf1762019-01-16 09:41:43 -070061 si.status_message = message
62 if valid:
63 si.onu_state = "ENABLED"
64 self.update_onu(si.serial_number, "ENABLED")
65 else:
66 si.onu_state = "DISABLED"
67 self.update_onu(si.serial_number, "DISABLED")
Andy Bavier7fdb12a2019-01-31 13:01:42 -070068 else: # DISABLED
69 if not valid:
70 si.status_message = message
71 else:
72 si.status_message = "ONU has been disabled"
Andy Bavierafaf1762019-01-16 09:41:43 -070073 self.update_onu(si.serial_number, "DISABLED")
74
Andy Bavier11ffbf52019-02-08 11:53:21 -070075 # If the ONU has been disabled then we force re-authentication when it
76 # is re-enabled.
77 # Setting si.authentication_state = AWAITING:
78 # -> subscriber status = "awaiting_auth"
79 # -> service chain deleted
80 # -> need authentication to restore connectivity after ONU enabled
Andy Bavierafaf1762019-01-16 09:41:43 -070081 def process_auth_state(self, si):
82 auth_msgs = {
83 "AWAITING": " - Awaiting Authentication",
84 "REQUESTED": " - Authentication requested",
85 "STARTED": " - Authentication started",
86 "APPROVED": " - Authentication succeeded",
87 "DENIED": " - Authentication denied"
88 }
89 if si.onu_state == "DISABLED":
90 si.authentication_state = "AWAITING"
91 else:
92 si.status_message += auth_msgs[si.authentication_state]
93
Andy Bavier11ffbf52019-02-08 11:53:21 -070094 # The DhcpL2Relay ONOS app generates events that update the fields below.
95 # It only sends events when it processes DHCP packets. It keeps no internal state.
96 # We reset dhcp_state when:
97 # si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]
98 # -> subscriber status = "awaiting_auth"
99 # -> service chain not present
100 # -> subscriber's OLT flow rules, xconnect not present
101 # -> DHCP packets won't go through
102 # Note, however, that the DHCP state at the endpoints is not changed.
103 # A previously issued DHCP lease may still be valid.
Andy Bavierafaf1762019-01-16 09:41:43 -0700104 def process_dhcp_state(self, si):
Andy Bavier8ed30c92018-12-11 13:46:25 -0700105 if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]:
106 si.ip_address = ""
107 si.mac_address = ""
Andy Bavier15310cf2018-12-13 14:16:49 -0700108 si.dhcp_state = "AWAITING"
Andy Bavier8ed30c92018-12-11 13:46:25 -0700109
Andy Bavierafaf1762019-01-16 09:41:43 -0700110 # Make sure the object is in a legitimate state
111 # It should be after the above processing steps
112 # However this might still fail if an event has fired in the meantime
113 # Valid states:
114 # ONU | Auth | DHCP
115 # ===============================
116 # AWAITING | AWAITING | AWAITING
117 # ENABLED | * | AWAITING
118 # ENABLED | APPROVED | *
119 # DISABLED | AWAITING | AWAITING
120 def validate_states(self, si):
121 if (si.onu_state == "AWAITING" or si.onu_state == "DISABLED") and si.authentication_state == "AWAITING" and si.dhcp_state == "AWAITING":
122 return
123 if si.onu_state == "ENABLED" and (si.authentication_state == "APPROVED" or si.dhcp_state == "AWAITING"):
124 return
125 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 -0700126
Matteo Scandoload0c1752018-08-09 15:47:16 -0700127
Matteo Scandoloea529092018-09-11 16:36:39 -0700128 def update_onu(self, serial_number, admin_state):
Scott Baker71d20472019-02-01 12:05:35 -0800129 onu = [onu for onu in self.model_accessor.ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0]
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700130 if onu.admin_state == admin_state:
131 self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state))
132 else:
133 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
134 onu.admin_state = admin_state
Andy Bavier0d631eb2018-10-17 18:05:04 -0700135 onu.save_changed_fields(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700136
Matteo Scandoloea529092018-09-11 16:36:39 -0700137 def get_subscriber(self, serial_number):
138 try:
Scott Baker71d20472019-02-01 12:05:35 -0800139 return [s for s in self.model_accessor.RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
Matteo Scandoloea529092018-09-11 16:36:39 -0700140 except IndexError:
141 # If the subscriber doesn't exist we don't do anything
142 self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
143 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -0700144
Matteo Scandolo74f63302018-11-01 14:05:01 -0700145 def update_subscriber_ip(self, subscriber, ip):
146 # TODO check if the subscriber has an IP and update it,
147 # or create a new one
148 try:
Scott Baker71d20472019-02-01 12:05:35 -0800149 ip = self.model_accessor.RCORDIpAddress.objects.filter(
Matteo Scandolo74f63302018-11-01 14:05:01 -0700150 subscriber_id=subscriber.id,
151 ip=ip
152 )[0]
153 self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
154 ip.save_changed_fields()
155 except IndexError:
156 self.logger.debug("MODEL_POLICY: Creating new RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
Scott Baker71d20472019-02-01 12:05:35 -0800157 ip = self.model_accessor.RCORDIpAddress(
Matteo Scandolo74f63302018-11-01 14:05:01 -0700158 subscriber_id=subscriber.id,
159 ip=ip,
160 description="DHCP Assigned IP Address"
161 )
162 ip.save()
163
Andy Bavier8ed30c92018-12-11 13:46:25 -0700164 def delete_subscriber_ip(self, subscriber, ip):
165 try:
Scott Baker71d20472019-02-01 12:05:35 -0800166 ip = self.model_accessor.RCORDIpAddress.objects.filter(
Andy Bavier8ed30c92018-12-11 13:46:25 -0700167 subscriber_id=subscriber.id,
168 ip=ip
169 )[0]
170 self.logger.debug("MODEL_POLICY: delete RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
171 ip.delete()
172 except:
173 self.logger.warning("MODEL_POLICY: no RCORDIpAddress object found, cannot delete", ip=ip)
174
Matteo Scandoloea529092018-09-11 16:36:39 -0700175 def update_subscriber(self, subscriber, si):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700176 cur_status = subscriber.status
Andy Bavierafaf1762019-01-16 09:41:43 -0700177 # Don't change state if someone has disabled the subscriber
178 if subscriber.status != "disabled":
179 if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]:
180 subscriber.status = "awaiting-auth"
181 elif si.authentication_state == "APPROVED":
182 subscriber.status = "enabled"
183 elif si.authentication_state == "DENIED":
184 subscriber.status = "auth-failed"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700185
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700186 # NOTE we save the subscriber only if:
187 # - the status has changed
188 # - we get a DHCPACK event
189 if cur_status != subscriber.status or si.dhcp_state == "DHCPACK":
190 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 -0700191 if subscriber.status == "awaiting-auth":
192 self.delete_subscriber_ip(subscriber, si.ip_address)
193 subscriber.mac_address = ""
194 elif si.ip_address and si.mac_address:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700195 self.update_subscriber_ip(subscriber, si.ip_address)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700196 subscriber.mac_address = si.mac_address
Andy Bavier0d631eb2018-10-17 18:05:04 -0700197 subscriber.save_changed_fields(always_update_timestamp=True)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700198 else:
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700199 self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device,
200 authentication_state=si.authentication_state, subscriber_status=subscriber.status)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700201
202 def handle_delete(self, si):
203 pass