alshabib

tests for flow service

Change-Id: I7138c7642f266c0691ead13ff1f230d063eb6b71
...@@ -5,6 +5,6 @@ import org.onlab.onos.event.EventListener; ...@@ -5,6 +5,6 @@ import org.onlab.onos.event.EventListener;
5 /** 5 /**
6 * Entity capable of receiving device mastership-related events. 6 * Entity capable of receiving device mastership-related events.
7 */ 7 */
8 -public interface MastershipListener extends EventListener<MastershipEvent>{ 8 +public interface MastershipListener extends EventListener<MastershipEvent> {
9 9
10 } 10 }
......
...@@ -168,6 +168,7 @@ public class DefaultFlowRule implements FlowRule { ...@@ -168,6 +168,7 @@ public class DefaultFlowRule implements FlowRule {
168 .add("selector", selector) 168 .add("selector", selector)
169 .add("treatment", treatment) 169 .add("treatment", treatment)
170 .add("created", created) 170 .add("created", created)
171 + .add("state", state)
171 .toString(); 172 .toString();
172 } 173 }
173 174
......
...@@ -84,8 +84,9 @@ implements FlowRuleService, FlowRuleProviderRegistry { ...@@ -84,8 +84,9 @@ implements FlowRuleService, FlowRuleProviderRegistry {
84 84
85 @Override 85 @Override
86 public void removeFlowRules(FlowRule... flowRules) { 86 public void removeFlowRules(FlowRule... flowRules) {
87 + FlowRule f;
87 for (int i = 0; i < flowRules.length; i++) { 88 for (int i = 0; i < flowRules.length; i++) {
88 - FlowRule f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_REMOVE); 89 + f = new DefaultFlowRule(flowRules[i], FlowRuleState.PENDING_REMOVE);
89 final Device device = deviceService.getDevice(f.deviceId()); 90 final Device device = deviceService.getDevice(f.deviceId());
90 final FlowRuleProvider frp = getProvider(device.providerId()); 91 final FlowRuleProvider frp = getProvider(device.providerId());
91 store.deleteFlowRule(f); 92 store.deleteFlowRule(f);
...@@ -134,7 +135,7 @@ implements FlowRuleService, FlowRuleProviderRegistry { ...@@ -134,7 +135,7 @@ implements FlowRuleService, FlowRuleProviderRegistry {
134 public void flowMissing(FlowRule flowRule) { 135 public void flowMissing(FlowRule flowRule) {
135 checkNotNull(flowRule, FLOW_RULE_NULL); 136 checkNotNull(flowRule, FLOW_RULE_NULL);
136 checkValidity(); 137 checkValidity();
137 - log.info("Flow {} has not been installed."); 138 + log.info("Flow {} has not been installed.", flowRule);
138 139
139 } 140 }
140 141
...@@ -142,7 +143,7 @@ implements FlowRuleService, FlowRuleProviderRegistry { ...@@ -142,7 +143,7 @@ implements FlowRuleService, FlowRuleProviderRegistry {
142 public void extraneousFlow(FlowRule flowRule) { 143 public void extraneousFlow(FlowRule flowRule) {
143 checkNotNull(flowRule, FLOW_RULE_NULL); 144 checkNotNull(flowRule, FLOW_RULE_NULL);
144 checkValidity(); 145 checkValidity();
145 - log.info("Flow {} is on switch but not in store."); 146 + log.info("Flow {} is on switch but not in store.", flowRule);
146 } 147 }
147 148
148 @Override 149 @Override
...@@ -170,8 +171,8 @@ implements FlowRuleService, FlowRuleProviderRegistry { ...@@ -170,8 +171,8 @@ implements FlowRuleService, FlowRuleProviderRegistry {
170 @Override 171 @Override
171 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowRule> flowEntries) { 172 public void pushFlowMetrics(DeviceId deviceId, Iterable<FlowRule> flowEntries) {
172 List<FlowRule> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId)); 173 List<FlowRule> storedRules = Lists.newLinkedList(store.getFlowEntries(deviceId));
173 - //List<FlowRule> switchRules = Lists.newLinkedList(flowEntries); 174 +
174 - Iterator<FlowRule> switchRulesIterator = flowEntries.iterator(); //switchRules.iterator(); 175 + Iterator<FlowRule> switchRulesIterator = flowEntries.iterator();
175 176
176 while (switchRulesIterator.hasNext()) { 177 while (switchRulesIterator.hasNext()) {
177 FlowRule rule = switchRulesIterator.next(); 178 FlowRule rule = switchRulesIterator.next();
......
...@@ -26,6 +26,7 @@ import org.onlab.onos.net.device.DeviceListener; ...@@ -26,6 +26,7 @@ import org.onlab.onos.net.device.DeviceListener;
26 import org.onlab.onos.net.device.DeviceService; 26 import org.onlab.onos.net.device.DeviceService;
27 import org.onlab.onos.net.flow.DefaultFlowRule; 27 import org.onlab.onos.net.flow.DefaultFlowRule;
28 import org.onlab.onos.net.flow.FlowRule; 28 import org.onlab.onos.net.flow.FlowRule;
29 +import org.onlab.onos.net.flow.FlowRule.FlowRuleState;
29 import org.onlab.onos.net.flow.FlowRuleEvent; 30 import org.onlab.onos.net.flow.FlowRuleEvent;
30 import org.onlab.onos.net.flow.FlowRuleListener; 31 import org.onlab.onos.net.flow.FlowRuleListener;
31 import org.onlab.onos.net.flow.FlowRuleProvider; 32 import org.onlab.onos.net.flow.FlowRuleProvider;
...@@ -57,7 +58,7 @@ public class FlowRuleManagerTest { ...@@ -57,7 +58,7 @@ public class FlowRuleManagerTest {
57 58
58 protected FlowRuleService service; 59 protected FlowRuleService service;
59 protected FlowRuleProviderRegistry registry; 60 protected FlowRuleProviderRegistry registry;
60 - protected FlowRuleProviderService providerSerivce; 61 + protected FlowRuleProviderService providerService;
61 protected TestProvider provider; 62 protected TestProvider provider;
62 protected TestListener listener = new TestListener(); 63 protected TestListener listener = new TestListener();
63 64
...@@ -73,7 +74,7 @@ public class FlowRuleManagerTest { ...@@ -73,7 +74,7 @@ public class FlowRuleManagerTest {
73 mgr.activate(); 74 mgr.activate();
74 mgr.addListener(listener); 75 mgr.addListener(listener);
75 provider = new TestProvider(PID); 76 provider = new TestProvider(PID);
76 - providerSerivce = registry.register(provider); 77 + providerService = registry.register(provider);
77 assertTrue("provider should be registered", 78 assertTrue("provider should be registered",
78 registry.getProviders().contains(provider.id())); 79 registry.getProviders().contains(provider.id()));
79 } 80 }
...@@ -95,10 +96,15 @@ public class FlowRuleManagerTest { ...@@ -95,10 +96,15 @@ public class FlowRuleManagerTest {
95 return new DefaultFlowRule(DID, ts, tr, 0); 96 return new DefaultFlowRule(DID, ts, tr, 0);
96 } 97 }
97 98
98 - private void addFlowRule(int hval) { 99 + private FlowRule flowRule(FlowRule rule, FlowRuleState state) {
100 + return new DefaultFlowRule(rule, state);
101 + }
102 +
103 + private FlowRule addFlowRule(int hval) {
99 FlowRule rule = flowRule(hval, hval); 104 FlowRule rule = flowRule(hval, hval);
100 - providerSerivce.flowAdded(rule); 105 + providerService.flowAdded(rule);
101 assertNotNull("rule should be found", service.getFlowEntries(DID)); 106 assertNotNull("rule should be found", service.getFlowEntries(DID));
107 + return rule;
102 } 108 }
103 109
104 private void validateEvents(FlowRuleEvent.Type ... events) { 110 private void validateEvents(FlowRuleEvent.Type ... events) {
...@@ -135,40 +141,53 @@ public class FlowRuleManagerTest { ...@@ -135,40 +141,53 @@ public class FlowRuleManagerTest {
135 validateEvents(RULE_UPDATED); 141 validateEvents(RULE_UPDATED);
136 } 142 }
137 143
144 +
145 + //backing store is sensitive to the order of additions/removals
146 + private boolean validateState(FlowRuleState... state) {
147 + Iterable<FlowRule> rules = service.getFlowEntries(DID);
148 + int i = 0;
149 + for (FlowRule f : rules) {
150 + if (f.state() != state[i]) {
151 + return false;
152 + }
153 + i++;
154 + }
155 + return true;
156 + }
157 +
138 @Test 158 @Test
139 public void applyFlowRules() { 159 public void applyFlowRules() {
140 - TestSelector ts = new TestSelector(1); 160 +
141 FlowRule r1 = flowRule(1, 1); 161 FlowRule r1 = flowRule(1, 1);
142 FlowRule r2 = flowRule(1, 2); 162 FlowRule r2 = flowRule(1, 2);
143 FlowRule r3 = flowRule(1, 3); 163 FlowRule r3 = flowRule(1, 3);
144 164
145 - //current FlowRules always return 0. FlowEntries inherit the value
146 - FlowRule e1 = new DefaultFlowRule(DID, ts, r1.treatment(), 0);
147 - FlowRule e2 = new DefaultFlowRule(DID, ts, r2.treatment(), 0);
148 - FlowRule e3 = new DefaultFlowRule(DID, ts, r3.treatment(), 0);
149 - List<FlowRule> fel = Lists.newArrayList(e1, e2, e3);
150 -
151 assertTrue("store should be empty", 165 assertTrue("store should be empty",
152 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty()); 166 Sets.newHashSet(service.getFlowEntries(DID)).isEmpty());
153 mgr.applyFlowRules(r1, r2, r3); 167 mgr.applyFlowRules(r1, r2, r3);
154 assertEquals("3 rules should exist", 3, flowCount()); 168 assertEquals("3 rules should exist", 3, flowCount());
155 - assertTrue("3 entries should result", fel.containsAll(Lists.newArrayList(r1, r2, r3))); 169 + assertTrue("Entries should be pending add.",
170 + validateState(FlowRuleState.PENDING_ADD, FlowRuleState.PENDING_ADD,
171 + FlowRuleState.PENDING_ADD));
156 } 172 }
157 173
158 @Test 174 @Test
159 public void removeFlowRules() { 175 public void removeFlowRules() {
160 - addFlowRule(1); 176 + FlowRule f1 = addFlowRule(1);
161 - addFlowRule(2); 177 + FlowRule f2 = addFlowRule(2);
162 addFlowRule(3); 178 addFlowRule(3);
163 assertEquals("3 rules should exist", 3, flowCount()); 179 assertEquals("3 rules should exist", 3, flowCount());
164 validateEvents(RULE_ADDED, RULE_ADDED, RULE_ADDED); 180 validateEvents(RULE_ADDED, RULE_ADDED, RULE_ADDED);
165 181
166 - FlowRule rem1 = flowRule(1, 1); 182 + FlowRule rem1 = flowRule(f1, FlowRuleState.REMOVED);
167 - FlowRule rem2 = flowRule(2, 2); 183 + FlowRule rem2 = flowRule(f2, FlowRuleState.REMOVED);
168 mgr.removeFlowRules(rem1, rem2); 184 mgr.removeFlowRules(rem1, rem2);
169 //removing from north, so no events generated 185 //removing from north, so no events generated
170 validateEvents(); 186 validateEvents();
171 assertEquals("3 rule should exist", 3, flowCount()); 187 assertEquals("3 rule should exist", 3, flowCount());
188 + assertTrue("Entries should be pending remove.",
189 + validateState(FlowRuleState.CREATED, FlowRuleState.PENDING_REMOVE,
190 + FlowRuleState.PENDING_REMOVE));
172 191
173 mgr.removeFlowRules(rem1); 192 mgr.removeFlowRules(rem1);
174 assertEquals("3 rule should still exist", 3, flowCount()); 193 assertEquals("3 rule should still exist", 3, flowCount());
...@@ -176,16 +195,34 @@ public class FlowRuleManagerTest { ...@@ -176,16 +195,34 @@ public class FlowRuleManagerTest {
176 195
177 @Test 196 @Test
178 public void flowRemoved() { 197 public void flowRemoved() {
179 - addFlowRule(1); 198 + FlowRule f1 = addFlowRule(1);
180 addFlowRule(2); 199 addFlowRule(2);
181 - FlowRule rem1 = flowRule(1, 1); 200 + FlowRule rem1 = flowRule(f1, FlowRuleState.REMOVED);
182 - providerSerivce.flowRemoved(rem1); 201 + providerService.flowRemoved(rem1);
183 validateEvents(RULE_ADDED, RULE_ADDED, RULE_REMOVED); 202 validateEvents(RULE_ADDED, RULE_ADDED, RULE_REMOVED);
184 203
185 - providerSerivce.flowRemoved(rem1); 204 + providerService.flowRemoved(rem1);
186 validateEvents(); 205 validateEvents();
187 } 206 }
188 207
208 + @Test
209 + public void flowMetrics() {
210 + FlowRule f1 = flowRule(1, 1);
211 + FlowRule f2 = flowRule(2, 2);
212 + FlowRule f3 = flowRule(3, 3);
213 +
214 + FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
215 + FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
216 + mgr.applyFlowRules(f1, f2, f3);
217 +
218 + providerService.pushFlowMetrics(DID, Lists.newArrayList(updatedF1, updatedF2));
219 +
220 + assertTrue("Entries should be added.",
221 + validateState(FlowRuleState.PENDING_ADD, FlowRuleState.ADDED,
222 + FlowRuleState.ADDED));
223 + //TODO: add tests for flowmissing and extraneous flows
224 + }
225 +
189 private static class TestListener implements FlowRuleListener { 226 private static class TestListener implements FlowRuleListener {
190 final List<FlowRuleEvent> events = new ArrayList<>(); 227 final List<FlowRuleEvent> events = new ArrayList<>();
191 228
......
...@@ -60,6 +60,7 @@ public class SimpleFlowRuleStore implements FlowRuleStore { ...@@ -60,6 +60,7 @@ public class SimpleFlowRuleStore implements FlowRuleStore {
60 * find the rule and mark it for deletion. 60 * find the rule and mark it for deletion.
61 * Ultimately a flow removed will come remove it. 61 * Ultimately a flow removed will come remove it.
62 */ 62 */
63 +
63 if (flowEntries.containsEntry(did, rule)) { 64 if (flowEntries.containsEntry(did, rule)) {
64 synchronized (flowEntries) { 65 synchronized (flowEntries) {
65 66
......