blob: 4aaebdf4a82b4151811de573080486238f07edeb [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
15import json
16from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
17from synchronizers.new_base.modelaccessor import OLTDevice
18
19from xosconfig import Config
20from multistructlog import create_logger
21from time import sleep
22import requests
23from requests.auth import HTTPBasicAuth
Matteo Scandolof6337eb2018-04-05 15:58:37 -070024from helpers import Helpers
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080025
26log = create_logger(Config().get('logging'))
27
28class SyncOLTDevice(SyncStep):
29 provides = [OLTDevice]
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080030 observes = OLTDevice
31
32 @staticmethod
Matteo Scandolof6337eb2018-04-05 15:58:37 -070033 def get_ids_from_logical_device(o):
Luca Preteca974c82018-05-01 18:06:16 -070034 voltha = Helpers.get_voltha_info(o.volt_service)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080035
Luca Preteca974c82018-05-01 18:06:16 -070036 request = requests.get("%s:%d/api/v1/logical_devices" % (voltha['url'], voltha['port']))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080037
Luca Preteca974c82018-05-01 18:06:16 -070038 if request.status_code != 200:
39 raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % request.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080040
Luca Preteca974c82018-05-01 18:06:16 -070041 response = request.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080042
Luca Preteca974c82018-05-01 18:06:16 -070043 for ld in response["items"]:
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080044 if ld["root_device_id"] == o.device_id:
Matteo Scandolof6337eb2018-04-05 15:58:37 -070045 o.of_id = ld["id"]
Luca Preteca974c82018-05-01 18:06:16 -070046 o.dp_id = "of:%s" % (Helpers.datapath_id_to_hex(ld["datapath_id"])) # Convert to hex
Matteo Scandolof6337eb2018-04-05 15:58:37 -070047 return o
48
Matteo Scandolo2c144932018-05-04 14:06:24 -070049 raise Exception("Can't find a logical_device for OLT device id: %s" % o.device_id)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080050
Matteo Scandolo2c144932018-05-04 14:06:24 -070051 def pre_provision_olt_device(self, model):
52 log.info("Pre-provisioning OLT device in VOLTHA", object=str(model), **model.tologdict())
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080053
Matteo Scandolo2c144932018-05-04 14:06:24 -070054 voltha = Helpers.get_voltha_info(model.volt_service)
Luca Preteca974c82018-05-01 18:06:16 -070055
Matteo Scandolo2c144932018-05-04 14:06:24 -070056 data = {
57 "type": model.device_type,
58 "host_and_port": "%s:%s" % (model.host, model.port)
59 }
60
61 if model.device_type == 'simulated_olt':
62 # Simulated devices won't accept host and port. This is to enable tests in voltha without having a real olt or ponsim
63 data.pop('host_and_port')
64 data['mac_address'] = "00:0c:e2:31:40:00"
65
66 log.info("Pushing OLT to Voltha", data=data)
67
68 request = requests.post("%s:%d/api/v1/devices" % (voltha['url'], voltha['port']), json=data)
69
70 if request.status_code != 200:
71 raise Exception("Failed to add OLT device: %s" % request.text)
72
73 log.info("Add device response", text=request.text)
74
75 res = request.json()
76
77 log.info("Add device json res", res=res)
78
79 if not res['id']:
80 raise Exception(
81 'VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA')
82 else:
83 model.device_id = res['id'];
84
85 return model
86
87 def activate_olt(self, model):
88
89 voltha = Helpers.get_voltha_info(model.volt_service)
90
91 # Enable device
92 request = requests.post("%s:%d/api/v1/devices/%s/enable" % (voltha['url'], voltha['port'], model.device_id))
93
94 if request.status_code != 200:
95 raise Exception("Failed to enable OLT device: %s" % request.text)
96
97 # Read state
98 request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json()
99 while request['oper_status'] == "ACTIVATING":
100 log.info("Waiting for OLT device %s (%s) to activate" % (model.name, model.device_id))
101 sleep(5)
102 request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json()
103
104 model.admin_state = request['admin_state']
105 model.oper_status = request['oper_status']
106
107 # Find the of_id of the device
108 model = self.get_ids_from_logical_device(model)
109 model.save()
110
111 return model
112
113 def configure_onos(self, model):
114
115 onos_voltha = Helpers.get_onos_voltha_info(model.volt_service)
Luca Preteca974c82018-05-01 18:06:16 -0700116 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800117
Luca Preteca974c82018-05-01 18:06:16 -0700118 # For now, we assume that each OLT has only one port
Matteo Scandolo2c144932018-05-04 14:06:24 -0700119 vlan = model.ports.all()[0].s_tag
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800120
Luca Preteca974c82018-05-01 18:06:16 -0700121 # Add device info to onos-voltha
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800122 data = {
123 "devices": {
Matteo Scandolo2c144932018-05-04 14:06:24 -0700124 model.dp_id: {
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800125 "basic": {
Matteo Scandolo2c144932018-05-04 14:06:24 -0700126 "driver": model.driver
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800127 },
128 "accessDevice": {
Matteo Scandolo2c144932018-05-04 14:06:24 -0700129 "uplink": model.uplink,
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700130 "vlan": vlan
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800131 }
132 }
133 }
134 }
135
Matteo Scandolo2c144932018-05-04 14:06:24 -0700136 url = "%s:%d/onos/v1/network/configuration/" % (onos_voltha['url'], onos_voltha['port'])
137 request = requests.post(url, json=data, auth=onos_voltha_basic_auth)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800138
Luca Preteca974c82018-05-01 18:06:16 -0700139 if request.status_code != 200:
140 log.error(request.text)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700141 raise Exception("Failed to add OLT device %s into ONOS" % model.name)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800142 else:
143 try:
Luca Preteca974c82018-05-01 18:06:16 -0700144 print request.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800145 except Exception:
Luca Preteca974c82018-05-01 18:06:16 -0700146 print request.text
Matteo Scandolo2c144932018-05-04 14:06:24 -0700147 return model
148
Matteo Scandolo2c144932018-05-04 14:06:24 -0700149 def sync_record(self, model):
150 log.info("Synching device", object=str(model), **model.tologdict())
151
152 # If the device has feedback_state is already present in voltha
153 if not model.device_id and not model.admin_state and not model.oper_status and not model.of_id:
154 log.info("Pushing OLT device to VOLTHA", object=str(model), **model.tologdict())
155 model = self.pre_provision_olt_device(model)
156 self.activate_olt(model)
157 else:
158 log.info("OLT device already exists in VOLTHA", object=str(model), **model.tologdict())
159
160 self.configure_onos(model)
161
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800162 def delete_record(self, o):
Matteo Scandolo2c144932018-05-04 14:06:24 -0700163 log.info("Deleting OLT device", object=str(o), **o.tologdict())
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800164
Luca Preteca974c82018-05-01 18:06:16 -0700165 voltha = Helpers.get_voltha_info(o.volt_service)
166 onos_voltha = Helpers.get_onos_voltha_info(o.volt_service)
167 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
168
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800169 if not o.device_id:
Matteo Scandolo2c144932018-05-04 14:06:24 -0700170 log.error("OLTDevice %s has no device_id" % o.name)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800171 else:
Matteo Scandolo2c144932018-05-04 14:06:24 -0700172 # Disable the OLT device
Luca Preteca974c82018-05-01 18:06:16 -0700173 request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], o.device_id))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800174
Luca Preteca974c82018-05-01 18:06:16 -0700175 if request.status_code != 200:
Matteo Scandolo2c144932018-05-04 14:06:24 -0700176 log.error("Failed to disable OLT device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_response=request.text, rest_status_code=request.status_code)
177 raise Exception("Failed to disable OLT device in VOLTHA")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800178
Matteo Scandolo2c144932018-05-04 14:06:24 -0700179 # Delete the OLT device
Luca Preteca974c82018-05-01 18:06:16 -0700180 request = requests.delete("%s:%d/api/v1/devices/%s/delete" % (voltha['url'], voltha['port'], o.device_id))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800181
Luca Preteca974c82018-05-01 18:06:16 -0700182 if request.status_code != 200:
Matteo Scandolo2c144932018-05-04 14:06:24 -0700183 log.error("Failed to delete OLT device from VOLTHA: %s - %s" % (o.name, o.device_id), rest_response=request.text, rest_status_code=request.status_code)
184 raise Exception("Failed to delete OLT device from VOLTHA")
185
186 # Remove the device from ONOS
187 request = requests.delete("%s:%d/onos/v1/network/configuration/devices/%s" % (
188 onos_voltha['url'], onos_voltha['port'], o.of_id), auth=onos_voltha_basic_auth)
189
190 if request.status_code != 204:
191 log.error("Failed to remove OLT device from ONOS: %s - %s" % (o.name, o.of_id), rest_response=request.text, rest_status_code=request.status_code)
192 raise Exception("Failed to remove OLT device from ONOS")