blob: 9a5a9dd389cfb16c82d3e2b81df899d1c2a4b39e [file] [log] [blame]
alshabib1cc04f72014-09-16 16:09:58 -07001package org.onlab.onos.provider.of.flow.impl;
2
3import static org.slf4j.LoggerFactory.getLogger;
4
alshabibeec3a062014-09-17 18:01:26 -07005import java.util.Map;
6
alshabib1cc04f72014-09-16 16:09:58 -07007import org.apache.felix.scr.annotations.Activate;
8import org.apache.felix.scr.annotations.Component;
9import org.apache.felix.scr.annotations.Deactivate;
10import org.apache.felix.scr.annotations.Reference;
11import org.apache.felix.scr.annotations.ReferenceCardinality;
12import org.onlab.onos.net.DeviceId;
alshabib8f1cf4a2014-09-17 14:44:48 -070013import org.onlab.onos.net.flow.DefaultFlowRule;
alshabib1cc04f72014-09-16 16:09:58 -070014import org.onlab.onos.net.flow.FlowEntry;
15import org.onlab.onos.net.flow.FlowRule;
16import org.onlab.onos.net.flow.FlowRuleProvider;
17import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
18import org.onlab.onos.net.flow.FlowRuleProviderService;
19import org.onlab.onos.net.provider.AbstractProvider;
20import org.onlab.onos.net.provider.ProviderId;
21import org.onlab.onos.net.topology.TopologyService;
tom9c94c5b2014-09-17 13:14:42 -070022import org.onlab.onos.openflow.controller.Dpid;
23import org.onlab.onos.openflow.controller.OpenFlowController;
alshabibeec3a062014-09-17 18:01:26 -070024import org.onlab.onos.openflow.controller.OpenFlowEventListener;
tom9c94c5b2014-09-17 13:14:42 -070025import org.onlab.onos.openflow.controller.OpenFlowSwitch;
alshabibce4e5782014-09-17 14:56:42 -070026import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
alshabib8f1cf4a2014-09-17 14:44:48 -070027import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
28import org.projectfloodlight.openflow.protocol.OFMessage;
29import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib1cc04f72014-09-16 16:09:58 -070030import org.slf4j.Logger;
31
alshabibeec3a062014-09-17 18:01:26 -070032import com.google.common.collect.Maps;
33
alshabib1cc04f72014-09-16 16:09:58 -070034/**
35 * Provider which uses an OpenFlow controller to detect network
36 * end-station hosts.
37 */
38@Component(immediate = true)
39public 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
alshabibeec3a062014-09-17 18:01:26 -070054 private final InternalFlowProvider listener = new InternalFlowProvider();
55
alshabib1cc04f72014-09-16 16:09:58 -070056 /**
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);
alshabibeec3a062014-09-17 18:01:26 -070066 controller.addListener(listener);
67 controller.addEventListener(listener);
alshabib1cc04f72014-09-16 16:09:58 -070068 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) {
alshabib35edb1a2014-09-16 17:44:44 -070080 for (int i = 0; i < flowRules.length; i++) {
81 applyRule(flowRules[i]);
82 }
alshabib1cc04f72014-09-16 16:09:58 -070083 }
84
alshabib35edb1a2014-09-16 17:44:44 -070085 private void applyRule(FlowRule flowRule) {
86 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabib8f1cf4a2014-09-17 14:44:48 -070087 sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowMod());
alshabib35edb1a2014-09-16 17:44:44 -070088 }
89
alshabib35edb1a2014-09-16 17:44:44 -070090
alshabib35edb1a2014-09-16 17:44:44 -070091
alshabib1cc04f72014-09-16 16:09:58 -070092 @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...
alshabib8f1cf4a2014-09-17 14:44:48 -0700107 private class InternalFlowProvider
108 implements OpenFlowSwitchListener, OpenFlowEventListener {
109
alshabibeec3a062014-09-17 18:01:26 -0700110 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
alshabib8f1cf4a2014-09-17 14:44:48 -0700111
112 @Override
113 public void switchAdded(Dpid dpid) {
alshabibeec3a062014-09-17 18:01:26 -0700114 FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), 1);
115 fsc.start();
116 collectors.put(dpid, fsc);
alshabib8f1cf4a2014-09-17 14:44:48 -0700117 }
118
119 @Override
120 public void switchRemoved(Dpid dpid) {
alshabibeec3a062014-09-17 18:01:26 -0700121 collectors.remove(dpid).stop();
alshabib8f1cf4a2014-09-17 14:44:48 -0700122
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:
alshabibeec3a062014-09-17 18:01:26 -0700134 //TODO: make this better
alshabib8f1cf4a2014-09-17 14:44:48 -0700135 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:
alshabibeec3a062014-09-17 18:01:26 -0700140 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700141 case BARRIER_REPLY:
142 case ERROR:
143 default:
144 log.warn("Unhandled message type: {}", msg.getType());
145 }
146
147 }
148
149 }
alshabib1cc04f72014-09-16 16:09:58 -0700150
151
152}