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