blob: 16a4c3b5397a8a0ab5473d57b5912e0118a8b9bc [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
23 def handle_update(self, si):
24 self.logger.debug("MODEL_POLICY: handle_update for HippieOSSServiceInstance %s " % si.id)
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070025
26 if not hasattr(si, 'valid') or si.valid is "awaiting":
27 self.logger.debug("MODEL_POLICY: skipping handle_update for HippieOSSServiceInstance %s as not validated yet" % si.id)
28 return
29 if si.valid == "invalid":
30 self.logger.debug("MODEL_POLICY: disabling ONUDevice [%s] for HippieOSSServiceInstance %s" % (si.serial_number, si.id))
31 onu = ONUDevice.objects.get(serial_number=si.serial_number)
32 onu.admin_state = "DISABLED"
33 onu.save(always_update_timestamp=True)
34 return
35 if si.valid == "valid":
Matteo Scandolo75298822018-05-22 11:25:42 -070036
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070037 # reactivating the ONUDevice
Matteo Scandoloce85bca2018-06-25 16:57:16 -070038 try:
39 onu = ONUDevice.objects.get(serial_number=si.serial_number)
40 except IndexError:
41 raise Exception("MODEL_POLICY: cannot find ONUDevice [%s] for HippieOSSServiceInstance %s" % (si.serial_number, si.id))
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070042 if onu.admin_state == "DISABLED":
Matteo Scandoloce85bca2018-06-25 16:57:16 -070043 self.logger.debug("MODEL_POLICY: enabling ONUDevice [%s] for HippieOSSServiceInstance %s" % (si.serial_number, si.id))
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070044 onu.admin_state = "ENABLED"
45 onu.save(always_update_timestamp=True)
46
47 # NOTE this assumes that an ONUDevice has only one Subscriber
48 try:
Matteo Scandoloce85bca2018-06-25 16:57:16 -070049 subscriber_changed = False
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070050 subscriber = RCORDSubscriber.objects.get(onu_device=si.serial_number)
Matteo Scandoloce85bca2018-06-25 16:57:16 -070051 self.logger.debug("MODEL_POLICY: found subscriber for valid ONU", onu=si.serial_number)
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070052
53 # If the OSS returns a c_tag and the subscriber doesn't already have his one
54 if si.c_tag and not subscriber.c_tag:
55 self.logger.debug("MODEL_POLICY: updating c_tag for RCORDSubscriber %s and HippieOSSServiceInstance %s" % (subscriber.id, si.id))
56 subscriber.c_tag = si.c_tag
Matteo Scandoloce85bca2018-06-25 16:57:16 -070057 subscriber_changed = True
58
59 # if the subscriber was in pre-provisioned state, change it's status, otherwise leave it as is
60 if subscriber.status == "pre-provisioned":
61 subscriber.status = "awaiting-auth"
62 self.logger.debug("MODEL_POLICY: setting subscriber status", status=subscriber.status)
63 subscriber_changed = True
64
65 if not subscriber_changed:
66 # do not trigger an update unless it's needed
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070067 return
Matteo Scandoloce85bca2018-06-25 16:57:16 -070068 except IndexError:
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070069 self.logger.debug("MODEL_POLICY: creating RCORDSubscriber for HippieOSSServiceInstance %s" % si.id)
70
71 subscriber = RCORDSubscriber()
72 subscriber.onu_device = si.serial_number
Matteo Scandolo87d896c2018-07-23 15:56:50 -070073 subscriber.status = "awaiting-auth"
Matteo Scandolo3c842ef2018-06-19 15:46:06 -070074
75 # If the OSS returns a c_tag use that one
76 if si.c_tag:
77 subscriber.c_tag = si.c_tag
Matteo Scandolo75298822018-05-22 11:25:42 -070078
Matteo Scandoloce85bca2018-06-25 16:57:16 -070079 subscriber.save(always_update_timestamp=True)
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070080 return
Matteo Scandolo75298822018-05-22 11:25:42 -070081
82 def handle_delete(self, si):
83 pass