blob: 9eed3c90b5dcb6ca7b09d0c1f7f56996f2a43ebf [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
5import org.apache.felix.scr.annotations.Activate;
6import org.apache.felix.scr.annotations.Component;
7import org.apache.felix.scr.annotations.Deactivate;
8import org.apache.felix.scr.annotations.Reference;
9import org.apache.felix.scr.annotations.ReferenceCardinality;
10import org.onlab.onos.net.DeviceId;
alshabib8f1cf4a2014-09-17 14:44:48 -070011import org.onlab.onos.net.flow.DefaultFlowRule;
alshabib1cc04f72014-09-16 16:09:58 -070012import org.onlab.onos.net.flow.FlowEntry;
13import org.onlab.onos.net.flow.FlowRule;
14import org.onlab.onos.net.flow.FlowRuleProvider;
15import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
16import org.onlab.onos.net.flow.FlowRuleProviderService;
17import org.onlab.onos.net.provider.AbstractProvider;
18import org.onlab.onos.net.provider.ProviderId;
19import org.onlab.onos.net.topology.TopologyService;
alshabib35edb1a2014-09-16 17:44:44 -070020import org.onlab.onos.of.controller.Dpid;
alshabib1cc04f72014-09-16 16:09:58 -070021import org.onlab.onos.of.controller.OpenFlowController;
alshabib8f1cf4a2014-09-17 14:44:48 -070022import org.onlab.onos.of.controller.OpenFlowEventListener;
alshabib35edb1a2014-09-16 17:44:44 -070023import org.onlab.onos.of.controller.OpenFlowSwitch;
alshabib8f1cf4a2014-09-17 14:44:48 -070024import org.onlab.onos.of.controller.OpenFlowSwitchListener;
25import org.projectfloodlight.openflow.protocol.OFFlowRemoved;
26import org.projectfloodlight.openflow.protocol.OFMessage;
27import org.projectfloodlight.openflow.protocol.OFPortStatus;
alshabib1cc04f72014-09-16 16:09:58 -070028import org.slf4j.Logger;
29
30/**
31 * Provider which uses an OpenFlow controller to detect network
32 * end-station hosts.
33 */
34@Component(immediate = true)
35public 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) {
alshabib35edb1a2014-09-16 17:44:44 -070072 for (int i = 0; i < flowRules.length; i++) {
73 applyRule(flowRules[i]);
74 }
alshabib1cc04f72014-09-16 16:09:58 -070075 }
76
alshabib35edb1a2014-09-16 17:44:44 -070077 private void applyRule(FlowRule flowRule) {
78 OpenFlowSwitch sw = controller.getSwitch(Dpid.dpid(flowRule.deviceId().uri()));
alshabib8f1cf4a2014-09-17 14:44:48 -070079 sw.sendMsg(new FlowModBuilder(flowRule, sw.factory()).buildFlowMod());
alshabib35edb1a2014-09-16 17:44:44 -070080 }
81
alshabib35edb1a2014-09-16 17:44:44 -070082
alshabib35edb1a2014-09-16 17:44:44 -070083
alshabib1cc04f72014-09-16 16:09:58 -070084 @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...
alshabib8f1cf4a2014-09-17 14:44:48 -070099 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 }
alshabib1cc04f72014-09-16 16:09:58 -0700138
139
140}