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 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 35 | separator = " // " |
| 36 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 37 | def handle_create(self, si): |
| 38 | self.logger.debug("MODEL_POLICY: handle_create for AttWorkflowDriverServiceInstance %s " % si.id) |
| 39 | self.handle_update(si) |
| 40 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 41 | def handle_update(self, si): |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 42 | self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s " % (si.id)) |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 43 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 44 | # validating ONU |
| 45 | if si.onu_state == "AWAITING" or si.onu_state == "ENABLED": |
| 46 | # we validate the ONU state only if it is enabled or awaiting, |
| 47 | # if it's disabled it means someone has disabled it |
| 48 | self.validate_onu_state(si) |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 49 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 50 | # handling the subscriber status |
| 51 | subscriber = self.get_subscriber(si.serial_number) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 52 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 53 | if subscriber: |
| 54 | self.update_subscriber(subscriber, si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 55 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 56 | si.save() |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 57 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 58 | def validate_onu_state(self, si): |
| 59 | [valid, message] = AttHelpers.validate_onu(si) |
| 60 | si.status_message += self.separator + message |
| 61 | if valid: |
| 62 | si.onu_state = "ENABLED" |
| 63 | self.update_onu(si.serial_number, "ENABLED") |
| 64 | else: |
| 65 | si.onu_state = "DISABLED" |
| 66 | self.update_onu(si.serial_number, "DISABLED") |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 67 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 68 | def update_onu(self, serial_number, admin_state): |
| 69 | self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state)) |
| 70 | onu = ONUDevice.objects.get(serial_number=serial_number) |
| 71 | onu.admin_state = admin_state |
| 72 | onu.save(always_update_timestamp=True) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 73 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 74 | def get_subscriber(self, serial_number): |
| 75 | try: |
| 76 | return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0] |
| 77 | except IndexError: |
| 78 | # If the subscriber doesn't exist we don't do anything |
| 79 | self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number) |
| 80 | return None |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 81 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 82 | def update_subscriber(self, subscriber, si): |
| 83 | if si.authentication_state == "AWAITING": |
| 84 | subscriber.status = "awaiting-auth" |
| 85 | si.status_message += self.separator + "Awaiting Authentication" |
| 86 | elif si.authentication_state == "REQUESTED": |
| 87 | subscriber.status = "awaiting-auth" |
| 88 | si.status_message += self.separator + "Authentication requested" |
| 89 | elif si.authentication_state == "STARTED": |
| 90 | subscriber.status = "awaiting-auth" |
| 91 | si.status_message += self.separator + "Authentication started" |
| 92 | elif si.authentication_state == "APPROVED": |
| 93 | subscriber.status = "enabled" |
| 94 | si.status_message += self.separator + "Authentication succeded" |
| 95 | elif si.authentication_state == "DENIED": |
| 96 | subscriber.status = "auth-failed" |
| 97 | si.status_message += self.separator + "Authentication denied" |
| 98 | self.logger.debug("MODEL_POLICY: handling subscriber", onu_device=subscriber.onu_device, authentication_state=si.authentication_state, subscriber_status=subscriber.status) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 99 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 100 | subscriber.save(always_update_timestamp=True) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 101 | |
| 102 | def handle_delete(self, si): |
| 103 | pass |