blob: 9ca767871bd6f432e23f01f4373e5088b14a21f0 [file] [log] [blame]
Matteo Scandolo75298822018-05-22 11:25:42 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import json
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070016from synchronizers.new_base.syncstep import SyncStep, model_accessor
Matteo Scandolo75298822018-05-22 11:25:42 -070017from synchronizers.new_base.modelaccessor import HippieOSSServiceInstance
18
19from xosconfig import Config
20from multistructlog import create_logger
21
22log = create_logger(Config().get('logging'))
23
24class SyncOSSServiceInstance(SyncStep):
25 provides = [HippieOSSServiceInstance]
26 observes = HippieOSSServiceInstance
27
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070028 def validate_in_external_oss(self, si):
Matteo Scandolo75298822018-05-22 11:25:42 -070029 # This is where you may want to call your OSS Database to verify if this ONU can be activated
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070030
Scott Baker3685b042018-07-09 12:43:46 -070031 # for demonstration the HippieOSSService has a whitelist and if the serial_number
32 # you provided is not in that blacklist, it won't be validated
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070033 oss_service = si.owner.leaf_model
34
Scott Baker3685b042018-07-09 12:43:46 -070035 if si.serial_number not in [x.strip() for x in oss_service.whitelist.split(',')]:
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070036 return False
Matteo Scandolo75298822018-05-22 11:25:42 -070037 return True
38
39 def get_suscriber_c_tag(self, serial_number):
40 # If it's up to your OSS to generate c_tags, fetch them here
41 # otherwise XOS will generate one for your subscriber
42 return None
43
44 def sync_record(self, o):
45 log.info("synching HippieOSSServiceInstance", object=str(o), **o.tologdict())
46
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070047 if not self.validate_in_external_oss(o):
48 log.error("ONU with serial number %s is not valid in the OSS Database" % o.serial_number)
49 o.valid = "invalid"
50 else:
51 if self.get_suscriber_c_tag(o.serial_number):
52 self.c_tag = self.get_suscriber_c_tag(o.serial_number)
Matteo Scandolo75298822018-05-22 11:25:42 -070053
Matteo Scandolo5a0eed22018-06-01 14:42:43 -070054 o.valid = "valid"
Matteo Scandolo75298822018-05-22 11:25:42 -070055
Scott Baker249e84e2018-07-09 16:16:28 -070056 # Set no_sync=True to prevent the syncstep from running again, and set alway_update_timestamp=True to cause
57 # the model_policy to run again.
58 # TODO(smbaker): Revisit this after fixing this issue in the core.
Matteo Scandolo75298822018-05-22 11:25:42 -070059 o.no_sync = True
Scott Baker249e84e2018-07-09 16:16:28 -070060 o.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True)
Matteo Scandolo75298822018-05-22 11:25:42 -070061
62 def delete_record(self, o):
63 pass