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 | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 18 | from synchronizers.new_base.modelaccessor import RCORDSubscriber, ONUDevice, model_accessor |
| 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: |
| 48 | # just clean the status |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 49 | si.status_message = "ONU has been disabled" |
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 |
| 52 | subscriber = self.get_subscriber(si.serial_number) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 53 | |
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 | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 57 | si.save_changed_fields() |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 58 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 59 | def validate_onu_state(self, si): |
| 60 | [valid, message] = AttHelpers.validate_onu(si) |
Matteo Scandolo | 200dab4 | 2018-09-12 13:52:09 -0700 | [diff] [blame] | 61 | si.status_message = message |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 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") |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 68 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 69 | def update_onu(self, serial_number, admin_state): |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 70 | onu = [onu for onu in ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0] |
| 71 | if onu.admin_state == admin_state: |
| 72 | self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state)) |
| 73 | else: |
| 74 | self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state)) |
| 75 | onu.admin_state = admin_state |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 76 | onu.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 77 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 78 | def get_subscriber(self, serial_number): |
| 79 | try: |
| 80 | return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0] |
| 81 | except IndexError: |
| 82 | # If the subscriber doesn't exist we don't do anything |
| 83 | self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number) |
| 84 | return None |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 85 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 86 | def update_subscriber(self, subscriber, si): |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 87 | cur_status = subscriber.status |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 88 | if si.authentication_state == "AWAITING": |
| 89 | subscriber.status = "awaiting-auth" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 90 | si.status_message += " - Awaiting Authentication" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 91 | elif si.authentication_state == "REQUESTED": |
| 92 | subscriber.status = "awaiting-auth" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 93 | si.status_message += " - Authentication requested" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 94 | elif si.authentication_state == "STARTED": |
| 95 | subscriber.status = "awaiting-auth" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 96 | si.status_message += " - Authentication started" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 97 | elif si.authentication_state == "APPROVED": |
| 98 | subscriber.status = "enabled" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 99 | si.status_message += " - Authentication succeded" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 100 | elif si.authentication_state == "DENIED": |
| 101 | subscriber.status = "auth-failed" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 102 | si.status_message += " - Authentication denied" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 103 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 104 | # NOTE we save the subscriber only if: |
| 105 | # - the status has changed |
| 106 | # - we get a DHCPACK event |
| 107 | if cur_status != subscriber.status or si.dhcp_state == "DHCPACK": |
| 108 | self.logger.debug("MODEL_POLICY: updating subscriber", onu_device=subscriber.onu_device, authentication_state=si.authentication_state, subscriber_status=subscriber.status) |
| 109 | if si.ip_address and si.mac_address: |
| 110 | subscriber.ip_address = si.ip_address |
| 111 | subscriber.mac_address = si.mac_address |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 112 | subscriber.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 113 | else: |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 114 | self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device, |
| 115 | authentication_state=si.authentication_state, subscriber_status=subscriber.status) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 116 | |
| 117 | def handle_delete(self, si): |
| 118 | pass |