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 | |
| 17 | from synchronizers.new_base.modelaccessor import RCORDSubscriber, ONUDevice, model_accessor |
| 18 | from synchronizers.new_base.policy import Policy |
| 19 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 20 | class DeferredException(Exception): |
| 21 | pass |
| 22 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 23 | class AttWorkflowDriverServiceInstancePolicy(Policy): |
| 24 | model_name = "AttWorkflowDriverServiceInstance" |
| 25 | |
| 26 | def handle_create(self, si): |
| 27 | self.logger.debug("MODEL_POLICY: handle_create for AttWorkflowDriverServiceInstance %s " % si.id) |
| 28 | self.handle_update(si) |
| 29 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 30 | def handle_update(self, si): |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 31 | |
| 32 | # TODO if si.onu_state = DISABLED set subscriber.status to need_auth |
| 33 | # TODO cleanup |
| 34 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 35 | self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s, valid=%s " % (si.id, si.valid)) |
| 36 | |
| 37 | # Check to make sure the object has been synced. This is to cover a race condition where the model_policy |
| 38 | # runs, is interrupted by the sync step, the sync step completes, and then the model policy ends up saving |
| 39 | # a policed_timestamp that is later the updated timestamp set by the sync_step. |
| 40 | if (si.backend_code!=1): |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 41 | raise DeferredException("MODEL_POLICY: AttWorkflowDriverServiceInstance %s has not been synced yet" % si.id) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 42 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 43 | # waiting for Whitelist validation |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 44 | if not hasattr(si, 'valid') or si.valid is "awaiting": |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 45 | raise DeferredException("MODEL_POLICY: deferring handle_update for AttWorkflowDriverServiceInstance %s as not validated yet" % si.id) |
| 46 | |
| 47 | # disabling ONU |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 48 | if si.valid == "invalid": |
| 49 | self.logger.debug("MODEL_POLICY: disabling ONUDevice [%s] for AttWorkflowDriverServiceInstance %s" % (si.serial_number, si.id)) |
| 50 | onu = ONUDevice.objects.get(serial_number=si.serial_number) |
| 51 | onu.admin_state = "DISABLED" |
| 52 | onu.save(always_update_timestamp=True) |
| 53 | return |
| 54 | if si.valid == "valid": |
| 55 | |
| 56 | # reactivating the ONUDevice |
| 57 | try: |
| 58 | onu = ONUDevice.objects.get(serial_number=si.serial_number) |
| 59 | except IndexError: |
| 60 | raise Exception("MODEL_POLICY: cannot find ONUDevice [%s] for AttWorkflowDriverServiceInstance %s" % (si.serial_number, si.id)) |
| 61 | if onu.admin_state == "DISABLED": |
| 62 | self.logger.debug("MODEL_POLICY: enabling ONUDevice [%s] for AttWorkflowDriverServiceInstance %s" % (si.serial_number, si.id)) |
| 63 | onu.admin_state = "ENABLED" |
| 64 | onu.save(always_update_timestamp=True) |
| 65 | |
| 66 | # handling the subscriber status |
| 67 | |
| 68 | subscriber = None |
| 69 | try: |
| 70 | subscriber = RCORDSubscriber.objects.get(onu_device=si.serial_number) |
| 71 | except IndexError: |
| 72 | # we just want to find out if it exists or not |
| 73 | pass |
| 74 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 75 | if subscriber: |
| 76 | # if the subscriber is there and authentication is complete, update its state |
| 77 | self.logger.debug("MODEL_POLICY: handling subscriber", onu_device=si.serial_number, authentication_state=si.authentication_state, onu_state=si.onu_state) |
| 78 | if si.onu_state == "DISABLED": |
| 79 | # NOTE do not mess with onu.admin_state as that triggered this condition |
| 80 | subscriber.status = "awaiting-auth" |
| 81 | elif si.authentication_state == "STARTED": |
| 82 | subscriber.status = "awaiting-auth" |
| 83 | elif si.authentication_state == "REQUESTED": |
| 84 | subscriber.status = "awaiting-auth" |
| 85 | elif si.authentication_state == "APPROVED": |
| 86 | subscriber.status = "enabled" |
| 87 | elif si.authentication_state == "DENIED": |
| 88 | subscriber.status = "auth-failed" |
| 89 | |
| 90 | subscriber.save(always_update_timestamp=True) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 91 | # if subscriber does not exist |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 92 | else: |
| 93 | self.logger.warn("MODEL_POLICY: subscriber does not exists for this SI, doing nothing") |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 94 | |
| 95 | def handle_delete(self, si): |
| 96 | pass |