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