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