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 | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 49 | raise Exception("Can't find a logical device for device id: %s" % o.device_id) |
| 50 | |
| 51 | |
| 52 | def sync_record(self, o): |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 53 | log.info("Synching device", object=str(o), **o.tologdict()) |
| 54 | |
| 55 | voltha = Helpers.get_voltha_info(o.volt_service) |
| 56 | onos_voltha = Helpers.get_onos_voltha_info(o.volt_service) |
| 57 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 58 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 59 | # If the device has feedback_state is already present in voltha |
| 60 | if not o.device_id and not o.admin_state and not o.oper_status and not o.of_id: |
| 61 | log.info("Pushing device to VOLTHA", object=str(o), **o.tologdict()) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 62 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 63 | data = { |
| 64 | "type": o.device_type, |
| 65 | "host_and_port": "%s:%s" % (o.host, o.port) |
| 66 | } |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 67 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 68 | if o.device_type == 'simulated_olt': |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 69 | # Simulated devices will not accept host and port. This is for test only |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 70 | data.pop('host_and_port') |
| 71 | data['mac_address'] = "00:0c:e2:31:40:00" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 72 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 73 | log.info("Pushing OLT to Voltha", data=data) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 74 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 75 | request = requests.post("%s:%d/api/v1/devices" % (voltha['url'], voltha['port']), json=data) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 76 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 77 | if request.status_code != 200: |
| 78 | raise Exception("Failed to add device: %s" % request.text) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 79 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 80 | log.info("Add device response", text=request.text) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 81 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 82 | res = request.json() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 83 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 84 | log.info("Add device json res", res=res) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 85 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 86 | if not res['id']: |
| 87 | raise Exception('VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA') |
| 88 | else: |
| 89 | o.device_id = res['id']; |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 90 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 91 | # Enable device |
| 92 | request = requests.post("%s:%d/api/v1/devices/%s/enable" % (voltha['url'], voltha['port'], o.device_id)) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 93 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 94 | if request.status_code != 200: |
| 95 | raise Exception("Failed to enable device: %s" % request.text) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 96 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 97 | # Read state |
| 98 | request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], o.device_id)).json() |
| 99 | while request['oper_status'] == "ACTIVATING": |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 100 | log.info("Waiting for device %s (%s) to activate" % (o.name, o.device_id)) |
| 101 | sleep(5) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 102 | request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], o.device_id)).json() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 103 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 104 | o.admin_state = request['admin_state'] |
| 105 | o.oper_status = request['oper_status'] |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 106 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 107 | # Find the of_id of the device |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 108 | o = self.get_ids_from_logical_device(o) |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 109 | o.save() |
| 110 | else: |
| 111 | log.info("Device already exists in VOLTHA", object=str(o), **o.tologdict()) |
| 112 | |
| 113 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 114 | # Do we need to move this synchronization in a PON_PORT specific step? |
| 115 | # For now, we assume that each OLT has only one port |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 116 | vlan = o.ports.all()[0].s_tag |
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 | # Add device info to onos-voltha |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 119 | data = { |
| 120 | "devices": { |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 121 | o.dp_id: { |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 122 | "basic": { |
| 123 | "driver": o.driver |
| 124 | }, |
| 125 | "accessDevice": { |
| 126 | "uplink": o.uplink, |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 127 | "vlan": vlan |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 133 | request = requests.post("%s:%d/onos/v1/network/configuration/" % (onos_voltha['url'], onos_voltha['port']), data=json.dumps(data), auth=onos_voltha_basic_auth) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 134 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 135 | if request.status_code != 200: |
| 136 | log.error(request.text) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 137 | raise Exception("Failed to add device %s into ONOS" % o.name) |
| 138 | else: |
| 139 | try: |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 140 | print request.json() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 141 | except Exception: |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 142 | print request.text |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 143 | |
| 144 | def delete_record(self, o): |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 145 | log.info("Deleting device", object=str(o), **o.tologdict()) |
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 | voltha = Helpers.get_voltha_info(o.volt_service) |
| 148 | onos_voltha = Helpers.get_onos_voltha_info(o.volt_service) |
| 149 | onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass']) |
| 150 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 151 | if not o.device_id: |
| 152 | log.error("Device %s has no device_id" % o.name) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 153 | else: |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 154 | # Remove the device from ONOS |
| 155 | request = requests.delete("%s:%d/onos/v1/network/configuration/devices/%s" % (onos_voltha['url'], onos_voltha['port'], o.of_id), auth=onos_voltha_basic_auth) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 156 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 157 | if request.status_code != 200: |
| 158 | log.error("Failed to remove device from ONOS: %s - %s" % (o.name, o.of_id), rest_responese=request.text, rest_status_code=request.status_code) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 159 | raise Exception("Failed to remove device in ONOS") |
| 160 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 161 | # Disable the device |
| 162 | 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] | 163 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 164 | if request.status_code != 200: |
| 165 | log.error("Failed to disable device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_responese=request.text, rest_status_code=request.status_code) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 166 | raise Exception("Failed to disable device in VOLTHA") |
| 167 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 168 | # Delete the device |
| 169 | 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] | 170 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame^] | 171 | if request.status_code != 200: |
| 172 | log.error("Failed to delete device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_responese=request.text, rest_status_code=request.status_code) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 173 | raise Exception("Failed to delete device in VOLTHA") |