blob: 5dc7221b24f406244032b11148e70830ac182bea [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 Scandolo75298822018-05-22 11:25:42 -070027 def handle_update(self, si):
Scott Baker249e84e2018-07-09 16:16:28 -070028 self.logger.debug("MODEL_POLICY: handle_update for HippieOSSServiceInstance %s, valid=%s " % (si.id, si.valid))
29
30 # Check to make sure the object has been synced. This is to cover a race condition where the model_policy
31 # runs, is interrupted by the sync step, the sync step completes, and then the model policy ends up saving
32 # a policed_timestamp that is later the updated timestamp set by the sync_step.
33 if (si.backend_code!=1):
34 raise Exception("MODEL_POLICY: HippieOSSServiceInstance %s has not been synced yet" % si.id)
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070035
36 if not hasattr(si, 'valid') or si.valid is "awaiting":
37 self.logger.debug("MODEL_POLICY: skipping handle_update for HippieOSSServiceInstance %s as not validated yet" % si.id)
38 return
39 if si.valid == "invalid":
40 self.logger.debug("MODEL_POLICY: disabling ONUDevice [%s] for HippieOSSServiceInstance %s" % (si.serial_number, si.id))
41 onu = ONUDevice.objects.get(serial_number=si.serial_number)
42 onu.admin_state = "DISABLED"
43 onu.save(always_update_timestamp=True)
44 return
45 if si.valid == "valid":
Matteo Scandolo75298822018-05-22 11:25:42 -070046
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070047 # reactivating the ONUDevice
Matteo Scandoloce85bca2018-06-25 16:57:16 -070048 try:
49 onu = ONUDevice.objects.get(serial_number=si.serial_number)
50 except IndexError:
51 raise Exception("MODEL_POLICY: cannot find ONUDevice [%s] for HippieOSSServiceInstance %s" % (si.serial_number, si.id))
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070052 if onu.admin_state == "DISABLED":
Matteo Scandoloce85bca2018-06-25 16:57:16 -070053 self.logger.debug("MODEL_POLICY: enabling ONUDevice [%s] for HippieOSSServiceInstance %s" % (si.serial_number, si.id))
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070054 onu.admin_state = "ENABLED"
55 onu.save(always_update_timestamp=True)
56
57 # NOTE this assumes that an ONUDevice has only one Subscriber
58 try:
Matteo Scandoloce85bca2018-06-25 16:57:16 -070059 subscriber_changed = False
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070060 subscriber = RCORDSubscriber.objects.get(onu_device=si.serial_number)
Matteo Scandoloce85bca2018-06-25 16:57:16 -070061 self.logger.debug("MODEL_POLICY: found subscriber for valid ONU", onu=si.serial_number)
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070062
63 # If the OSS returns a c_tag and the subscriber doesn't already have his one
64 if si.c_tag and not subscriber.c_tag:
65 self.logger.debug("MODEL_POLICY: updating c_tag for RCORDSubscriber %s and HippieOSSServiceInstance %s" % (subscriber.id, si.id))
66 subscriber.c_tag = si.c_tag
Matteo Scandoloce85bca2018-06-25 16:57:16 -070067 subscriber_changed = True
68
69 # if the subscriber was in pre-provisioned state, change it's status, otherwise leave it as is
70 if subscriber.status == "pre-provisioned":
71 subscriber.status = "awaiting-auth"
72 self.logger.debug("MODEL_POLICY: setting subscriber status", status=subscriber.status)
73 subscriber_changed = True
74
75 if not subscriber_changed:
76 # do not trigger an update unless it's needed
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070077 return
Matteo Scandoloce85bca2018-06-25 16:57:16 -070078 except IndexError:
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070079 self.logger.debug("MODEL_POLICY: creating RCORDSubscriber for HippieOSSServiceInstance %s" % si.id)
80
81 subscriber = RCORDSubscriber()
82 subscriber.onu_device = si.serial_number
Matteo Scandoloce85bca2018-06-25 16:57:16 -070083 subscriber.status == "awaiting-auth"
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070084
85 # If the OSS returns a c_tag use that one
86 if si.c_tag:
87 subscriber.c_tag = si.c_tag
Matteo Scandolo75298822018-05-22 11:25:42 -070088
Matteo Scandoloce85bca2018-06-25 16:57:16 -070089 subscriber.save(always_update_timestamp=True)
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070090 return
Matteo Scandolo75298822018-05-22 11:25:42 -070091
92 def handle_delete(self, si):
93 pass