blob: c8d9c25eca02c622ca2b88129bcf97db2b35f090 [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;
alshabiba68eb962014-09-24 20:34:13 -070012import org.onlab.onos.ApplicationId;
alshabiba7f7ca82014-09-22 11:41:23 -070013import org.onlab.onos.net.DeviceId;
alshabib1cc04f72014-09-16 16:09:58 -070014import org.onlab.onos.net.flow.FlowRule;
15import org.onlab.onos.net.flow.FlowRuleProvider;
16import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
17import org.onlab.onos.net.flow.FlowRuleProviderService;
18import org.onlab.onos.net.provider.AbstractProvider;
19import org.onlab.onos.net.provider.ProviderId;
20import org.onlab.onos.net.topology.TopologyService;
tom9c94c5b2014-09-17 13:14:42 -070021import org.onlab.onos.openflow.controller.Dpid;
22import org.onlab.onos.openflow.controller.OpenFlowController;
alshabibeec3a062014-09-17 18:01:26 -070023import org.onlab.onos.openflow.controller.OpenFlowEventListener;
tom9c94c5b2014-09-17 13:14:42 -070024import org.onlab.onos.openflow.controller.OpenFlowSwitch;
alshabibce4e5782014-09-17 14:56:42 -070025import org.onlab.onos.openflow.controller.OpenFlowSwitchListener;
Ayaka Koshibeab91cc42014-09-25 10:20:52 -070026import org.onlab.onos.openflow.controller.RoleState;
alshabib8f1cf4a2014-09-17 14:44:48 -070027import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
alshabib5c370ff2014-09-18 10:12:14 -070028import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
29import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
alshabib8f1cf4a2014-09-17 14:44:48 -070030import org.projectfloodlight.openflow.protocol.OFMessage;
31import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib5c370ff2014-09-18 10:12:14 -070032import org.projectfloodlight.openflow.protocol.OFStatsReply;
alshabib54ce5892014-09-23 17:50:51 -070033import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
alshabib5c370ff2014-09-18 10:12:14 -070034import org.projectfloodlight.openflow.protocol.OFStatsType;
alshabib1cc04f72014-09-16 16:09:58 -070035import org.slf4j.Logger;
36
alshabib54ce5892014-09-23 17:50:51 -070037import com.google.common.collect.ArrayListMultimap;
alshabib5c370ff2014-09-18 10:12:14 -070038import com.google.common.collect.Maps;
alshabib54ce5892014-09-23 17:50:51 -070039import com.google.common.collect.Multimap;
alshabibeec3a062014-09-17 18:01:26 -070040
alshabib1cc04f72014-09-16 16:09:58 -070041/**
42 * Provider which uses an OpenFlow controller to detect network
43 * end-station hosts.
44 */
45@Component(immediate = true)
46public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider {
47
48 private final Logger log = getLogger(getClass());
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected FlowRuleProviderRegistry providerRegistry;
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected OpenFlowController controller;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected TopologyService topologyService;
58
59 private FlowRuleProviderService providerService;
60
alshabibeec3a062014-09-17 18:01:26 -070061 private final InternalFlowProvider listener = new InternalFlowProvider();
62
alshabib1cc04f72014-09-16 16:09:58 -070063 /**
64 * Creates an OpenFlow host provider.
65 */
66 public OpenFlowRuleProvider() {
tom7e02cda2014-09-18 12:05:46 -070067 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -070068 }
69
70 @Activate
71 public void activate() {
72 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -070073 controller.addListener(listener);
74 controller.addEventListener(listener);
alshabib1cc04f72014-09-16 16:09:58 -070075 log.info("Started");
76 }
77
78 @Deactivate
79 public void deactivate() {
80 providerRegistry.unregister(this);
81 providerService = null;
82
83 log.info("Stopped");
84 }
85 @Override
86 public void applyFlowRule(FlowRule... flowRules) {
alshabib35edb1a2014-09-16 17:44:44 -070087 for (int i = 0; i < flowRules.length; i++) {
88 applyRule(flowRules[i]);
89 }
alshabib1cc04f72014-09-16 16:09:58 -070090 }
91
alshabib35edb1a2014-09-16 17:44:44 -070092 private void applyRule(FlowRule flowRule) {
93 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabib8f1cf4a2014-09-17 14:44:48 -070094 sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowMod());
alshabib35edb1a2014-09-16 17:44:44 -070095 }
96
alshabib35edb1a2014-09-16 17:44:44 -070097
alshabib35edb1a2014-09-16 17:44:44 -070098
alshabib1cc04f72014-09-16 16:09:58 -070099 @Override
100 public void removeFlowRule(FlowRule... flowRules) {
alshabib219ebaa2014-09-22 15:41:24 -0700101 for (int i = 0; i < flowRules.length; i++) {
102 removeRule(flowRules[i]);
103 }
alshabib1cc04f72014-09-16 16:09:58 -0700104
105 }
106
alshabib219ebaa2014-09-22 15:41:24 -0700107 private void removeRule(FlowRule flowRule) {
108 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
109 sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowDel());
110 }
111
alshabiba68eb962014-09-24 20:34:13 -0700112 @Override
113 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
114 // TODO: optimize using the ApplicationId
115 removeFlowRule(flowRules);
116 }
117
alshabib219ebaa2014-09-22 15:41:24 -0700118
alshabib1cc04f72014-09-16 16:09:58 -0700119 //TODO: InternalFlowRuleProvider listening to stats and error and flowremoved.
120 // possibly barriers as well. May not be internal at all...
alshabib8f1cf4a2014-09-17 14:44:48 -0700121 private class InternalFlowProvider
122 implements OpenFlowSwitchListener, OpenFlowEventListener {
123
alshabibeec3a062014-09-17 18:01:26 -0700124 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
alshabib54ce5892014-09-23 17:50:51 -0700125 private final Multimap<DeviceId, FlowRule> completeEntries =
126 ArrayListMultimap.create();
alshabib8f1cf4a2014-09-17 14:44:48 -0700127
128 @Override
129 public void switchAdded(Dpid dpid) {
alshabib219ebaa2014-09-22 15:41:24 -0700130 FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), 5);
alshabibeec3a062014-09-17 18:01:26 -0700131 fsc.start();
132 collectors.put(dpid, fsc);
alshabib8f1cf4a2014-09-17 14:44:48 -0700133 }
134
135 @Override
136 public void switchRemoved(Dpid dpid) {
alshabibeec3a062014-09-17 18:01:26 -0700137 collectors.remove(dpid).stop();
alshabib8f1cf4a2014-09-17 14:44:48 -0700138 }
139
140 @Override
141 public void portChanged(Dpid dpid, OFPortStatus status) {
142 //TODO: Decide whether to evict flows internal store.
143 }
144
145 @Override
146 public void handleMessage(Dpid dpid, OFMessage msg) {
147 switch (msg.getType()) {
148 case FLOW_REMOVED:
alshabibeec3a062014-09-17 18:01:26 -0700149 //TODO: make this better
alshabib8f1cf4a2014-09-17 14:44:48 -0700150 OFFlowRemoved removed = (OFFlowRemoved) msg;
alshabib6b5cfec2014-09-18 17:42:18 -0700151
152 FlowRule fr = new FlowRuleBuilder(dpid, removed).build();
alshabib8f1cf4a2014-09-17 14:44:48 -0700153 providerService.flowRemoved(fr);
154 break;
155 case STATS_REPLY:
alshabib97044902014-09-18 14:52:16 -0700156 pushFlowMetrics(dpid, (OFStatsReply) msg);
alshabibeec3a062014-09-17 18:01:26 -0700157 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700158 case BARRIER_REPLY:
159 case ERROR:
160 default:
161 log.warn("Unhandled message type: {}", msg.getType());
162 }
163
164 }
165
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700166 @Override
167 public void roleAssertFailed(Dpid dpid, RoleState role) {
168 // TODO Auto-generated method stub
169
170 }
171
alshabib54ce5892014-09-23 17:50:51 -0700172 private synchronized void pushFlowMetrics(Dpid dpid, OFStatsReply stats) {
alshabib5c370ff2014-09-18 10:12:14 -0700173 if (stats.getStatsType() != OFStatsType.FLOW) {
174 return;
175 }
alshabib54ce5892014-09-23 17:50:51 -0700176 DeviceId did = DeviceId.deviceId(Dpid.uri(dpid));
alshabib5c370ff2014-09-18 10:12:14 -0700177 final OFFlowStatsReply replies = (OFFlowStatsReply) stats;
alshabib54ce5892014-09-23 17:50:51 -0700178 //final List<FlowRule> entries = Lists.newLinkedList();
179
alshabib5c370ff2014-09-18 10:12:14 -0700180 for (OFFlowStatsEntry reply : replies.getEntries()) {
alshabib54ce5892014-09-23 17:50:51 -0700181 completeEntries.put(did, new FlowRuleBuilder(dpid, reply).build());
182 //entries.add(new FlowRuleBuilder(dpid, reply).build());
alshabib5c370ff2014-09-18 10:12:14 -0700183 }
alshabib54ce5892014-09-23 17:50:51 -0700184
185 if (!stats.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
186 log.debug("sending flowstats to core {}", completeEntries.get(did));
187 providerService.pushFlowMetrics(did, completeEntries.get(did));
188 completeEntries.removeAll(did);
189 }
alshabib5c370ff2014-09-18 10:12:14 -0700190 }
191
alshabib8f1cf4a2014-09-17 14:44:48 -0700192 }
alshabib1cc04f72014-09-16 16:09:58 -0700193
194
alshabiba68eb962014-09-24 20:34:13 -0700195
196
alshabib1cc04f72014-09-16 16:09:58 -0700197}