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 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 17 | from xossynchronizer.model_policies.policy import Policy |
Matteo Scandolo | b8da43d | 2018-09-12 15:52:16 -0700 | [diff] [blame] | 18 | import os |
| 19 | import sys |
| 20 | |
| 21 | sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) |
| 22 | sys.path.append(sync_path) |
| 23 | |
| 24 | from helpers import AttHelpers |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 25 | |
| 26 | class AttWorkflowDriverWhiteListEntryPolicy(Policy): |
| 27 | model_name = "AttWorkflowDriverWhiteListEntry" |
| 28 | |
| 29 | def handle_create(self, whitelist): |
| 30 | self.handle_update(whitelist) |
| 31 | |
Matteo Scandolo | b8da43d | 2018-09-12 15:52:16 -0700 | [diff] [blame] | 32 | def validate_onu_state(self, si): |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 33 | [valid, message] = AttHelpers.validate_onu(self.model_accessor, self.logger, si) |
Matteo Scandolo | b8da43d | 2018-09-12 15:52:16 -0700 | [diff] [blame] | 34 | si.status_message = message |
| 35 | if valid: |
| 36 | si.onu_state = "ENABLED" |
| 37 | else: |
| 38 | si.onu_state = "DISABLED" |
| 39 | si.authentication_state = "AWAITING" |
| 40 | |
| 41 | self.logger.debug( |
| 42 | "MODEL_POLICY: activating AttWorkflowDriverServiceInstance because of change in the whitelist", si=si, onu_state=si.onu_state, authentication_state=si.authentication_state) |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 43 | si.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | b8da43d | 2018-09-12 15:52:16 -0700 | [diff] [blame] | 44 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 45 | def handle_update(self, whitelist): |
| 46 | self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverWhiteListEntry", whitelist=whitelist) |
| 47 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 48 | sis = self.model_accessor.AttWorkflowDriverServiceInstance.objects.all() |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 49 | |
| 50 | for si in sis: |
Matteo Scandolo | d6c325a | 2018-09-04 14:28:23 -0700 | [diff] [blame] | 51 | |
| 52 | if si.serial_number.lower() != whitelist.serial_number.lower(): |
| 53 | # NOTE we don't care about this SI as it has a different serial number |
| 54 | continue |
| 55 | |
Matteo Scandolo | b8da43d | 2018-09-12 15:52:16 -0700 | [diff] [blame] | 56 | self.validate_onu_state(si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 57 | |
| 58 | whitelist.backend_need_delete_policy=True |
| 59 | whitelist.save(update_fields=["backend_need_delete_policy"]) |
| 60 | |
| 61 | def handle_delete(self, whitelist): |
Matteo Scandolo | c5c558f | 2018-11-02 10:33:40 -0700 | [diff] [blame] | 62 | self.logger.debug("MODEL_POLICY: handle_delete for AttWorkflowDriverWhiteListEntry", serial_number=whitelist.serial_number, pon_port=whitelist.pon_port_id, device=whitelist.device_id) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 63 | |
| 64 | # BUG: Sometimes the delete policy is not called, because the reaper deletes |
| 65 | |
| 66 | assert(whitelist.owner) |
| 67 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 68 | sis = self.model_accessor.AttWorkflowDriverServiceInstance.objects.all() |
Matteo Scandolo | b8da43d | 2018-09-12 15:52:16 -0700 | [diff] [blame] | 69 | sis = [si for si in sis if si.serial_number.lower() == whitelist.serial_number.lower()] |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 70 | |
| 71 | for si in sis: |
Matteo Scandolo | b8da43d | 2018-09-12 15:52:16 -0700 | [diff] [blame] | 72 | self.validate_onu_state(si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 73 | |
| 74 | whitelist.backend_need_reap=True |
| 75 | whitelist.save(update_fields=["backend_need_reap"]) |