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