blob: 3b7b74e2b8986abec2ef866d3e590463b79d826f [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
Scott Bakerc2a633d2019-04-01 19:27:41 -070017from helpers import AttHelpers
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
Matteo Scandoloea529092018-09-11 16:36:39 -070026
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070027class DeferredException(Exception):
28 pass
29
Scott Bakerc2a633d2019-04-01 19:27:41 -070030
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):
Scott Bakerc2a633d2019-04-01 19:27:41 -070039 self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s " %
40 (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):
Scott Baker71d20472019-02-01 12:05:35 -080060 [valid, message] = AttHelpers.validate_onu(self.model_accessor, 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")
Scott Bakerc2a633d2019-04-01 19:27:41 -070069 else: # DISABLED
Andy Bavier7fdb12a2019-01-31 13:01:42 -070070 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
Andy Bavier11ffbf52019-02-08 11:53:21 -070076 # If the ONU has been disabled then we force re-authentication when it
77 # is re-enabled.
78 # Setting si.authentication_state = AWAITING:
79 # -> subscriber status = "awaiting_auth"
80 # -> service chain deleted
81 # -> need authentication to restore connectivity after ONU enabled
Andy Bavierafaf1762019-01-16 09:41:43 -070082 def process_auth_state(self, si):
83 auth_msgs = {
84 "AWAITING": " - Awaiting Authentication",
85 "REQUESTED": " - Authentication requested",
86 "STARTED": " - Authentication started",
87 "APPROVED": " - Authentication succeeded",
88 "DENIED": " - Authentication denied"
89 }
90 if si.onu_state == "DISABLED":
91 si.authentication_state = "AWAITING"
92 else:
93 si.status_message += auth_msgs[si.authentication_state]
94
Andy Bavier11ffbf52019-02-08 11:53:21 -070095 # The DhcpL2Relay ONOS app generates events that update the fields below.
96 # It only sends events when it processes DHCP packets. It keeps no internal state.
97 # We reset dhcp_state when:
98 # si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]
99 # -> subscriber status = "awaiting_auth"
100 # -> service chain not present
101 # -> subscriber's OLT flow rules, xconnect not present
102 # -> DHCP packets won't go through
103 # Note, however, that the DHCP state at the endpoints is not changed.
104 # A previously issued DHCP lease may still be valid.
Andy Bavierafaf1762019-01-16 09:41:43 -0700105 def process_dhcp_state(self, si):
Andy Bavier8ed30c92018-12-11 13:46:25 -0700106 if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]:
107 si.ip_address = ""
108 si.mac_address = ""
Andy Bavier15310cf2018-12-13 14:16:49 -0700109 si.dhcp_state = "AWAITING"
Andy Bavier8ed30c92018-12-11 13:46:25 -0700110
Andy Bavierafaf1762019-01-16 09:41:43 -0700111 # Make sure the object is in a legitimate state
112 # It should be after the above processing steps
113 # However this might still fail if an event has fired in the meantime
114 # Valid states:
115 # ONU | Auth | DHCP
116 # ===============================
117 # AWAITING | AWAITING | AWAITING
118 # ENABLED | * | AWAITING
119 # ENABLED | APPROVED | *
120 # DISABLED | AWAITING | AWAITING
121 def validate_states(self, si):
Scott Bakerc2a633d2019-04-01 19:27:41 -0700122 if (si.onu_state == "AWAITING" or si.onu_state ==
123 "DISABLED") and si.authentication_state == "AWAITING" and si.dhcp_state == "AWAITING":
Andy Bavierafaf1762019-01-16 09:41:43 -0700124 return
125 if si.onu_state == "ENABLED" and (si.authentication_state == "APPROVED" or si.dhcp_state == "AWAITING"):
126 return
Scott Bakerc2a633d2019-04-01 19:27:41 -0700127 self.logger.warning(
128 "MODEL_POLICY (validate_states): invalid state combination",
129 onu_state=si.onu_state,
130 auth_state=si.authentication_state,
131 dhcp_state=si.dhcp_state)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700132
Matteo Scandoloea529092018-09-11 16:36:39 -0700133 def update_onu(self, serial_number, admin_state):
Scott Bakerc2a633d2019-04-01 19:27:41 -0700134 onu = [onu for onu in self.model_accessor.ONUDevice.objects.all() if onu.serial_number.lower()
135 == serial_number.lower()][0]
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700136 if onu.admin_state == admin_state:
Scott Bakerc2a633d2019-04-01 19:27:41 -0700137 self.logger.debug(
138 "MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" %
139 (serial_number, admin_state))
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700140 else:
141 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
142 onu.admin_state = admin_state
Andy Bavier0d631eb2018-10-17 18:05:04 -0700143 onu.save_changed_fields(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700144
Matteo Scandoloea529092018-09-11 16:36:39 -0700145 def get_subscriber(self, serial_number):
146 try:
Scott Bakerc2a633d2019-04-01 19:27:41 -0700147 return [s for s in self.model_accessor.RCORDSubscriber.objects.all() if s.onu_device.lower()
148 == serial_number.lower()][0]
Matteo Scandoloea529092018-09-11 16:36:39 -0700149 except IndexError:
150 # If the subscriber doesn't exist we don't do anything
Scott Bakerc2a633d2019-04-01 19:27:41 -0700151 self.logger.debug(
152 "MODEL_POLICY: subscriber does not exists for this SI, doing nothing",
153 onu_device=serial_number)
Matteo Scandoloea529092018-09-11 16:36:39 -0700154 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -0700155
Matteo Scandolo74f63302018-11-01 14:05:01 -0700156 def update_subscriber_ip(self, subscriber, ip):
157 # TODO check if the subscriber has an IP and update it,
158 # or create a new one
159 try:
Scott Baker71d20472019-02-01 12:05:35 -0800160 ip = self.model_accessor.RCORDIpAddress.objects.filter(
Matteo Scandolo74f63302018-11-01 14:05:01 -0700161 subscriber_id=subscriber.id,
162 ip=ip
163 )[0]
Scott Bakerc2a633d2019-04-01 19:27:41 -0700164 self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber",
165 onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
Matteo Scandolo74f63302018-11-01 14:05:01 -0700166 ip.save_changed_fields()
167 except IndexError:
Scott Bakerc2a633d2019-04-01 19:27:41 -0700168 self.logger.debug(
169 "MODEL_POLICY: Creating new RCORDIpAddress for subscriber",
170 onu_device=subscriber.onu_device,
171 subscriber_status=subscriber.status,
172 ip=ip)
Scott Baker71d20472019-02-01 12:05:35 -0800173 ip = self.model_accessor.RCORDIpAddress(
Matteo Scandolo74f63302018-11-01 14:05:01 -0700174 subscriber_id=subscriber.id,
175 ip=ip,
176 description="DHCP Assigned IP Address"
177 )
178 ip.save()
179
Andy Bavier8ed30c92018-12-11 13:46:25 -0700180 def delete_subscriber_ip(self, subscriber, ip):
181 try:
Scott Baker71d20472019-02-01 12:05:35 -0800182 ip = self.model_accessor.RCORDIpAddress.objects.filter(
Andy Bavier8ed30c92018-12-11 13:46:25 -0700183 subscriber_id=subscriber.id,
184 ip=ip
185 )[0]
Scott Bakerc2a633d2019-04-01 19:27:41 -0700186 self.logger.debug(
187 "MODEL_POLICY: delete RCORDIpAddress for subscriber",
188 onu_device=subscriber.onu_device,
189 subscriber_status=subscriber.status,
190 ip=ip)
Andy Bavier8ed30c92018-12-11 13:46:25 -0700191 ip.delete()
Scott Bakerc2a633d2019-04-01 19:27:41 -0700192 except BaseException:
Andy Bavier8ed30c92018-12-11 13:46:25 -0700193 self.logger.warning("MODEL_POLICY: no RCORDIpAddress object found, cannot delete", ip=ip)
194
Matteo Scandoloea529092018-09-11 16:36:39 -0700195 def update_subscriber(self, subscriber, si):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700196 cur_status = subscriber.status
Andy Bavierafaf1762019-01-16 09:41:43 -0700197 # Don't change state if someone has disabled the subscriber
198 if subscriber.status != "disabled":
199 if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]:
200 subscriber.status = "awaiting-auth"
201 elif si.authentication_state == "APPROVED":
202 subscriber.status = "enabled"
203 elif si.authentication_state == "DENIED":
204 subscriber.status = "auth-failed"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700205
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700206 # NOTE we save the subscriber only if:
207 # - the status has changed
208 # - we get a DHCPACK event
209 if cur_status != subscriber.status or si.dhcp_state == "DHCPACK":
Scott Bakerc2a633d2019-04-01 19:27:41 -0700210 self.logger.debug(
211 "MODEL_POLICY: updating subscriber",
212 onu_device=subscriber.onu_device,
213 authentication_state=si.authentication_state,
214 subscriber_status=subscriber.status)
Andy Bavier8ed30c92018-12-11 13:46:25 -0700215 if subscriber.status == "awaiting-auth":
216 self.delete_subscriber_ip(subscriber, si.ip_address)
217 subscriber.mac_address = ""
218 elif si.ip_address and si.mac_address:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700219 self.update_subscriber_ip(subscriber, si.ip_address)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700220 subscriber.mac_address = si.mac_address
Andy Bavier0d631eb2018-10-17 18:05:04 -0700221 subscriber.save_changed_fields(always_update_timestamp=True)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700222 else:
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700223 self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device,
224 authentication_state=si.authentication_state, subscriber_status=subscriber.status)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700225
226 def handle_delete(self, si):
227 pass