blob: 617b0ba47580bc26dbe7e46e732dc8f008140bc9 [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;
alshabib4680bb62014-09-04 17:15:08 -070049import org.projectfloodlight.openflow.protocol.OFPortState;
alshabiba14f3642014-09-05 09:31:31 -070050import org.projectfloodlight.openflow.protocol.OFPortStatus;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070051import org.projectfloodlight.openflow.protocol.OFVersion;
52import org.projectfloodlight.openflow.types.PortSpeed;
tomb5a46e62014-08-26 14:20:00 -070053import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070054
tom782a7cf2014-09-11 23:58:38 -070055import java.util.ArrayList;
56import java.util.List;
57
58import static org.onlab.onos.net.DeviceId.deviceId;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070059import static org.onlab.onos.net.Port.Type.COPPER;
60import static org.onlab.onos.net.Port.Type.FIBER;
tom9c94c5b2014-09-17 13:14:42 -070061import static org.onlab.onos.openflow.controller.Dpid.dpid;
62import static org.onlab.onos.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070063import static org.slf4j.LoggerFactory.getLogger;
64
tomb5a46e62014-08-26 14:20:00 -070065/**
tomb1260e42014-08-26 18:39:57 -070066 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070067 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070068 */
tomb1260e42014-08-26 18:39:57 -070069@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070070public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070071
alshabiba89cc582014-09-09 16:43:00 -070072 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070073 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -070074
75 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070076 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070077
tom5f38b3a2014-08-27 23:50:54 -070078 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected OpenFlowController controller;
80
tomab21e7c2014-08-26 15:23:08 -070081 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070082
alshabib4680bb62014-09-04 17:15:08 -070083 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070084
tomab21e7c2014-08-26 15:23:08 -070085 /**
86 * Creates an OpenFlow device provider.
87 */
88 public OpenFlowDeviceProvider() {
tom7e02cda2014-09-18 12:05:46 -070089 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -070090 }
91
tomb5a46e62014-08-26 14:20:00 -070092 @Activate
93 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070094 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070095 controller.addListener(listener);
alshabibc944fd02014-09-10 17:55:17 -070096 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -080097 try {
98 listener.switchAdded(new Dpid(sw.getId()));
99 } catch (Exception e) {
100 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
101 LOG.debug("Error details:", e);
102 // disconnect to trigger switch-add later
103 sw.disconnectSwitch();
104 }
alshabibc944fd02014-09-10 17:55:17 -0700105 }
alshabiba89cc582014-09-09 16:43:00 -0700106 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700107 }
108
109 @Deactivate
110 public void deactivate() {
alshabibc944fd02014-09-10 17:55:17 -0700111 for (OpenFlowSwitch sw : controller.getSwitches()) {
tom782a7cf2014-09-11 23:58:38 -0700112 providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
alshabibc944fd02014-09-10 17:55:17 -0700113 }
tom96dfcab2014-08-28 09:26:03 -0700114 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700115 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -0700116 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -0700117
alshabiba89cc582014-09-09 16:43:00 -0700118 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700119 }
120
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700121
122 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700123 public boolean isReachable(DeviceId deviceId) {
124 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700125 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700126 return false;
127 }
128 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700129 }
130
tomab21e7c2014-08-26 15:23:08 -0700131 @Override
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800132 public void triggerProbe(DeviceId deviceId) {
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700133 LOG.info("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700134
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700135 final Dpid dpid = dpid(deviceId.uri());
136 OpenFlowSwitch sw = controller.getSwitch(dpid);
137 if (sw == null || !sw.isConnected()) {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800138 LOG.error("Failed to probe device {} on sw={}", deviceId, sw);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700139 providerService.deviceDisconnected(deviceId);
140 } else {
Ayaka Koshibe78bcbc12014-11-19 14:28:58 -0800141 LOG.trace("Confirmed device {} connection", deviceId);
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700142 // FIXME require something like below to match javadoc description
143 // but this starts infinite loop with current DeviceManager
144// final ChassisId cId = new ChassisId(dpid.value());
145// final Type deviceType = device.type();
146// DeviceDescription description =
147// new DefaultDeviceDescription(deviceId.uri(), deviceType,
148// sw.manfacturerDescription(),
149// sw.hardwareDescription(),
150// sw.softwareDescription(),
151// sw.serialNumber(),
152// cId);
153// providerService.deviceConnected(deviceId, description);
154 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700155
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700156 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700157 OFFactory fact = sw.factory();
158 switch (fact.getVersion()) {
159 case OF_10:
160 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
161 break;
162 case OF_13:
163 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
164 break;
165 default:
166 LOG.warn("Unhandled protocol version");
167 }
tomab21e7c2014-08-26 15:23:08 -0700168 }
169
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700170 // Checks if the OF channel is connected.
171 //private boolean checkChannel(Device device, OpenFlowSwitch sw) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700172 // FIXME if possible, we might want this to be part of
173 // OpenFlowSwitch interface so the driver interface isn't misused.
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700174 // if (sw == null || !((OpenFlowSwitchDriver) sw).isConnected()) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700175 // return false;
176 // }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700177 // return true;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700178 // }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700179
tomab21e7c2014-08-26 15:23:08 -0700180 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700181 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700182 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700183 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700184 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700185 break;
186 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700187 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700188 break;
189 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700190 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700191 break;
192 default:
193 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700194
195 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700196 LOG.info("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700197 }
198
alshabibf1216ed2014-09-03 11:53:54 -0700199 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700200 @Override
tomd1900f32014-09-03 14:08:16 -0700201 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700202 if (providerService == null) {
203 return;
204 }
tom782a7cf2014-09-11 23:58:38 -0700205 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700206 OpenFlowSwitch sw = controller.getSwitch(dpid);
207
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700208 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
209 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700210 ChassisId cId = new ChassisId(dpid.value());
Thomas Vachuska82041f52014-11-30 22:14:02 -0800211 SparseAnnotations annotations = DefaultAnnotations.builder()
212 .set("protocol", sw.factory().getVersion().toString()).build();
tomd1900f32014-09-03 14:08:16 -0700213 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700214 new DefaultDeviceDescription(did.uri(), deviceType,
tom782a7cf2014-09-11 23:58:38 -0700215 sw.manfacturerDescription(),
216 sw.hardwareDescription(),
217 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700218 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800219 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700220 providerService.deviceConnected(did, description);
221 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700222 }
223
alshabibf1216ed2014-09-03 11:53:54 -0700224 @Override
225 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700226 if (providerService == null) {
227 return;
228 }
tom782a7cf2014-09-11 23:58:38 -0700229 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700230 }
231
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700232
233 @Override
234 public void switchChanged(Dpid dpid) {
235 if (providerService == null) {
236 return;
237 }
238 DeviceId did = deviceId(uri(dpid));
239 OpenFlowSwitch sw = controller.getSwitch(dpid);
240 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
241 }
242
alshabiba14f3642014-09-05 09:31:31 -0700243 @Override
244 public void portChanged(Dpid dpid, OFPortStatus status) {
tom782a7cf2014-09-11 23:58:38 -0700245 PortDescription portDescription = buildPortDescription(status.getDesc());
246 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700247 }
248
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700249 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700250 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
251 MastershipRole request = roleOf(requested);
252 MastershipRole reply = roleOf(response);
253
254 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
255 }
256
257 /**
258 * Translates a RoleState to the corresponding MastershipRole.
259 *
260 * @param response
261 * @return a MastershipRole
262 */
263 private MastershipRole roleOf(RoleState response) {
264 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700265 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700266 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700267 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700268 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700269 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700270 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700271 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700272 LOG.warn("unknown role {}", response);
273 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700274 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700275 }
276
alshabiba14f3642014-09-05 09:31:31 -0700277 /**
278 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700279 *
alshabiba14f3642014-09-05 09:31:31 -0700280 * @param ports the list of ports
281 * @return list of portdescriptions
282 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700283 private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700284 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700285 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700286 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700287 }
288 return portDescs;
289 }
290
alshabiba14f3642014-09-05 09:31:31 -0700291 /**
292 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700293 *
alshabiba14f3642014-09-05 09:31:31 -0700294 * @param port the port to build from.
295 * @return portDescription for the port.
296 */
297 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700298 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
299 boolean enabled =
300 !port.getState().contains(OFPortState.LINK_DOWN) &&
301 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
302 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
303 return new DefaultPortDescription(portNo, enabled, type, portSpeed(port));
alshabiba14f3642014-09-05 09:31:31 -0700304 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700305
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700306 private long portSpeed(OFPortDesc port) {
307 if (port.getVersion() == OFVersion.OF_13) {
308 return port.getCurrSpeed() / MBPS;
309 }
310
311 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
312 for (OFPortFeatures feat : port.getCurr()) {
313 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
314 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700315 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700316 }
alshabibf1216ed2014-09-03 11:53:54 -0700317 }
318
tomb5a46e62014-08-26 14:20:00 -0700319}