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 | |
| 15 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
| 16 | from synchronizers.new_base.modelaccessor import model_accessor |
| 17 | from synchronizers.new_base.modelaccessor import VOLTService, VOLTServiceInstance, ServiceInstance, OLTDevice |
| 18 | |
| 19 | from xosconfig import Config |
| 20 | from multistructlog import create_logger |
| 21 | import requests |
| 22 | from requests.auth import HTTPBasicAuth |
| 23 | from helpers import Helpers |
| 24 | |
| 25 | log = create_logger(Config().get("logging")) |
| 26 | |
| 27 | class SyncVOLTServiceInstance(SyncStep): |
| 28 | provides = [VOLTServiceInstance] |
| 29 | |
| 30 | observes = VOLTServiceInstance |
| 31 | |
| 32 | def sync_record(self, o): |
| 33 | |
| 34 | volt_service = VOLTService.objects.get(id=o.owner_id) |
| 35 | |
| 36 | si = ServiceInstance.objects.get(id=o.id) |
| 37 | |
| 38 | log.info("sync'ing OLTServiceInstance", object=str(o), **o.tologdict()) |
| 39 | |
| 40 | c_tag = si.get_westbound_service_instance_properties("c_tag") |
| 41 | uni_port_id = si.get_westbound_service_instance_properties("uni_port_id") |
| 42 | |
| 43 | olt_device_name = si.get_westbound_service_instance_properties("olt_device") |
| 44 | |
| 45 | olt_device = OLTDevice.objects.get(name=olt_device_name) |
| 46 | |
| 47 | if not olt_device.dp_id: |
| 48 | raise DeferredException("Waiting for OLTDevice %s to be synchronized" % olt_device.name) |
| 49 | |
| 50 | log.debug("Adding subscriber with info", |
| 51 | c_tag=c_tag, |
| 52 | uni_port_id=uni_port_id, |
| 53 | dp_id=olt_device.dp_id) |
| 54 | |
| 55 | # sending request to ONOS |
| 56 | |
| 57 | onos = Helpers.get_p_onos_info(volt_service) |
| 58 | |
| 59 | url = onos['url'] + "/onos/olt/oltapp/%s/%s/%s" % (olt_device.dp_id, uni_port_id, c_tag) |
| 60 | |
| 61 | log.info("sending request to P_ONOS", url=url) |
| 62 | |
| 63 | r = requests.post(url,auth=HTTPBasicAuth(onos['user'], onos['pass'])) |
| 64 | |
| 65 | if r.status_code != 200: |
| 66 | raise Exception("Failed to add subscriber in P_ONOS: %s" % r.text) |
| 67 | |
| 68 | log.info("P_ONOS response", res=r.text) |