Matteo Scandolo | 53d6119 | 2018-06-01 16:11:14 -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 os, sys |
| 16 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 17 | |
| 18 | from helpers import Helpers |
| 19 | |
| 20 | import requests |
| 21 | from multistructlog import create_logger |
| 22 | from requests.auth import HTTPBasicAuth |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 23 | from xossynchronizer.modelaccessor import ONUDevice, model_accessor |
| 24 | from xossynchronizer.steps.syncstep import SyncStep |
Matteo Scandolo | 53d6119 | 2018-06-01 16:11:14 -0700 | [diff] [blame] | 25 | from xosconfig import Config |
| 26 | |
| 27 | log = create_logger(Config().get("logging")) |
| 28 | |
| 29 | class SyncONUDevice(SyncStep): |
| 30 | provides = [ONUDevice] |
| 31 | |
| 32 | observes = ONUDevice |
| 33 | |
Matteo Scandolo | e3daaa2 | 2018-06-19 14:36:31 -0700 | [diff] [blame] | 34 | def disable_onu(self, o): |
| 35 | volt_service = o.pon_port.olt_device.volt_service |
| 36 | voltha = Helpers.get_voltha_info(volt_service) |
| 37 | |
| 38 | log.info("Disabling device %s in voltha" % o.device_id) |
| 39 | request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], o.device_id)) |
| 40 | |
| 41 | if request.status_code != 200: |
| 42 | raise Exception("Failed to disable ONU device %s: %s" % (o.serial_number, request.text)) |
| 43 | |
| 44 | def enable_onu(self, o): |
| 45 | volt_service = o.pon_port.olt_device.volt_service |
| 46 | voltha = Helpers.get_voltha_info(volt_service) |
| 47 | |
| 48 | log.info("Enabling device %s in voltha" % o.device_id) |
| 49 | request = requests.post("%s:%d/api/v1/devices/%s/enable" % (voltha['url'], voltha['port'], o.device_id)) |
| 50 | |
| 51 | if request.status_code != 200: |
| 52 | raise Exception("Failed to enable ONU device %s: %s" % (o.serial_number, request.text)) |
| 53 | |
Matteo Scandolo | 53d6119 | 2018-06-01 16:11:14 -0700 | [diff] [blame] | 54 | def sync_record(self, o): |
| 55 | |
Andy Bavier | cd94019 | 2019-05-15 12:59:35 -0700 | [diff] [blame] | 56 | if o.admin_state in ["DISABLED", "ADMIN_DISABLED"]: |
Matteo Scandolo | e3daaa2 | 2018-06-19 14:36:31 -0700 | [diff] [blame] | 57 | self.disable_onu(o) |
| 58 | if o.admin_state == "ENABLED": |
| 59 | self.enable_onu(o) |