blob: c788d232fe81091300592b789d30d80b66a424db [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -07001
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
17from synchronizers.new_base.modelaccessor import AttWorkflowDriverServiceInstance, AttWorkflowDriverWhiteListEntry, model_accessor
18from synchronizers.new_base.policy import Policy
19
20class 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
Matteo Scandolod6c325a2018-09-04 14:28:23 -070029 # TODO is Django construct '__iexact' available here?
30 # sis = AttWorkflowDriverServiceInstance.objects.filter(
31 # serial_number = whitelist.serial_number,
32 # owner_id = whitelist.owner.id)
33
34 sis = AttWorkflowDriverServiceInstance.objects.all()
Matteo Scandoload0c1752018-08-09 15:47:16 -070035
36 for si in sis:
Matteo Scandolod6c325a2018-09-04 14:28:23 -070037
38 if si.serial_number.lower() != whitelist.serial_number.lower():
39 # NOTE we don't care about this SI as it has a different serial number
40 continue
41
Matteo Scandoload0c1752018-08-09 15:47:16 -070042 if si.valid != "valid":
43 self.logger.debug("MODEL_POLICY: activating AttWorkflowDriverServiceInstance because of change in the whitelist", si=si)
44 si.valid = "valid"
45 si.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True)
46
47 whitelist.backend_need_delete_policy=True
48 whitelist.save(update_fields=["backend_need_delete_policy"])
49
50 def handle_delete(self, whitelist):
51 self.logger.debug("MODEL_POLICY: handle_delete for AttWorkflowDriverWhiteListEntry", whitelist=whitelist)
52
53 # BUG: Sometimes the delete policy is not called, because the reaper deletes
54
55 assert(whitelist.owner)
56
57 sis = AttWorkflowDriverServiceInstance.objects.filter(serial_number = whitelist.serial_number,
58 owner_id = whitelist.owner.id)
59
60 for si in sis:
61 if si.valid != "invalid":
62 self.logger.debug(
63 "MODEL_POLICY: disabling AttWorkflowDriverServiceInstance because of change in the whitelist", si=si)
64 si.valid = "invalid"
65 si.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True)
66
67 whitelist.backend_need_reap=True
68 whitelist.save(update_fields=["backend_need_reap"])