alshabib

trying to fix flow state

Change-Id: I62845842c66cb99cb14bd54bc9602edf7c0cae39
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
2 2
3 <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> 3 <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
4 <command> 4 <command>
5 + <action class="org.onlab.onos.cli.net.FlowsListCommand"/>
6 + </command>
7 + <command>
5 <action class="org.onlab.onos.cli.net.DevicesListCommand"/> 8 <action class="org.onlab.onos.cli.net.DevicesListCommand"/>
6 </command> 9 </command>
7 <command> 10 <command>
......
...@@ -15,19 +15,20 @@ public class DefaultFlowRule implements FlowRule { ...@@ -15,19 +15,20 @@ public class DefaultFlowRule implements FlowRule {
15 private final FlowId id; 15 private final FlowId id;
16 private final long created; 16 private final long created;
17 private final long life; 17 private final long life;
18 - private final long idle;
19 private final long packets; 18 private final long packets;
20 private final long bytes; 19 private final long bytes;
20 + private final FlowRuleState state;
21 21
22 22
23 public DefaultFlowRule(DeviceId deviceId, 23 public DefaultFlowRule(DeviceId deviceId,
24 - TrafficSelector selector, TrafficTreatment treatment, int priority) { 24 + TrafficSelector selector, TrafficTreatment treatment,
25 + int priority, FlowRuleState state) {
25 this.deviceId = deviceId; 26 this.deviceId = deviceId;
26 this.priority = priority; 27 this.priority = priority;
27 this.selector = selector; 28 this.selector = selector;
28 this.treatment = treatment; 29 this.treatment = treatment;
30 + this.state = state;
29 this.life = 0; 31 this.life = 0;
30 - this.idle = 0;
31 this.packets = 0; 32 this.packets = 0;
32 this.bytes = 0; 33 this.bytes = 0;
33 this.id = FlowId.valueOf(this.hashCode()); 34 this.id = FlowId.valueOf(this.hashCode());
...@@ -35,22 +36,32 @@ public class DefaultFlowRule implements FlowRule { ...@@ -35,22 +36,32 @@ public class DefaultFlowRule implements FlowRule {
35 } 36 }
36 37
37 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector, 38 public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
38 - TrafficTreatment treatment, int priority, 39 + TrafficTreatment treatment, int priority, FlowRuleState state,
39 - long life, long idle, long packets, long bytes, Integer flowId) { 40 + long life, long packets, long bytes, Integer flowId) {
40 this.deviceId = deviceId; 41 this.deviceId = deviceId;
41 this.priority = priority; 42 this.priority = priority;
42 this.selector = selector; 43 this.selector = selector;
43 this.treatment = treatment; 44 this.treatment = treatment;
45 + this.state = state;
44 46
45 this.id = FlowId.valueOf(flowId); 47 this.id = FlowId.valueOf(flowId);
46 48
47 this.life = life; 49 this.life = life;
48 - this.idle = idle;
49 this.packets = packets; 50 this.packets = packets;
50 this.bytes = bytes; 51 this.bytes = bytes;
51 this.created = System.currentTimeMillis(); 52 this.created = System.currentTimeMillis();
52 } 53 }
53 54
55 + public DefaultFlowRule(DeviceId deviceId, TrafficSelector selector,
56 + TrafficTreatment treatement, int priority) {
57 + this(deviceId, selector, treatement, priority, FlowRuleState.CREATED);
58 + }
59 +
60 + public DefaultFlowRule(FlowRule rule, FlowRuleState state) {
61 + this(rule.deviceId(), rule.selector(), rule.treatment(),
62 + rule.priority(), state);
63 + }
64 +
54 65
55 @Override 66 @Override
56 public FlowId id() { 67 public FlowId id() {
...@@ -83,11 +94,6 @@ public class DefaultFlowRule implements FlowRule { ...@@ -83,11 +94,6 @@ public class DefaultFlowRule implements FlowRule {
83 } 94 }
84 95
85 @Override 96 @Override
86 - public long idleMillis() {
87 - return idle;
88 - }
89 -
90 - @Override
91 public long packets() { 97 public long packets() {
92 return packets; 98 return packets;
93 } 99 }
...@@ -98,6 +104,12 @@ public class DefaultFlowRule implements FlowRule { ...@@ -98,6 +104,12 @@ public class DefaultFlowRule implements FlowRule {
98 } 104 }
99 105
100 @Override 106 @Override
107 + public FlowRuleState state() {
108 + return this.state;
109 + }
110 +
111 +
112 + @Override
101 /* 113 /*
102 * The priority and statistics can change on a given treatment and selector 114 * The priority and statistics can change on a given treatment and selector
103 * 115 *
...@@ -116,19 +128,14 @@ public class DefaultFlowRule implements FlowRule { ...@@ -116,19 +128,14 @@ public class DefaultFlowRule implements FlowRule {
116 * @see java.lang.Object#equals(java.lang.Object) 128 * @see java.lang.Object#equals(java.lang.Object)
117 */ 129 */
118 public boolean equals(Object obj) { 130 public boolean equals(Object obj) {
119 - if (obj instanceof FlowRule) { 131 + if (this == obj) {
120 - DefaultFlowRule that = (DefaultFlowRule) obj;
121 - if (!this.deviceId().equals(that.deviceId())) {
122 - return false;
123 - }
124 - if (!this.treatment().equals(that.treatment())) {
125 - return false;
126 - }
127 - if (!this.selector().equals(that.selector())) {
128 - return false;
129 - }
130 return true; 132 return true;
131 } 133 }
134 + if (obj instanceof FlowRule) {
135 + FlowRule that = (FlowRule) obj;
136 + return Objects.equals(deviceId, that.deviceId()) &&
137 + Objects.equals(id, that.id());
138 + }
132 return false; 139 return false;
133 } 140 }
134 141
...@@ -144,5 +151,4 @@ public class DefaultFlowRule implements FlowRule { ...@@ -144,5 +151,4 @@ public class DefaultFlowRule implements FlowRule {
144 .toString(); 151 .toString();
145 } 152 }
146 153
147 -
148 } 154 }
......
1 package org.onlab.onos.net.flow; 1 package org.onlab.onos.net.flow;
2 2
3 +import com.google.common.base.Objects;
4 +
3 /** 5 /**
4 * Representation of a Flow ID. 6 * Representation of a Flow ID.
5 */ 7 */
6 public final class FlowId { 8 public final class FlowId {
7 9
8 - private final int flowid; 10 + private final long flowid;
9 11
10 - private FlowId(int id) { 12 + private FlowId(long id) {
11 this.flowid = id; 13 this.flowid = id;
12 } 14 }
13 15
...@@ -15,7 +17,24 @@ public final class FlowId { ...@@ -15,7 +17,24 @@ public final class FlowId {
15 return new FlowId(id); 17 return new FlowId(id);
16 } 18 }
17 19
18 - public int value() { 20 + public long value() {
19 return flowid; 21 return flowid;
20 } 22 }
23 +
24 + @Override
25 + public boolean equals(Object obj) {
26 + if (this == obj) {
27 + return true;
28 + }
29 + if (obj.getClass() == this.getClass()) {
30 + FlowId that = (FlowId) obj;
31 + return Objects.equal(this.flowid, that.flowid);
32 + }
33 + return false;
34 + }
35 +
36 + @Override
37 + public int hashCode() {
38 + return Objects.hashCode(this.flowid);
39 + }
21 } 40 }
......
...@@ -8,6 +8,42 @@ import org.onlab.onos.net.DeviceId; ...@@ -8,6 +8,42 @@ import org.onlab.onos.net.DeviceId;
8 */ 8 */
9 public interface FlowRule { 9 public interface FlowRule {
10 10
11 +
12 + public enum FlowRuleState {
13 + /**
14 + * Indicates that this rule has been created.
15 + */
16 + CREATED,
17 +
18 + /**
19 + * Indicates that this rule has been submitted for addition.
20 + * Not necessarily in the flow table.
21 + */
22 + PENDING_ADD,
23 +
24 + /**
25 + * Rule has been added which means it is in the flow table.
26 + */
27 + ADDED,
28 +
29 + /**
30 + * Flow has been marked for removal, might still be in flow table.
31 + */
32 + PENDING_REMOVE,
33 +
34 + /**
35 + * Flow has been removed from flow table and can be purged.
36 + */
37 + REMOVED
38 + }
39 +
40 + /**
41 + * Returns the flow rule state.
42 + *
43 + * @return flow rule state
44 + */
45 + FlowRuleState state();
46 +
11 //TODO: build cookie value 47 //TODO: build cookie value
12 /** 48 /**
13 * Returns the ID of this flow. 49 * Returns the ID of this flow.
...@@ -54,13 +90,6 @@ public interface FlowRule { ...@@ -54,13 +90,6 @@ public interface FlowRule {
54 long lifeMillis(); 90 long lifeMillis();
55 91
56 /** 92 /**
57 - * Returns the number of milliseconds this flow rule has been idle.
58 - *
59 - * @return number of millis
60 - */
61 - long idleMillis();
62 -
63 - /**
64 * Returns the number of packets this flow rule has matched. 93 * Returns the number of packets this flow rule has matched.
65 * 94 *
66 * @return number of packets 95 * @return number of packets
......
1 package org.onlab.onos.net.flow; 1 package org.onlab.onos.net.flow;
2 2
3 +import org.onlab.onos.net.DeviceId;
3 import org.onlab.onos.net.provider.ProviderService; 4 import org.onlab.onos.net.provider.ProviderService;
4 5
5 /** 6 /**
...@@ -35,6 +36,6 @@ public interface FlowRuleProviderService extends ProviderService<FlowRuleProvide ...@@ -35,6 +36,6 @@ public interface FlowRuleProviderService extends ProviderService<FlowRuleProvide
35 * 36 *
36 * @param flowRules collection of flow rules 37 * @param flowRules collection of flow rules
37 */ 38 */
38 - void pushFlowMetrics(Iterable<FlowRule> flowRules); 39 + void pushFlowMetrics(DeviceId deviceId, Iterable<FlowRule> flowRules);
39 40
40 } 41 }
......
...@@ -4,6 +4,7 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -4,6 +4,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
4 import static org.slf4j.LoggerFactory.getLogger; 4 import static org.slf4j.LoggerFactory.getLogger;
5 5
6 import java.util.ArrayList; 6 import java.util.ArrayList;
7 +import java.util.Iterator;
7 import java.util.List; 8 import java.util.List;
8 9
9 import org.apache.felix.scr.annotations.Activate; 10 import org.apache.felix.scr.annotations.Activate;
...@@ -17,7 +18,9 @@ import org.onlab.onos.event.EventDeliveryService; ...@@ -17,7 +18,9 @@ import org.onlab.onos.event.EventDeliveryService;
17 import org.onlab.onos.net.Device; 18 import org.onlab.onos.net.Device;
18 import org.onlab.onos.net.DeviceId; 19 import org.onlab.onos.net.DeviceId;
19 import org.onlab.onos.net.device.DeviceService; 20 import org.onlab.onos.net.device.DeviceService;
21 +import org.onlab.onos.net.flow.DefaultFlowRule;
20 import org.onlab.onos.net.flow.FlowRule; 22 import org.onlab.onos.net.flow.FlowRule;
23 +import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
21 import org.onlab.onos.net.flow.FlowRuleEvent; 24 import org.onlab.onos.net.flow.FlowRuleEvent;
22 import org.onlab.onos.net.flow.FlowRuleListener; 25 import org.onlab.onos.net.flow.FlowRuleListener;
23 import org.onlab.onos.net.flow.FlowRuleProvider; 26 import org.onlab.onos.net.flow.FlowRuleProvider;
...@@ -29,17 +32,19 @@ import org.onlab.onos.net.provider.AbstractProviderRegistry; ...@@ -29,17 +32,19 @@ import org.onlab.onos.net.provider.AbstractProviderRegistry;
29 import org.onlab.onos.net.provider.AbstractProviderService; 32 import org.onlab.onos.net.provider.AbstractProviderService;
30 import org.slf4j.Logger; 33 import org.slf4j.Logger;
31 34
35 +import com.google.common.collect.Lists;
36 +
32 @Component(immediate = true) 37 @Component(immediate = true)
33 @Service 38 @Service
34 public class FlowRuleManager 39 public class FlowRuleManager
35 - extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService> 40 +extends AbstractProviderRegistry<FlowRuleProvider, FlowRuleProviderService>
36 - implements FlowRuleService, FlowRuleProviderRegistry { 41 +implements FlowRuleService, FlowRuleProviderRegistry {
37 42
38 public static final String FLOW_RULE_NULL = "FlowRule cannot be null"; 43 public static final String FLOW_RULE_NULL = "FlowRule cannot be null";
39 private final Logger log = getLogger(getClass()); 44 private final Logger log = getLogger(getClass());
40 45
41 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> 46 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
42 - listenerRegistry = new AbstractListenerRegistry<>(); 47 + listenerRegistry = new AbstractListenerRegistry<>();
43 48
44 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected FlowRuleStore store; 50 protected FlowRuleStore store;
...@@ -72,7 +77,7 @@ public class FlowRuleManager ...@@ -72,7 +77,7 @@ public class FlowRuleManager
72 List<FlowRule> entries = new ArrayList<FlowRule>(); 77 List<FlowRule> entries = new ArrayList<FlowRule>();
73 78
74 for (int i = 0; i < flowRules.length; i++) { 79 for (int i = 0; i < flowRules.length; i++) {
75 - FlowRule f = flowRules[i]; 80 + FlowRule f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_ADD);
76 final Device device = deviceService.getDevice(f.deviceId()); 81 final Device device = deviceService.getDevice(f.deviceId());
77 final FlowRuleProvider frp = getProvider(device.providerId()); 82 final FlowRuleProvider frp = getProvider(device.providerId());
78 entries.add(store.storeFlowRule(f)); 83 entries.add(store.storeFlowRule(f));
...@@ -85,7 +90,7 @@ public class FlowRuleManager ...@@ -85,7 +90,7 @@ public class FlowRuleManager
85 @Override 90 @Override
86 public void removeFlowRules(FlowRule... flowRules) { 91 public void removeFlowRules(FlowRule... flowRules) {
87 for (int i = 0; i < flowRules.length; i++) { 92 for (int i = 0; i < flowRules.length; i++) {
88 - FlowRule f = flowRules[i]; 93 + FlowRule f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_REMOVE);
89 final Device device = deviceService.getDevice(f.deviceId()); 94 final Device device = deviceService.getDevice(f.deviceId());
90 final FlowRuleProvider frp = getProvider(device.providerId()); 95 final FlowRuleProvider frp = getProvider(device.providerId());
91 store.removeFlowRule(f); 96 store.removeFlowRule(f);
...@@ -111,8 +116,8 @@ public class FlowRuleManager ...@@ -111,8 +116,8 @@ public class FlowRuleManager
111 } 116 }
112 117
113 private class InternalFlowRuleProviderService 118 private class InternalFlowRuleProviderService
114 - extends AbstractProviderService<FlowRuleProvider> 119 + extends AbstractProviderService<FlowRuleProvider>
115 - implements FlowRuleProviderService { 120 + implements FlowRuleProviderService {
116 121
117 protected InternalFlowRuleProviderService(FlowRuleProvider provider) { 122 protected InternalFlowRuleProviderService(FlowRuleProvider provider) {
118 super(provider); 123 super(provider);
...@@ -160,8 +165,32 @@ public class FlowRuleManager ...@@ -160,8 +165,32 @@ public class FlowRuleManager
160 } 165 }
161 166
162 @Override 167 @Override
163 - public void pushFlowMetrics(Iterable<FlowRule> flowEntries) { 168 + public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowRule> flowEntries) {
164 - // TODO Auto-generated method stub 169 + List<FlowRule> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId));
170 + List<FlowRule> switchRules = Lists.newLinkedList(flowEntries);
171 + Iterator<FlowRule> switchRulesIterator = switchRules.iterator();
172 + List<FlowRule> extraRules = Lists.newLinkedList();
173 +
174 + while (switchRulesIterator.hasNext()) {
175 + FlowRule rule = switchRulesIterator.next();
176 + if (storedRules.remove(rule)) {
177 + // we both have the rule let's update some info then.
178 + log.info("rule {} is added. {}", rule.id(), rule.state());
179 + flowAdded(rule);
180 + } else {
181 + // the device a rule the store does not have
182 + extraRules.add(rule);
183 + }
184 + }
185 + for (FlowRule rule : storedRules) {
186 + // there are rules in the store that aren't on the switch
187 + flowMissing(rule);
188 + }
189 + if (extraRules.size() > 0) {
190 + log.warn("Device {} has extra flow rules: {}", deviceId, extraRules);
191 + // TODO do something with this.
192 + }
193 +
165 194
166 } 195 }
167 } 196 }
......
...@@ -10,6 +10,7 @@ import org.onlab.onos.net.flow.DefaultFlowRule; ...@@ -10,6 +10,7 @@ import org.onlab.onos.net.flow.DefaultFlowRule;
10 import org.onlab.onos.net.flow.DefaultTrafficSelector; 10 import org.onlab.onos.net.flow.DefaultTrafficSelector;
11 import org.onlab.onos.net.flow.DefaultTrafficTreatment; 11 import org.onlab.onos.net.flow.DefaultTrafficTreatment;
12 import org.onlab.onos.net.flow.FlowRule; 12 import org.onlab.onos.net.flow.FlowRule;
13 +import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
13 import org.onlab.onos.net.flow.TrafficSelector; 14 import org.onlab.onos.net.flow.TrafficSelector;
14 import org.onlab.onos.net.flow.TrafficTreatment; 15 import org.onlab.onos.net.flow.TrafficTreatment;
15 import org.onlab.onos.net.flow.criteria.Criteria; 16 import org.onlab.onos.net.flow.criteria.Criteria;
...@@ -69,14 +70,14 @@ public class FlowRuleBuilder { ...@@ -69,14 +70,14 @@ public class FlowRuleBuilder {
69 if (stat != null) { 70 if (stat != null) {
70 return new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)), 71 return new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
71 buildSelector(), buildTreatment(), stat.getPriority(), 72 buildSelector(), buildTreatment(), stat.getPriority(),
72 - stat.getDurationNsec() / 1000000, stat.getIdleTimeout(), 73 + FlowRuleState.ADDED, stat.getDurationNsec() / 1000000,
73 stat.getPacketCount().getValue(), stat.getByteCount().getValue(), 74 stat.getPacketCount().getValue(), stat.getByteCount().getValue(),
74 (int) (stat.getCookie().getValue() & 0xFFFFFFFF)); 75 (int) (stat.getCookie().getValue() & 0xFFFFFFFF));
75 } else { 76 } else {
76 // TODO: revisit potentially. 77 // TODO: revisit potentially.
77 return new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)), 78 return new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)),
78 buildSelector(), null, removed.getPriority(), 79 buildSelector(), null, removed.getPriority(),
79 - removed.getDurationNsec() / 1000000, removed.getIdleTimeout(), 80 + FlowRuleState.REMOVED, removed.getDurationNsec() / 1000000,
80 removed.getPacketCount().getValue(), removed.getByteCount().getValue(), 81 removed.getPacketCount().getValue(), removed.getByteCount().getValue(),
81 (int) (removed.getCookie().getValue() & 0xFFFFFFFF)); 82 (int) (removed.getCookie().getValue() & 0xFFFFFFFF));
82 } 83 }
......
...@@ -10,6 +10,7 @@ import org.apache.felix.scr.annotations.Component; ...@@ -10,6 +10,7 @@ import org.apache.felix.scr.annotations.Component;
10 import org.apache.felix.scr.annotations.Deactivate; 10 import org.apache.felix.scr.annotations.Deactivate;
11 import org.apache.felix.scr.annotations.Reference; 11 import org.apache.felix.scr.annotations.Reference;
12 import org.apache.felix.scr.annotations.ReferenceCardinality; 12 import org.apache.felix.scr.annotations.ReferenceCardinality;
13 +import org.onlab.onos.net.DeviceId;
13 import org.onlab.onos.net.flow.FlowRule; 14 import org.onlab.onos.net.flow.FlowRule;
14 import org.onlab.onos.net.flow.FlowRuleProvider; 15 import org.onlab.onos.net.flow.FlowRuleProvider;
15 import org.onlab.onos.net.flow.FlowRuleProviderRegistry; 16 import org.onlab.onos.net.flow.FlowRuleProviderRegistry;
...@@ -154,7 +155,7 @@ public class OpenFlowRuleProvider extends AbstractProvider implements FlowRulePr ...@@ -154,7 +155,7 @@ public class OpenFlowRuleProvider extends AbstractProvider implements FlowRulePr
154 entries.add(new FlowRuleBuilder(dpid, reply).build()); 155 entries.add(new FlowRuleBuilder(dpid, reply).build());
155 } 156 }
156 log.debug("sending flowstats to core {}", entries); 157 log.debug("sending flowstats to core {}", entries);
157 - providerService.pushFlowMetrics(entries); 158 + providerService.pushFlowMetrics(DeviceId.deviceId(Dpid.uri(dpid)), entries);
158 } 159 }
159 160
160 } 161 }
......