blob: 3d5f34c8971988d81417775a10c6667d367e2781 [file] [log] [blame]
Andy Bavier561f3e52019-03-15 10:46:05 -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 xossynchronizer.model_policies.policy import Policy
18import os
19import sys
20
21sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
22sys.path.append(sync_path)
23
24from helpers import TtHelpers
25
26class TtWorkflowDriverWhiteListEntryPolicy(Policy):
27 model_name = "TtWorkflowDriverWhiteListEntry"
28
29 def handle_create(self, whitelist):
30 self.handle_update(whitelist)
31
32 # Update the SI if the onu_state has changed.
33 # The SI model policy will take care of updating other state.
34 def validate_onu_state(self, si):
35 [valid, message] = TtHelpers.validate_onu(self.model_accessor, self.logger, si)
36 if valid:
Andy Bavier0ce0dae2019-05-10 17:46:02 -070037 si.admin_onu_state = "ENABLED"
Andy Bavier561f3e52019-03-15 10:46:05 -070038 else:
Andy Bavier0ce0dae2019-05-10 17:46:02 -070039 si.admin_onu_state = "DISABLED"
Andy Bavier561f3e52019-03-15 10:46:05 -070040
41 self.logger.debug(
Andy Bavier0ce0dae2019-05-10 17:46:02 -070042 "MODEL_POLICY: activating TtWorkflowDriverServiceInstance because of change in the whitelist",
43 si=si,
44 onu_state=si.admin_onu_state)
Andy Bavier561f3e52019-03-15 10:46:05 -070045 si.save_changed_fields(always_update_timestamp=True)
46
47 def handle_update(self, whitelist):
48 self.logger.debug("MODEL_POLICY: handle_update for TtWorkflowDriverWhiteListEntry", whitelist=whitelist)
49
50 sis = self.model_accessor.TtWorkflowDriverServiceInstance.objects.all()
51
52 for si in sis:
53
54 if si.serial_number.lower() != whitelist.serial_number.lower():
55 # NOTE we don't care about this SI as it has a different serial number
56 continue
57
58 self.validate_onu_state(si)
59
60 whitelist.backend_need_delete_policy=True
61 whitelist.save_changed_fields()
62
63 def handle_delete(self, whitelist):
64 self.logger.debug("MODEL_POLICY: handle_delete for TtWorkflowDriverWhiteListEntry", serial_number=whitelist.serial_number, pon_port=whitelist.pon_port_id, device=whitelist.device_id)
65
66 # BUG: Sometimes the delete policy is not called, because the reaper deletes
67
68 assert(whitelist.owner)
69
70 sis = self.model_accessor.TtWorkflowDriverServiceInstance.objects.all()
71 sis = [si for si in sis if si.serial_number.lower() == whitelist.serial_number.lower()]
72
73 for si in sis:
74 self.validate_onu_state(si)
75
76 whitelist.backend_need_reap=True
77 whitelist.save_changed_fields()