alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 1 | package org.onlab.onos.provider.of.flow.impl; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 5 | import java.util.Map; |
| 6 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Reference; |
| 11 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | a7f7ca8 | 2014-09-22 11:41:23 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.DeviceId; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.flow.FlowRule; |
| 14 | import org.onlab.onos.net.flow.FlowRuleProvider; |
| 15 | import org.onlab.onos.net.flow.FlowRuleProviderRegistry; |
| 16 | import org.onlab.onos.net.flow.FlowRuleProviderService; |
| 17 | import org.onlab.onos.net.provider.AbstractProvider; |
| 18 | import org.onlab.onos.net.provider.ProviderId; |
| 19 | import org.onlab.onos.net.topology.TopologyService; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 20 | import org.onlab.onos.openflow.controller.Dpid; |
| 21 | import org.onlab.onos.openflow.controller.OpenFlowController; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 22 | import org.onlab.onos.openflow.controller.OpenFlowEventListener; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 23 | import org.onlab.onos.openflow.controller.OpenFlowSwitch; |
alshabib | ce4e578 | 2014-09-17 14:56:42 -0700 | [diff] [blame] | 24 | import org.onlab.onos.openflow.controller.OpenFlowSwitchListener; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 25 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 27 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 29 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 30 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame^] | 31 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 32 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame^] | 35 | import com.google.common.collect.ArrayListMultimap; |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 36 | import com.google.common.collect.Maps; |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame^] | 37 | import com.google.common.collect.Multimap; |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 38 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Provider which uses an OpenFlow controller to detect network |
| 41 | * end-station hosts. |
| 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider { |
| 45 | |
| 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected FlowRuleProviderRegistry providerRegistry; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected OpenFlowController controller; |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected TopologyService topologyService; |
| 56 | |
| 57 | private FlowRuleProviderService providerService; |
| 58 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 59 | private final InternalFlowProvider listener = new InternalFlowProvider(); |
| 60 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Creates an OpenFlow host provider. |
| 63 | */ |
| 64 | public OpenFlowRuleProvider() { |
tom | 7e02cda | 2014-09-18 12:05:46 -0700 | [diff] [blame] | 65 | super(new ProviderId("of", "org.onlab.onos.provider.openflow")); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
| 70 | providerService = providerRegistry.register(this); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 71 | controller.addListener(listener); |
| 72 | controller.addEventListener(listener); |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 73 | log.info("Started"); |
| 74 | } |
| 75 | |
| 76 | @Deactivate |
| 77 | public void deactivate() { |
| 78 | providerRegistry.unregister(this); |
| 79 | providerService = null; |
| 80 | |
| 81 | log.info("Stopped"); |
| 82 | } |
| 83 | @Override |
| 84 | public void applyFlowRule(FlowRule... flowRules) { |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 85 | for (int i = 0; i < flowRules.length; i++) { |
| 86 | applyRule(flowRules[i]); |
| 87 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 88 | } |
| 89 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 90 | private void applyRule(FlowRule flowRule) { |
| 91 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 92 | sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowMod()); |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 93 | } |
| 94 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 95 | |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 96 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public void removeFlowRule(FlowRule... flowRules) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 99 | for (int i = 0; i < flowRules.length; i++) { |
| 100 | removeRule(flowRules[i]); |
| 101 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 102 | |
| 103 | } |
| 104 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 105 | |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 106 | private void removeRule(FlowRule flowRule) { |
| 107 | OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri())); |
| 108 | sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowDel()); |
| 109 | } |
| 110 | |
| 111 | |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 112 | //TODO: InternalFlowRuleProvider listening to stats and error and flowremoved. |
| 113 | // possibly barriers as well. May not be internal at all... |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 114 | private class InternalFlowProvider |
| 115 | implements OpenFlowSwitchListener, OpenFlowEventListener { |
| 116 | |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 117 | private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap(); |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame^] | 118 | private final Multimap<DeviceId, FlowRule> completeEntries = |
| 119 | ArrayListMultimap.create(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 120 | |
| 121 | @Override |
| 122 | public void switchAdded(Dpid dpid) { |
alshabib | 219ebaa | 2014-09-22 15:41:24 -0700 | [diff] [blame] | 123 | FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), 5); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 124 | fsc.start(); |
| 125 | collectors.put(dpid, fsc); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void switchRemoved(Dpid dpid) { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 130 | collectors.remove(dpid).stop(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 135 | //TODO: Decide whether to evict flows internal store. |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 140 | switch (msg.getType()) { |
| 141 | case FLOW_REMOVED: |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 142 | //TODO: make this better |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 143 | OFFlowRemoved removed = (OFFlowRemoved) msg; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 144 | |
| 145 | FlowRule fr = new FlowRuleBuilder(dpid, removed).build(); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 146 | providerService.flowRemoved(fr); |
| 147 | break; |
| 148 | case STATS_REPLY: |
alshabib | 9704490 | 2014-09-18 14:52:16 -0700 | [diff] [blame] | 149 | pushFlowMetrics(dpid, (OFStatsReply) msg); |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 150 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 151 | case BARRIER_REPLY: |
| 152 | case ERROR: |
| 153 | default: |
| 154 | log.warn("Unhandled message type: {}", msg.getType()); |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame^] | 159 | private synchronized void pushFlowMetrics(Dpid dpid, OFStatsReply stats) { |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 160 | if (stats.getStatsType() != OFStatsType.FLOW) { |
| 161 | return; |
| 162 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame^] | 163 | DeviceId did = DeviceId.deviceId(Dpid.uri(dpid)); |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 164 | final OFFlowStatsReply replies = (OFFlowStatsReply) stats; |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame^] | 165 | //final List<FlowRule> entries = Lists.newLinkedList(); |
| 166 | |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 167 | for (OFFlowStatsEntry reply : replies.getEntries()) { |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame^] | 168 | completeEntries.put(did, new FlowRuleBuilder(dpid, reply).build()); |
| 169 | //entries.add(new FlowRuleBuilder(dpid, reply).build()); |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 170 | } |
alshabib | 54ce589 | 2014-09-23 17:50:51 -0700 | [diff] [blame^] | 171 | |
| 172 | if (!stats.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 173 | log.debug("sending flowstats to core {}", completeEntries.get(did)); |
| 174 | providerService.pushFlowMetrics(did, completeEntries.get(did)); |
| 175 | completeEntries.removeAll(did); |
| 176 | } |
alshabib | 5c370ff | 2014-09-18 10:12:14 -0700 | [diff] [blame] | 177 | } |
| 178 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 179 | } |
alshabib | 1cc04f7 | 2014-09-16 16:09:58 -0700 | [diff] [blame] | 180 | |
| 181 | |
| 182 | } |