blob: 37946edbc9d4859dfe94ebf60dd32617044dfc4e [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
20class AttWorkflowDriverServiceInstancePolicy(Policy):
21 model_name = "AttWorkflowDriverServiceInstance"
22
23 def handle_create(self, si):
24 self.logger.debug("MODEL_POLICY: handle_create for AttWorkflowDriverServiceInstance %s " % si.id)
25 self.handle_update(si)
26
27 def update_and_save_subscriber(self, subscriber, si, update_timestamp=False):
28 if si.authentication_state == "STARTED":
29 subscriber.status = "awaiting-auth"
30 elif si.authentication_state == "REQUESTED":
31 subscriber.status = "awaiting-auth"
32 elif si.authentication_state == "APPROVED":
33 subscriber.status = "enabled"
34 elif si.authentication_state == "DENIED":
35 subscriber.status = "auth-failed"
36
Matteo Scandoload0c1752018-08-09 15:47:16 -070037 subscriber.save(always_update_timestamp=update_timestamp)
38
39 def create_subscriber(self, si):
40 subscriber = RCORDSubscriber()
41 subscriber.onu_device = si.serial_number
42 subscriber.status == "awaiting-auth"
43
44 return subscriber
45
46 def handle_update(self, si):
47 self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s, valid=%s " % (si.id, si.valid))
48
49 # Check to make sure the object has been synced. This is to cover a race condition where the model_policy
50 # runs, is interrupted by the sync step, the sync step completes, and then the model policy ends up saving
51 # a policed_timestamp that is later the updated timestamp set by the sync_step.
52 if (si.backend_code!=1):
53 raise Exception("MODEL_POLICY: AttWorkflowDriverServiceInstance %s has not been synced yet" % si.id)
54
55 if not hasattr(si, 'valid') or si.valid is "awaiting":
56 self.logger.debug("MODEL_POLICY: skipping handle_update for AttWorkflowDriverServiceInstance %s as not validated yet" % si.id)
57 return
58 if si.valid == "invalid":
59 self.logger.debug("MODEL_POLICY: disabling ONUDevice [%s] for AttWorkflowDriverServiceInstance %s" % (si.serial_number, si.id))
60 onu = ONUDevice.objects.get(serial_number=si.serial_number)
61 onu.admin_state = "DISABLED"
62 onu.save(always_update_timestamp=True)
63 return
64 if si.valid == "valid":
65
66 # reactivating the ONUDevice
67 try:
68 onu = ONUDevice.objects.get(serial_number=si.serial_number)
69 except IndexError:
70 raise Exception("MODEL_POLICY: cannot find ONUDevice [%s] for AttWorkflowDriverServiceInstance %s" % (si.serial_number, si.id))
71 if onu.admin_state == "DISABLED":
72 self.logger.debug("MODEL_POLICY: enabling ONUDevice [%s] for AttWorkflowDriverServiceInstance %s" % (si.serial_number, si.id))
73 onu.admin_state = "ENABLED"
74 onu.save(always_update_timestamp=True)
75
76 # handling the subscriber status
77
78 subscriber = None
79 try:
80 subscriber = RCORDSubscriber.objects.get(onu_device=si.serial_number)
81 except IndexError:
82 # we just want to find out if it exists or not
83 pass
84
85 # if subscriber does not exist
86 self.logger.debug("MODEL_POLICY: handling subscriber", onu_device=si.serial_number, create_on_discovery=si.owner.leaf_model.create_on_discovery)
87 if not subscriber:
88 # and create_on_discovery is false
89 if not si.owner.leaf_model.create_on_discovery:
90 # do not create the subscriber, unless it has been approved
91 if si.authentication_state == "APPROVED":
92 self.logger.debug("MODEL_POLICY: creating subscriber as authentication_sate=APPROVED")
93 subscriber = self.create_subscriber(si)
94 self.update_and_save_subscriber(subscriber, si)
95 else:
96 self.logger.debug("MODEL_POLICY: creating subscriber")
97 subscriber = self.create_subscriber(si)
98 self.update_and_save_subscriber(subscriber, si)
99 # if the subscriber is there and authentication is complete, update its state
100 elif subscriber and si.authentication_state == "APPROVED":
101 self.logger.debug("MODEL_POLICY: updating subscriber status")
102 self.update_and_save_subscriber(subscriber, si, update_timestamp=True)
103
104 def handle_delete(self, si):
105 pass