blob: 8e591cb87191924a88edc6195fd0ea12b8d1efee [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
75 def process_auth_state(self, si):
76 auth_msgs = {
77 "AWAITING": " - Awaiting Authentication",
78 "REQUESTED": " - Authentication requested",
79 "STARTED": " - Authentication started",
80 "APPROVED": " - Authentication succeeded",
81 "DENIED": " - Authentication denied"
82 }
83 if si.onu_state == "DISABLED":
84 si.authentication_state = "AWAITING"
85 else:
86 si.status_message += auth_msgs[si.authentication_state]
87
88 def process_dhcp_state(self, si):
Andy Bavier8ed30c92018-12-11 13:46:25 -070089 if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]:
90 si.ip_address = ""
91 si.mac_address = ""
Andy Bavier15310cf2018-12-13 14:16:49 -070092 si.dhcp_state = "AWAITING"
Andy Bavier8ed30c92018-12-11 13:46:25 -070093
Andy Bavierafaf1762019-01-16 09:41:43 -070094 # Make sure the object is in a legitimate state
95 # It should be after the above processing steps
96 # However this might still fail if an event has fired in the meantime
97 # Valid states:
98 # ONU | Auth | DHCP
99 # ===============================
100 # AWAITING | AWAITING | AWAITING
101 # ENABLED | * | AWAITING
102 # ENABLED | APPROVED | *
103 # DISABLED | AWAITING | AWAITING
104 def validate_states(self, si):
105 if (si.onu_state == "AWAITING" or si.onu_state == "DISABLED") and si.authentication_state == "AWAITING" and si.dhcp_state == "AWAITING":
106 return
107 if si.onu_state == "ENABLED" and (si.authentication_state == "APPROVED" or si.dhcp_state == "AWAITING"):
108 return
109 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 -0700110
Matteo Scandoload0c1752018-08-09 15:47:16 -0700111
Matteo Scandoloea529092018-09-11 16:36:39 -0700112 def update_onu(self, serial_number, admin_state):
Scott Baker71d20472019-02-01 12:05:35 -0800113 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 -0700114 if onu.admin_state == admin_state:
115 self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state))
116 else:
117 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
118 onu.admin_state = admin_state
Andy Bavier0d631eb2018-10-17 18:05:04 -0700119 onu.save_changed_fields(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700120
Matteo Scandoloea529092018-09-11 16:36:39 -0700121 def get_subscriber(self, serial_number):
122 try:
Scott Baker71d20472019-02-01 12:05:35 -0800123 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 -0700124 except IndexError:
125 # If the subscriber doesn't exist we don't do anything
126 self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
127 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -0700128
Matteo Scandolo74f63302018-11-01 14:05:01 -0700129 def update_subscriber_ip(self, subscriber, ip):
130 # TODO check if the subscriber has an IP and update it,
131 # or create a new one
132 try:
Scott Baker71d20472019-02-01 12:05:35 -0800133 ip = self.model_accessor.RCORDIpAddress.objects.filter(
Matteo Scandolo74f63302018-11-01 14:05:01 -0700134 subscriber_id=subscriber.id,
135 ip=ip
136 )[0]
137 self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
138 ip.save_changed_fields()
139 except IndexError:
140 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 -0800141 ip = self.model_accessor.RCORDIpAddress(
Matteo Scandolo74f63302018-11-01 14:05:01 -0700142 subscriber_id=subscriber.id,
143 ip=ip,
144 description="DHCP Assigned IP Address"
145 )
146 ip.save()
147
Andy Bavier8ed30c92018-12-11 13:46:25 -0700148 def delete_subscriber_ip(self, subscriber, ip):
149 try:
Scott Baker71d20472019-02-01 12:05:35 -0800150 ip = self.model_accessor.RCORDIpAddress.objects.filter(
Andy Bavier8ed30c92018-12-11 13:46:25 -0700151 subscriber_id=subscriber.id,
152 ip=ip
153 )[0]
154 self.logger.debug("MODEL_POLICY: delete RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
155 ip.delete()
156 except:
157 self.logger.warning("MODEL_POLICY: no RCORDIpAddress object found, cannot delete", ip=ip)
158
Matteo Scandoloea529092018-09-11 16:36:39 -0700159 def update_subscriber(self, subscriber, si):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700160 cur_status = subscriber.status
Andy Bavierafaf1762019-01-16 09:41:43 -0700161 # Don't change state if someone has disabled the subscriber
162 if subscriber.status != "disabled":
163 if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]:
164 subscriber.status = "awaiting-auth"
165 elif si.authentication_state == "APPROVED":
166 subscriber.status = "enabled"
167 elif si.authentication_state == "DENIED":
168 subscriber.status = "auth-failed"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700169
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700170 # NOTE we save the subscriber only if:
171 # - the status has changed
172 # - we get a DHCPACK event
173 if cur_status != subscriber.status or si.dhcp_state == "DHCPACK":
174 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 -0700175 if subscriber.status == "awaiting-auth":
176 self.delete_subscriber_ip(subscriber, si.ip_address)
177 subscriber.mac_address = ""
178 elif si.ip_address and si.mac_address:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700179 self.update_subscriber_ip(subscriber, si.ip_address)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700180 subscriber.mac_address = si.mac_address
Andy Bavier0d631eb2018-10-17 18:05:04 -0700181 subscriber.save_changed_fields(always_update_timestamp=True)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700182 else:
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700183 self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device,
184 authentication_state=si.authentication_state, subscriber_status=subscriber.status)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700185
186 def handle_delete(self, si):
187 pass