Charles Chan
Committed by Gerrit Code Review

ONOS-3763 Change flow state to PENDING_ADD when retrying

Will emit a RULE_UPDATE event if the state is changed.
Update unit test accordingly.

Change-Id: Ie84778c62f52f15b7636d41db246814145e73f77
...@@ -97,6 +97,16 @@ public interface FlowRuleStore extends Store<FlowRuleBatchEvent, FlowRuleStoreDe ...@@ -97,6 +97,16 @@ public interface FlowRuleStore extends Store<FlowRuleBatchEvent, FlowRuleStoreDe
97 FlowRuleEvent removeFlowRule(FlowEntry rule); 97 FlowRuleEvent removeFlowRule(FlowEntry rule);
98 98
99 /** 99 /**
100 + * Marks a flow rule as PENDING_ADD during retry.
101 + *
102 + * Emits flow_update event if the state is changed
103 + *
104 + * @param rule the flow rule that is retrying
105 + * @return flow_updated event, or null if nothing updated
106 + */
107 + FlowRuleEvent pendingFlowRule(FlowEntry rule);
108 +
109 + /**
100 * Updates the flow table statistics of the specified device using 110 * Updates the flow table statistics of the specified device using
101 * the given statistics. 111 * the given statistics.
102 * 112 *
......
...@@ -258,6 +258,23 @@ public class SimpleFlowRuleStore ...@@ -258,6 +258,23 @@ public class SimpleFlowRuleStore
258 } 258 }
259 259
260 @Override 260 @Override
261 + public FlowRuleEvent pendingFlowRule(FlowEntry rule) {
262 + List<StoredFlowEntry> entries = getFlowEntries(rule.deviceId(), rule.id());
263 + synchronized (entries) {
264 + for (StoredFlowEntry entry : entries) {
265 + if (entry.equals(rule) &&
266 + entry.state() != FlowEntryState.PENDING_ADD) {
267 + synchronized (entry) {
268 + entry.setState(FlowEntryState.PENDING_ADD);
269 + return new FlowRuleEvent(Type.RULE_UPDATED, rule);
270 + }
271 + }
272 + }
273 + }
274 + return null;
275 + }
276 +
277 + @Override
261 public void storeBatch( 278 public void storeBatch(
262 FlowRuleBatchOperation operation) { 279 FlowRuleBatchOperation operation) {
263 List<FlowRuleBatchEntry> toAdd = new ArrayList<>(); 280 List<FlowRuleBatchEntry> toAdd = new ArrayList<>();
......
...@@ -305,6 +305,7 @@ public class FlowRuleManager ...@@ -305,6 +305,7 @@ public class FlowRuleManager
305 break; 305 break;
306 case ADDED: 306 case ADDED:
307 case PENDING_ADD: 307 case PENDING_ADD:
308 + event = store.pendingFlowRule(flowRule);
308 try { 309 try {
309 frp.applyFlowRule(flowRule); 310 frp.applyFlowRule(flowRule);
310 } catch (UnsupportedOperationException e) { 311 } catch (UnsupportedOperationException e) {
......
...@@ -206,7 +206,7 @@ public class FlowRuleManagerTest { ...@@ -206,7 +206,7 @@ public class FlowRuleManagerTest {
206 assertEquals("should still be 2 rules", 2, flowCount()); 206 assertEquals("should still be 2 rules", 2, flowCount());
207 207
208 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1)); 208 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
209 - validateEvents(RULE_UPDATED); 209 + validateEvents(RULE_UPDATED, RULE_UPDATED);
210 } 210 }
211 211
212 private boolean validateState(Map<FlowRule, FlowEntryState> expected) { 212 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
...@@ -293,7 +293,7 @@ public class FlowRuleManagerTest { ...@@ -293,7 +293,7 @@ public class FlowRuleManagerTest {
293 service.applyFlowRules(f3); 293 service.applyFlowRules(f3);
294 294
295 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3)); 295 providerService.pushFlowMetrics(DID, Collections.singletonList(fe3));
296 - validateEvents(RULE_ADD_REQUESTED, RULE_ADDED); 296 + validateEvents(RULE_ADD_REQUESTED, RULE_ADDED, RULE_UPDATED);
297 297
298 providerService.flowRemoved(fe3); 298 providerService.flowRemoved(fe3);
299 validateEvents(); 299 validateEvents();
......
...@@ -526,6 +526,19 @@ public class NewDistributedFlowRuleStore ...@@ -526,6 +526,19 @@ public class NewDistributedFlowRuleStore
526 } 526 }
527 527
528 @Override 528 @Override
529 + public FlowRuleEvent pendingFlowRule(FlowEntry rule) {
530 + if (mastershipService.isLocalMaster(rule.deviceId())) {
531 + StoredFlowEntry stored = flowTable.getFlowEntry(rule);
532 + if (stored != null &&
533 + stored.state() != FlowEntryState.PENDING_ADD) {
534 + stored.setState(FlowEntryState.PENDING_ADD);
535 + return new FlowRuleEvent(Type.RULE_UPDATED, rule);
536 + }
537 + }
538 + return null;
539 + }
540 +
541 + @Override
529 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) { 542 public FlowRuleEvent addOrUpdateFlowRule(FlowEntry rule) {
530 NodeId master = mastershipService.getMasterFor(rule.deviceId()); 543 NodeId master = mastershipService.getMasterFor(rule.deviceId());
531 if (Objects.equals(local, master)) { 544 if (Objects.equals(local, master)) {
......