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