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 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 15 | from time import sleep |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 16 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 17 | import requests |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 18 | from multistructlog import create_logger |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 19 | from requests.auth import HTTPBasicAuth |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 20 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 21 | from synchronizers.new_base.modelaccessor import OLTDevice, model_accessor |
| 22 | from xosconfig import Config |
| 23 | |
| 24 | import os, sys |
| 25 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 26 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 27 | from helpers import Helpers |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 28 | |
| 29 | log = create_logger(Config().get('logging')) |
| 30 | |
| 31 | class SyncOLTDevice(SyncStep): |
| 32 | provides = [OLTDevice] |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 33 | observes = OLTDevice |
| 34 | |
| 35 | @staticmethod |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 36 | def get_ids_from_logical_device(o): |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 37 | voltha = Helpers.get_voltha_info(o.volt_service) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 38 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 39 | request = requests.get("%s:%d/api/v1/logical_devices" % (voltha['url'], voltha['port'])) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 40 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 41 | if request.status_code != 200: |
| 42 | raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % request.text) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 43 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 44 | response = request.json() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 45 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 46 | for ld in response["items"]: |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 47 | if ld["root_device_id"] == o.device_id: |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 48 | o.of_id = ld["id"] |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 49 | o.dp_id = "of:%s" % (Helpers.datapath_id_to_hex(ld["datapath_id"])) # Convert to hex |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 50 | return o |
| 51 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 52 | raise Exception("Can't find a logical_device for OLT device id: %s" % o.device_id) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 53 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 54 | def pre_provision_olt_device(self, model): |
| 55 | log.info("Pre-provisioning OLT device in VOLTHA", object=str(model), **model.tologdict()) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 56 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 57 | voltha = Helpers.get_voltha_info(model.volt_service) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 58 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 59 | data = { |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 60 | "type": model.device_type |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 63 | if hasattr(model, "host") and hasattr(model, "port"): |
| 64 | data["host_and_port"] = "%s:%s" % (model.host, model.port) |
| 65 | elif hasattr(model, "mac_address"): |
| 66 | data["mac_address"] = model.mac_address |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 67 | |
| 68 | log.info("Pushing OLT to Voltha", data=data) |
| 69 | |
| 70 | request = requests.post("%s:%d/api/v1/devices" % (voltha['url'], voltha['port']), json=data) |
| 71 | |
| 72 | if request.status_code != 200: |
| 73 | raise Exception("Failed to add OLT device: %s" % request.text) |
| 74 | |
| 75 | log.info("Add device response", text=request.text) |
| 76 | |
| 77 | res = request.json() |
| 78 | |
| 79 | log.info("Add device json res", res=res) |
| 80 | |
| 81 | if not res['id']: |
| 82 | raise Exception( |
| 83 | 'VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA') |
| 84 | else: |
| 85 | model.device_id = res['id']; |
| 86 | |
| 87 | return model |
| 88 | |
| 89 | def activate_olt(self, model): |
| 90 | |
| 91 | voltha = Helpers.get_voltha_info(model.volt_service) |
| 92 | |
| 93 | # Enable device |
| 94 | request = requests.post("%s:%d/api/v1/devices/%s/enable" % (voltha['url'], voltha['port'], model.device_id)) |
| 95 | |
| 96 | if request.status_code != 200: |
| 97 | raise Exception("Failed to enable OLT device: %s" % request.text) |
| 98 | |
| 99 | # Read state |
| 100 | request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json() |
| 101 | while request['oper_status'] == "ACTIVATING": |
| 102 | log.info("Waiting for OLT device %s (%s) to activate" % (model.name, model.device_id)) |
| 103 | sleep(5) |
| 104 | request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json() |
| 105 | |
| 106 | model.admin_state = request['admin_state'] |
| 107 | model.oper_status = request['oper_status'] |
| 108 | |
| 109 | # Find the of_id of the device |
| 110 | model = self.get_ids_from_logical_device(model) |
| 111 | model.save() |
| 112 | |
| 113 | return model |
| 114 | |
| 115 | def configure_onos(self, model): |
| 116 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 117 | log.info("Adding OLT device in onos-voltha", object=str(model), **model.tologdict()) |
| 118 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 119 | onos_voltha = Helpers.get_onos_voltha_info(model.volt_service) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 120 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 121 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 122 | try: |
| 123 | # NOTE For now, we assume that each OLT has only one pon port |
| 124 | vlan = model.pon_ports.all()[0].s_tag |
| 125 | except Exception as e: |
| 126 | raise DeferredException("Waiting for pon_ports to come up") |
| 127 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 128 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 129 | # Add device info to onos-voltha |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 130 | data = { |
| 131 | "devices": { |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 132 | model.dp_id: { |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 133 | "basic": { |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 134 | "driver": model.driver |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 135 | }, |
| 136 | "accessDevice": { |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 137 | "uplink": model.uplink, |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 138 | "vlan": vlan |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 144 | url = "%s:%d/onos/v1/network/configuration/" % (onos_voltha['url'], onos_voltha['port']) |
| 145 | request = requests.post(url, json=data, auth=onos_voltha_basic_auth) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 146 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 147 | if request.status_code != 200: |
| 148 | log.error(request.text) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 149 | raise Exception("Failed to add OLT device %s into ONOS" % model.name) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 150 | else: |
| 151 | try: |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 152 | print request.json() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 153 | except Exception: |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 154 | print request.text |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 155 | return model |
| 156 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 157 | def sync_record(self, model): |
| 158 | log.info("Synching device", object=str(model), **model.tologdict()) |
| 159 | |
| 160 | # If the device has feedback_state is already present in voltha |
| 161 | if not model.device_id and not model.admin_state and not model.oper_status and not model.of_id: |
| 162 | log.info("Pushing OLT device to VOLTHA", object=str(model), **model.tologdict()) |
| 163 | model = self.pre_provision_olt_device(model) |
| 164 | self.activate_olt(model) |
| 165 | else: |
| 166 | log.info("OLT device already exists in VOLTHA", object=str(model), **model.tologdict()) |
| 167 | |
| 168 | self.configure_onos(model) |
| 169 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 170 | def delete_record(self, model): |
| 171 | log.info("Deleting OLT device", object=str(model), **model.tologdict()) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 172 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 173 | voltha = Helpers.get_voltha_info(model.volt_service) |
| 174 | onos_voltha = Helpers.get_onos_voltha_info(model.volt_service) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 175 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
| 176 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 177 | if not model.device_id: |
| 178 | log.error("OLTDevice %s has no device_id" % model.name) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 179 | else: |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 180 | # Disable the OLT device |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 181 | request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], model.device_id)) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 182 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 183 | if request.status_code != 200: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 184 | log.error("Failed to disable OLT device in VOLTHA: %s - %s" % (model.name, model.device_id), rest_response=request.text, rest_status_code=request.status_code) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 185 | raise Exception("Failed to disable OLT device in VOLTHA") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 186 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 187 | # Delete the OLT device |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 188 | request = requests.delete("%s:%d/api/v1/devices/%s/delete" % (voltha['url'], voltha['port'], model.device_id)) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 189 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 190 | if request.status_code != 200: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 191 | log.error("Failed to delete OLT device from VOLTHA: %s - %s" % (model.name, model.device_id), rest_response=request.text, rest_status_code=request.status_code) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 192 | raise Exception("Failed to delete OLT device from VOLTHA") |
| 193 | |
| 194 | # Remove the device from ONOS |
| 195 | request = requests.delete("%s:%d/onos/v1/network/configuration/devices/%s" % ( |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 196 | onos_voltha['url'], onos_voltha['port'], model.of_id), auth=onos_voltha_basic_auth) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 197 | |
| 198 | if request.status_code != 204: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 199 | log.error("Failed to remove OLT device from ONOS: %s - %s" % (model.name, model.of_id), rest_response=request.text, rest_status_code=request.status_code) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 200 | raise Exception("Failed to remove OLT device from ONOS") |