tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.of.device.impl; |
| 2 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame^] | 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.net.URI; |
| 6 | import java.net.URISyntaxException; |
| 7 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 8 | import org.apache.felix.scr.annotations.Activate; |
| 9 | import org.apache.felix.scr.annotations.Component; |
| 10 | import org.apache.felix.scr.annotations.Deactivate; |
| 11 | import org.apache.felix.scr.annotations.Reference; |
| 12 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.Device; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame^] | 14 | import org.onlab.onos.net.DeviceId; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.MastershipRole; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame^] | 16 | import org.onlab.onos.net.device.DeviceDescription; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.device.DeviceProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.device.DeviceProviderRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.device.DeviceProviderService; |
| 20 | import org.onlab.onos.net.provider.AbstractProvider; |
| 21 | import org.onlab.onos.net.provider.ProviderId; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame^] | 22 | import org.onlab.onos.of.controller.Dpid; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 23 | import org.onlab.onos.of.controller.OpenFlowController; |
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; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 26 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 27 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 28 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 29 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 30 | * infrastructure devices. |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 31 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 32 | @Component(immediate = true) |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 33 | public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider { |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 34 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 35 | private final Logger log = getLogger(getClass()); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 36 | |
| 37 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 38 | protected DeviceProviderRegistry providerRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 39 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 40 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 41 | protected OpenFlowController controller; |
| 42 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 43 | private DeviceProviderService providerService; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 44 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Creates an OpenFlow device provider. |
| 47 | */ |
| 48 | public OpenFlowDeviceProvider() { |
| 49 | super(new ProviderId("org.onlab.onos.provider.of.device")); |
| 50 | } |
| 51 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 52 | @Activate |
| 53 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 54 | providerService = providerRegistry.register(this); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame^] | 55 | controller.addListener(new InternalDeviceProvider()); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 56 | log.info("Started"); |
| 57 | } |
| 58 | |
| 59 | @Deactivate |
| 60 | public void deactivate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 61 | providerRegistry.unregister(this); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 62 | providerService = null; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 63 | log.info("Stopped"); |
| 64 | } |
| 65 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 66 | @Override |
| 67 | public void triggerProbe(Device device) { |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 68 | log.info("Triggering probe on device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void roleChanged(Device device, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame^] | 73 | switch (newRole) { |
| 74 | case MASTER: |
| 75 | controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), |
| 76 | RoleState.MASTER); |
| 77 | break; |
| 78 | case STANDBY: |
| 79 | controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), |
| 80 | RoleState.EQUAL); |
| 81 | case NONE: |
| 82 | controller.setRole(new Dpid(device.id().uri().getSchemeSpecificPart()), |
| 83 | RoleState.SLAVE); |
| 84 | break; |
| 85 | default: |
| 86 | log.error("Unknown Mastership state : {}", newRole); |
| 87 | |
| 88 | } |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 89 | log.info("Accepting mastership role change for device {}", device.id()); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 90 | } |
| 91 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame^] | 92 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
| 93 | |
| 94 | @Override |
| 95 | public void switchAdded(final Dpid dpid) { |
| 96 | URI uri = buildURI(dpid); |
| 97 | providerService.deviceConnected(new DeviceId(uri), new DeviceDescription() { |
| 98 | |
| 99 | @Override |
| 100 | public URI deviceURI() { |
| 101 | return buildURI(dpid); |
| 102 | } |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void switchRemoved(Dpid dpid) { |
| 108 | URI uri = buildURI(dpid); |
| 109 | providerService.deviceDisconnected(new DeviceId(uri)); |
| 110 | } |
| 111 | |
| 112 | private URI buildURI(Dpid dpid) { |
| 113 | URI uri = null; |
| 114 | try { |
| 115 | uri = new URI("of", Long.toHexString(dpid.value()), null); |
| 116 | } catch (URISyntaxException e) { |
| 117 | log.warn("URI construction for device {} failed.", dpid); |
| 118 | } |
| 119 | return uri; |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 124 | } |