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; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 21 | import org.onlab.onos.openflow.controller.Dpid; |
| 22 | import org.onlab.onos.openflow.controller.OpenFlowController; |
| 23 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
| 24 | import org.onlab.onos.openflow.controller.OpenFlowSwitchListener; |
| 25 | import org.onlab.onos.openflow.controller.RoleState; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame^] | 26 | import org.onlab.packet.ChassisId; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 27 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 29 | import org.projectfloodlight.openflow.protocol.OFPortState; |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 30 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 32 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | |
| 36 | import static org.onlab.onos.net.DeviceId.deviceId; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 37 | import static org.onlab.onos.openflow.controller.Dpid.dpid; |
| 38 | import static org.onlab.onos.openflow.controller.Dpid.uri; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 41 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 42 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 43 | * infrastructure devices. |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 44 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 45 | @Component(immediate = true) |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 46 | public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider { |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 47 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 48 | private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 51 | protected DeviceProviderRegistry providerRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 52 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected OpenFlowController controller; |
| 55 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 56 | private DeviceProviderService providerService; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 57 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 58 | private final OpenFlowSwitchListener listener = new InternalDeviceProvider(); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 59 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Creates an OpenFlow device provider. |
| 62 | */ |
| 63 | public OpenFlowDeviceProvider() { |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 64 | super(new ProviderId("of", "org.onlab.onos.provider.openflow")); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 65 | } |
| 66 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 67 | @Activate |
| 68 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 69 | providerService = providerRegistry.register(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 70 | controller.addListener(listener); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 71 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 72 | listener.switchAdded(new Dpid(sw.getId())); |
| 73 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 74 | LOG.info("Started"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Deactivate |
| 78 | public void deactivate() { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 79 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 80 | providerService.deviceDisconnected(DeviceId.deviceId(uri(sw.getId()))); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 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) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 97 | case MASTER: |
| 98 | controller.setRole(dpid(device.id().uri()), RoleState.MASTER); |
| 99 | break; |
| 100 | case STANDBY: |
| 101 | controller.setRole(dpid(device.id().uri()), RoleState.EQUAL); |
| 102 | break; |
| 103 | case NONE: |
| 104 | controller.setRole(dpid(device.id().uri()), RoleState.SLAVE); |
| 105 | break; |
| 106 | default: |
| 107 | LOG.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 108 | |
| 109 | } |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 110 | LOG.info("Accepting mastership role change for device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 111 | } |
| 112 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 113 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 114 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 115 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 116 | if (providerService == null) { |
| 117 | return; |
| 118 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 119 | DeviceId did = deviceId(uri(dpid)); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 120 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame^] | 121 | ChassisId cId = new ChassisId(dpid.value()); |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 122 | DeviceDescription description = |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 123 | new DefaultDeviceDescription(did.uri(), Device.Type.SWITCH, |
| 124 | sw.manfacturerDescription(), |
| 125 | sw.hardwareDescription(), |
| 126 | sw.softwareDescription(), |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame^] | 127 | sw.serialNumber(), |
| 128 | cId); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 129 | providerService.deviceConnected(did, description); |
| 130 | providerService.updatePorts(did, buildPortDescriptions(sw.getPorts())); |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 131 | } |
| 132 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public void switchRemoved(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 135 | if (providerService == null) { |
| 136 | return; |
| 137 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 138 | providerService.deviceDisconnected(deviceId(uri(dpid))); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 139 | } |
| 140 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 141 | @Override |
| 142 | public void portChanged(Dpid dpid, OFPortStatus status) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 143 | PortDescription portDescription = buildPortDescription(status.getDesc()); |
| 144 | providerService.portStatusChanged(deviceId(uri(dpid)), portDescription); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public void roleAssertFailed(Dpid dpid, RoleState role) { |
| 149 | MastershipRole failed; |
| 150 | switch (role) { |
| 151 | case MASTER: |
| 152 | failed = MastershipRole.MASTER; |
| 153 | break; |
| 154 | case EQUAL: |
| 155 | failed = MastershipRole.STANDBY; |
| 156 | break; |
| 157 | case SLAVE: |
| 158 | failed = MastershipRole.NONE; |
| 159 | break; |
| 160 | default: |
| 161 | LOG.warn("unknown role {}", role); |
| 162 | return; |
| 163 | } |
| 164 | providerService.unableToAssertRole(deviceId(uri(dpid)), failed); |
| 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) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 175 | final List<PortDescription> portDescs = new ArrayList<>(); |
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 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 194 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 195 | } |
| 196 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 197 | } |