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 |
| 19 | |
| 20 | class AttWorkflowDriverWhiteListEntryPolicy(Policy): |
| 21 | model_name = "AttWorkflowDriverWhiteListEntry" |
| 22 | |
| 23 | def handle_create(self, whitelist): |
| 24 | self.handle_update(whitelist) |
| 25 | |
| 26 | def handle_update(self, whitelist): |
| 27 | self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverWhiteListEntry", whitelist=whitelist) |
| 28 | |
| 29 | sis = AttWorkflowDriverServiceInstance.objects.filter(serial_number = whitelist.serial_number, |
| 30 | owner_id = whitelist.owner.id) |
| 31 | |
| 32 | for si in sis: |
| 33 | if si.valid != "valid": |
| 34 | self.logger.debug("MODEL_POLICY: activating AttWorkflowDriverServiceInstance because of change in the whitelist", si=si) |
| 35 | si.valid = "valid" |
| 36 | si.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True) |
| 37 | |
| 38 | whitelist.backend_need_delete_policy=True |
| 39 | whitelist.save(update_fields=["backend_need_delete_policy"]) |
| 40 | |
| 41 | def handle_delete(self, whitelist): |
| 42 | self.logger.debug("MODEL_POLICY: handle_delete for AttWorkflowDriverWhiteListEntry", whitelist=whitelist) |
| 43 | |
| 44 | # BUG: Sometimes the delete policy is not called, because the reaper deletes |
| 45 | |
| 46 | assert(whitelist.owner) |
| 47 | |
| 48 | sis = AttWorkflowDriverServiceInstance.objects.filter(serial_number = whitelist.serial_number, |
| 49 | owner_id = whitelist.owner.id) |
| 50 | |
| 51 | for si in sis: |
| 52 | if si.valid != "invalid": |
| 53 | self.logger.debug( |
| 54 | "MODEL_POLICY: disabling AttWorkflowDriverServiceInstance because of change in the whitelist", si=si) |
| 55 | si.valid = "invalid" |
| 56 | si.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True) |
| 57 | |
| 58 | whitelist.backend_need_reap=True |
| 59 | whitelist.save(update_fields=["backend_need_reap"]) |