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 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 46 | private final Logger log = getLogger(getClass()); |
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); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 69 | log.info("Started"); |
| 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; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 77 | log.info("Stopped"); |
| 78 | } |
| 79 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 80 | @Override |
| 81 | public void triggerProbe(Device device) { |
tom | e06f855 | 2014-08-26 16:58:42 -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) { |
tom | ff7eb7c | 2014-09-08 12:49:03 -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 | case NONE: |
| 96 | controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), |
| 97 | RoleState.SLAVE); |
| 98 | break; |
| 99 | default: |
| 100 | log.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 101 | |
| 102 | } |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 103 | log.info("Accepting mastership role change for device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 104 | } |
| 105 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 106 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 107 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 108 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 109 | if (providerService == null) { |
| 110 | return; |
| 111 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 112 | URI uri = buildURI(dpid); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 113 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 114 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 115 | DeviceDescription description = |
| 116 | new DefaultDeviceDescription(buildURI(dpid), Device.Type.SWITCH, |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame^] | 117 | sw.manfacturerDescription(), |
| 118 | sw.hardwareDescription(), |
| 119 | sw.softwareDescription(), |
| 120 | sw.serialNumber()); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 121 | providerService.deviceConnected(deviceId(uri), description); |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 122 | providerService.updatePorts(deviceId(uri), buildPortDescriptions(sw.getPorts())); |
| 123 | } |
| 124 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 125 | @Override |
| 126 | public void switchRemoved(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 127 | if (providerService == null) { |
| 128 | return; |
| 129 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 130 | URI uri = buildURI(dpid); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 131 | providerService.deviceDisconnected(deviceId(uri)); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 132 | } |
| 133 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 134 | @Override |
| 135 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 136 | final PortDescription portDescription = buildPortDescription(status.getDesc()); |
| 137 | final URI uri = buildURI(dpid); |
| 138 | providerService.portStatusChanged(deviceId(uri), portDescription); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Given a dpid builds a URI for the device. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame^] | 143 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 144 | * @param dpid the dpid to build the uri from |
| 145 | * @return returns a uri of the form of:<dpidHexForm> |
| 146 | */ |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 147 | private URI buildURI(Dpid dpid) { |
| 148 | URI uri = null; |
| 149 | try { |
| 150 | uri = new URI("of", Long.toHexString(dpid.value()), null); |
| 151 | } catch (URISyntaxException e) { |
| 152 | log.warn("URI construction for device {} failed.", dpid); |
| 153 | } |
| 154 | return uri; |
| 155 | } |
| 156 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame^] | 159 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 160 | * @param ports the list of ports |
| 161 | * @return list of portdescriptions |
| 162 | */ |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 163 | private List<PortDescription> buildPortDescriptions( |
| 164 | List<OFPortDesc> ports) { |
| 165 | final List<PortDescription> portDescs = new ArrayList<PortDescription>(); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 166 | for (OFPortDesc port : ports) { |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 167 | portDescs.add(buildPortDescription(port)); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 168 | } |
| 169 | return portDescs; |
| 170 | } |
| 171 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 172 | /** |
| 173 | * Build a portDescription from a given port. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame^] | 174 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 175 | * @param port the port to build from. |
| 176 | * @return portDescription for the port. |
| 177 | */ |
| 178 | private PortDescription buildPortDescription(OFPortDesc port) { |
| 179 | final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 180 | final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) && |
| 181 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 182 | return new DefaultPortDescription(portNo, enabled); |
| 183 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 184 | } |
| 185 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 186 | } |