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; |
| 17 | import org.onlab.onos.net.MastershipRole; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.PortNumber; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.device.DefaultPortDescription; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.device.DeviceDescription; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.device.DeviceProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.device.DeviceProviderService; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.device.PortDescription; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.provider.AbstractProvider; |
| 27 | import org.onlab.onos.net.provider.ProviderId; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 28 | import org.onlab.onos.of.controller.Dpid; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 29 | import org.onlab.onos.of.controller.OpenFlowController; |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 30 | import org.onlab.onos.of.controller.OpenFlowSwitch; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 31 | import org.onlab.onos.of.controller.OpenFlowSwitchListener; |
| 32 | import org.onlab.onos.of.controller.RoleState; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 33 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 34 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 35 | import org.projectfloodlight.openflow.protocol.OFPortState; |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 38 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 39 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 40 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 41 | * infrastructure devices. |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 42 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 43 | @Component(immediate = true) |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 44 | public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider { |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 45 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 46 | private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 49 | protected DeviceProviderRegistry providerRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 50 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected OpenFlowController controller; |
| 53 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 54 | private DeviceProviderService providerService; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 55 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 56 | private final OpenFlowSwitchListener listener = new InternalDeviceProvider(); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 57 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Creates an OpenFlow device provider. |
| 60 | */ |
| 61 | public OpenFlowDeviceProvider() { |
| 62 | super(new ProviderId("org.onlab.onos.provider.of.device")); |
| 63 | } |
| 64 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 65 | @Activate |
| 66 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 67 | providerService = providerRegistry.register(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 68 | controller.addListener(listener); |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 69 | LOG.info("Started"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 74 | providerRegistry.unregister(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 75 | controller.removeListener(listener); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 76 | providerService = null; |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 77 | LOG.info("Stopped"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 80 | @Override |
| 81 | public void triggerProbe(Device device) { |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 82 | LOG.info("Triggering probe on device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void roleChanged(Device device, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 87 | switch (newRole) { |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 88 | 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); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 102 | |
| 103 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 104 | LOG.info("Accepting mastership role change for device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 107 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 108 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 109 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 110 | if (providerService == null) { |
| 111 | return; |
| 112 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 113 | URI uri = buildURI(dpid); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 114 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 115 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 116 | DeviceDescription description = |
| 117 | new DefaultDeviceDescription(buildURI(dpid), Device.Type.SWITCH, |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 118 | sw.manfacturerDescription(), |
| 119 | sw.hardwareDescription(), |
| 120 | sw.softwareDescription(), |
| 121 | sw.serialNumber()); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 122 | providerService.deviceConnected(deviceId(uri), description); |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 123 | providerService.updatePorts(deviceId(uri), buildPortDescriptions(sw.getPorts())); |
| 124 | } |
| 125 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public void switchRemoved(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 128 | if (providerService == null) { |
| 129 | return; |
| 130 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 131 | URI uri = buildURI(dpid); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 132 | providerService.deviceDisconnected(deviceId(uri)); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 133 | } |
| 134 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 135 | @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. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 144 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 145 | * @param dpid the dpid to build the uri from |
| 146 | * @return returns a uri of the form of:<dpidHexForm> |
| 147 | */ |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 148 | 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) { |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 153 | LOG.warn("URI construction for device {} failed.", dpid); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 154 | } |
| 155 | return uri; |
| 156 | } |
| 157 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 160 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 161 | * @param ports the list of ports |
| 162 | * @return list of portdescriptions |
| 163 | */ |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 164 | private List<PortDescription> buildPortDescriptions( |
| 165 | List<OFPortDesc> ports) { |
| 166 | final List<PortDescription> portDescs = new ArrayList<PortDescription>(); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 167 | for (OFPortDesc port : ports) { |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 168 | portDescs.add(buildPortDescription(port)); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 169 | } |
| 170 | return portDescs; |
| 171 | } |
| 172 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Build a portDescription from a given port. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 175 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 176 | * @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 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 185 | } |
| 186 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 187 | } |