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