blob: c44a4d4230254dae2ff5787a55c9dee089ec0b9e [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;
Thomas Vachuska82041f52014-11-30 22:14:02 -080023import org.onlab.onos.net.DefaultAnnotations;
tomab21e7c2014-08-26 15:23:08 -070024import org.onlab.onos.net.Device;
alshabibc944fd02014-09-10 17:55:17 -070025import org.onlab.onos.net.DeviceId;
tomab21e7c2014-08-26 15:23:08 -070026import org.onlab.onos.net.MastershipRole;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070027import org.onlab.onos.net.Port;
alshabib25c8eec2014-09-04 16:41:31 -070028import org.onlab.onos.net.PortNumber;
Thomas Vachuska82041f52014-11-30 22:14:02 -080029import org.onlab.onos.net.SparseAnnotations;
tomd1900f32014-09-03 14:08:16 -070030import org.onlab.onos.net.device.DefaultDeviceDescription;
alshabib25c8eec2014-09-04 16:41:31 -070031import org.onlab.onos.net.device.DefaultPortDescription;
alshabibf1216ed2014-09-03 11:53:54 -070032import org.onlab.onos.net.device.DeviceDescription;
tomab21e7c2014-08-26 15:23:08 -070033import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070034import org.onlab.onos.net.device.DeviceProviderRegistry;
tomab21e7c2014-08-26 15:23:08 -070035import org.onlab.onos.net.device.DeviceProviderService;
alshabib25c8eec2014-09-04 16:41:31 -070036import org.onlab.onos.net.device.PortDescription;
tomab21e7c2014-08-26 15:23:08 -070037import org.onlab.onos.net.provider.AbstractProvider;
38import org.onlab.onos.net.provider.ProviderId;
tom9c94c5b2014-09-17 13:14:42 -070039import org.onlab.onos.openflow.controller.Dpid;
40import org.onlab.onos.openflow.controller.OpenFlowController;
41import org.onlab.onos.openflow.controller.OpenFlowSwitch;
42import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
43import org.onlab.onos.openflow.controller.RoleState;
alshabib7911a052014-10-16 17:49:37 -070044import org.onlab.packet.ChassisId;
Ayaka Koshibee8708e32014-10-22 13:40:18 -070045import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib4680bb62014-09-04 17:15:08 -070046import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070047import org.projectfloodlight.openflow.protocol.OFPortDesc;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070048import org.projectfloodlight.openflow.protocol.OFPortFeatures;
alshabibafc514a2014-12-01 14:44:05 -080049import org.projectfloodlight.openflow.protocol.OFPortReason;
alshabib4680bb62014-09-04 17:15:08 -070050import org.projectfloodlight.openflow.protocol.OFPortState;
alshabiba14f3642014-09-05 09:31:31 -070051import org.projectfloodlight.openflow.protocol.OFPortStatus;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070052import org.projectfloodlight.openflow.protocol.OFVersion;
53import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -070054import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070055
tom782a7cf2014-09-11 23:58:38 -070056import java.util.ArrayList;
57import java.util.List;
58
59import static org.onlab.onos.net.DeviceId.deviceId;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070060import static org.onlab.onos.net.Port.Type.COPPER;
61import static org.onlab.onos.net.Port.Type.FIBER;
tom9c94c5b2014-09-17 13:14:42 -070062import static org.onlab.onos.openflow.controller.Dpid.dpid;
63import static org.onlab.onos.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070064import static org.slf4j.LoggerFactory.getLogger;
65
tomb5a46e62014-08-26 14:20:00 -070066/**
tomb1260e42014-08-26 18:39:57 -070067 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070068 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070069 */
tomb1260e42014-08-26 18:39:57 -070070@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070071public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070072
alshabiba89cc582014-09-09 16:43:00 -070073 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070074 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -070075
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070077 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070078
tom5f38b3a2014-08-27 23:50:54 -070079 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected OpenFlowController controller;
81
tomab21e7c2014-08-26 15:23:08 -070082 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070083
alshabib4680bb62014-09-04 17:15:08 -070084 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070085
tomab21e7c2014-08-26 15:23:08 -070086 /**
87 * Creates an OpenFlow device provider.
88 */
89 public OpenFlowDeviceProvider() {
tom7e02cda2014-09-18 12:05:46 -070090 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -070091 }
92
tomb5a46e62014-08-26 14:20:00 -070093 @Activate
94 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070095 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070096 controller.addListener(listener);
alshabibc944fd02014-09-10 17:55:17 -070097 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -080098 try {
99 listener.switchAdded(new Dpid(sw.getId()));
100 } catch (Exception e) {
101 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
102 LOG.debug("Error details:", e);
103 // disconnect to trigger switch-add later
104 sw.disconnectSwitch();
105 }
alshabibc944fd02014-09-10 17:55:17 -0700106 }
alshabiba89cc582014-09-09 16:43:00 -0700107 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700108 }
109
110 @Deactivate
111 public void deactivate() {
alshabibc944fd02014-09-10 17:55:17 -0700112 for (OpenFlowSwitch sw : controller.getSwitches()) {
tom782a7cf2014-09-11 23:58:38 -0700113 providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
alshabibc944fd02014-09-10 17:55:17 -0700114 }
tom96dfcab2014-08-28 09:26:03 -0700115 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700116 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -0700117 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -0700118
alshabiba89cc582014-09-09 16:43:00 -0700119 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700120 }
121
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700122
123 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700124 public boolean isReachable(DeviceId deviceId) {
125 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700126 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700127 return false;
128 }
129 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700130 }
131
tomab21e7c2014-08-26 15:23:08 -0700132 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800133 public void triggerProbe(DeviceId deviceId) {
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700134 LOG.info("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700135
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700136 final Dpid dpid = dpid(deviceId.uri());
137 OpenFlowSwitch sw = controller.getSwitch(dpid);
138 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800139 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700140 providerService.deviceDisconnected(deviceId);
141 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800142 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700143 // FIXME require something like below to match javadoc description
144 // but this starts infinite loop with current DeviceManager
145// final ChassisId cId = new ChassisId(dpid.value());
146// final Type deviceType = device.type();
147// DeviceDescription description =
148// new DefaultDeviceDescription(deviceId.uri(), deviceType,
149// sw.manfacturerDescription(),
150// sw.hardwareDescription(),
151// sw.softwareDescription(),
152// sw.serialNumber(),
153// cId);
154// providerService.deviceConnected(deviceId, description);
155 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700156
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700157 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700158 OFFactory fact = sw.factory();
159 switch (fact.getVersion()) {
160 case OF_10:
161 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
162 break;
163 case OF_13:
164 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
165 break;
166 default:
167 LOG.warn("Unhandled protocol version");
168 }
tomab21e7c2014-08-26 15:23:08 -0700169 }
170
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700171 // Checks if the OF channel is connected.
172 //private boolean checkChannel(Device device, OpenFlowSwitch sw) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700173 // FIXME if possible, we might want this to be part of
174 // OpenFlowSwitch interface so the driver interface isn't misused.
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700175 // if (sw == null || !((OpenFlowSwitchDriver) sw).isConnected()) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700176 // return false;
177 // }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700178 // return true;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700179 // }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700180
tomab21e7c2014-08-26 15:23:08 -0700181 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700182 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700183 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700184 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700185 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700186 break;
187 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700188 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700189 break;
190 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700191 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700192 break;
193 default:
194 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700195
196 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700197 LOG.info("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700198 }
199
alshabibf1216ed2014-09-03 11:53:54 -0700200 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700201 @Override
tomd1900f32014-09-03 14:08:16 -0700202 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700203 if (providerService == null) {
204 return;
205 }
tom782a7cf2014-09-11 23:58:38 -0700206 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700207 OpenFlowSwitch sw = controller.getSwitch(dpid);
208
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700209 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
210 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700211 ChassisId cId = new ChassisId(dpid.value());
Thomas Vachuska82041f52014-11-30 22:14:02 -0800212 SparseAnnotations annotations = DefaultAnnotations.builder()
213 .set("protocol", sw.factory().getVersion().toString()).build();
tomd1900f32014-09-03 14:08:16 -0700214 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700215 new DefaultDeviceDescription(did.uri(), deviceType,
tom782a7cf2014-09-11 23:58:38 -0700216 sw.manfacturerDescription(),
217 sw.hardwareDescription(),
218 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700219 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800220 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700221 providerService.deviceConnected(did, description);
222 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700223 }
224
alshabibf1216ed2014-09-03 11:53:54 -0700225 @Override
226 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700227 if (providerService == null) {
228 return;
229 }
tom782a7cf2014-09-11 23:58:38 -0700230 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700231 }
232
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700233
234 @Override
235 public void switchChanged(Dpid dpid) {
236 if (providerService == null) {
237 return;
238 }
239 DeviceId did = deviceId(uri(dpid));
240 OpenFlowSwitch sw = controller.getSwitch(dpid);
241 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
242 }
243
alshabiba14f3642014-09-05 09:31:31 -0700244 @Override
245 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800246 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700247 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700248 }
249
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700250 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700251 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
252 MastershipRole request = roleOf(requested);
253 MastershipRole reply = roleOf(response);
254
255 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
256 }
257
258 /**
259 * Translates a RoleState to the corresponding MastershipRole.
260 *
261 * @param response
262 * @return a MastershipRole
263 */
264 private MastershipRole roleOf(RoleState response) {
265 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700266 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700267 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700268 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700269 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700270 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700271 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700272 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700273 LOG.warn("unknown role {}", response);
274 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700275 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700276 }
277
alshabiba14f3642014-09-05 09:31:31 -0700278 /**
279 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700280 *
alshabiba14f3642014-09-05 09:31:31 -0700281 * @param ports the list of ports
282 * @return list of portdescriptions
283 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700284 private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700285 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700286 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700287 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700288 }
289 return portDescs;
290 }
291
alshabiba14f3642014-09-05 09:31:31 -0700292 /**
293 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700294 *
alshabiba14f3642014-09-05 09:31:31 -0700295 * @param port the port to build from.
296 * @return portDescription for the port.
297 */
298 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700299 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
300 boolean enabled =
301 !port.getState().contains(OFPortState.LINK_DOWN) &&
302 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
303 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
304 return new DefaultPortDescription(portNo, enabled, type, portSpeed(port));
alshabiba14f3642014-09-05 09:31:31 -0700305 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700306
alshabibafc514a2014-12-01 14:44:05 -0800307 private PortDescription buildPortDescription(OFPortStatus status) {
308 OFPortDesc port = status.getDesc();
309 if (status.getReason() != OFPortReason.DELETE) {
310 return buildPortDescription(port);
311 } else {
312 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
313 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
314 return new DefaultPortDescription(portNo, false, type, portSpeed(port));
315 }
316 }
317
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700318 private long portSpeed(OFPortDesc port) {
319 if (port.getVersion() == OFVersion.OF_13) {
320 return port.getCurrSpeed() / MBPS;
321 }
322
323 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
324 for (OFPortFeatures feat : port.getCurr()) {
325 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
326 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700327 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700328 }
alshabibf1216ed2014-09-03 11:53:54 -0700329 }
330
tomb5a46e62014-08-26 14:20:00 -0700331}