alshabib

flowmanager tests

Change-Id: I27ff62e4483254e565153294eddc20b525f5db01
1 package org.onlab.onos.net.flow.impl; 1 package org.onlab.onos.net.flow.impl;
2 2
3 -import com.google.common.collect.Lists; 3 +import static com.google.common.base.Preconditions.checkNotNull;
4 +import static org.slf4j.LoggerFactory.getLogger;
5 +
6 +import java.util.Iterator;
7 +import java.util.List;
8 +
4 import org.apache.felix.scr.annotations.Activate; 9 import org.apache.felix.scr.annotations.Activate;
5 import org.apache.felix.scr.annotations.Component; 10 import org.apache.felix.scr.annotations.Component;
6 import org.apache.felix.scr.annotations.Deactivate; 11 import org.apache.felix.scr.annotations.Deactivate;
...@@ -26,11 +31,7 @@ import org.onlab.onos.net.provider.AbstractProviderRegistry; ...@@ -26,11 +31,7 @@ import org.onlab.onos.net.provider.AbstractProviderRegistry;
26 import org.onlab.onos.net.provider.AbstractProviderService; 31 import org.onlab.onos.net.provider.AbstractProviderService;
27 import org.slf4j.Logger; 32 import org.slf4j.Logger;
28 33
29 -import java.util.Iterator; 34 +import com.google.common.collect.Lists;
30 -import java.util.List;
31 -
32 -import static com.google.common.base.Preconditions.checkNotNull;
33 -import static org.slf4j.LoggerFactory.getLogger;
34 35
35 /** 36 /**
36 * Provides implementation of the flow NB & SB APIs. 37 * Provides implementation of the flow NB & SB APIs.
...@@ -47,7 +48,7 @@ implements FlowRuleService, FlowRuleProviderRegistry { ...@@ -47,7 +48,7 @@ implements FlowRuleService, FlowRuleProviderRegistry {
47 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener> 48 private final AbstractListenerRegistry<FlowRuleEvent, FlowRuleListener>
48 listenerRegistry = new AbstractListenerRegistry<>(); 49 listenerRegistry = new AbstractListenerRegistry<>();
49 50
50 - private FlowRuleStoreDelegate delegate = new InternalStoreDelegate(); 51 + private final FlowRuleStoreDelegate delegate = new InternalStoreDelegate();
51 52
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected FlowRuleStore store; 54 protected FlowRuleStore store;
...@@ -107,6 +108,7 @@ implements FlowRuleService, FlowRuleProviderRegistry { ...@@ -107,6 +108,7 @@ implements FlowRuleService, FlowRuleProviderRegistry {
107 Iterable<FlowRule> rules = getFlowRulesById(id); 108 Iterable<FlowRule> rules = getFlowRulesById(id);
108 FlowRuleProvider frp; 109 FlowRuleProvider frp;
109 Device device; 110 Device device;
111 +
110 for (FlowRule f : rules) { 112 for (FlowRule f : rules) {
111 store.deleteFlowRule(f); 113 store.deleteFlowRule(f);
112 device = deviceService.getDevice(f.deviceId()); 114 device = deviceService.getDevice(f.deviceId());
...@@ -181,10 +183,11 @@ implements FlowRuleService, FlowRuleProviderRegistry { ...@@ -181,10 +183,11 @@ implements FlowRuleService, FlowRuleProviderRegistry {
181 checkValidity(); 183 checkValidity();
182 Device device = deviceService.getDevice(flowRule.deviceId()); 184 Device device = deviceService.getDevice(flowRule.deviceId());
183 FlowRuleProvider frp = getProvider(device.providerId()); 185 FlowRuleProvider frp = getProvider(device.providerId());
186 + FlowRuleEvent event = null;
184 switch (flowRule.state()) { 187 switch (flowRule.state()) {
185 case PENDING_REMOVE: 188 case PENDING_REMOVE:
186 case REMOVED: 189 case REMOVED:
187 - store.removeFlowRule(flowRule); 190 + event = store.removeFlowRule(flowRule);
188 frp.removeFlowRule(flowRule); 191 frp.removeFlowRule(flowRule);
189 break; 192 break;
190 case ADDED: 193 case ADDED:
...@@ -195,6 +198,10 @@ implements FlowRuleService, FlowRuleProviderRegistry { ...@@ -195,6 +198,10 @@ implements FlowRuleService, FlowRuleProviderRegistry {
195 log.debug("Flow {} has not been installed.", flowRule); 198 log.debug("Flow {} has not been installed.", flowRule);
196 } 199 }
197 200
201 + if (event != null) {
202 + log.debug("Flow {} removed", flowRule);
203 + post(event);
204 + }
198 205
199 } 206 }
200 207
...@@ -245,7 +252,7 @@ implements FlowRuleService, FlowRuleProviderRegistry { ...@@ -245,7 +252,7 @@ implements FlowRuleService, FlowRuleProviderRegistry {
245 } 252 }
246 } 253 }
247 for (FlowRule rule : storedRules) { 254 for (FlowRule rule : storedRules) {
248 - 255 + log.info("missing rule is {}", rule);
249 // there are rules in the store that aren't on the switch 256 // there are rules in the store that aren't on the switch
250 flowMissing(rule); 257 flowMissing(rule);
251 258
......
...@@ -116,6 +116,7 @@ public class FlowRuleManagerTest { ...@@ -116,6 +116,7 @@ public class FlowRuleManagerTest {
116 } 116 }
117 117
118 int i = 0; 118 int i = 0;
119 + System.err.println("events :" + listener.events);
119 for (FlowRuleEvent e : listener.events) { 120 for (FlowRuleEvent e : listener.events) {
120 assertTrue("unexpected event", e.type().equals(events[i])); 121 assertTrue("unexpected event", e.type().equals(events[i]));
121 i++; 122 i++;
...@@ -207,6 +208,12 @@ public class FlowRuleManagerTest { ...@@ -207,6 +208,12 @@ public class FlowRuleManagerTest {
207 208
208 providerService.flowRemoved(rem1); 209 providerService.flowRemoved(rem1);
209 validateEvents(); 210 validateEvents();
211 +
212 + FlowRule f3 = flowRule(flowRule(3, 3), FlowRuleState.ADDED);
213 + providerService.flowAdded(f3);
214 + validateEvents(RULE_ADDED);
215 + providerService.flowRemoved(f3);
216 + validateEvents();
210 } 217 }
211 218
212 @Test 219 @Test
...@@ -215,6 +222,7 @@ public class FlowRuleManagerTest { ...@@ -215,6 +222,7 @@ public class FlowRuleManagerTest {
215 FlowRule f2 = flowRule(2, 2); 222 FlowRule f2 = flowRule(2, 2);
216 FlowRule f3 = flowRule(3, 3); 223 FlowRule f3 = flowRule(3, 3);
217 224
225 +
218 FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED); 226 FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
219 FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED); 227 FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
220 mgr.applyFlowRules(f1, f2, f3); 228 mgr.applyFlowRules(f1, f2, f3);
...@@ -224,7 +232,71 @@ public class FlowRuleManagerTest { ...@@ -224,7 +232,71 @@ public class FlowRuleManagerTest {
224 assertTrue("Entries should be added.", 232 assertTrue("Entries should be added.",
225 validateState(FlowRuleState.PENDING_ADD, FlowRuleState.ADDED, 233 validateState(FlowRuleState.PENDING_ADD, FlowRuleState.ADDED,
226 FlowRuleState.ADDED)); 234 FlowRuleState.ADDED));
227 - //TODO: add tests for flowmissing and extraneous flows 235 +
236 + validateEvents(RULE_UPDATED, RULE_UPDATED);
237 + }
238 +
239 + @Test
240 + public void extraneousFlow() {
241 + FlowRule f1 = flowRule(1, 1);
242 + FlowRule f2 = flowRule(2, 2);
243 + FlowRule f3 = flowRule(3, 3);
244 +
245 + FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
246 + FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
247 + FlowRule updatedF3 = flowRule(f3, FlowRuleState.ADDED);
248 + mgr.applyFlowRules(f1, f2);
249 +
250 + providerService.pushFlowMetrics(DID, Lists.newArrayList(updatedF1, updatedF2, updatedF3));
251 +
252 + validateEvents(RULE_UPDATED, RULE_UPDATED);
253 +
254 + }
255 +
256 + /*
257 + * Tests whether a rule that was marked for removal but no flowRemoved was received
258 + * is indeed removed at the next stats update.
259 + */
260 + @Test
261 + public void flowMissingRemove() {
262 + FlowRule f1 = flowRule(1, 1);
263 + FlowRule f2 = flowRule(2, 2);
264 + FlowRule f3 = flowRule(3, 3);
265 +
266 + FlowRule updatedF1 = flowRule(f1, FlowRuleState.ADDED);
267 + FlowRule updatedF2 = flowRule(f2, FlowRuleState.ADDED);
268 + mgr.applyFlowRules(f1, f2, f3);
269 +
270 + mgr.removeFlowRules(f3);
271 +
272 + providerService.pushFlowMetrics(DID, Lists.newArrayList(updatedF1, updatedF2));
273 +
274 + validateEvents(RULE_UPDATED, RULE_UPDATED, RULE_REMOVED);
275 +
276 + }
277 +
278 + @Test
279 + public void getByAppId() {
280 + FlowRule f1 = flowRule(1, 1);
281 + FlowRule f2 = flowRule(2, 2);
282 + mgr.applyFlowRules(f1, f2);
283 +
284 + assertTrue("should have two rules",
285 + Lists.newLinkedList(mgr.getFlowRulesById(appId)).size() == 2);
286 + }
287 +
288 + @Test
289 + public void removeByAppId() {
290 + FlowRule f1 = flowRule(1, 1);
291 + FlowRule f2 = flowRule(2, 2);
292 + mgr.applyFlowRules(f1, f2);
293 +
294 +
295 + mgr.removeFlowRulesById(appId);
296 +
297 + //only check that we are in pending remove. Events and actual remove state will
298 + // be set by flowRemoved call.
299 + validateState(FlowRuleState.PENDING_REMOVE, FlowRuleState.PENDING_REMOVE);
228 } 300 }
229 301
230 private static class TestListener implements FlowRuleListener { 302 private static class TestListener implements FlowRuleListener {
......