blob: efb76abd535445b69cfb9b34b895271dd5e4ce06 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
tomb5a46e62014-08-26 14:20:00 -070016package org.onlab.onos.provider.of.device.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
tomab21e7c2014-08-26 15:23:08 -070023import org.onlab.onos.net.Device;
alshabibc944fd02014-09-10 17:55:17 -070024import org.onlab.onos.net.DeviceId;
tomab21e7c2014-08-26 15:23:08 -070025import org.onlab.onos.net.MastershipRole;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070026import org.onlab.onos.net.Port;
alshabib25c8eec2014-09-04 16:41:31 -070027import org.onlab.onos.net.PortNumber;
tomd1900f32014-09-03 14:08:16 -070028import org.onlab.onos.net.device.DefaultDeviceDescription;
alshabib25c8eec2014-09-04 16:41:31 -070029import org.onlab.onos.net.device.DefaultPortDescription;
alshabibf1216ed2014-09-03 11:53:54 -070030import org.onlab.onos.net.device.DeviceDescription;
tomab21e7c2014-08-26 15:23:08 -070031import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070032import org.onlab.onos.net.device.DeviceProviderRegistry;
tomab21e7c2014-08-26 15:23:08 -070033import org.onlab.onos.net.device.DeviceProviderService;
alshabib25c8eec2014-09-04 16:41:31 -070034import org.onlab.onos.net.device.PortDescription;
tomab21e7c2014-08-26 15:23:08 -070035import org.onlab.onos.net.provider.AbstractProvider;
36import org.onlab.onos.net.provider.ProviderId;
tom9c94c5b2014-09-17 13:14:42 -070037import org.onlab.onos.openflow.controller.Dpid;
38import org.onlab.onos.openflow.controller.OpenFlowController;
39import org.onlab.onos.openflow.controller.OpenFlowSwitch;
40import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
41import org.onlab.onos.openflow.controller.RoleState;
alshabib7911a052014-10-16 17:49:37 -070042import org.onlab.packet.ChassisId;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070043import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib4680bb62014-09-04 17:15:08 -070044import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070045import org.projectfloodlight.openflow.protocol.OFPortDesc;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070046import org.projectfloodlight.openflow.protocol.OFPortFeatures;
alshabib4680bb62014-09-04 17:15:08 -070047import org.projectfloodlight.openflow.protocol.OFPortState;
alshabiba14f3642014-09-05 09:31:31 -070048import org.projectfloodlight.openflow.protocol.OFPortStatus;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070049import org.projectfloodlight.openflow.protocol.OFVersion;
50import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -070051import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070052
tom782a7cf2014-09-11 23:58:38 -070053import java.util.ArrayList;
54import java.util.List;
55
56import static org.onlab.onos.net.DeviceId.deviceId;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070057import static org.onlab.onos.net.Port.Type.COPPER;
58import static org.onlab.onos.net.Port.Type.FIBER;
tom9c94c5b2014-09-17 13:14:42 -070059import static org.onlab.onos.openflow.controller.Dpid.dpid;
60import static org.onlab.onos.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070061import static org.slf4j.LoggerFactory.getLogger;
62
tomb5a46e62014-08-26 14:20:00 -070063/**
tomb1260e42014-08-26 18:39:57 -070064 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070065 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070066 */
tomb1260e42014-08-26 18:39:57 -070067@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070068public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070069
alshabiba89cc582014-09-09 16:43:00 -070070 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070071 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -070072
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070074 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070075
tom5f38b3a2014-08-27 23:50:54 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected OpenFlowController controller;
78
tomab21e7c2014-08-26 15:23:08 -070079 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070080
alshabib4680bb62014-09-04 17:15:08 -070081 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070082
tomab21e7c2014-08-26 15:23:08 -070083 /**
84 * Creates an OpenFlow device provider.
85 */
86 public OpenFlowDeviceProvider() {
tom7e02cda2014-09-18 12:05:46 -070087 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -070088 }
89
tomb5a46e62014-08-26 14:20:00 -070090 @Activate
91 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070092 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070093 controller.addListener(listener);
alshabibc944fd02014-09-10 17:55:17 -070094 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -080095 try {
96 listener.switchAdded(new Dpid(sw.getId()));
97 } catch (Exception e) {
98 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
99 LOG.debug("Error details:", e);
100 // disconnect to trigger switch-add later
101 sw.disconnectSwitch();
102 }
alshabibc944fd02014-09-10 17:55:17 -0700103 }
alshabiba89cc582014-09-09 16:43:00 -0700104 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700105 }
106
107 @Deactivate
108 public void deactivate() {
alshabibc944fd02014-09-10 17:55:17 -0700109 for (OpenFlowSwitch sw : controller.getSwitches()) {
tom782a7cf2014-09-11 23:58:38 -0700110 providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
alshabibc944fd02014-09-10 17:55:17 -0700111 }
tom96dfcab2014-08-28 09:26:03 -0700112 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700113 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -0700114 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -0700115
alshabiba89cc582014-09-09 16:43:00 -0700116 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700117 }
118
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700119
120 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700121 public boolean isReachable(DeviceId deviceId) {
122 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700123 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700124 return false;
125 }
126 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700127 }
128
tomab21e7c2014-08-26 15:23:08 -0700129 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800130 public void triggerProbe(DeviceId deviceId) {
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700131 LOG.info("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700132
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700133 final Dpid dpid = dpid(deviceId.uri());
134 OpenFlowSwitch sw = controller.getSwitch(dpid);
135 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800136 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700137 providerService.deviceDisconnected(deviceId);
138 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800139 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700140 // FIXME require something like below to match javadoc description
141 // but this starts infinite loop with current DeviceManager
142// final ChassisId cId = new ChassisId(dpid.value());
143// final Type deviceType = device.type();
144// DeviceDescription description =
145// new DefaultDeviceDescription(deviceId.uri(), deviceType,
146// sw.manfacturerDescription(),
147// sw.hardwareDescription(),
148// sw.softwareDescription(),
149// sw.serialNumber(),
150// cId);
151// providerService.deviceConnected(deviceId, description);
152 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700153
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700154 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700155 OFFactory fact = sw.factory();
156 switch (fact.getVersion()) {
157 case OF_10:
158 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
159 break;
160 case OF_13:
161 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
162 break;
163 default:
164 LOG.warn("Unhandled protocol version");
165 }
tomab21e7c2014-08-26 15:23:08 -0700166 }
167
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700168 // Checks if the OF channel is connected.
169 //private boolean checkChannel(Device device, OpenFlowSwitch sw) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700170 // FIXME if possible, we might want this to be part of
171 // OpenFlowSwitch interface so the driver interface isn't misused.
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700172 // if (sw == null || !((OpenFlowSwitchDriver) sw).isConnected()) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700173 // return false;
174 // }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700175 // return true;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700176 // }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700177
tomab21e7c2014-08-26 15:23:08 -0700178 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700179 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700180 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700181 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700182 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700183 break;
184 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700185 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700186 break;
187 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700188 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700189 break;
190 default:
191 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700192
193 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700194 LOG.info("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700195 }
196
alshabibf1216ed2014-09-03 11:53:54 -0700197 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700198 @Override
tomd1900f32014-09-03 14:08:16 -0700199 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700200 if (providerService == null) {
201 return;
202 }
tom782a7cf2014-09-11 23:58:38 -0700203 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700204 OpenFlowSwitch sw = controller.getSwitch(dpid);
205
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700206 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
207 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700208 ChassisId cId = new ChassisId(dpid.value());
tomd1900f32014-09-03 14:08:16 -0700209 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700210 new DefaultDeviceDescription(did.uri(), deviceType,
tom782a7cf2014-09-11 23:58:38 -0700211 sw.manfacturerDescription(),
212 sw.hardwareDescription(),
213 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700214 sw.serialNumber(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700215 cId);
tom782a7cf2014-09-11 23:58:38 -0700216 providerService.deviceConnected(did, description);
217 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700218 }
219
alshabibf1216ed2014-09-03 11:53:54 -0700220 @Override
221 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700222 if (providerService == null) {
223 return;
224 }
tom782a7cf2014-09-11 23:58:38 -0700225 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700226 }
227
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700228
229 @Override
230 public void switchChanged(Dpid dpid) {
231 if (providerService == null) {
232 return;
233 }
234 DeviceId did = deviceId(uri(dpid));
235 OpenFlowSwitch sw = controller.getSwitch(dpid);
236 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
237 }
238
alshabiba14f3642014-09-05 09:31:31 -0700239 @Override
240 public void portChanged(Dpid dpid, OFPortStatus status) {
tom782a7cf2014-09-11 23:58:38 -0700241 PortDescription portDescription = buildPortDescription(status.getDesc());
242 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700243 }
244
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700245 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700246 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
247 MastershipRole request = roleOf(requested);
248 MastershipRole reply = roleOf(response);
249
250 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
251 }
252
253 /**
254 * Translates a RoleState to the corresponding MastershipRole.
255 *
256 * @param response
257 * @return a MastershipRole
258 */
259 private MastershipRole roleOf(RoleState response) {
260 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700261 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700262 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700263 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700264 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700265 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700266 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700267 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700268 LOG.warn("unknown role {}", response);
269 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700270 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700271 }
272
alshabiba14f3642014-09-05 09:31:31 -0700273 /**
274 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700275 *
alshabiba14f3642014-09-05 09:31:31 -0700276 * @param ports the list of ports
277 * @return list of portdescriptions
278 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700279 private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700280 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700281 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700282 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700283 }
284 return portDescs;
285 }
286
alshabiba14f3642014-09-05 09:31:31 -0700287 /**
288 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700289 *
alshabiba14f3642014-09-05 09:31:31 -0700290 * @param port the port to build from.
291 * @return portDescription for the port.
292 */
293 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700294 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
295 boolean enabled =
296 !port.getState().contains(OFPortState.LINK_DOWN) &&
297 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
298 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
299 return new DefaultPortDescription(portNo, enabled, type, portSpeed(port));
alshabiba14f3642014-09-05 09:31:31 -0700300 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700301
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700302 private long portSpeed(OFPortDesc port) {
303 if (port.getVersion() == OFVersion.OF_13) {
304 return port.getCurrSpeed() / MBPS;
305 }
306
307 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
308 for (OFPortFeatures feat : port.getCurr()) {
309 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
310 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700311 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700312 }
alshabibf1216ed2014-09-03 11:53:54 -0700313 }
314
tomb5a46e62014-08-26 14:20:00 -0700315}