blob: 255a3d1ae9c23a88ff676bdd5848ac5765cfc39f [file] [log] [blame]
Matteo Scandolo75298822018-05-22 11:25:42 -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
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070017from synchronizers.new_base.modelaccessor import RCORDSubscriber, ONUDevice, model_accessor
Matteo Scandolo75298822018-05-22 11:25:42 -070018from synchronizers.new_base.policy import Policy
19
20class OSSServiceInstancePolicy(Policy):
21 model_name = "HippieOSSServiceInstance"
22
Scott Baker249e84e2018-07-09 16:16:28 -070023 def handle_create(self, si):
24 self.logger.debug("MODEL_POLICY: handle_create for HippieOSSServiceInstance %s " % si.id)
25 self.handle_update(si)
26
Matteo Scandolob8dec872018-07-17 16:56:23 -040027 def update_and_save_subscriber(self, subscriber, si):
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=False)
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
Matteo Scandolo75298822018-05-22 11:25:42 -070050 def handle_update(self, si):
Scott Baker249e84e2018-07-09 16:16:28 -070051 self.logger.debug("MODEL_POLICY: handle_update for HippieOSSServiceInstance %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: HippieOSSServiceInstance %s has not been synced yet" % si.id)
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070058
59 if not hasattr(si, 'valid') or si.valid is "awaiting":
60 self.logger.debug("MODEL_POLICY: skipping handle_update for HippieOSSServiceInstance %s as not validated yet" % si.id)
61 return
62 if si.valid == "invalid":
63 self.logger.debug("MODEL_POLICY: disabling ONUDevice [%s] for HippieOSSServiceInstance %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":
Matteo Scandolo75298822018-05-22 11:25:42 -070069
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070070 # reactivating the ONUDevice
Matteo Scandoloce85bca2018-06-25 16:57:16 -070071 try:
72 onu = ONUDevice.objects.get(serial_number=si.serial_number)
73 except IndexError:
74 raise Exception("MODEL_POLICY: cannot find ONUDevice [%s] for HippieOSSServiceInstance %s" % (si.serial_number, si.id))
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070075 if onu.admin_state == "DISABLED":
Matteo Scandoloce85bca2018-06-25 16:57:16 -070076 self.logger.debug("MODEL_POLICY: enabling ONUDevice [%s] for HippieOSSServiceInstance %s" % (si.serial_number, si.id))
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070077 onu.admin_state = "ENABLED"
78 onu.save(always_update_timestamp=True)
79
Matteo Scandolob8dec872018-07-17 16:56:23 -040080 # handling the subscriber status
81
82 subscriber = None
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070083 try:
84 subscriber = RCORDSubscriber.objects.get(onu_device=si.serial_number)
Matteo Scandoloce85bca2018-06-25 16:57:16 -070085 except IndexError:
Matteo Scandolob8dec872018-07-17 16:56:23 -040086 # we just want to find out if it exists or not
87 pass
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070088
Matteo Scandolob8dec872018-07-17 16:56:23 -040089 # 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
104 elif subscriber:
105 # and create_on_discovery is false
106 if not si.owner.leaf_model.create_on_discovery:
107 # and in status pre-provisioned, do nothing
108 if subscriber.status == "pre-provisioned":
109 self.logger.debug("MODEL_POLICY: not updating subscriber status as original status is 'pre-provisioned'")
110 return
111 # else update the status
112 else:
113 self.logger.debug("MODEL_POLICY: updating subscriber status as original status is not 'pre-provisioned'")
114 self.update_and_save_subscriber(subscriber, si)
115 # if create_on_discovery is true
116 else:
117 self.logger.debug("MODEL_POLICY: updating subscriber status")
118 self.update_and_save_subscriber(subscriber, si)
Matteo Scandolo75298822018-05-22 11:25:42 -0700119
120 def handle_delete(self, si):
121 pass