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