Brian O'Connor

Updating MockFlowRuleService with new semantics

Change-Id: I0a373e6cb25728ca48736902feedc3ed8869fa44
......@@ -396,14 +396,14 @@ public class IntentManagerTest {
@Test
public void errorIntentInstallFromFlows() {
final Long id = MockIntent.nextId();
flowRuleService.setFuture(false, 1);
flowRuleService.setFuture(false);
MockIntent intent = new MockIntent(id);
listener.setLatch(1, Type.FAILED);
listener.setLatch(1, Type.INSTALL_REQ);
service.submit(intent);
listener.await(Type.INSTALL_REQ);
delay(10); // need to make sure we have some failed futures returned first
flowRuleService.setFuture(true, 0);
flowRuleService.setFuture(true);
listener.await(Type.FAILED);
}
......@@ -429,7 +429,7 @@ public class IntentManagerTest {
@Test
public void errorIntentInstallNeverTrue() {
final Long id = MockIntent.nextId();
flowRuleService.setFuture(false, 1);
flowRuleService.setFuture(false);
MockIntent intent = new MockIntent(id);
listener.setLatch(1, Type.WITHDRAWN);
listener.setLatch(1, Type.INSTALL_REQ);
......@@ -437,7 +437,7 @@ public class IntentManagerTest {
listener.await(Type.INSTALL_REQ);
// The delay here forces the retry loop in the intent manager to time out
delay(100);
flowRuleService.setFuture(false, 1);
flowRuleService.setFuture(false);
service.withdraw(intent);
listener.await(Type.WITHDRAWN);
}
......
......@@ -15,68 +15,67 @@
*/
package org.onosproject.net.intent.impl;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.Future;
import com.google.common.collect.Sets;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.flow.CompletedBatchOperation;
import org.onosproject.net.flow.DefaultFlowEntry;
import org.onosproject.net.flow.FlowEntry;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleBatchEntry;
import org.onosproject.net.flow.FlowRuleBatchOperation;
import org.onosproject.net.flow.FlowRuleListener;
import org.onosproject.net.flow.FlowRuleOperations;
import org.onosproject.net.flow.FlowRuleService;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.Futures;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
public class MockFlowRuleService implements FlowRuleService {
private Future<CompletedBatchOperation> future;
final Set<FlowRule> flows = Sets.newHashSet();
boolean success;
public void setFuture(boolean success) {
setFuture(success, 0);
this.success = success;
}
public void setFuture(boolean success, long intentId) {
if (success) {
future = Futures.immediateFuture(new CompletedBatchOperation(true, Collections.emptySet(), null));
} else {
final Set<Long> failedIds = ImmutableSet.of(intentId);
future = Futures.immediateFuture(
new CompletedBatchOperation(false, flows, failedIds, null));
}
@Override
public Future<CompletedBatchOperation> applyBatch(FlowRuleBatchOperation batch) {
throw new UnsupportedOperationException("deprecated");
}
@Override
public Future<CompletedBatchOperation> applyBatch(FlowRuleBatchOperation batch) {
for (FlowRuleBatchEntry fbe : batch.getOperations()) {
FlowRule fr = fbe.target();
switch (fbe.operator()) {
public void apply(FlowRuleOperations ops) {
ops.stages().forEach(stage -> stage.forEach(flow -> {
switch (flow.type()) {
case ADD:
flows.add(fr);
case MODIFY: //TODO is this the right behavior for modify?
flows.add(flow.rule());
break;
case REMOVE:
flows.remove(fr);
break;
case MODIFY:
flows.remove(flow.rule());
break;
default:
break;
}
}));
if (success) {
ops.callback().onSuccess(ops);
} else {
ops.callback().onError(ops);
}
return future;
}
@Override
public void apply(FlowRuleOperations ops) {
public void addListener(FlowRuleListener listener) {
//TODO not implemented
}
@Override
public void removeListener(FlowRuleListener listener) {
//TODO not implemented
}
@Override
......@@ -86,39 +85,43 @@ public class MockFlowRuleService implements FlowRuleService {
@Override
public Iterable<FlowEntry> getFlowEntries(DeviceId deviceId) {
return null;
return flows.stream()
.filter(flow -> flow.deviceId().equals(deviceId))
.map(DefaultFlowEntry::new)
.collect(Collectors.toList());
}
@Override
public void applyFlowRules(FlowRule... flowRules) {
for (FlowRule flow : flowRules) {
flows.add(flow);
}
}
@Override
public void removeFlowRules(FlowRule... flowRules) {
for (FlowRule flow : flowRules) {
flows.remove(flow);
}
}
@Override
public void removeFlowRulesById(ApplicationId appId) {
//TODO not implemented
}
@Override
public Iterable<FlowRule> getFlowRulesById(ApplicationId id) {
return null;
return flows.stream()
.filter(flow -> flow.appId() == id.id())
.collect(Collectors.toList());
}
@Override
public Iterable<FlowRule> getFlowRulesByGroupId(ApplicationId appId, short groupId) {
return null;
}
@Override
public void addListener(FlowRuleListener listener) {
}
@Override
public void removeListener(FlowRuleListener listener) {
return flows.stream()
.filter(flow -> flow.appId() == appId.id() && flow.groupId().id() == groupId)
.collect(Collectors.toList());
}
}
......