Matteo Scandolo | b517fb2 | 2018-07-29 09:57:17 -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 HippieOSSServiceInstance, model_accessor |
| 18 | from synchronizers.new_base.policy import Policy |
| 19 | |
| 20 | class OSSServicePolicy(Policy): |
| 21 | model_name = "HippieOSSService" |
| 22 | |
| 23 | def handle_update(self, service): |
| 24 | self.logger.debug("MODEL_POLICY: handle_update for HippieOSSService", oss=service) |
| 25 | |
| 26 | sis = HippieOSSServiceInstance.objects.all() |
| 27 | |
Scott Baker | 26437eb | 2018-07-31 16:46:31 -0700 | [diff] [blame^] | 28 | # TODO(smbaker): This is redudant with HippieOSSWhiteListEntry model policy, though etaining this does provide |
| 29 | # a handy way to trigger a full reexamination of the whitelist. |
| 30 | |
| 31 | whitelist = [x.serial_number for x in service.whitelist_entries.all()] |
Matteo Scandolo | b517fb2 | 2018-07-29 09:57:17 -0700 | [diff] [blame] | 32 | |
| 33 | for si in sis: |
| 34 | if si.serial_number in whitelist and not si.valid == "valid": |
| 35 | self.logger.debug("MODEL_POLICY: activating HippieOSSServiceInstance because of change in the whitelist", si=si) |
| 36 | si.valid = "valid" |
| 37 | si.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True) |
| 38 | if si.serial_number not in whitelist and not si.valid == "invalid": |
| 39 | self.logger.debug( |
| 40 | "MODEL_POLICY: disabling HippieOSSServiceInstance because of change in the whitelist", si=si) |
| 41 | si.valid = "invalid" |
| 42 | si.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True) |
| 43 | |
| 44 | |
| 45 | def handle_delete(self, si): |
| 46 | pass |