blob: a9e58e1f4d095a8782fdfe4f5e186bfab5caa0f2 [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
Scott Bakerc2a633d2019-04-01 19:27:41 -070016from helpers import AttHelpers
Scott Baker71d20472019-02-01 12:05:35 -080017from xossynchronizer.model_policies.policy import Policy
Matteo Scandolob8da43d2018-09-12 15:52:16 -070018import 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
Matteo Scandoload0c1752018-08-09 15:47:16 -070024
25class AttWorkflowDriverWhiteListEntryPolicy(Policy):
26 model_name = "AttWorkflowDriverWhiteListEntry"
27
28 def handle_create(self, whitelist):
29 self.handle_update(whitelist)
30
Andy Bavier11ffbf52019-02-08 11:53:21 -070031 # Update the SI if the onu_state has changed.
32 # The SI model policy will take care of updating other state.
Matteo Scandolob8da43d2018-09-12 15:52:16 -070033 def validate_onu_state(self, si):
Scott Baker71d20472019-02-01 12:05:35 -080034 [valid, message] = AttHelpers.validate_onu(self.model_accessor, self.logger, si)
Matteo Scandolob8da43d2018-09-12 15:52:16 -070035 if valid:
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -070036 si.admin_onu_state = "ENABLED"
Matteo Scandolob8da43d2018-09-12 15:52:16 -070037 else:
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -070038 si.admin_onu_state = "DISABLED"
Matteo Scandolob8da43d2018-09-12 15:52:16 -070039
40 self.logger.debug(
Scott Bakerc2a633d2019-04-01 19:27:41 -070041 "MODEL_POLICY: activating AttWorkflowDriverServiceInstance because of change in the whitelist",
42 si=si,
Matteo Scandolo2d9f40d2019-04-19 08:38:10 -070043 onu_state=si.admin_onu_state,
Scott Bakerc2a633d2019-04-01 19:27:41 -070044 authentication_state=si.authentication_state)
Himanshu Bhandari37643272020-05-27 20:04:04 +053045
46 si.status_message = message
Andy Bavier0d631eb2018-10-17 18:05:04 -070047 si.save_changed_fields(always_update_timestamp=True)
Matteo Scandolob8da43d2018-09-12 15:52:16 -070048
Matteo Scandoload0c1752018-08-09 15:47:16 -070049 def handle_update(self, whitelist):
50 self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverWhiteListEntry", whitelist=whitelist)
51
Scott Baker71d20472019-02-01 12:05:35 -080052 sis = self.model_accessor.AttWorkflowDriverServiceInstance.objects.all()
Matteo Scandoload0c1752018-08-09 15:47:16 -070053
54 for si in sis:
Matteo Scandolod6c325a2018-09-04 14:28:23 -070055
56 if si.serial_number.lower() != whitelist.serial_number.lower():
57 # NOTE we don't care about this SI as it has a different serial number
58 continue
59
Matteo Scandolob8da43d2018-09-12 15:52:16 -070060 self.validate_onu_state(si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070061
Scott Bakerc2a633d2019-04-01 19:27:41 -070062 whitelist.backend_need_delete_policy = True
Andy Bavier11ffbf52019-02-08 11:53:21 -070063 whitelist.save_changed_fields()
Matteo Scandoload0c1752018-08-09 15:47:16 -070064
65 def handle_delete(self, whitelist):
Scott Bakerc2a633d2019-04-01 19:27:41 -070066 self.logger.debug(
67 "MODEL_POLICY: handle_delete for AttWorkflowDriverWhiteListEntry",
68 serial_number=whitelist.serial_number,
69 pon_port=whitelist.pon_port_id,
70 device=whitelist.device_id)
Matteo Scandoload0c1752018-08-09 15:47:16 -070071
72 # BUG: Sometimes the delete policy is not called, because the reaper deletes
73
74 assert(whitelist.owner)
75
Scott Baker71d20472019-02-01 12:05:35 -080076 sis = self.model_accessor.AttWorkflowDriverServiceInstance.objects.all()
Matteo Scandolob8da43d2018-09-12 15:52:16 -070077 sis = [si for si in sis if si.serial_number.lower() == whitelist.serial_number.lower()]
Matteo Scandoload0c1752018-08-09 15:47:16 -070078
79 for si in sis:
Matteo Scandolob8da43d2018-09-12 15:52:16 -070080 self.validate_onu_state(si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070081
Scott Bakerc2a633d2019-04-01 19:27:41 -070082 whitelist.backend_need_reap = True
Andy Bavier11ffbf52019-02-08 11:53:21 -070083 whitelist.save_changed_fields()