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 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 42 | # validating ONU |
| 43 | if si.onu_state == "AWAITING" or si.onu_state == "ENABLED": |
| 44 | # we validate the ONU state only if it is enabled or awaiting, |
| 45 | # if it's disabled it means someone has disabled it |
| 46 | self.validate_onu_state(si) |
Matteo Scandolo | 744effb | 2018-10-02 14:36:19 -0700 | [diff] [blame] | 47 | else: |
Matteo Scandolo | c5c558f | 2018-11-02 10:33:40 -0700 | [diff] [blame] | 48 | # clean the status and verify that the ONU is actually disabled |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 49 | si.status_message = "ONU has been disabled" |
Matteo Scandolo | c5c558f | 2018-11-02 10:33:40 -0700 | [diff] [blame] | 50 | self.update_onu(si.serial_number, "DISABLED") |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 51 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 52 | # handling the subscriber status |
| 53 | subscriber = self.get_subscriber(si.serial_number) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 54 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 55 | if subscriber: |
| 56 | self.update_subscriber(subscriber, si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 57 | |
Andy Bavier | 8ed30c9 | 2018-12-11 13:46:25 -0700 | [diff] [blame^] | 58 | if si.authentication_state in ["AWAITING", "REQUESTED", "STARTED"]: |
| 59 | si.ip_address = "" |
| 60 | si.mac_address = "" |
| 61 | si.dhcp_state = "" |
| 62 | |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 63 | si.save_changed_fields() |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 64 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 65 | def validate_onu_state(self, si): |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 66 | [valid, message] = AttHelpers.validate_onu(self.logger, si) |
Matteo Scandolo | 200dab4 | 2018-09-12 13:52:09 -0700 | [diff] [blame] | 67 | si.status_message = message |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 68 | if valid: |
| 69 | si.onu_state = "ENABLED" |
| 70 | self.update_onu(si.serial_number, "ENABLED") |
| 71 | else: |
| 72 | si.onu_state = "DISABLED" |
| 73 | self.update_onu(si.serial_number, "DISABLED") |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 74 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 75 | def update_onu(self, serial_number, admin_state): |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 76 | onu = [onu for onu in ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0] |
| 77 | if onu.admin_state == admin_state: |
| 78 | self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state)) |
| 79 | else: |
| 80 | self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state)) |
| 81 | onu.admin_state = admin_state |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 82 | onu.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 83 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 84 | def get_subscriber(self, serial_number): |
| 85 | try: |
| 86 | return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0] |
| 87 | except IndexError: |
| 88 | # If the subscriber doesn't exist we don't do anything |
| 89 | self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number) |
| 90 | return None |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 91 | |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 92 | def update_subscriber_ip(self, subscriber, ip): |
| 93 | # TODO check if the subscriber has an IP and update it, |
| 94 | # or create a new one |
| 95 | try: |
| 96 | ip = RCORDIpAddress.objects.filter( |
| 97 | subscriber_id=subscriber.id, |
| 98 | ip=ip |
| 99 | )[0] |
| 100 | self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip) |
| 101 | ip.save_changed_fields() |
| 102 | except IndexError: |
| 103 | self.logger.debug("MODEL_POLICY: Creating new RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip) |
| 104 | ip = RCORDIpAddress( |
| 105 | subscriber_id=subscriber.id, |
| 106 | ip=ip, |
| 107 | description="DHCP Assigned IP Address" |
| 108 | ) |
| 109 | ip.save() |
| 110 | |
Andy Bavier | 8ed30c9 | 2018-12-11 13:46:25 -0700 | [diff] [blame^] | 111 | def delete_subscriber_ip(self, subscriber, ip): |
| 112 | try: |
| 113 | ip = RCORDIpAddress.objects.filter( |
| 114 | subscriber_id=subscriber.id, |
| 115 | ip=ip |
| 116 | )[0] |
| 117 | self.logger.debug("MODEL_POLICY: delete RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip) |
| 118 | ip.delete() |
| 119 | except: |
| 120 | self.logger.warning("MODEL_POLICY: no RCORDIpAddress object found, cannot delete", ip=ip) |
| 121 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 122 | def update_subscriber(self, subscriber, si): |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 123 | cur_status = subscriber.status |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 124 | if si.authentication_state == "AWAITING": |
| 125 | subscriber.status = "awaiting-auth" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 126 | si.status_message += " - Awaiting Authentication" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 127 | elif si.authentication_state == "REQUESTED": |
| 128 | subscriber.status = "awaiting-auth" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 129 | si.status_message += " - Authentication requested" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 130 | elif si.authentication_state == "STARTED": |
| 131 | subscriber.status = "awaiting-auth" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 132 | si.status_message += " - Authentication started" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 133 | elif si.authentication_state == "APPROVED": |
| 134 | subscriber.status = "enabled" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 135 | si.status_message += " - Authentication succeded" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 136 | elif si.authentication_state == "DENIED": |
| 137 | subscriber.status = "auth-failed" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 138 | si.status_message += " - Authentication denied" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 139 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 140 | # NOTE we save the subscriber only if: |
| 141 | # - the status has changed |
| 142 | # - we get a DHCPACK event |
| 143 | if cur_status != subscriber.status or si.dhcp_state == "DHCPACK": |
| 144 | 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^] | 145 | if subscriber.status == "awaiting-auth": |
| 146 | self.delete_subscriber_ip(subscriber, si.ip_address) |
| 147 | subscriber.mac_address = "" |
| 148 | elif si.ip_address and si.mac_address: |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 149 | self.update_subscriber_ip(subscriber, si.ip_address) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 150 | subscriber.mac_address = si.mac_address |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 151 | subscriber.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 152 | else: |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 153 | self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device, |
| 154 | authentication_state=si.authentication_state, subscriber_status=subscriber.status) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 155 | |
| 156 | def handle_delete(self, si): |
| 157 | pass |