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, model_accessor |
| 18 | from synchronizers.new_base.policy import Policy |
| 19 | |
| 20 | class AttWorkflowDriverServicePolicy(Policy): |
| 21 | model_name = "AttWorkflowDriverService" |
| 22 | |
| 23 | def handle_update(self, service): |
| 24 | self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverService", oss=service) |
| 25 | |
| 26 | sis = AttWorkflowDriverServiceInstance.objects.all() |
| 27 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 28 | # TODO(teone): use the method defined in helpers.py |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 29 | |
Matteo Scandolo | d6c325a | 2018-09-04 14:28:23 -0700 | [diff] [blame] | 30 | whitelist = [x.serial_number.lower() for x in service.whitelist_entries.all()] |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 31 | |
| 32 | for si in sis: |
Matteo Scandolo | d6c325a | 2018-09-04 14:28:23 -0700 | [diff] [blame] | 33 | if si.serial_number.lower() in whitelist and not si.valid == "valid": |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 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) |
Matteo Scandolo | d6c325a | 2018-09-04 14:28:23 -0700 | [diff] [blame] | 37 | if si.serial_number.lower() not in whitelist and not si.valid == "invalid": |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 38 | self.logger.debug( |
| 39 | "MODEL_POLICY: disabling AttWorkflowDriverServiceInstance because of change in the whitelist", si=si) |
| 40 | si.valid = "invalid" |
| 41 | si.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True) |
| 42 | |
| 43 | |
| 44 | def handle_delete(self, si): |
| 45 | pass |