blob: 8ebef8937eb86aa85990ad8682ae7106ad2586a6 [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
35 @staticmethod
Matteo Scandolof6337eb2018-04-05 15:58:37 -070036 def get_ids_from_logical_device(o):
Luca Preteca974c82018-05-01 18:06:16 -070037 voltha = Helpers.get_voltha_info(o.volt_service)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080038
Luca Preteca974c82018-05-01 18:06:16 -070039 request = requests.get("%s:%d/api/v1/logical_devices" % (voltha['url'], voltha['port']))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080040
Luca Preteca974c82018-05-01 18:06:16 -070041 if request.status_code != 200:
42 raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % request.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080043
Luca Preteca974c82018-05-01 18:06:16 -070044 response = request.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080045
Luca Preteca974c82018-05-01 18:06:16 -070046 for ld in response["items"]:
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080047 if ld["root_device_id"] == o.device_id:
Matteo Scandolof6337eb2018-04-05 15:58:37 -070048 o.of_id = ld["id"]
Luca Preteca974c82018-05-01 18:06:16 -070049 o.dp_id = "of:%s" % (Helpers.datapath_id_to_hex(ld["datapath_id"])) # Convert to hex
Matteo Scandolof6337eb2018-04-05 15:58:37 -070050 return o
51
Matteo Scandolo2c144932018-05-04 14:06:24 -070052 raise Exception("Can't find a logical_device for OLT device id: %s" % o.device_id)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080053
Matteo Scandolo2c144932018-05-04 14:06:24 -070054 def pre_provision_olt_device(self, model):
55 log.info("Pre-provisioning OLT device in VOLTHA", object=str(model), **model.tologdict())
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080056
Matteo Scandolo2c144932018-05-04 14:06:24 -070057 voltha = Helpers.get_voltha_info(model.volt_service)
Luca Preteca974c82018-05-01 18:06:16 -070058
Matteo Scandolo2c144932018-05-04 14:06:24 -070059 data = {
60 "type": model.device_type,
61 "host_and_port": "%s:%s" % (model.host, model.port)
62 }
63
64 if model.device_type == 'simulated_olt':
65 # Simulated devices won't accept host and port. This is to enable tests in voltha without having a real olt or ponsim
66 data.pop('host_and_port')
67 data['mac_address'] = "00:0c:e2:31:40:00"
68
69 log.info("Pushing OLT to Voltha", data=data)
70
71 request = requests.post("%s:%d/api/v1/devices" % (voltha['url'], voltha['port']), json=data)
72
73 if request.status_code != 200:
74 raise Exception("Failed to add OLT device: %s" % request.text)
75
76 log.info("Add device response", text=request.text)
77
78 res = request.json()
79
80 log.info("Add device json res", res=res)
81
82 if not res['id']:
83 raise Exception(
84 'VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA')
85 else:
86 model.device_id = res['id'];
87
88 return model
89
90 def activate_olt(self, model):
91
92 voltha = Helpers.get_voltha_info(model.volt_service)
93
94 # Enable device
95 request = requests.post("%s:%d/api/v1/devices/%s/enable" % (voltha['url'], voltha['port'], model.device_id))
96
97 if request.status_code != 200:
98 raise Exception("Failed to enable OLT device: %s" % request.text)
99
100 # Read state
101 request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json()
102 while request['oper_status'] == "ACTIVATING":
103 log.info("Waiting for OLT device %s (%s) to activate" % (model.name, model.device_id))
104 sleep(5)
105 request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], model.device_id)).json()
106
107 model.admin_state = request['admin_state']
108 model.oper_status = request['oper_status']
109
110 # Find the of_id of the device
111 model = self.get_ids_from_logical_device(model)
112 model.save()
113
114 return model
115
116 def configure_onos(self, model):
117
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700118 log.info("Adding OLT device in onos-voltha", object=str(model), **model.tologdict())
119
Matteo Scandolo2c144932018-05-04 14:06:24 -0700120 onos_voltha = Helpers.get_onos_voltha_info(model.volt_service)
Luca Preteca974c82018-05-01 18:06:16 -0700121 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800122
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700123 try:
124 # NOTE For now, we assume that each OLT has only one pon port
125 vlan = model.pon_ports.all()[0].s_tag
126 except Exception as e:
127 raise DeferredException("Waiting for pon_ports to come up")
128
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800129
Luca Preteca974c82018-05-01 18:06:16 -0700130 # Add device info to onos-voltha
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800131 data = {
132 "devices": {
Matteo Scandolo2c144932018-05-04 14:06:24 -0700133 model.dp_id: {
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800134 "basic": {
Matteo Scandolo2c144932018-05-04 14:06:24 -0700135 "driver": model.driver
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800136 },
137 "accessDevice": {
Matteo Scandolo2c144932018-05-04 14:06:24 -0700138 "uplink": model.uplink,
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700139 "vlan": vlan
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800140 }
141 }
142 }
143 }
144
Matteo Scandolo2c144932018-05-04 14:06:24 -0700145 url = "%s:%d/onos/v1/network/configuration/" % (onos_voltha['url'], onos_voltha['port'])
146 request = requests.post(url, json=data, auth=onos_voltha_basic_auth)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800147
Luca Preteca974c82018-05-01 18:06:16 -0700148 if request.status_code != 200:
149 log.error(request.text)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700150 raise Exception("Failed to add OLT device %s into ONOS" % model.name)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800151 else:
152 try:
Luca Preteca974c82018-05-01 18:06:16 -0700153 print request.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800154 except Exception:
Luca Preteca974c82018-05-01 18:06:16 -0700155 print request.text
Matteo Scandolo2c144932018-05-04 14:06:24 -0700156 return model
157
Matteo Scandolo2c144932018-05-04 14:06:24 -0700158 def sync_record(self, model):
159 log.info("Synching device", object=str(model), **model.tologdict())
160
161 # If the device has feedback_state is already present in voltha
162 if not model.device_id and not model.admin_state and not model.oper_status and not model.of_id:
163 log.info("Pushing OLT device to VOLTHA", object=str(model), **model.tologdict())
164 model = self.pre_provision_olt_device(model)
165 self.activate_olt(model)
166 else:
167 log.info("OLT device already exists in VOLTHA", object=str(model), **model.tologdict())
168
169 self.configure_onos(model)
170
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700171 def delete_record(self, model):
172 log.info("Deleting OLT device", object=str(model), **model.tologdict())
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800173
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700174 voltha = Helpers.get_voltha_info(model.volt_service)
175 onos_voltha = Helpers.get_onos_voltha_info(model.volt_service)
Luca Preteca974c82018-05-01 18:06:16 -0700176 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
177
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700178 if not model.device_id:
179 log.error("OLTDevice %s has no device_id" % model.name)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800180 else:
Matteo Scandolo2c144932018-05-04 14:06:24 -0700181 # Disable the OLT device
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700182 request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], model.device_id))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800183
Luca Preteca974c82018-05-01 18:06:16 -0700184 if request.status_code != 200:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700185 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 -0700186 raise Exception("Failed to disable OLT device in VOLTHA")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800187
Matteo Scandolo2c144932018-05-04 14:06:24 -0700188 # Delete the OLT device
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700189 request = requests.delete("%s:%d/api/v1/devices/%s/delete" % (voltha['url'], voltha['port'], model.device_id))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800190
Luca Preteca974c82018-05-01 18:06:16 -0700191 if request.status_code != 200:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700192 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 -0700193 raise Exception("Failed to delete OLT device from VOLTHA")
194
195 # Remove the device from ONOS
196 request = requests.delete("%s:%d/onos/v1/network/configuration/devices/%s" % (
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700197 onos_voltha['url'], onos_voltha['port'], model.of_id), auth=onos_voltha_basic_auth)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700198
199 if request.status_code != 204:
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700200 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 -0700201 raise Exception("Failed to remove OLT device from ONOS")