blob: 3008a57ef43e5e455c4bce9852b7a1822ade39ec [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
38 onu = ONUDevice.objects.get(serial_number=si.serial_number)
39 if onu.admin_state == "DISABLED":
40 self.logger.debug("MODEL_POLICY: enabling ONUDevice [%s] for HippieOSSServiceInstance %s" % (
41 si.serial_number, si.id))
42 onu.admin_state = "ENABLED"
43 onu.save(always_update_timestamp=True)
44
45 # NOTE this assumes that an ONUDevice has only one Subscriber
46 try:
47 subscriber = RCORDSubscriber.objects.get(onu_device=si.serial_number)
48
49 # If the OSS returns a c_tag and the subscriber doesn't already have his one
50 if si.c_tag and not subscriber.c_tag:
51 self.logger.debug("MODEL_POLICY: updating c_tag for RCORDSubscriber %s and HippieOSSServiceInstance %s" % (subscriber.id, si.id))
52 subscriber.c_tag = si.c_tag
53 else:
54 # if we're not changing anything in the subscriber, we don't need to update it
55 return
56 except IndexError, e:
57 self.logger.debug("MODEL_POLICY: creating RCORDSubscriber for HippieOSSServiceInstance %s" % si.id)
58
59 subscriber = RCORDSubscriber()
60 subscriber.onu_device = si.serial_number
61
62 # If the OSS returns a c_tag use that one
63 if si.c_tag:
64 subscriber.c_tag = si.c_tag
Matteo Scandolo75298822018-05-22 11:25:42 -070065
66 subscriber.save()
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070067 return
Matteo Scandolo75298822018-05-22 11:25:42 -070068
69 def handle_delete(self, si):
70 pass