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 | |
Scott Baker | 3685b04 | 2018-07-09 12:43:46 -0700 | [diff] [blame] | 31 | # 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 Scandolo | 5a0eed2 | 2018-06-01 14:42:43 -0700 | [diff] [blame] | 33 | oss_service = si.owner.leaf_model |
| 34 | |
Scott Baker | 3685b04 | 2018-07-09 12:43:46 -0700 | [diff] [blame] | 35 | if si.serial_number not in [x.strip() for x in oss_service.whitelist.split(',')]: |
Matteo Scandolo | 5a0eed2 | 2018-06-01 14:42:43 -0700 | [diff] [blame] | 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 | |
Scott Baker | 249e84e | 2018-07-09 16:16:28 -0700 | [diff] [blame^] | 56 | # 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 Scandolo | 7529882 | 2018-05-22 11:25:42 -0700 | [diff] [blame] | 59 | o.no_sync = True |
Scott Baker | 249e84e | 2018-07-09 16:16:28 -0700 | [diff] [blame^] | 60 | o.save(update_fields=["valid", "no_sync", "updated"], always_update_timestamp=True) |
Matteo Scandolo | 7529882 | 2018-05-22 11:25:42 -0700 | [diff] [blame] | 61 | |
| 62 | def delete_record(self, o): |
| 63 | pass |