blob: 5dbcd806a764d0c1e39279ef15ff0c4d512d2d61 [file] [log] [blame]
tomb5a46e62014-08-26 14:20:00 -07001package org.onlab.onos.provider.of.device.impl;
2
alshabib25c8eec2014-09-04 16:41:31 -07003import static org.onlab.onos.net.DeviceId.deviceId;
4import static org.slf4j.LoggerFactory.getLogger;
5
6import java.net.URI;
7import java.net.URISyntaxException;
8import java.util.ArrayList;
9import java.util.List;
10
tomb5a46e62014-08-26 14:20:00 -070011import org.apache.felix.scr.annotations.Activate;
12import org.apache.felix.scr.annotations.Component;
13import org.apache.felix.scr.annotations.Deactivate;
14import org.apache.felix.scr.annotations.Reference;
15import org.apache.felix.scr.annotations.ReferenceCardinality;
tomab21e7c2014-08-26 15:23:08 -070016import org.onlab.onos.net.Device;
17import org.onlab.onos.net.MastershipRole;
alshabib25c8eec2014-09-04 16:41:31 -070018import org.onlab.onos.net.PortNumber;
tomd1900f32014-09-03 14:08:16 -070019import org.onlab.onos.net.device.DefaultDeviceDescription;
alshabib25c8eec2014-09-04 16:41:31 -070020import org.onlab.onos.net.device.DefaultPortDescription;
alshabibf1216ed2014-09-03 11:53:54 -070021import org.onlab.onos.net.device.DeviceDescription;
tomab21e7c2014-08-26 15:23:08 -070022import org.onlab.onos.net.device.DeviceProvider;
tom96dfcab2014-08-28 09:26:03 -070023import org.onlab.onos.net.device.DeviceProviderRegistry;
tomab21e7c2014-08-26 15:23:08 -070024import org.onlab.onos.net.device.DeviceProviderService;
alshabib25c8eec2014-09-04 16:41:31 -070025import org.onlab.onos.net.device.PortDescription;
tomab21e7c2014-08-26 15:23:08 -070026import org.onlab.onos.net.provider.AbstractProvider;
27import org.onlab.onos.net.provider.ProviderId;
alshabibf1216ed2014-09-03 11:53:54 -070028import org.onlab.onos.of.controller.Dpid;
tom5f38b3a2014-08-27 23:50:54 -070029import org.onlab.onos.of.controller.OpenFlowController;
alshabib6f5460b2014-09-03 14:46:17 -070030import org.onlab.onos.of.controller.OpenFlowSwitch;
alshabibf1216ed2014-09-03 11:53:54 -070031import org.onlab.onos.of.controller.OpenFlowSwitchListener;
32import org.onlab.onos.of.controller.RoleState;
alshabib4680bb62014-09-04 17:15:08 -070033import org.projectfloodlight.openflow.protocol.OFPortConfig;
alshabib25c8eec2014-09-04 16:41:31 -070034import org.projectfloodlight.openflow.protocol.OFPortDesc;
alshabib4680bb62014-09-04 17:15:08 -070035import org.projectfloodlight.openflow.protocol.OFPortState;
alshabiba14f3642014-09-05 09:31:31 -070036import org.projectfloodlight.openflow.protocol.OFPortStatus;
tomb5a46e62014-08-26 14:20:00 -070037import org.slf4j.Logger;
tom5f38b3a2014-08-27 23:50:54 -070038
tomb5a46e62014-08-26 14:20:00 -070039/**
tomb1260e42014-08-26 18:39:57 -070040 * Provider which uses an OpenFlow controller to detect network
tome06f8552014-08-26 16:58:42 -070041 * infrastructure devices.
tomb5a46e62014-08-26 14:20:00 -070042 */
tomb1260e42014-08-26 18:39:57 -070043@Component(immediate = true)
tomab21e7c2014-08-26 15:23:08 -070044public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider {
tomb5a46e62014-08-26 14:20:00 -070045
alshabiba89cc582014-09-09 16:43:00 -070046 private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class);
tomb5a46e62014-08-26 14:20:00 -070047
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
tom96dfcab2014-08-28 09:26:03 -070049 protected DeviceProviderRegistry providerRegistry;
tomab21e7c2014-08-26 15:23:08 -070050
tom5f38b3a2014-08-27 23:50:54 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected OpenFlowController controller;
53
tomab21e7c2014-08-26 15:23:08 -070054 private DeviceProviderService providerService;
tomb5a46e62014-08-26 14:20:00 -070055
alshabib4680bb62014-09-04 17:15:08 -070056 private final OpenFlowSwitchListener listener = new InternalDeviceProvider();
tomd40fc7a2014-09-04 16:41:10 -070057
tomab21e7c2014-08-26 15:23:08 -070058 /**
59 * Creates an OpenFlow device provider.
60 */
61 public OpenFlowDeviceProvider() {
62 super(new ProviderId("org.onlab.onos.provider.of.device"));
63 }
64
tomb5a46e62014-08-26 14:20:00 -070065 @Activate
66 public void activate() {
tom96dfcab2014-08-28 09:26:03 -070067 providerService = providerRegistry.register(this);
tomd40fc7a2014-09-04 16:41:10 -070068 controller.addListener(listener);
alshabiba89cc582014-09-09 16:43:00 -070069 LOG.info("Started");
tomb5a46e62014-08-26 14:20:00 -070070 }
71
72 @Deactivate
73 public void deactivate() {
tom96dfcab2014-08-28 09:26:03 -070074 providerRegistry.unregister(this);
tomd40fc7a2014-09-04 16:41:10 -070075 controller.removeListener(listener);
tomab21e7c2014-08-26 15:23:08 -070076 providerService = null;
alshabiba89cc582014-09-09 16:43:00 -070077 LOG.info("Stopped");
tomb5a46e62014-08-26 14:20:00 -070078 }
79
tomab21e7c2014-08-26 15:23:08 -070080 @Override
81 public void triggerProbe(Device device) {
alshabiba89cc582014-09-09 16:43:00 -070082 LOG.info("Triggering probe on device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -070083 }
84
85 @Override
86 public void roleChanged(Device device, MastershipRole newRole) {
alshabibf1216ed2014-09-03 11:53:54 -070087 switch (newRole) {
alshabiba89cc582014-09-09 16:43:00 -070088 case MASTER:
89 controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
90 RoleState.MASTER);
91 break;
92 case STANDBY:
93 controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
94 RoleState.EQUAL);
95 break;
96 case NONE:
97 controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()),
98 RoleState.SLAVE);
99 break;
100 default:
101 LOG.error("Unknown Mastership state : {}", newRole);
alshabibf1216ed2014-09-03 11:53:54 -0700102
103 }
alshabiba89cc582014-09-09 16:43:00 -0700104 LOG.info("Accepting mastership role change for device {}", device.id());
tomab21e7c2014-08-26 15:23:08 -0700105 }
106
alshabibf1216ed2014-09-03 11:53:54 -0700107 private class InternalDeviceProvider implements OpenFlowSwitchListener {
alshabibf1216ed2014-09-03 11:53:54 -0700108 @Override
tomd1900f32014-09-03 14:08:16 -0700109 public void switchAdded(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700110 if (providerService == null) {
111 return;
112 }
alshabibf1216ed2014-09-03 11:53:54 -0700113 URI uri = buildURI(dpid);
alshabib6f5460b2014-09-03 14:46:17 -0700114 OpenFlowSwitch sw = controller.getSwitch(dpid);
115
tomd1900f32014-09-03 14:08:16 -0700116 DeviceDescription description =
117 new DefaultDeviceDescription(buildURI(dpid), Device.Type.SWITCH,
alshabiba89cc582014-09-09 16:43:00 -0700118 sw.manfacturerDescription(),
119 sw.hardwareDescription(),
120 sw.softwareDescription(),
121 sw.serialNumber());
tomca20e0c2014-09-03 23:22:24 -0700122 providerService.deviceConnected(deviceId(uri), description);
alshabib25c8eec2014-09-04 16:41:31 -0700123 providerService.updatePorts(deviceId(uri), buildPortDescriptions(sw.getPorts()));
124 }
125
alshabibf1216ed2014-09-03 11:53:54 -0700126 @Override
127 public void switchRemoved(Dpid dpid) {
alshabib6f5460b2014-09-03 14:46:17 -0700128 if (providerService == null) {
129 return;
130 }
alshabibf1216ed2014-09-03 11:53:54 -0700131 URI uri = buildURI(dpid);
tomca20e0c2014-09-03 23:22:24 -0700132 providerService.deviceDisconnected(deviceId(uri));
alshabibf1216ed2014-09-03 11:53:54 -0700133 }
134
alshabiba14f3642014-09-05 09:31:31 -0700135 @Override
136 public void portChanged(Dpid dpid, OFPortStatus status) {
137 final PortDescription portDescription = buildPortDescription(status.getDesc());
138 final URI uri = buildURI(dpid);
139 providerService.portStatusChanged(deviceId(uri), portDescription);
140 }
141
142 /**
143 * Given a dpid builds a URI for the device.
tomff7eb7c2014-09-08 12:49:03 -0700144 *
alshabiba14f3642014-09-05 09:31:31 -0700145 * @param dpid the dpid to build the uri from
146 * @return returns a uri of the form of:<dpidHexForm>
147 */
alshabibf1216ed2014-09-03 11:53:54 -0700148 private URI buildURI(Dpid dpid) {
149 URI uri = null;
150 try {
151 uri = new URI("of", Long.toHexString(dpid.value()), null);
152 } catch (URISyntaxException e) {
alshabiba89cc582014-09-09 16:43:00 -0700153 LOG.warn("URI construction for device {} failed.", dpid);
alshabibf1216ed2014-09-03 11:53:54 -0700154 }
155 return uri;
156 }
157
alshabiba14f3642014-09-05 09:31:31 -0700158 /**
159 * Builds a list of port descriptions for a given list of ports.
tomff7eb7c2014-09-08 12:49:03 -0700160 *
alshabiba14f3642014-09-05 09:31:31 -0700161 * @param ports the list of ports
162 * @return list of portdescriptions
163 */
alshabib4680bb62014-09-04 17:15:08 -0700164 private List<PortDescription> buildPortDescriptions(
165 List<OFPortDesc> ports) {
166 final List<PortDescription> portDescs = new ArrayList<PortDescription>();
alshabib4680bb62014-09-04 17:15:08 -0700167 for (OFPortDesc port : ports) {
alshabiba14f3642014-09-05 09:31:31 -0700168 portDescs.add(buildPortDescription(port));
alshabib4680bb62014-09-04 17:15:08 -0700169 }
170 return portDescs;
171 }
172
alshabiba14f3642014-09-05 09:31:31 -0700173 /**
174 * Build a portDescription from a given port.
tomff7eb7c2014-09-08 12:49:03 -0700175 *
alshabiba14f3642014-09-05 09:31:31 -0700176 * @param port the port to build from.
177 * @return portDescription for the port.
178 */
179 private PortDescription buildPortDescription(OFPortDesc port) {
180 final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber());
181 final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) &&
182 !port.getConfig().contains(OFPortConfig.PORT_DOWN);
183 return new DefaultPortDescription(portNo, enabled);
184 }
alshabibf1216ed2014-09-03 11:53:54 -0700185 }
186
tomb5a46e62014-08-26 14:20:00 -0700187}