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