tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.of.device.impl; |
| 2 | |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 3 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 4 | import static org.slf4j.LoggerFactory.getLogger; |
| 5 | |
| 6 | import java.net.URI; |
| 7 | import java.net.URISyntaxException; |
| 8 | import java.util.ArrayList; |
| 9 | import java.util.List; |
| 10 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 11 | import org.apache.felix.scr.annotations.Activate; |
| 12 | import org.apache.felix.scr.annotations.Component; |
| 13 | import org.apache.felix.scr.annotations.Deactivate; |
| 14 | import org.apache.felix.scr.annotations.Reference; |
| 15 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.Device; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.DeviceId; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.MastershipRole; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.PortNumber; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.device.DefaultPortDescription; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.device.DeviceDescription; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.device.DeviceProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.device.DeviceProviderService; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.device.PortDescription; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.provider.AbstractProvider; |
| 28 | import org.onlab.onos.net.provider.ProviderId; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 29 | import org.onlab.onos.of.controller.Dpid; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 30 | import org.onlab.onos.of.controller.OpenFlowController; |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 31 | import org.onlab.onos.of.controller.OpenFlowSwitch; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 32 | import org.onlab.onos.of.controller.OpenFlowSwitchListener; |
| 33 | import org.onlab.onos.of.controller.RoleState; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 34 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 35 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.OFPortState; |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 38 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 39 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 40 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 41 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 42 | * infrastructure devices. |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 43 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 44 | @Component(immediate = true) |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 45 | public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider { |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 46 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 47 | private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 50 | protected DeviceProviderRegistry providerRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 51 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected OpenFlowController controller; |
| 54 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 55 | private DeviceProviderService providerService; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 56 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 57 | private final OpenFlowSwitchListener listener = new InternalDeviceProvider(); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 58 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Creates an OpenFlow device provider. |
| 61 | */ |
| 62 | public OpenFlowDeviceProvider() { |
tom | 578ebdc | 2014-09-11 11:12:51 -0700 | [diff] [blame^] | 63 | super(new ProviderId("org.onlab.onos.provider.openflow")); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 64 | } |
| 65 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 66 | @Activate |
| 67 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 68 | providerService = providerRegistry.register(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 69 | controller.addListener(listener); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 70 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 71 | listener.switchAdded(new Dpid(sw.getId())); |
| 72 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 73 | LOG.info("Started"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Deactivate |
| 77 | public void deactivate() { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 78 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 79 | providerService.deviceDisconnected(DeviceId.deviceId("of:" |
| 80 | + Long.toHexString(sw.getId()))); |
| 81 | } |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 82 | providerRegistry.unregister(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 83 | controller.removeListener(listener); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 84 | providerService = null; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 85 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 86 | LOG.info("Stopped"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 87 | } |
| 88 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public void triggerProbe(Device device) { |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 91 | LOG.info("Triggering probe on device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void roleChanged(Device device, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 96 | switch (newRole) { |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 97 | case MASTER: |
| 98 | controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), |
| 99 | RoleState.MASTER); |
| 100 | break; |
| 101 | case STANDBY: |
| 102 | controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), |
| 103 | RoleState.EQUAL); |
| 104 | break; |
| 105 | case NONE: |
| 106 | controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), |
| 107 | RoleState.SLAVE); |
| 108 | break; |
| 109 | default: |
| 110 | LOG.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 111 | |
| 112 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 113 | LOG.info("Accepting mastership role change for device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 114 | } |
| 115 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 116 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 117 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 118 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 119 | if (providerService == null) { |
| 120 | return; |
| 121 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 122 | URI uri = buildURI(dpid); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 123 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 124 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 125 | DeviceDescription description = |
| 126 | new DefaultDeviceDescription(buildURI(dpid), Device.Type.SWITCH, |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 127 | sw.manfacturerDescription(), |
| 128 | sw.hardwareDescription(), |
| 129 | sw.softwareDescription(), |
| 130 | sw.serialNumber()); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 131 | providerService.deviceConnected(deviceId(uri), description); |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 132 | providerService.updatePorts(deviceId(uri), buildPortDescriptions(sw.getPorts())); |
| 133 | } |
| 134 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public void switchRemoved(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 137 | if (providerService == null) { |
| 138 | return; |
| 139 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 140 | URI uri = buildURI(dpid); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 141 | providerService.deviceDisconnected(deviceId(uri)); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 142 | } |
| 143 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 144 | @Override |
| 145 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 146 | final PortDescription portDescription = buildPortDescription(status.getDesc()); |
| 147 | final URI uri = buildURI(dpid); |
| 148 | providerService.portStatusChanged(deviceId(uri), portDescription); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Given a dpid builds a URI for the device. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 153 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 154 | * @param dpid the dpid to build the uri from |
| 155 | * @return returns a uri of the form of:<dpidHexForm> |
| 156 | */ |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 157 | private URI buildURI(Dpid dpid) { |
| 158 | URI uri = null; |
| 159 | try { |
| 160 | uri = new URI("of", Long.toHexString(dpid.value()), null); |
| 161 | } catch (URISyntaxException e) { |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 162 | LOG.warn("URI construction for device {} failed.", dpid); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 163 | } |
| 164 | return uri; |
| 165 | } |
| 166 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 169 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 170 | * @param ports the list of ports |
| 171 | * @return list of portdescriptions |
| 172 | */ |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 173 | private List<PortDescription> buildPortDescriptions( |
| 174 | List<OFPortDesc> ports) { |
| 175 | final List<PortDescription> portDescs = new ArrayList<PortDescription>(); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 176 | for (OFPortDesc port : ports) { |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 177 | portDescs.add(buildPortDescription(port)); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 178 | } |
| 179 | return portDescs; |
| 180 | } |
| 181 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Build a portDescription from a given port. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 184 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 185 | * @param port the port to build from. |
| 186 | * @return portDescription for the port. |
| 187 | */ |
| 188 | private PortDescription buildPortDescription(OFPortDesc port) { |
| 189 | final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 190 | final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) && |
| 191 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 192 | return new DefaultPortDescription(portNo, enabled); |
| 193 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 194 | } |
| 195 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 196 | } |