Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [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.pullstep import PullStep |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 16 | from synchronizers.new_base.modelaccessor import model_accessor, OLTDevice, VOLTService |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 17 | |
| 18 | from xosconfig import Config |
| 19 | from multistructlog import create_logger |
| 20 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 21 | import requests |
| 22 | from requests import ConnectionError |
| 23 | from requests.models import InvalidURL |
| 24 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame^] | 25 | import os, sys |
| 26 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 27 | |
| 28 | from helpers import Helpers |
| 29 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 30 | log = create_logger(Config().get('logging')) |
| 31 | |
| 32 | class OLTDevicePullStep(PullStep): |
| 33 | def __init__(self): |
| 34 | super(OLTDevicePullStep, self).__init__(observed_model=OLTDevice) |
| 35 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 36 | @staticmethod |
| 37 | def get_ids_from_logical_device(o): |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame^] | 38 | voltha_url = Helpers.get_voltha_info(o.volt_service)['url'] |
| 39 | voltha_port = Helpers.get_voltha_info(o.volt_service)['port'] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 40 | |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 41 | r = requests.get("%s:%s/api/v1/logical_devices" % (voltha_url, voltha_port)) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 42 | |
| 43 | if r.status_code != 200: |
| 44 | raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % r.text) |
| 45 | |
| 46 | res = r.json() |
| 47 | |
| 48 | for ld in res["items"]: |
| 49 | if ld["root_device_id"] == o.device_id: |
| 50 | o.of_id = ld["id"] |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame^] | 51 | o.dp_id = "of:" + Helpers.datapath_id_to_hex(ld["datapath_id"]) # convert to hex |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 52 | return o |
| 53 | |
| 54 | raise Exception("Can't find a logical device for device id: %s" % o.device_id) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 55 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 56 | def pull_records(self): |
| 57 | log.info("pulling OLT devices from VOLTHA") |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 58 | |
| 59 | try: |
| 60 | self.volt_service = VOLTService.objects.all()[0] |
| 61 | except IndexError: |
| 62 | log.warn('VOLTService not found') |
| 63 | return |
| 64 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame^] | 65 | voltha_url = Helpers.get_voltha_info(self.volt_service)['url'] |
| 66 | voltha_port = Helpers.get_voltha_info(self.volt_service)['port'] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 67 | |
| 68 | try: |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 69 | r = requests.get("%s:%s/api/v1/devices" % (voltha_url, voltha_port)) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 70 | |
| 71 | if r.status_code != 200: |
| 72 | log.info("It was not possible to fetch devices from VOLTHA") |
| 73 | |
| 74 | # keeping only OLTs |
| 75 | devices = [d for d in r.json()["items"] if "olt" in d["type"]] |
| 76 | |
| 77 | log.debug("received devices", olts=devices) |
| 78 | |
| 79 | # TODO |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 80 | # [ ] delete OLTS as OLTDevice.objects.all() - updated OLTs |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 81 | |
| 82 | if r.status_code != 200: |
| 83 | log.info("It was not possible to fetch devices from VOLTHA") |
| 84 | |
| 85 | olts_in_voltha = self.create_or_update_olts(devices) |
| 86 | |
| 87 | except ConnectionError, e: |
| 88 | log.warn("It was not possible to connect to VOLTHA", reason=e) |
| 89 | return |
| 90 | except InvalidURL, e: |
| 91 | log.warn("VOLTHA url is invalid, is it configured in the VOLTService?", reason=e) |
| 92 | return |
| 93 | |
| 94 | def create_or_update_olts(self, olts): |
| 95 | |
| 96 | updated_olts = [] |
| 97 | |
| 98 | for olt in olts: |
| 99 | try: |
| 100 | if olt["type"] == "simulated_olt": |
| 101 | [host, port] = ["172.17.0.1", "50060"] |
| 102 | else: |
| 103 | [host, port] = olt["host_and_port"].split(":") |
| 104 | model = OLTDevice.objects.filter(device_type=olt["type"], host=host, port=port)[0] |
| 105 | log.debug("OLTDevice already exists, updating it", device_type=olt["type"], host=host, port=port) |
| 106 | |
| 107 | if model.enacted < model.updated: |
| 108 | log.info("Skipping pull on OLTDevice %s as enacted < updated" % model.name, name=model.name, id=model.id, enacted=model.enacted, updated=model.updated) |
| 109 | return |
| 110 | |
| 111 | except IndexError: |
| 112 | model = OLTDevice() |
| 113 | model.device_type = olt["type"] |
| 114 | |
| 115 | if olt["type"] == "simulated_olt": |
| 116 | model.host = "172.17.0.1" |
| 117 | model.port = 50060 |
| 118 | |
| 119 | log.debug("OLTDevice is new, creating it", device_type=olt["type"], host=host, port=port) |
| 120 | |
| 121 | # Adding feedback state to the device |
| 122 | model.device_id = olt["id"] |
| 123 | model.admin_state = olt["admin_state"] |
| 124 | model.oper_status = olt["oper_status"] |
| 125 | |
| 126 | model.volt_service = self.volt_service |
| 127 | model.volt_service_id = self.volt_service.id |
| 128 | |
| 129 | # get logical device |
| 130 | OLTDevicePullStep.get_ids_from_logical_device(model) |
| 131 | |
| 132 | model.save() |
| 133 | |
| 134 | updated_olts.append(model) |
| 135 | |
| 136 | return updated_olts |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |