Takahiro Suzuki | 2b66b94 | 2020-12-17 11:58:14 +0900 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2020-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 helpers import NttHelpers |
| 18 | from xossynchronizer.model_policies.policy import Policy |
| 19 | |
| 20 | import os |
| 21 | import sys |
| 22 | |
| 23 | sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) |
| 24 | sys.path.append(sync_path) |
| 25 | |
| 26 | |
| 27 | class DeferredException(Exception): |
| 28 | pass |
| 29 | |
| 30 | |
| 31 | class NttWorkflowDriverServiceInstancePolicy(Policy): |
| 32 | model_name = "NttWorkflowDriverServiceInstance" |
| 33 | |
| 34 | def handle_create(self, si): |
| 35 | self.logger.debug("MODEL_POLICY: handle_create for NttWorkflowDriverServiceInstance %s " % si.id) |
| 36 | self.handle_update(si) |
| 37 | |
| 38 | def handle_update(self, si): |
| 39 | self.logger.debug("MODEL_POLICY: handle_update for NttWorkflowDriverServiceInstance %s " % |
| 40 | (si.id), onu_state=si.admin_onu_state, authentication_state=si.authentication_state) |
| 41 | self.process_onu_state(si) |
| 42 | si.save_changed_fields() |
| 43 | |
| 44 | # Check the whitelist to see if the ONU is valid. If it is, make sure that it's enabled. |
| 45 | def process_onu_state(self, si): |
| 46 | [valid, message] = NttHelpers.validate_onu(self.model_accessor, self.logger, si) |
| 47 | si.status_message = message |
| 48 | if valid: |
| 49 | si.admin_onu_state = "ENABLED" |
| 50 | self.update_onu(si.serial_number, "ENABLED") |
| 51 | if si.authentication_state == '': |
| 52 | si.authentication_state = "APPROVED" |
| 53 | else: |
| 54 | si.admin_onu_state = "DISABLED" |
| 55 | self.update_onu(si.serial_number, "DISABLED") |
| 56 | si.authentication_state = "DENIED" |
| 57 | |
| 58 | def update_onu(self, serial_number, admin_state): |
| 59 | onu = [onu for onu in self.model_accessor.ONUDevice.objects.all() |
| 60 | if onu.serial_number.lower() == serial_number.lower().split('-')[0]][0] |
| 61 | if onu.admin_state == "ADMIN_DISABLED": |
| 62 | self.logger.debug( |
| 63 | "MODEL_POLICY: ONUDevice [%s] has been manually disabled, not changing state to %s" % |
| 64 | (onu.serial_number, admin_state)) |
| 65 | return |
| 66 | if onu.admin_state == admin_state: |
| 67 | self.logger.debug( |
| 68 | "MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % |
| 69 | (onu.serial_number, admin_state)) |
| 70 | else: |
| 71 | self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (onu.serial_number, admin_state)) |
| 72 | onu.admin_state = admin_state |
| 73 | onu.save_changed_fields(always_update_timestamp=True) |
| 74 | |
| 75 | def handle_delete(self, si): |
| 76 | pass |