blob: 73e00bbc9361f1aad8d9225829c2fad8ce764062 [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
alshabibb45d1962014-09-18 14:25:45 -07005import java.util.List;
alshabibeec3a062014-09-17 18:01:26 -07006import java.util.Map;
7
alshabib1cc04f72014-09-16 16:09:58 -07008import org.apache.felix.scr.annotations.Activate;
9import org.apache.felix.scr.annotations.Component;
10import org.apache.felix.scr.annotations.Deactivate;
11import org.apache.felix.scr.annotations.Reference;
12import org.apache.felix.scr.annotations.ReferenceCardinality;
13import org.onlab.onos.net.DeviceId;
alshabib8f1cf4a2014-09-17 14:44:48 -070014import org.onlab.onos.net.flow.DefaultFlowRule;
alshabib1cc04f72014-09-16 16:09:58 -070015import 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;
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;
33import org.projectfloodlight.openflow.protocol.OFStatsType;
alshabib1cc04f72014-09-16 16:09:58 -070034import org.slf4j.Logger;
35
alshabib5c370ff2014-09-18 10:12:14 -070036import com.google.common.collect.Lists;
37import com.google.common.collect.Maps;
alshabibeec3a062014-09-17 18:01:26 -070038
alshabib1cc04f72014-09-16 16:09:58 -070039/**
40 * Provider which uses an OpenFlow controller to detect network
41 * end-station hosts.
42 */
43@Component(immediate = true)
44public class OpenFlowRuleProvider extends AbstractProvider implements FlowRuleProvider {
45
46 private final Logger log = getLogger(getClass());
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected FlowRuleProviderRegistry providerRegistry;
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected OpenFlowController controller;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected TopologyService topologyService;
56
57 private FlowRuleProviderService providerService;
58
alshabibeec3a062014-09-17 18:01:26 -070059 private final InternalFlowProvider listener = new InternalFlowProvider();
60
alshabib1cc04f72014-09-16 16:09:58 -070061 /**
62 * Creates an OpenFlow host provider.
63 */
64 public OpenFlowRuleProvider() {
tom7e02cda2014-09-18 12:05:46 -070065 super(new ProviderId("of", "org.onlab.onos.provider.openflow"));
alshabib1cc04f72014-09-16 16:09:58 -070066 }
67
68 @Activate
69 public void activate() {
70 providerService = providerRegistry.register(this);
alshabibeec3a062014-09-17 18:01:26 -070071 controller.addListener(listener);
72 controller.addEventListener(listener);
alshabib1cc04f72014-09-16 16:09:58 -070073 log.info("Started");
74 }
75
76 @Deactivate
77 public void deactivate() {
78 providerRegistry.unregister(this);
79 providerService = null;
80
81 log.info("Stopped");
82 }
83 @Override
84 public void applyFlowRule(FlowRule... flowRules) {
alshabib35edb1a2014-09-16 17:44:44 -070085 for (int i = 0; i < flowRules.length; i++) {
86 applyRule(flowRules[i]);
87 }
alshabib1cc04f72014-09-16 16:09:58 -070088 }
89
alshabib35edb1a2014-09-16 17:44:44 -070090 private void applyRule(FlowRule flowRule) {
91 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabib8f1cf4a2014-09-17 14:44:48 -070092 sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowMod());
alshabib35edb1a2014-09-16 17:44:44 -070093 }
94
alshabib35edb1a2014-09-16 17:44:44 -070095
alshabib35edb1a2014-09-16 17:44:44 -070096
alshabib1cc04f72014-09-16 16:09:58 -070097 @Override
98 public void removeFlowRule(FlowRule... flowRules) {
99 // TODO Auto-generated method stub
100
101 }
102
alshabib1cc04f72014-09-16 16:09:58 -0700103
104 //TODO: InternalFlowRuleProvider listening to stats and error and flowremoved.
105 // possibly barriers as well. May not be internal at all...
alshabib8f1cf4a2014-09-17 14:44:48 -0700106 private class InternalFlowProvider
107 implements OpenFlowSwitchListener, OpenFlowEventListener {
108
alshabibeec3a062014-09-17 18:01:26 -0700109 private final Map<Dpid, FlowStatsCollector> collectors = Maps.newHashMap();
alshabib8f1cf4a2014-09-17 14:44:48 -0700110
111 @Override
112 public void switchAdded(Dpid dpid) {
alshabibeec3a062014-09-17 18:01:26 -0700113 FlowStatsCollector fsc = new FlowStatsCollector(controller.getSwitch(dpid), 1);
114 fsc.start();
115 collectors.put(dpid, fsc);
alshabib8f1cf4a2014-09-17 14:44:48 -0700116 }
117
118 @Override
119 public void switchRemoved(Dpid dpid) {
alshabibeec3a062014-09-17 18:01:26 -0700120 collectors.remove(dpid).stop();
alshabib8f1cf4a2014-09-17 14:44:48 -0700121 }
122
123 @Override
124 public void portChanged(Dpid dpid, OFPortStatus status) {
125 //TODO: Decide whether to evict flows internal store.
126 }
127
128 @Override
129 public void handleMessage(Dpid dpid, OFMessage msg) {
130 switch (msg.getType()) {
131 case FLOW_REMOVED:
alshabibeec3a062014-09-17 18:01:26 -0700132 //TODO: make this better
alshabib8f1cf4a2014-09-17 14:44:48 -0700133 OFFlowRemoved removed = (OFFlowRemoved) msg;
Ayaka Koshibed4e53e12014-09-18 14:24:55 -0700134 FlowRule fr = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)), null, null, 0);
alshabib8f1cf4a2014-09-17 14:44:48 -0700135 providerService.flowRemoved(fr);
136 break;
137 case STATS_REPLY:
alshabib97044902014-09-18 14:52:16 -0700138 pushFlowMetrics(dpid, (OFStatsReply) msg);
alshabibeec3a062014-09-17 18:01:26 -0700139 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700140 case BARRIER_REPLY:
141 case ERROR:
142 default:
143 log.warn("Unhandled message type: {}", msg.getType());
144 }
145
146 }
147
alshabib97044902014-09-18 14:52:16 -0700148 private void pushFlowMetrics(Dpid dpid, OFStatsReply stats) {
alshabib5c370ff2014-09-18 10:12:14 -0700149 if (stats.getStatsType() != OFStatsType.FLOW) {
150 return;
151 }
152 final OFFlowStatsReply replies = (OFFlowStatsReply) stats;
alshabibb45d1962014-09-18 14:25:45 -0700153 final List<FlowRule> entries = Lists.newLinkedList();
alshabib5c370ff2014-09-18 10:12:14 -0700154 for (OFFlowStatsEntry reply : replies.getEntries()) {
alshabib97044902014-09-18 14:52:16 -0700155 entries.add(new FlowRuleBuilder(dpid, reply).build());
alshabib5c370ff2014-09-18 10:12:14 -0700156 }
alshabibb45d1962014-09-18 14:25:45 -0700157 providerService.pushFlowMetrics(entries);
alshabib5c370ff2014-09-18 10:12:14 -0700158 }
159
alshabib8f1cf4a2014-09-17 14:44:48 -0700160 }
alshabib1cc04f72014-09-16 16:09:58 -0700161
162
163}