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 |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 23 | from synchronizers.new_base.modelaccessor import VOLTService, VOLTServiceInstance, ServiceInstance, ONUDevice, model_accessor |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 24 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
| 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 | |
| 36 | volt_service = VOLTService.objects.get(id=o.owner_id) |
| 37 | |
| 38 | si = ServiceInstance.objects.get(id=o.id) |
| 39 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 40 | log.info("Synching OLTServiceInstance", object=str(o), **o.tologdict()) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 41 | |
| 42 | c_tag = si.get_westbound_service_instance_properties("c_tag") |
| 43 | uni_port_id = si.get_westbound_service_instance_properties("uni_port_id") |
| 44 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 45 | onu_device_name = si.get_westbound_service_instance_properties("onu_device") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 46 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 47 | onu_device = ONUDevice.objects.get(serial_number=onu_device_name) |
| 48 | |
| 49 | olt_device = onu_device.pon_port.olt_device |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 50 | |
| 51 | if not olt_device.dp_id: |
| 52 | raise DeferredException("Waiting for OLTDevice %s to be synchronized" % olt_device.name) |
| 53 | |
| 54 | log.debug("Adding subscriber with info", |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 55 | c_tag = c_tag, |
| 56 | uni_port_id = uni_port_id, |
| 57 | dp_id = olt_device.dp_id) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 58 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 59 | # Send request to ONOS |
| 60 | onos_voltha = Helpers.get_onos_voltha_info(volt_service) |
| 61 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 62 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 63 | full_url = "%s:%d/onos/olt/oltapp/%s/%s/%s" % (onos_voltha['url'], onos_voltha['port'], olt_device.dp_id, uni_port_id, c_tag) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 64 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 65 | log.info("Sending request to onos-voltha", url=full_url) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 66 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 67 | request = requests.post(full_url, auth=onos_voltha_basic_auth) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 68 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 69 | if request.status_code != 200: |
| 70 | raise Exception("Failed to add subscriber in onos-voltha: %s" % request.text) |
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 | log.info("onos voltha response", response=request.text) |