Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -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 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 15 | import os, sys |
| 16 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 17 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 18 | from helpers import Helpers |
| 19 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 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 VOLTService, VOLTServiceInstance, ServiceInstance, model_accessor |
| 24 | from xossynchronizer.steps.syncstep import SyncStep, DeferredException |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 25 | from xosconfig import Config |
| 26 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 27 | log = create_logger(Config().get("logging")) |
| 28 | |
| 29 | class SyncVOLTServiceInstance(SyncStep): |
| 30 | provides = [VOLTServiceInstance] |
| 31 | |
| 32 | observes = VOLTServiceInstance |
| 33 | |
| 34 | def sync_record(self, o): |
| 35 | |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame] | 36 | if o.policy_code != 1: |
| 37 | raise DeferredException("Waiting for ModelPolicy to complete") |
| 38 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 39 | volt_service = VOLTService.objects.get(id=o.owner_id) |
| 40 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 41 | log.info("Synching OLTServiceInstance", object=str(o), **o.tologdict()) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 42 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 43 | olt_device = o.onu_device.pon_port.olt_device |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 44 | |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame] | 45 | try: |
| 46 | # NOTE each ONU has only one UNI port! |
| 47 | uni_port_id = o.onu_device.uni_ports.first().port_no |
| 48 | except AttributeError: |
| 49 | # This is because the ONUDevice is set by model_policy |
| 50 | raise DeferredException("Waiting for ONUDevice %s " % olt_device.name) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 51 | |
| 52 | if not olt_device.dp_id: |
| 53 | raise DeferredException("Waiting for OLTDevice %s to be synchronized" % olt_device.name) |
| 54 | |
| 55 | log.debug("Adding subscriber with info", |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 56 | uni_port_id = uni_port_id, |
Matteo Scandolo | 25ad290 | 2018-08-14 17:02:05 -0700 | [diff] [blame] | 57 | dp_id = olt_device.dp_id |
| 58 | ) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 59 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 60 | # Send request to ONOS |
| 61 | onos_voltha = Helpers.get_onos_voltha_info(volt_service) |
| 62 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 63 | |
Matteo Scandolo | 1835882 | 2018-08-15 17:17:43 -0700 | [diff] [blame] | 64 | handle = "%s/%s" % (olt_device.dp_id, uni_port_id) |
| 65 | |
| 66 | full_url = "%s:%d/onos/olt/oltapp/%s" % (onos_voltha['url'], onos_voltha['port'], handle) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 67 | |
Matteo Scandolo | 25ad290 | 2018-08-14 17:02:05 -0700 | [diff] [blame] | 68 | log.info("Sending request to onos-voltha", url=full_url) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 69 | |
Matteo Scandolo | 25ad290 | 2018-08-14 17:02:05 -0700 | [diff] [blame] | 70 | request = requests.post(full_url, auth=onos_voltha_basic_auth) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 71 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 72 | if request.status_code != 200: |
| 73 | raise Exception("Failed to add subscriber in onos-voltha: %s" % request.text) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 74 | |
Matteo Scandolo | 1835882 | 2018-08-15 17:17:43 -0700 | [diff] [blame] | 75 | o.backend_handle = handle |
| 76 | o.save(update_fields=["backend_handle"]) |
| 77 | |
Matteo Scandolo | e48a464 | 2018-06-26 10:08:34 -0700 | [diff] [blame] | 78 | log.info("Added Subscriber in onos voltha", response=request.text) |
| 79 | |
| 80 | def delete_record(self, o): |
| 81 | |
| 82 | log.info("Removing OLTServiceInstance", object=str(o), **o.tologdict()) |
| 83 | |
| 84 | volt_service = VOLTService.objects.get(id=o.owner_id) |
| 85 | onos_voltha = Helpers.get_onos_voltha_info(volt_service) |
| 86 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
| 87 | |
Matteo Scandolo | 1835882 | 2018-08-15 17:17:43 -0700 | [diff] [blame] | 88 | full_url = "%s:%d/onos/olt/oltapp/%s" % (onos_voltha['url'], onos_voltha['port'], o.backend_handle) |
Matteo Scandolo | e48a464 | 2018-06-26 10:08:34 -0700 | [diff] [blame] | 89 | |
| 90 | request = requests.delete(full_url, auth=onos_voltha_basic_auth) |
| 91 | |
| 92 | if request.status_code != 204: |
| 93 | raise Exception("Failed to remove subscriber from onos-voltha: %s" % request.text) |
| 94 | |
| 95 | log.info("Removed Subscriber from onos voltha", response=request.text) |