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