Matteo Scandolo | 7529882 | 2018-05-22 11:25:42 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import json |
Matteo Scandolo | 5a0eed2 | 2018-06-01 14:42:43 -0700 | [diff] [blame] | 16 | from synchronizers.new_base.syncstep import SyncStep, model_accessor |
Matteo Scandolo | 7529882 | 2018-05-22 11:25:42 -0700 | [diff] [blame] | 17 | from synchronizers.new_base.modelaccessor import HippieOSSServiceInstance |
| 18 | |
| 19 | from xosconfig import Config |
| 20 | from multistructlog import create_logger |
| 21 | |
| 22 | log = create_logger(Config().get('logging')) |
| 23 | |
| 24 | class SyncOSSServiceInstance(SyncStep): |
| 25 | provides = [HippieOSSServiceInstance] |
| 26 | observes = HippieOSSServiceInstance |
| 27 | |
Matteo Scandolo | 5a0eed2 | 2018-06-01 14:42:43 -0700 | [diff] [blame] | 28 | def validate_in_external_oss(self, si): |
Matteo Scandolo | 7529882 | 2018-05-22 11:25:42 -0700 | [diff] [blame] | 29 | # This is where you may want to call your OSS Database to verify if this ONU can be activated |
Matteo Scandolo | 5a0eed2 | 2018-06-01 14:42:43 -0700 | [diff] [blame] | 30 | |
| 31 | # for demonstration the HippieOSSService has a blacklist and if the serial_number |
| 32 | # you provided is in that blacklist, it won't be validated |
| 33 | oss_service = si.owner.leaf_model |
| 34 | |
| 35 | if si.serial_number in [x.strip() for x in oss_service.blacklist.split(',')]: |
| 36 | return False |
Matteo Scandolo | 7529882 | 2018-05-22 11:25:42 -0700 | [diff] [blame] | 37 | 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 Scandolo | 5a0eed2 | 2018-06-01 14:42:43 -0700 | [diff] [blame] | 47 | 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 Scandolo | 7529882 | 2018-05-22 11:25:42 -0700 | [diff] [blame] | 53 | |
Matteo Scandolo | 5a0eed2 | 2018-06-01 14:42:43 -0700 | [diff] [blame] | 54 | o.valid = "valid" |
Matteo Scandolo | 7529882 | 2018-05-22 11:25:42 -0700 | [diff] [blame] | 55 | |
Matteo Scandolo | ce85bca | 2018-06-25 16:57:16 -0700 | [diff] [blame] | 56 | # we need to update the timestamp to run model_policies again, but we don't want to loop over the sync_steps |
| 57 | # maybe we need an "after_sync" model policy method? |
Matteo Scandolo | 7529882 | 2018-05-22 11:25:42 -0700 | [diff] [blame] | 58 | o.no_sync = True |
| 59 | o.save(always_update_timestamp=True) |
| 60 | |
| 61 | def delete_record(self, o): |
| 62 | pass |