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