blob: 044a1a334f2e5aba85a153e04f8f2b049a07a2b8 [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
Brian O'Connorabafb502014-12-02 22:26:20 -080059import static org.onosproject.net.DeviceId.deviceId;
60import static org.onosproject.net.Port.Type.COPPER;
61import static org.onosproject.net.Port.Type.FIBER;
62import static org.onosproject.openflow.controller.Dpid.dpid;
63import static org.onosproject.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() {
Brian O'Connorabafb502014-12-02 22:26:20 -080090 super(new ProviderId("of", "org.onosproject.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() {
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 }
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700141
Ayaka Koshibee60d4522014-10-28 15:07:00 -0700142 // Prompt an update of port information. We can use any XID for this.
Ayaka Koshibee8708e32014-10-22 13:40:18 -0700143 OFFactory fact = sw.factory();
144 switch (fact.getVersion()) {
145 case OF_10:
146 sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build());
147 break;
148 case OF_13:
149 sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build());
150 break;
151 default:
152 LOG.warn("Unhandled protocol version");
153 }
tomab21e7c2014-08-26 15:23:08 -0700154 }
155
156 @Override
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700157 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -0700158 switch (newRole) {
tom782a7cf2014-09-11 23:58:38 -0700159 case MASTER:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700160 controller.setRole(dpid(deviceId.uri()), RoleState.MASTER);
tom782a7cf2014-09-11 23:58:38 -0700161 break;
162 case STANDBY:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700163 controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL);
tom782a7cf2014-09-11 23:58:38 -0700164 break;
165 case NONE:
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700166 controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE);
tom782a7cf2014-09-11 23:58:38 -0700167 break;
168 default:
169 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700170
171 }
Yuta HIGUCHI54815322014-10-31 23:17:08 -0700172 LOG.info("Accepting mastership role change for device {}", deviceId);
tomab21e7c2014-08-26 15:23:08 -0700173 }
174
alshabibf1216ed2014-09-03 11:53:54 -0700175 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700176 @Override
tomd1900f32014-09-03 14:08:16 -0700177 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700178 if (providerService == null) {
179 return;
180 }
tom782a7cf2014-09-11 23:58:38 -0700181 DeviceId did = deviceId(uri(dpid));
alshabib6f5460b2014-09-03 14:46:17 -0700182 OpenFlowSwitch sw = controller.getSwitch(dpid);
183
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700184 Device.Type deviceType = sw.isOptical() ? Device.Type.ROADM :
185 Device.Type.SWITCH;
alshabib7911a052014-10-16 17:49:37 -0700186 ChassisId cId = new ChassisId(dpid.value());
Thomas Vachuska82041f52014-11-30 22:14:02 -0800187 SparseAnnotations annotations = DefaultAnnotations.builder()
188 .set("protocol", sw.factory().getVersion().toString()).build();
tomd1900f32014-09-03 14:08:16 -0700189 DeviceDescription description =
Praseed Balakrishnana22eadf2014-10-20 14:21:45 -0700190 new DefaultDeviceDescription(did.uri(), deviceType,
Ray Milkeyd3edd032015-01-16 11:38:58 -0800191 sw.manufacturerDescription(),
tom782a7cf2014-09-11 23:58:38 -0700192 sw.hardwareDescription(),
193 sw.softwareDescription(),
alshabib7911a052014-10-16 17:49:37 -0700194 sw.serialNumber(),
Thomas Vachuska82041f52014-11-30 22:14:02 -0800195 cId, annotations);
tom782a7cf2014-09-11 23:58:38 -0700196 providerService.deviceConnected(did, description);
197 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
alshabib25c8eec2014-09-04 16:41:31 -0700198 }
199
alshabibf1216ed2014-09-03 11:53:54 -0700200 @Override
201 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700202 if (providerService == null) {
203 return;
204 }
tom782a7cf2014-09-11 23:58:38 -0700205 providerService.deviceDisconnected(deviceId(uri(dpid)));
alshabibf1216ed2014-09-03 11:53:54 -0700206 }
207
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700208
209 @Override
210 public void switchChanged(Dpid dpid) {
211 if (providerService == null) {
212 return;
213 }
214 DeviceId did = deviceId(uri(dpid));
215 OpenFlowSwitch sw = controller.getSwitch(dpid);
216 providerService.updatePorts(did, buildPortDescriptions(sw.getPorts()));
217 }
218
alshabiba14f3642014-09-05 09:31:31 -0700219 @Override
220 public void portChanged(Dpid dpid, OFPortStatus status) {
alshabibafc514a2014-12-01 14:44:05 -0800221 PortDescription portDescription = buildPortDescription(status);
tom782a7cf2014-09-11 23:58:38 -0700222 providerService.portStatusChanged(deviceId(uri(dpid)), portDescription);
alshabibf1216ed2014-09-03 11:53:54 -0700223 }
224
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700225 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700226 public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
227 MastershipRole request = roleOf(requested);
228 MastershipRole reply = roleOf(response);
229
230 providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply);
231 }
232
233 /**
234 * Translates a RoleState to the corresponding MastershipRole.
235 *
236 * @param response
237 * @return a MastershipRole
238 */
239 private MastershipRole roleOf(RoleState response) {
240 switch (response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700241 case MASTER:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700242 return MastershipRole.MASTER;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700243 case EQUAL:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700244 return MastershipRole.STANDBY;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700245 case SLAVE:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700246 return MastershipRole.NONE;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700247 default:
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700248 LOG.warn("unknown role {}", response);
249 return null;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700250 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700251 }
252
alshabiba14f3642014-09-05 09:31:31 -0700253 /**
254 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700255 *
alshabiba14f3642014-09-05 09:31:31 -0700256 * @param ports the list of ports
257 * @return list of portdescriptions
258 */
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700259 private List<PortDescription> buildPortDescriptions(List<OFPortDesc> ports) {
Yuta HIGUCHI38294a92014-10-16 18:09:49 -0700260 final List<PortDescription> portDescs = new ArrayList<>(ports.size());
alshabib4680bb62014-09-04 17:15:08 -0700261 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700262 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700263 }
264 return portDescs;
265 }
266
alshabiba14f3642014-09-05 09:31:31 -0700267 /**
268 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700269 *
alshabiba14f3642014-09-05 09:31:31 -0700270 * @param port the port to build from.
271 * @return portDescription for the port.
272 */
273 private PortDescription buildPortDescription(OFPortDesc port) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700274 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
275 boolean enabled =
276 !port.getState().contains(OFPortState.LINK_DOWN) &&
277 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
278 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
279 return new DefaultPortDescription(portNo, enabled, type, portSpeed(port));
alshabiba14f3642014-09-05 09:31:31 -0700280 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700281
alshabibafc514a2014-12-01 14:44:05 -0800282 private PortDescription buildPortDescription(OFPortStatus status) {
283 OFPortDesc port = status.getDesc();
284 if (status.getReason() != OFPortReason.DELETE) {
285 return buildPortDescription(port);
286 } else {
287 PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
288 Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER;
289 return new DefaultPortDescription(portNo, false, type, portSpeed(port));
290 }
291 }
292
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700293 private long portSpeed(OFPortDesc port) {
294 if (port.getVersion() == OFVersion.OF_13) {
295 return port.getCurrSpeed() / MBPS;
296 }
297
298 PortSpeed portSpeed = PortSpeed.SPEED_NONE;
299 for (OFPortFeatures feat : port.getCurr()) {
300 portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed());
301 }
Thomas Vachuska98eda532014-10-29 17:31:02 -0700302 return portSpeed.getSpeedBps() / MBPS;
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700303 }
alshabibf1216ed2014-09-03 11:53:54 -0700304 }
305
tomb5a46e62014-08-26 14:20:00 -0700306}