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 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 35 | max_attempt = 120 # we give 10 minutes to the OLT to activate |
| 36 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 37 | @staticmethod |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 38 | def get_ids_from_logical_device(o): |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 39 | voltha = Helpers.get_voltha_info(o.volt_service) |
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 | 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] | 42 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 43 | if request.status_code != 200: |
| 44 | raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % request.text) |
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 | response = request.json() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 47 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 48 | for ld in response["items"]: |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 49 | if ld["root_device_id"] == o.device_id: |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 50 | o.of_id = ld["id"] |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 51 | 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] | 52 | return o |
| 53 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 54 | 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] | 55 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 56 | def pre_provision_olt_device(self, model): |
| 57 | 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] | 58 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 59 | voltha = Helpers.get_voltha_info(model.volt_service) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 60 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 61 | data = { |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 62 | "type": model.device_type |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 65 | if hasattr(model, "host") and hasattr(model, "port"): |
| 66 | data["host_and_port"] = "%s:%s" % (model.host, model.port) |
| 67 | elif hasattr(model, "mac_address"): |
| 68 | data["mac_address"] = model.mac_address |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 69 | |
| 70 | log.info("Pushing OLT to Voltha", data=data) |
| 71 | |
| 72 | request = requests.post("%s:%d/api/v1/devices" % (voltha['url'], voltha['port']), json=data) |
| 73 | |
| 74 | if request.status_code != 200: |
| 75 | raise Exception("Failed to add OLT device: %s" % request.text) |
| 76 | |
| 77 | log.info("Add device response", text=request.text) |
| 78 | |
| 79 | res = request.json() |
| 80 | |
| 81 | log.info("Add device json res", res=res) |
| 82 | |
| 83 | if not res['id']: |
| 84 | raise Exception( |
| 85 | 'VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA') |
| 86 | else: |
| 87 | model.device_id = res['id']; |
| 88 | |
| 89 | return model |
| 90 | |
| 91 | def activate_olt(self, model): |
| 92 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 93 | attempted = 0 |
| 94 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 95 | voltha = Helpers.get_voltha_info(model.volt_service) |
| 96 | |
| 97 | # Enable device |
| 98 | request = requests.post("%s:%d/api/v1/devices/%s/enable" % (voltha['url'], voltha['port'], model.device_id)) |
| 99 | |
| 100 | if request.status_code != 200: |
| 101 | raise Exception("Failed to enable OLT device: %s" % request.text) |
| 102 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 103 | model.backend_status = "Waiting for device to be activated" |
| 104 | model.save(always_update_timestamp=False) # we don't want to kickoff a new loop |
| 105 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 106 | # Read state |
| 107 | request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json() |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 108 | while request['oper_status'] == "ACTIVATING" and attempted < self.max_attempt: |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 109 | log.info("Waiting for OLT device %s (%s) to activate" % (model.name, model.device_id)) |
| 110 | sleep(5) |
| 111 | request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json() |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 112 | attempted = attempted + 1 |
| 113 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 114 | |
| 115 | model.admin_state = request['admin_state'] |
| 116 | model.oper_status = request['oper_status'] |
| 117 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 118 | if model.oper_status != "ACTIVE": |
| 119 | raise Exception("It was not possible to activate OLTDevice with id %s" % model.id) |
| 120 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 121 | # Find the of_id of the device |
| 122 | model = self.get_ids_from_logical_device(model) |
| 123 | model.save() |
| 124 | |
| 125 | return model |
| 126 | |
| 127 | def configure_onos(self, model): |
| 128 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 129 | log.info("Adding OLT device in onos-voltha", object=str(model), **model.tologdict()) |
| 130 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 131 | onos_voltha = Helpers.get_onos_voltha_info(model.volt_service) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 132 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 133 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 134 | try: |
| 135 | # NOTE For now, we assume that each OLT has only one pon port |
| 136 | vlan = model.pon_ports.all()[0].s_tag |
| 137 | except Exception as e: |
| 138 | raise DeferredException("Waiting for pon_ports to come up") |
| 139 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 140 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 141 | # Add device info to onos-voltha |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 142 | data = { |
| 143 | "devices": { |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 144 | model.dp_id: { |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 145 | "basic": { |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 146 | "driver": model.driver |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 147 | }, |
| 148 | "accessDevice": { |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 149 | "uplink": model.uplink, |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 150 | "vlan": vlan |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 156 | url = "%s:%d/onos/v1/network/configuration/" % (onos_voltha['url'], onos_voltha['port']) |
| 157 | request = requests.post(url, json=data, auth=onos_voltha_basic_auth) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 158 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 159 | if request.status_code != 200: |
| 160 | log.error(request.text) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 161 | raise Exception("Failed to add OLT device %s into ONOS" % model.name) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 162 | else: |
| 163 | try: |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 164 | print request.json() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 165 | except Exception: |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 166 | print request.text |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 167 | return model |
| 168 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 169 | def sync_record(self, model): |
| 170 | log.info("Synching device", object=str(model), **model.tologdict()) |
| 171 | |
| 172 | # If the device has feedback_state is already present in voltha |
| 173 | if not model.device_id and not model.admin_state and not model.oper_status and not model.of_id: |
| 174 | log.info("Pushing OLT device to VOLTHA", object=str(model), **model.tologdict()) |
| 175 | model = self.pre_provision_olt_device(model) |
| 176 | self.activate_olt(model) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 177 | elif model.oper_status != "ACTIVE": |
| 178 | raise Exception("It was not possible to activate OLTDevice with id %s" % model.id) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 179 | else: |
| 180 | log.info("OLT device already exists in VOLTHA", object=str(model), **model.tologdict()) |
| 181 | |
Matteo Scandolo | 88709c4 | 2018-06-20 16:59:38 -0700 | [diff] [blame] | 182 | # TODO configure onos only if we have: Switch datapath id, Switch port, Uplink |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 183 | if model.switch_datapath_id and model.switch_port and model.uplink: |
| 184 | log.info("Pushing OLT device to ONOS-VOLTHA", object=str(model), **model.tologdict()) |
| 185 | self.configure_onos(model) |
| 186 | else: |
| 187 | raise DeferredException("Not pushing OLTDevice (%s) to ONOS-VOLTHA as parameters are missing" % model.id) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 188 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 189 | def delete_record(self, model): |
| 190 | log.info("Deleting OLT device", object=str(model), **model.tologdict()) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 191 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 192 | voltha = Helpers.get_voltha_info(model.volt_service) |
| 193 | onos_voltha = Helpers.get_onos_voltha_info(model.volt_service) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 194 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
| 195 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 196 | if not model.device_id: |
| 197 | log.error("OLTDevice %s has no device_id" % model.name) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 198 | else: |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 199 | # Disable the OLT device |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 200 | 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] | 201 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 202 | if request.status_code != 200: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 203 | 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] | 204 | raise Exception("Failed to disable OLT device in VOLTHA") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 205 | |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 206 | # Delete the OLT device |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 207 | 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] | 208 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 209 | if request.status_code != 200: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 210 | 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] | 211 | raise Exception("Failed to delete OLT device from VOLTHA") |
| 212 | |
| 213 | # Remove the device from ONOS |
| 214 | request = requests.delete("%s:%d/onos/v1/network/configuration/devices/%s" % ( |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 215 | 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] | 216 | |
| 217 | if request.status_code != 204: |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 218 | 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] | 219 | raise Exception("Failed to remove OLT device from ONOS") |