Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 17 | |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 18 | from synchronizers.new_base.modelaccessor import RCORDSubscriber, RCORDIpAddress, ONUDevice, model_accessor |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 19 | from synchronizers.new_base.policy import Policy |
| 20 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 21 | import os |
| 22 | import sys |
| 23 | |
| 24 | sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) |
| 25 | sys.path.append(sync_path) |
| 26 | |
| 27 | from helpers import AttHelpers |
| 28 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 29 | class DeferredException(Exception): |
| 30 | pass |
| 31 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 32 | class 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 Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 39 | def handle_update(self, si): |
Matteo Scandolo | b8da43d | 2018-09-12 15:52:16 -0700 | [diff] [blame] | 40 | self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s " % (si.id), onu_state=si.onu_state, authentication_state=si.authentication_state) |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 41 | |
Andy Bavier | afaf176 | 2019-01-16 09:41:43 -0700 | [diff] [blame] | 42 | # 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 Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 50 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 51 | # handling the subscriber status |
Andy Bavier | afaf176 | 2019-01-16 09:41:43 -0700 | [diff] [blame] | 52 | # It's a combination of all the other states |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 53 | subscriber = self.get_subscriber(si.serial_number) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 54 | if subscriber: |
| 55 | self.update_subscriber(subscriber, si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 56 | |
Andy Bavier | afaf176 | 2019-01-16 09:41:43 -0700 | [diff] [blame] | 57 | si.save_changed_fields() |
| 58 | |
| 59 | def process_onu_state(self, si): |
Andy Bavier | 7fdb12a | 2019-01-31 13:01:42 -0700 | [diff] [blame^] | 60 | [valid, message] = AttHelpers.validate_onu(self.logger, si) |
Andy Bavier | afaf176 | 2019-01-16 09:41:43 -0700 | [diff] [blame] | 61 | if si.onu_state == "AWAITING" or si.onu_state == "ENABLED": |
Andy Bavier | afaf176 | 2019-01-16 09:41:43 -0700 | [diff] [blame] | 62 | 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 Bavier | 7fdb12a | 2019-01-31 13:01:42 -0700 | [diff] [blame^] | 69 | else: # DISABLED |
| 70 | if not valid: |
| 71 | si.status_message = message |
| 72 | else: |
| 73 | si.status_message = "ONU has been disabled" |
Andy Bavier | afaf176 | 2019-01-16 09:41:43 -0700 | [diff] [blame] | 74 | 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 Bavier | 8ed30c9 | 2018-12-11 13:46:25 -0700 | [diff] [blame] | 90 | if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]: |
| 91 | si.ip_address = "" |
| 92 | si.mac_address = "" |
Andy Bavier | 15310cf | 2018-12-13 14:16:49 -0700 | [diff] [blame] | 93 | si.dhcp_state = "AWAITING" |
Andy Bavier | 8ed30c9 | 2018-12-11 13:46:25 -0700 | [diff] [blame] | 94 | |
Andy Bavier | afaf176 | 2019-01-16 09:41:43 -0700 | [diff] [blame] | 95 | # 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 Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 111 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 112 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 113 | def update_onu(self, serial_number, admin_state): |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 114 | 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 Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 120 | onu.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 121 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 122 | 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 Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 129 | |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 130 | 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 Bavier | 8ed30c9 | 2018-12-11 13:46:25 -0700 | [diff] [blame] | 149 | 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 Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 160 | def update_subscriber(self, subscriber, si): |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 161 | cur_status = subscriber.status |
Andy Bavier | afaf176 | 2019-01-16 09:41:43 -0700 | [diff] [blame] | 162 | # 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 Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 170 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 171 | # 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 Bavier | 8ed30c9 | 2018-12-11 13:46:25 -0700 | [diff] [blame] | 176 | 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 Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 180 | self.update_subscriber_ip(subscriber, si.ip_address) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 181 | subscriber.mac_address = si.mac_address |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 182 | subscriber.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 183 | else: |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 184 | 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 Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 186 | |
| 187 | def handle_delete(self, si): |
| 188 | pass |