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 | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 58 | si.save_changed_fields() |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 59 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 60 | def validate_onu_state(self, si): |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame^] | 61 | [valid, message] = AttHelpers.validate_onu(self.logger, si) |
Matteo Scandolo | 200dab4 | 2018-09-12 13:52:09 -0700 | [diff] [blame] | 62 | si.status_message = message |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 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") |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 69 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 70 | def update_onu(self, serial_number, admin_state): |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 71 | onu = [onu for onu in ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0] |
| 72 | if onu.admin_state == admin_state: |
| 73 | self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state)) |
| 74 | else: |
| 75 | self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state)) |
| 76 | onu.admin_state = admin_state |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 77 | onu.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 78 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 79 | def get_subscriber(self, serial_number): |
| 80 | try: |
| 81 | return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0] |
| 82 | except IndexError: |
| 83 | # If the subscriber doesn't exist we don't do anything |
| 84 | self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number) |
| 85 | return None |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 86 | |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 87 | def update_subscriber_ip(self, subscriber, ip): |
| 88 | # TODO check if the subscriber has an IP and update it, |
| 89 | # or create a new one |
| 90 | try: |
| 91 | ip = RCORDIpAddress.objects.filter( |
| 92 | subscriber_id=subscriber.id, |
| 93 | ip=ip |
| 94 | )[0] |
| 95 | self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip) |
| 96 | ip.save_changed_fields() |
| 97 | except IndexError: |
| 98 | self.logger.debug("MODEL_POLICY: Creating new RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip) |
| 99 | ip = RCORDIpAddress( |
| 100 | subscriber_id=subscriber.id, |
| 101 | ip=ip, |
| 102 | description="DHCP Assigned IP Address" |
| 103 | ) |
| 104 | ip.save() |
| 105 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 106 | def update_subscriber(self, subscriber, si): |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 107 | cur_status = subscriber.status |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 108 | if si.authentication_state == "AWAITING": |
| 109 | subscriber.status = "awaiting-auth" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 110 | si.status_message += " - Awaiting Authentication" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 111 | elif si.authentication_state == "REQUESTED": |
| 112 | subscriber.status = "awaiting-auth" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 113 | si.status_message += " - Authentication requested" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 114 | elif si.authentication_state == "STARTED": |
| 115 | subscriber.status = "awaiting-auth" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 116 | si.status_message += " - Authentication started" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 117 | elif si.authentication_state == "APPROVED": |
| 118 | subscriber.status = "enabled" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 119 | si.status_message += " - Authentication succeded" |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 120 | elif si.authentication_state == "DENIED": |
| 121 | subscriber.status = "auth-failed" |
Matteo Scandolo | 075ec44 | 2018-09-13 14:19:02 -0700 | [diff] [blame] | 122 | si.status_message += " - Authentication denied" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 123 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 124 | # NOTE we save the subscriber only if: |
| 125 | # - the status has changed |
| 126 | # - we get a DHCPACK event |
| 127 | if cur_status != subscriber.status or si.dhcp_state == "DHCPACK": |
| 128 | self.logger.debug("MODEL_POLICY: updating subscriber", onu_device=subscriber.onu_device, authentication_state=si.authentication_state, subscriber_status=subscriber.status) |
| 129 | if si.ip_address and si.mac_address: |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 130 | # subscriber.ip_address = si.ip_address |
| 131 | self.update_subscriber_ip(subscriber, si.ip_address) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 132 | subscriber.mac_address = si.mac_address |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 133 | subscriber.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 134 | else: |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 135 | self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device, |
| 136 | authentication_state=si.authentication_state, subscriber_status=subscriber.status) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 137 | |
| 138 | def handle_delete(self, si): |
| 139 | pass |