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 | |
| 75 | voltha_url = self.get_voltha_info(o)['url'] |
| 76 | |
| 77 | data = { |
| 78 | "type": o.device_type, |
| 79 | "host_and_port": "%s:%s" % (o.host, o.port) |
| 80 | } |
| 81 | |
| 82 | if o.device_type == 'simulated_olt': |
| 83 | # simulated devices won't accept host and port, for testing only |
| 84 | data.pop('host_and_port') |
| 85 | data['mac_address'] = "00:0c:e2:31:40:00" |
| 86 | |
| 87 | log.info("pushing olt to voltha", data=data) |
| 88 | |
| 89 | r = requests.post(voltha_url + "/api/v1/devices", json=data) |
| 90 | |
| 91 | if r.status_code != 200: |
| 92 | raise Exception("Failed to add device: %s" % r.text) |
| 93 | |
| 94 | log.info("add device response", text=r.text) |
| 95 | |
| 96 | res = r.json() |
| 97 | |
| 98 | print log.info("add device json res", res=res) |
| 99 | |
| 100 | if not res['id']: |
| 101 | raise Exception('VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA') |
| 102 | else: |
| 103 | o.device_id = res['id']; |
| 104 | |
| 105 | # enable device |
| 106 | |
| 107 | r = requests.post(voltha_url + "/api/v1/devices/" + o.device_id + "/enable") |
| 108 | |
| 109 | if r.status_code != 200: |
| 110 | raise Exception("Failed to enable device: %s" % r.text) |
| 111 | |
| 112 | # read state |
| 113 | r = requests.get(voltha_url + "/api/v1/devices/" + o.device_id).json() |
| 114 | while r['oper_status'] == "ACTIVATING": |
| 115 | log.info("Waiting for device %s (%s) to activate" % (o.name, o.device_id)) |
| 116 | sleep(5) |
| 117 | r = requests.get(voltha_url + "/api/v1/devices/" + o.device_id).json() |
| 118 | |
| 119 | o.admin_state = r['admin_state'] |
| 120 | o.oper_status = r['oper_status'] |
| 121 | |
| 122 | # find of_id of device |
| 123 | o.of_id = self.get_of_id_from_device(o) |
| 124 | o.save() |
| 125 | |
| 126 | # add device info to P-ONOS |
| 127 | data = { |
| 128 | "devices": { |
| 129 | o.of_id: { |
| 130 | "basic": { |
| 131 | "driver": o.driver |
| 132 | }, |
| 133 | "accessDevice": { |
| 134 | "uplink": o.uplink, |
| 135 | "vlan": o.vlan |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | onos= self.get_p_onos_info(o) |
| 142 | |
| 143 | r = requests.post(onos['url'] + '/onos/v1/network/configuration/', data=json.dumps(data), auth=HTTPBasicAuth(onos['user'], onos['pass'])) |
| 144 | |
| 145 | if r.status_code != 200: |
| 146 | log.error(r.text) |
| 147 | raise Exception("Failed to add device %s into ONOS" % o.name) |
| 148 | else: |
| 149 | try: |
| 150 | print r.json() |
| 151 | except Exception: |
| 152 | print r.text |
| 153 | |
| 154 | def delete_record(self, o): |
| 155 | |
| 156 | voltha_url = self.get_voltha_info(o)['url'] |
| 157 | onos = self.get_p_onos_info(o) |
| 158 | if not o.device_id: |
| 159 | log.error("Device %s has no device_id" % o.name) |
| 160 | |
| 161 | else: |
| 162 | |
| 163 | # remove the device from ONOS |
| 164 | r = requests.delete(onos['url'] + '/onos/v1/network/configuration/devices/' + o.of_id, auth=HTTPBasicAuth(onos['user'], onos['pass'])) |
| 165 | |
| 166 | if r.status_code != 200: |
| 167 | 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) |
| 168 | raise Exception("Failed to remove device in ONOS") |
| 169 | |
| 170 | # disable the device |
| 171 | r = requests.post(voltha_url + "/api/v1/devices/" + o.device_id + "/disable") |
| 172 | |
| 173 | if r.status_code != 200: |
| 174 | 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) |
| 175 | raise Exception("Failed to disable device in VOLTHA") |
| 176 | |
| 177 | # delete the device |
| 178 | r = requests.delete(voltha_url + "/api/v1/devices/" + o.device_id + "/delete") |
| 179 | |
| 180 | if r.status_code != 200: |
| 181 | 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) |
| 182 | raise Exception("Failed to delete device in VOLTHA") |