blob: c0cc416cfcc788ca259f3549e525ec5f9b30a520 [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()) {
95 listener.switchAdded(new Dpid(sw.getId()));
96 }
alshabiba89cc582014-09-09 16:43:00 -070097 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -070098 }
99
100 @Deactivate
101 public void deactivate() {
alshabibc944fd02014-09-10 17:55:17 -0700102 for (OpenFlowSwitch sw : controller.getSwitches()) {
tom782a7cf2014-09-11 23:58:38 -0700103 providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId())));
alshabibc944fd02014-09-10 17:55:17 -0700104 }
tom96dfcab2014-08-28 09:26:03 -0700105 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -0700106 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -0700107 providerService = null;
alshabibc944fd02014-09-10 17:55:17 -0700108
alshabiba89cc582014-09-09 16:43:00 -0700109 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -0700110 }
111
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700112
113 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700114 public boolean isReachable(DeviceId deviceId) {
115 OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri()));
Yuta HIGUCHI69a27352014-10-31 15:48:37 -0700116 if (sw == null || !sw.isConnected()) {
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700117 return false;
118 }
119 return true;
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700120 }
121
tomab21e7c2014-08-26 15:23:08 -0700122 @Override
123 public void triggerProbe(Device device) {
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700124 final DeviceId deviceId = device.id();
125 LOG.info("Triggering probe on device {}", deviceId);
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700126
Yuta HIGUCHI802d0e82014-10-31 16:07:37 -0700127 final Dpid dpid = dpid(deviceId.uri());
128 OpenFlowSwitch sw = controller.getSwitch(dpid);
129 if (sw == null || !sw.isConnected()) {
130 LOG.error("Failed to probe device {} on sw={}", device, sw);
131 providerService.deviceDisconnected(deviceId);
132 } else {
133 LOG.trace("Confirmed device {} connection", device);
134 // FIXME require something like below to match javadoc description
135 // but this starts infinite loop with current DeviceManager
136// final ChassisId cId = new ChassisId(dpid.value());
137// final Type deviceType = device.type();
138// DeviceDescription description =
139// new DefaultDeviceDescription(deviceId.uri(), deviceType,
140// sw.manfacturerDescription(),
141// sw.hardwareDescription(),
142// sw.softwareDescription(),
143// sw.serialNumber(),
144// cId);
145// providerService.deviceConnected(deviceId, description);
146 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700147
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700148 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700149 OFFactory fact = sw.factory();
150 switch (fact.getVersion()) {
151 case OF_10:
152 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
153 break;
154 case OF_13:
155 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
156 break;
157 default:
158 LOG.warn("Unhandled protocol version");
159 }
tomab21e7c2014-08-26 15:23:08 -0700160 }
161
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700162 // Checks if the OF channel is connected.
163 //private boolean checkChannel(Device device, OpenFlowSwitch sw) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700164 // FIXME if possible, we might want this to be part of
165 // OpenFlowSwitch interface so the driver interface isn't misused.
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700166 // if (sw == null || !((OpenFlowSwitchDriver) sw).isConnected()) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700167 // return false;
168 // }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700169 // return true;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700170 // }
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700171
tomab21e7c2014-08-26 15:23:08 -0700172 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700173 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700174 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700175 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700176 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700177 break;
178 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700179 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700180 break;
181 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700182 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700183 break;
184 default:
185 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700186
187 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700188 LOG.info("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700189 }
190
alshabibf1216ed2014-09-03 11:53:54 -0700191 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700192 @Override
tomd1900f32014-09-03 14:08:16 -0700193 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700194 if (providerService == null) {
195 return;
196 }
tom782a7cf2014-09-11 23:58:38 -0700197 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700198 OpenFlowSwitch sw = controller.getSwitch(dpid);
199
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700200 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
201 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700202 ChassisId cId = new ChassisId(dpid.value());
tomd1900f32014-09-03 14:08:16 -0700203 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700204 new DefaultDeviceDescription(did.uri(), deviceType,
tom782a7cf2014-09-11 23:58:38 -0700205 sw.manfacturerDescription(),
206 sw.hardwareDescription(),
207 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700208 sw.serialNumber(),
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700209 cId);
tom782a7cf2014-09-11 23:58:38 -0700210 providerService.deviceConnected(did, description);
211 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700212 }
213
alshabibf1216ed2014-09-03 11:53:54 -0700214 @Override
215 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700216 if (providerService == null) {
217 return;
218 }
tom782a7cf2014-09-11 23:58:38 -0700219 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700220 }
221
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700222
223 @Override
224 public void switchChanged(Dpid dpid) {
225 if (providerService == null) {
226 return;
227 }
228 DeviceId did = deviceId(uri(dpid));
229 OpenFlowSwitch sw = controller.getSwitch(dpid);
230 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
231 }
232
alshabiba14f3642014-09-05 09:31:31 -0700233 @Override
234 public void portChanged(Dpid dpid, OFPortStatus status) {
tom782a7cf2014-09-11 23:58:38 -0700235 PortDescription portDescription = buildPortDescription(status.getDesc());
236 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700237 }
238
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700239 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700240 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
241 MastershipRole request = roleOf(requested);
242 MastershipRole reply = roleOf(response);
243
244 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
245 }
246
247 /**
248 * Translates a RoleState to the corresponding MastershipRole.
249 *
250 * @param response
251 * @return a MastershipRole
252 */
253 private MastershipRole roleOf(RoleState response) {
254 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700255 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700256 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700257 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700258 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700259 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700260 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700261 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700262 LOG.warn("unknown role {}", response);
263 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700264 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700265 }
266
alshabiba14f3642014-09-05 09:31:31 -0700267 /**
268 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700269 *
alshabiba14f3642014-09-05 09:31:31 -0700270 * @param ports the list of ports
271 * @return list of portdescriptions
272 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700273 private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700274 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700275 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700276 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700277 }
278 return portDescs;
279 }
280
alshabiba14f3642014-09-05 09:31:31 -0700281 /**
282 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700283 *
alshabiba14f3642014-09-05 09:31:31 -0700284 * @param port the port to build from.
285 * @return portDescription for the port.
286 */
287 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700288 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
289 boolean enabled =
290 !port.getState().contains(OFPortState.LINK_DOWN) &&
291 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
292 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
293 return new DefaultPortDescription(portNo, enabled, type, portSpeed(port));
alshabiba14f3642014-09-05 09:31:31 -0700294 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700295
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700296 private long portSpeed(OFPortDesc port) {
297 if (port.getVersion() == OFVersion.OF_13) {
298 return port.getCurrSpeed() / MBPS;
299 }
300
301 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
302 for (OFPortFeatures feat : port.getCurr()) {
303 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
304 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700305 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700306 }
alshabibf1216ed2014-09-03 11:53:54 -0700307 }
308
tomb5a46e62014-08-26 14:20:00 -0700309}