blob: 504d0478a6cfddc121f5c39bcfecd4918b3a01a9 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.device.impl;
tomb5a46e62014-08-26 14:20:00 -070017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.MastershipRole;
27import org.onosproject.net.Port;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.SparseAnnotations;
30import org.onosproject.net.device.DefaultDeviceDescription;
31import org.onosproject.net.device.DefaultPortDescription;
32import org.onosproject.net.device.DeviceDescription;
33import org.onosproject.net.device.DeviceProvider;
34import org.onosproject.net.device.DeviceProviderRegistry;
35import org.onosproject.net.device.DeviceProviderService;
36import org.onosproject.net.device.PortDescription;
37import org.onosproject.net.provider.AbstractProvider;
38import org.onosproject.net.provider.ProviderId;
39import org.onosproject.openflow.controller.Dpid;
40import org.onosproject.openflow.controller.OpenFlowController;
41import org.onosproject.openflow.controller.OpenFlowSwitch;
42import org.onosproject.openflow.controller.OpenFlowSwitchListener;
43import org.onosproject.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
Ray Milkeye0fade72015-01-15 13:29:47 -080059import com.google.common.base.Strings;
60
Brian O'Connorabafb502014-12-02 22:26:20 -080061import static org.onosproject.net.DeviceId.deviceId;
62import static org.onosproject.net.Port.Type.COPPER;
63import static org.onosproject.net.Port.Type.FIBER;
64import static org.onosproject.openflow.controller.Dpid.dpid;
65import static org.onosproject.openflow.controller.Dpid.uri;
tom782a7cf2014-09-11 23:58:38 -070066import static org.slf4j.LoggerFactory.getLogger;
67
tomb5a46e62014-08-26 14:20:00 -070068/**
tomb1260e42014-08-26 18:39:57 -070069 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070070 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070071 */
tomb1260e42014-08-26 18:39:57 -070072@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070073public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070074
alshabiba89cc582014-09-09 16:43:00 -070075 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070076 private static final long MBPS = 1_000 * 1_000;
tomb5a46e62014-08-26 14:20:00 -070077
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070079 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070080
tom5f38b3a2014-08-27 23:50:54 -070081 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
82 protected OpenFlowController controller;
83
tomab21e7c2014-08-26 15:23:08 -070084 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070085
alshabib4680bb62014-09-04 17:15:08 -070086 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070087
tomab21e7c2014-08-26 15:23:08 -070088 /**
89 * Creates an OpenFlow device provider.
90 */
91 public OpenFlowDeviceProvider() {
Brian O'Connorabafb502014-12-02 22:26:20 -080092 super(new ProviderId("of", "org.onosproject.provider.openflow"));
tomab21e7c2014-08-26 15:23:08 -070093 }
94
tomb5a46e62014-08-26 14:20:00 -070095 @Activate
96 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070097 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070098 controller.addListener(listener);
alshabibc944fd02014-09-10 17:55:17 -070099 for (OpenFlowSwitch sw : controller.getSwitches()) {
Yuta HIGUCHIac2972a2014-11-18 12:58:52 -0800100 try {
101 listener.switchAdded(new Dpid(sw.getId()));
102 } catch (Exception e) {
103 LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
104 LOG.debug("Error details:", e);
105 // disconnect to trigger switch-add later
106 sw.disconnectSwitch();
107 }
alshabibc944fd02014-09-10 17:55:17 -0700108 }
alshabiba89cc582014-09-09 16:43:00 -0700109 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -0700110 }
111
112 @Deactivate
113 public void deactivate() {
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 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700143
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700144 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700145 OFFactory fact = sw.factory();
146 switch (fact.getVersion()) {
147 case OF_10:
148 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
149 break;
150 case OF_13:
151 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
152 break;
153 default:
154 LOG.warn("Unhandled protocol version");
155 }
tomab21e7c2014-08-26 15:23:08 -0700156 }
157
158 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700159 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700160 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700161 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700162 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700163 break;
164 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700165 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700166 break;
167 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700168 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700169 break;
170 default:
171 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700172
173 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700174 LOG.info("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700175 }
176
alshabibf1216ed2014-09-03 11:53:54 -0700177 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700178 @Override
tomd1900f32014-09-03 14:08:16 -0700179 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700180 if (providerService == null) {
181 return;
182 }
tom782a7cf2014-09-11 23:58:38 -0700183 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700184 OpenFlowSwitch sw = controller.getSwitch(dpid);
185
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700186 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
187 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700188 ChassisId cId = new ChassisId(dpid.value());
Ray Milkeye53f1712015-01-16 09:17:16 -0800189
Thomas Vachuska82041f52014-11-30 22:14:02 -0800190 SparseAnnotations annotations = DefaultAnnotations.builder()
Ray Milkeye53f1712015-01-16 09:17:16 -0800191 .set("protocol", sw.factory().getVersion().toString())
192 .set("channelId", sw.channelId())
193 .build();
194
tomd1900f32014-09-03 14:08:16 -0700195 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700196 new DefaultDeviceDescription(did.uri(), deviceType,
Ray Milkeyd3edd032015-01-16 11:38:58 -0800197 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700198 sw.hardwareDescription(),
199 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700200 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800201 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700202 providerService.deviceConnected(did, description);
203 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700204 }
205
alshabibf1216ed2014-09-03 11:53:54 -0700206 @Override
207 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700208 if (providerService == null) {
209 return;
210 }
tom782a7cf2014-09-11 23:58:38 -0700211 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700212 }
213
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700214
215 @Override
216 public void switchChanged(Dpid dpid) {
217 if (providerService == null) {
218 return;
219 }
220 DeviceId did = deviceId(uri(dpid));
221 OpenFlowSwitch sw = controller.getSwitch(dpid);
222 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
223 }
224
alshabiba14f3642014-09-05 09:31:31 -0700225 @Override
226 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800227 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700228 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700229 }
230
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700231 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700232 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
233 MastershipRole request = roleOf(requested);
234 MastershipRole reply = roleOf(response);
235
236 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
237 }
238
239 /**
240 * Translates a RoleState to the corresponding MastershipRole.
241 *
242 * @param response
243 * @return a MastershipRole
244 */
245 private MastershipRole roleOf(RoleState response) {
246 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700247 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700248 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700249 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700250 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700251 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700252 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700253 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700254 LOG.warn("unknown role {}", response);
255 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700256 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700257 }
258
alshabiba14f3642014-09-05 09:31:31 -0700259 /**
260 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700261 *
alshabiba14f3642014-09-05 09:31:31 -0700262 * @param ports the list of ports
263 * @return list of portdescriptions
264 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700265 private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700266 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700267 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700268 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700269 }
270 return portDescs;
271 }
272
alshabiba14f3642014-09-05 09:31:31 -0700273 /**
Ray Milkeye0fade72015-01-15 13:29:47 -0800274 * Creates an annotation for the port name if one is available.
275 *
276 * @param port description of the port
277 * @return annotation containing the port name if one is found,
278 * null otherwise
279 */
280 private SparseAnnotations makePortNameAnnotation(OFPortDesc port) {
281 SparseAnnotations annotations = null;
282 String portName = Strings.emptyToNull(port.getName());
283 if (portName != null) {
284 annotations = DefaultAnnotations.builder()
285 .set("portName", portName).build();
286 }
287 return annotations;
288 }
289
290 /**
alshabiba14f3642014-09-05 09:31:31 -0700291 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700292 *
alshabiba14f3642014-09-05 09:31:31 -0700293 * @param port the port to build from.
294 * @return portDescription for the port.
295 */
296 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700297 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
298 boolean enabled =
299 !port.getState().contains(OFPortState.LINK_DOWN) &&
300 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
301 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
Ray Milkeye0fade72015-01-15 13:29:47 -0800302 SparseAnnotations annotations = makePortNameAnnotation(port);
303 return new DefaultPortDescription(portNo, enabled, type,
304 portSpeed(port), annotations);
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;
Ray Milkeye0fade72015-01-15 13:29:47 -0800314 SparseAnnotations annotations = makePortNameAnnotation(port);
315 return new DefaultPortDescription(portNo, false, type,
316 portSpeed(port), annotations);
alshabibafc514a2014-12-01 14:44:05 -0800317 }
318 }
319
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700320 private long portSpeed(OFPortDesc port) {
321 if (port.getVersion() == OFVersion.OF_13) {
322 return port.getCurrSpeed() / MBPS;
323 }
324
325 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
326 for (OFPortFeatures feat : port.getCurr()) {
327 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
328 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700329 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700330 }
alshabibf1216ed2014-09-03 11:53:54 -0700331 }
332
tomb5a46e62014-08-26 14:20:00 -0700333}