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