blob: ef3363b24af9c2115511e4f70c8763b732512b96 [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 RCORDSubscriber, ONUDevice, model_accessor
18from synchronizers.new_base.policy import Policy
19
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070020class DeferredException(Exception):
21 pass
22
Matteo Scandoload0c1752018-08-09 15:47:16 -070023class AttWorkflowDriverServiceInstancePolicy(Policy):
24 model_name = "AttWorkflowDriverServiceInstance"
25
26 def handle_create(self, si):
27 self.logger.debug("MODEL_POLICY: handle_create for AttWorkflowDriverServiceInstance %s " % si.id)
28 self.handle_update(si)
29
Matteo Scandoload0c1752018-08-09 15:47:16 -070030 def handle_update(self, si):
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070031
32 # TODO if si.onu_state = DISABLED set subscriber.status to need_auth
33 # TODO cleanup
34
Matteo Scandoload0c1752018-08-09 15:47:16 -070035 self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s, valid=%s " % (si.id, si.valid))
36
37 # Check to make sure the object has been synced. This is to cover a race condition where the model_policy
38 # runs, is interrupted by the sync step, the sync step completes, and then the model policy ends up saving
39 # a policed_timestamp that is later the updated timestamp set by the sync_step.
40 if (si.backend_code!=1):
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070041 raise DeferredException("MODEL_POLICY: AttWorkflowDriverServiceInstance %s has not been synced yet" % si.id)
Matteo Scandoload0c1752018-08-09 15:47:16 -070042
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070043 # waiting for Whitelist validation
Matteo Scandoload0c1752018-08-09 15:47:16 -070044 if not hasattr(si, 'valid') or si.valid is "awaiting":
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070045 raise DeferredException("MODEL_POLICY: deferring handle_update for AttWorkflowDriverServiceInstance %s as not validated yet" % si.id)
46
47 # disabling ONU
Matteo Scandoload0c1752018-08-09 15:47:16 -070048 if si.valid == "invalid":
49 self.logger.debug("MODEL_POLICY: disabling ONUDevice [%s] for AttWorkflowDriverServiceInstance %s" % (si.serial_number, si.id))
50 onu = ONUDevice.objects.get(serial_number=si.serial_number)
51 onu.admin_state = "DISABLED"
52 onu.save(always_update_timestamp=True)
53 return
54 if si.valid == "valid":
55
56 # reactivating the ONUDevice
57 try:
58 onu = ONUDevice.objects.get(serial_number=si.serial_number)
59 except IndexError:
60 raise Exception("MODEL_POLICY: cannot find ONUDevice [%s] for AttWorkflowDriverServiceInstance %s" % (si.serial_number, si.id))
61 if onu.admin_state == "DISABLED":
62 self.logger.debug("MODEL_POLICY: enabling ONUDevice [%s] for AttWorkflowDriverServiceInstance %s" % (si.serial_number, si.id))
63 onu.admin_state = "ENABLED"
64 onu.save(always_update_timestamp=True)
65
66 # handling the subscriber status
67
68 subscriber = None
69 try:
70 subscriber = RCORDSubscriber.objects.get(onu_device=si.serial_number)
71 except IndexError:
72 # we just want to find out if it exists or not
73 pass
74
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070075 if subscriber:
76 # if the subscriber is there and authentication is complete, update its state
77 self.logger.debug("MODEL_POLICY: handling subscriber", onu_device=si.serial_number, authentication_state=si.authentication_state, onu_state=si.onu_state)
78 if si.onu_state == "DISABLED":
79 # NOTE do not mess with onu.admin_state as that triggered this condition
80 subscriber.status = "awaiting-auth"
81 elif si.authentication_state == "STARTED":
82 subscriber.status = "awaiting-auth"
83 elif si.authentication_state == "REQUESTED":
84 subscriber.status = "awaiting-auth"
85 elif si.authentication_state == "APPROVED":
86 subscriber.status = "enabled"
87 elif si.authentication_state == "DENIED":
88 subscriber.status = "auth-failed"
89
90 subscriber.save(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -070091 # if subscriber does not exist
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070092 else:
93 self.logger.warn("MODEL_POLICY: subscriber does not exists for this SI, doing nothing")
Matteo Scandoload0c1752018-08-09 15:47:16 -070094
95 def handle_delete(self, si):
96 pass