tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.of.link.impl; |
| 2 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.Map; |
| 6 | import java.util.concurrent.ConcurrentHashMap; |
| 7 | |
tom | e06f855 | 2014-08-26 16:58:42 -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; |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.DeviceId; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.link.LinkProvider; |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.link.LinkProviderRegistry; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.link.LinkProviderService; |
| 17 | import org.onlab.onos.net.provider.AbstractProvider; |
| 18 | import org.onlab.onos.net.provider.ProviderId; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 19 | import org.onlab.onos.openflow.controller.Dpid; |
| 20 | import org.onlab.onos.openflow.controller.OpenFlowController; |
| 21 | import org.onlab.onos.openflow.controller.OpenFlowPacketContext; |
| 22 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
| 23 | import org.onlab.onos.openflow.controller.OpenFlowSwitchListener; |
| 24 | import org.onlab.onos.openflow.controller.PacketListener; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 25 | import org.onlab.onos.openflow.controller.RoleState; |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
| 27 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 28 | import org.projectfloodlight.openflow.protocol.OFPortState; |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 29 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 31 | |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 32 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 33 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 34 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 35 | * infrastructure links. |
| 36 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 37 | @Component(immediate = true) |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 38 | public class OpenFlowLinkProvider extends AbstractProvider implements LinkProvider { |
| 39 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 40 | private final Logger log = getLogger(getClass()); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 41 | |
| 42 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 43 | protected LinkProviderRegistry providerRegistry; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 44 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | protected OpenFlowController controller; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 47 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 48 | private LinkProviderService providerService; |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 49 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 50 | private final boolean useBDDP = true; |
| 51 | |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 52 | private final InternalLinkProvider listener = new InternalLinkProvider(); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 53 | |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 54 | protected final Map<Dpid, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 55 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Creates an OpenFlow link provider. |
| 58 | */ |
| 59 | public OpenFlowLinkProvider() { |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 60 | super(new ProviderId("of", "org.onlab.onos.provider.openflow")); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 65 | providerService = providerRegistry.register(this); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 66 | controller.addListener(listener); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 67 | controller.addPacketListener(0, listener); |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 68 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 69 | listener.switchAdded(new Dpid(sw.getId())); |
| 70 | } |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 71 | log.info("Started"); |
| 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | public void deactivate() { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 76 | for (LinkDiscovery ld : discoverers.values()) { |
| 77 | ld.stop(); |
| 78 | } |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 79 | providerRegistry.unregister(this); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 80 | controller.removeListener(listener); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 81 | controller.removePacketListener(listener); |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 82 | providerService = null; |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 83 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 84 | log.info("Stopped"); |
| 85 | } |
| 86 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 87 | |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 88 | private class InternalLinkProvider implements PacketListener, OpenFlowSwitchListener { |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 89 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 90 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 91 | @Override |
alshabib | e703156 | 2014-09-12 18:17:37 -0700 | [diff] [blame] | 92 | public void handlePacket(OpenFlowPacketContext pktCtx) { |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 93 | LinkDiscovery ld = discoverers.get(pktCtx.dpid()); |
| 94 | if (ld == null) { |
| 95 | return; |
| 96 | } |
alshabib | 505bc6b | 2014-09-09 15:04:13 -0700 | [diff] [blame] | 97 | if (ld.handleLLDP(pktCtx.unparsed(), pktCtx.inPort())) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 98 | pktCtx.block(); |
alshabib | 505bc6b | 2014-09-09 15:04:13 -0700 | [diff] [blame] | 99 | } |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 100 | |
| 101 | } |
| 102 | |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public void switchAdded(Dpid dpid) { |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 105 | discoverers.put(dpid, new LinkDiscovery(controller.getSwitch(dpid), |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 106 | controller, providerService, useBDDP)); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 107 | |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void switchRemoved(Dpid dpid) { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 112 | LinkDiscovery ld = discoverers.remove(dpid); |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 113 | if (ld != null) { |
| 114 | ld.removeAllPorts(); |
| 115 | } |
| 116 | providerService.linksVanished( |
| 117 | DeviceId.deviceId("of:" + Long.toHexString(dpid.value()))); |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void portChanged(Dpid dpid, OFPortStatus status) { |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 122 | LinkDiscovery ld = discoverers.get(dpid); |
| 123 | if (ld == null) { |
| 124 | return; |
| 125 | } |
| 126 | final OFPortDesc port = status.getDesc(); |
| 127 | final boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) && |
| 128 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 129 | if (enabled) { |
| 130 | ld.addPort(port); |
| 131 | } else { |
alshabib | c944fd0 | 2014-09-10 17:55:17 -0700 | [diff] [blame] | 132 | /* |
| 133 | * remove port calls linkVanished |
| 134 | */ |
alshabib | a159a32 | 2014-09-09 14:50:51 -0700 | [diff] [blame] | 135 | ld.removePort(port); |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 136 | } |
alshabib | 289652c | 2014-09-07 19:09:28 -0700 | [diff] [blame] | 137 | |
| 138 | } |
| 139 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public void roleAssertFailed(Dpid dpid, RoleState role) { |
alshabib | 64231d8 | 2014-09-25 18:25:31 -0700 | [diff] [blame] | 142 | // do nothing for this. |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 143 | } |
| 144 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 145 | } |
| 146 | |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 147 | } |