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