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