blob: 056a62c9f5d35fb22dfd6363020efda5657c088d [file] [log] [blame]
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -08001# 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
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080015from time import sleep
Matteo Scandolod44ca992018-05-17 15:02:10 -070016
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080017import requests
Matteo Scandolod44ca992018-05-17 15:02:10 -070018from multistructlog import create_logger
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080019from requests.auth import HTTPBasicAuth
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070020from synchronizers.new_base.syncstep import SyncStep, DeferredException
Matteo Scandolod44ca992018-05-17 15:02:10 -070021from synchronizers.new_base.modelaccessor import OLTDevice, model_accessor
22from xosconfig import Config
23
24import os, sys
25sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
26
Matteo Scandolof6337eb2018-04-05 15:58:37 -070027from helpers import Helpers
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080028
29log = create_logger(Config().get('logging'))
30
31class SyncOLTDevice(SyncStep):
32 provides = [OLTDevice]
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080033 observes = OLTDevice
34
Matteo Scandolo096a3cf2018-06-20 13:56:13 -070035 max_attempt = 120 # we give 10 minutes to the OLT to activate
36
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080037 @staticmethod
Matteo Scandolof6337eb2018-04-05 15:58:37 -070038 def get_ids_from_logical_device(o):
Luca Preteca974c82018-05-01 18:06:16 -070039 voltha = Helpers.get_voltha_info(o.volt_service)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080040
Luca Preteca974c82018-05-01 18:06:16 -070041 request = requests.get("%s:%d/api/v1/logical_devices" % (voltha['url'], voltha['port']))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080042
Luca Preteca974c82018-05-01 18:06:16 -070043 if request.status_code != 200:
44 raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % request.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080045
Luca Preteca974c82018-05-01 18:06:16 -070046 response = request.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080047
Luca Preteca974c82018-05-01 18:06:16 -070048 for ld in response["items"]:
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080049 if ld["root_device_id"] == o.device_id:
Matteo Scandolof6337eb2018-04-05 15:58:37 -070050 o.of_id = ld["id"]
Luca Preteca974c82018-05-01 18:06:16 -070051 o.dp_id = "of:%s" % (Helpers.datapath_id_to_hex(ld["datapath_id"])) # Convert to hex
Matteo Scandolof6337eb2018-04-05 15:58:37 -070052 return o
53
Matteo Scandolo2c144932018-05-04 14:06:24 -070054 raise Exception("Can't find a logical_device for OLT device id: %s" % o.device_id)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080055
Matteo Scandolo2c144932018-05-04 14:06:24 -070056 def pre_provision_olt_device(self, model):
57 log.info("Pre-provisioning OLT device in VOLTHA", object=str(model), **model.tologdict())
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080058
Matteo Scandolo2c144932018-05-04 14:06:24 -070059 voltha = Helpers.get_voltha_info(model.volt_service)
Luca Preteca974c82018-05-01 18:06:16 -070060
Matteo Scandolo2c144932018-05-04 14:06:24 -070061 data = {
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070062 "type": model.device_type
Matteo Scandolo2c144932018-05-04 14:06:24 -070063 }
64
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070065 if hasattr(model, "host") and hasattr(model, "port"):
66 data["host_and_port"] = "%s:%s" % (model.host, model.port)
67 elif hasattr(model, "mac_address"):
68 data["mac_address"] = model.mac_address
Matteo Scandolo2c144932018-05-04 14:06:24 -070069
70 log.info("Pushing OLT to Voltha", data=data)
71
72 request = requests.post("%s:%d/api/v1/devices" % (voltha['url'], voltha['port']), json=data)
73
74 if request.status_code != 200:
75 raise Exception("Failed to add OLT device: %s" % request.text)
76
77 log.info("Add device response", text=request.text)
78
79 res = request.json()
80
81 log.info("Add device json res", res=res)
82
83 if not res['id']:
84 raise Exception(
85 'VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA')
86 else:
87 model.device_id = res['id'];
88
89 return model
90
91 def activate_olt(self, model):
92
Matteo Scandolo096a3cf2018-06-20 13:56:13 -070093 attempted = 0
94
Matteo Scandolo2c144932018-05-04 14:06:24 -070095 voltha = Helpers.get_voltha_info(model.volt_service)
96
97 # Enable device
98 request = requests.post("%s:%d/api/v1/devices/%s/enable" % (voltha['url'], voltha['port'], model.device_id))
99
100 if request.status_code != 200:
101 raise Exception("Failed to enable OLT device: %s" % request.text)
102
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700103 model.backend_status = "Waiting for device to be activated"
104 model.save(always_update_timestamp=False) # we don't want to kickoff a new loop
105
Matteo Scandolo2c144932018-05-04 14:06:24 -0700106 # Read state
107 request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json()
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700108 while request['oper_status'] == "ACTIVATING" and attempted < self.max_attempt:
Matteo Scandolo2c144932018-05-04 14:06:24 -0700109 log.info("Waiting for OLT device %s (%s) to activate" % (model.name, model.device_id))
110 sleep(5)
111 request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json()
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700112 attempted = attempted + 1
113
Matteo Scandolo2c144932018-05-04 14:06:24 -0700114
115 model.admin_state = request['admin_state']
116 model.oper_status = request['oper_status']
117
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700118 if model.oper_status != "ACTIVE":
119 raise Exception("It was not possible to activate OLTDevice with id %s" % model.id)
120
Matteo Scandolo2c144932018-05-04 14:06:24 -0700121 # Find the of_id of the device
122 model = self.get_ids_from_logical_device(model)
123 model.save()
124
125 return model
126
127 def configure_onos(self, model):
128
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700129 log.info("Adding OLT device in onos-voltha", object=str(model), **model.tologdict())
130
Matteo Scandolo2c144932018-05-04 14:06:24 -0700131 onos_voltha = Helpers.get_onos_voltha_info(model.volt_service)
Luca Preteca974c82018-05-01 18:06:16 -0700132 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800133
Luca Preteca974c82018-05-01 18:06:16 -0700134 # Add device info to onos-voltha
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800135 data = {
136 "devices": {
Matteo Scandolo2c144932018-05-04 14:06:24 -0700137 model.dp_id: {
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800138 "basic": {
Matteo Scandolo2c144932018-05-04 14:06:24 -0700139 "driver": model.driver
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800140 },
141 "accessDevice": {
Matteo Scandolo2c144932018-05-04 14:06:24 -0700142 "uplink": model.uplink,
Matteo Scandolo80912942018-07-25 20:51:30 -0700143 "vlan": 1
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800144 }
145 }
146 }
147 }
148
Matteo Scandolo2c144932018-05-04 14:06:24 -0700149 url = "%s:%d/onos/v1/network/configuration/" % (onos_voltha['url'], onos_voltha['port'])
150 request = requests.post(url, json=data, auth=onos_voltha_basic_auth)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800151
Luca Preteca974c82018-05-01 18:06:16 -0700152 if request.status_code != 200:
153 log.error(request.text)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700154 raise Exception("Failed to add OLT device %s into ONOS" % model.name)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800155 else:
156 try:
Luca Preteca974c82018-05-01 18:06:16 -0700157 print request.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800158 except Exception:
Luca Preteca974c82018-05-01 18:06:16 -0700159 print request.text
Matteo Scandolo2c144932018-05-04 14:06:24 -0700160 return model
161
Matteo Scandolo2c144932018-05-04 14:06:24 -0700162 def sync_record(self, model):
163 log.info("Synching device", object=str(model), **model.tologdict())
164
165 # If the device has feedback_state is already present in voltha
166 if not model.device_id and not model.admin_state and not model.oper_status and not model.of_id:
167 log.info("Pushing OLT device to VOLTHA", object=str(model), **model.tologdict())
168 model = self.pre_provision_olt_device(model)
169 self.activate_olt(model)
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700170 elif model.oper_status != "ACTIVE":
171 raise Exception("It was not possible to activate OLTDevice with id %s" % model.id)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700172 else:
173 log.info("OLT device already exists in VOLTHA", object=str(model), **model.tologdict())
174
Matteo Scandolo88709c42018-06-20 16:59:38 -0700175 # TODO configure onos only if we have: Switch datapath id, Switch port, Uplink
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700176 if model.switch_datapath_id and model.switch_port and model.uplink:
177 log.info("Pushing OLT device to ONOS-VOLTHA", object=str(model), **model.tologdict())
178 self.configure_onos(model)
179 else:
180 raise DeferredException("Not pushing OLTDevice (%s) to ONOS-VOLTHA as parameters are missing" % model.id)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700181
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700182 def delete_record(self, model):
183 log.info("Deleting OLT device", object=str(model), **model.tologdict())
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800184
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700185 voltha = Helpers.get_voltha_info(model.volt_service)
186 onos_voltha = Helpers.get_onos_voltha_info(model.volt_service)
Luca Preteca974c82018-05-01 18:06:16 -0700187 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
188
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700189 if not model.device_id:
190 log.error("OLTDevice %s has no device_id" % model.name)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800191 else:
Matteo Scandolo2c144932018-05-04 14:06:24 -0700192 # Disable the OLT device
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700193 request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], model.device_id))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800194
Luca Preteca974c82018-05-01 18:06:16 -0700195 if request.status_code != 200:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700196 log.error("Failed to disable OLT device in VOLTHA: %s - %s" % (model.name, model.device_id), rest_response=request.text, rest_status_code=request.status_code)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700197 raise Exception("Failed to disable OLT device in VOLTHA")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800198
Matteo Scandolo2c144932018-05-04 14:06:24 -0700199 # Delete the OLT device
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700200 request = requests.delete("%s:%d/api/v1/devices/%s/delete" % (voltha['url'], voltha['port'], model.device_id))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800201
Luca Preteca974c82018-05-01 18:06:16 -0700202 if request.status_code != 200:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700203 log.error("Failed to delete OLT device from VOLTHA: %s - %s" % (model.name, model.device_id), rest_response=request.text, rest_status_code=request.status_code)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700204 raise Exception("Failed to delete OLT device from VOLTHA")
205
206 # Remove the device from ONOS
207 request = requests.delete("%s:%d/onos/v1/network/configuration/devices/%s" % (
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700208 onos_voltha['url'], onos_voltha['port'], model.of_id), auth=onos_voltha_basic_auth)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700209
210 if request.status_code != 204:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700211 log.error("Failed to remove OLT device from ONOS: %s - %s" % (model.name, model.of_id), rest_response=request.text, rest_status_code=request.status_code)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700212 raise Exception("Failed to remove OLT device from ONOS")