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