Takahiro Suzuki | 2b66b94 | 2020-12-17 11:58:14 +0900 | [diff] [blame^] | 1 | # Copyright 2020-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
| 16 | from helpers import NttHelpers |
| 17 | from xossynchronizer.model_policies.policy import Policy |
| 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 | |
| 25 | class NttWorkflowDriverWhiteListEntryPolicy(Policy): |
| 26 | model_name = "NttWorkflowDriverWhiteListEntry" |
| 27 | |
| 28 | def handle_create(self, whitelist): |
| 29 | self.handle_update(whitelist) |
| 30 | |
| 31 | # Update the SI if the onu_state has changed. |
| 32 | # The SI model policy will take care of updating other state. |
| 33 | def validate_onu_state(self, si): |
| 34 | [valid, message] = NttHelpers.validate_onu(self.model_accessor, self.logger, si) |
| 35 | if valid: |
| 36 | si.admin_onu_state = "ENABLED" |
| 37 | else: |
| 38 | si.admin_onu_state = "DISABLED" |
| 39 | |
| 40 | self.logger.debug( |
| 41 | "MODEL_POLICY: activating NttWorkflowDriverServiceInstance because of change in the whitelist", |
| 42 | si=si, |
| 43 | onu_state=si.admin_onu_state, |
| 44 | authentication_state=si.authentication_state) |
| 45 | |
| 46 | si.status_message = message |
| 47 | si.save_changed_fields(always_update_timestamp=True) |
| 48 | |
| 49 | def handle_update(self, whitelist): |
| 50 | self.logger.debug("MODEL_POLICY: handle_update for NttWorkflowDriverWhiteListEntry", whitelist=whitelist) |
| 51 | |
| 52 | sis = self.model_accessor.NttWorkflowDriverServiceInstance.objects.all() |
| 53 | |
| 54 | for si in sis: |
| 55 | |
| 56 | if si.mac_address.lower() != whitelist.mac_address.lower(): |
| 57 | # NOTE we don't care about this SI as it has a different serial number |
| 58 | continue |
| 59 | |
| 60 | self.validate_onu_state(si) |
| 61 | |
| 62 | whitelist.backend_need_delete_policy = True |
| 63 | whitelist.save_changed_fields() |
| 64 | |
| 65 | def handle_delete(self, whitelist): |
| 66 | self.logger.debug( |
| 67 | "MODEL_POLICY: handle_delete for NttWorkflowDriverWhiteListEntry") |
| 68 | |
| 69 | # BUG: Sometimes the delete policy is not called, because the reaper deletes |
| 70 | |
| 71 | assert(whitelist.owner) |
| 72 | |
| 73 | sis = self.model_accessor.NttWorkflowDriverServiceInstance.objects.all() |
| 74 | sis = [si for si in sis if si.mac_address.lower() == whitelist.mac_address.lower()] |
| 75 | |
| 76 | for si in sis: |
| 77 | self.validate_onu_state(si) |
| 78 | |
| 79 | whitelist.backend_need_reap = True |
| 80 | whitelist.save_changed_fields() |