tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.of.device.impl; |
| 2 | |
| 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Deactivate; |
| 6 | import org.apache.felix.scr.annotations.Reference; |
| 7 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.Device; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.DeviceId; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.MastershipRole; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.PortNumber; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.device.DefaultPortDescription; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.device.DeviceDescription; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.device.DeviceProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.device.DeviceProviderService; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.device.PortDescription; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.provider.AbstractProvider; |
| 20 | import org.onlab.onos.net.provider.ProviderId; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 21 | import org.onlab.onos.of.controller.Dpid; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 22 | import org.onlab.onos.of.controller.OpenFlowController; |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 23 | import org.onlab.onos.of.controller.OpenFlowSwitch; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 24 | import org.onlab.onos.of.controller.OpenFlowSwitchListener; |
| 25 | import org.onlab.onos.of.controller.RoleState; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 27 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.protocol.OFPortState; |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 29 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 31 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame^] | 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
| 35 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 36 | import static org.onlab.onos.of.controller.Dpid.dpid; |
| 37 | import static org.onlab.onos.of.controller.Dpid.uri; |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 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()) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame^] | 79 | providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId()))); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 80 | } |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 81 | providerRegistry.unregister(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 82 | controller.removeListener(listener); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 83 | providerService = null; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 84 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 85 | LOG.info("Stopped"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 86 | } |
| 87 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public void triggerProbe(Device device) { |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 90 | LOG.info("Triggering probe on device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void roleChanged(Device device, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 95 | switch (newRole) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame^] | 96 | case MASTER: |
| 97 | controller.setRole(dpid(device.id().uri()), RoleState.MASTER); |
| 98 | break; |
| 99 | case STANDBY: |
| 100 | controller.setRole(dpid(device.id().uri()), RoleState.EQUAL); |
| 101 | break; |
| 102 | case NONE: |
| 103 | controller.setRole(dpid(device.id().uri()), RoleState.SLAVE); |
| 104 | break; |
| 105 | default: |
| 106 | LOG.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 107 | |
| 108 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 109 | LOG.info("Accepting mastership role change for device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 112 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 113 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 114 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 115 | if (providerService == null) { |
| 116 | return; |
| 117 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame^] | 118 | DeviceId did = deviceId(uri(dpid)); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 119 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 120 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 121 | DeviceDescription description = |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame^] | 122 | new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH, |
| 123 | sw.manfacturerDescription(), |
| 124 | sw.hardwareDescription(), |
| 125 | sw.softwareDescription(), |
| 126 | sw.serialNumber()); |
| 127 | providerService.deviceConnected(did, description); |
| 128 | providerService.updatePorts(did, buildPortDescriptions(sw.getPorts())); |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 129 | } |
| 130 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 131 | @Override |
| 132 | public void switchRemoved(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 133 | if (providerService == null) { |
| 134 | return; |
| 135 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame^] | 136 | providerService.deviceDisconnected(deviceId(uri(dpid))); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 137 | } |
| 138 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 139 | @Override |
| 140 | public void portChanged(Dpid dpid, OFPortStatus status) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame^] | 141 | PortDescription portDescription = buildPortDescription(status.getDesc()); |
| 142 | providerService.portStatusChanged(deviceId(uri(dpid)), portDescription); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 143 | } |
| 144 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 147 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 148 | * @param ports the list of ports |
| 149 | * @return list of portdescriptions |
| 150 | */ |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 151 | private List<PortDescription> buildPortDescriptions( |
| 152 | List<OFPortDesc> ports) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame^] | 153 | final List<PortDescription> portDescs = new ArrayList<>(); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 154 | for (OFPortDesc port : ports) { |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 155 | portDescs.add(buildPortDescription(port)); |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 156 | } |
| 157 | return portDescs; |
| 158 | } |
| 159 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Build a portDescription from a given port. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 162 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 163 | * @param port the port to build from. |
| 164 | * @return portDescription for the port. |
| 165 | */ |
| 166 | private PortDescription buildPortDescription(OFPortDesc port) { |
| 167 | final PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 168 | final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) && |
| 169 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 170 | return new DefaultPortDescription(portNo, enabled); |
| 171 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 172 | } |
| 173 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 174 | } |