use of org.opendaylight.openflowplugin.impl.services.batch.BatchStepJob in project openflowplugin by opendaylight.
the class SalFlatBatchServiceImplTest method testExecuteBatchPlan.
@Test
public void testExecuteBatchPlan() throws Exception {
BatchStepJob batchStepJob1 = Mockito.mock(BatchStepJob.class);
BatchStepJob batchStepJob2 = Mockito.mock(BatchStepJob.class);
AsyncFunction<RpcResult<ProcessFlatBatchOutput>, RpcResult<ProcessFlatBatchOutput>> function1 = Mockito.mock(AsyncFunction.class);
AsyncFunction<RpcResult<ProcessFlatBatchOutput>, RpcResult<ProcessFlatBatchOutput>> function2 = Mockito.mock(AsyncFunction.class);
Mockito.when(batchStepJob1.getStepFunction()).thenReturn(function1);
Mockito.when(batchStepJob2.getStepFunction()).thenReturn(function2);
BatchPlanStep batchPlanStep1 = new BatchPlanStep(BatchStepType.GROUP_ADD);
batchPlanStep1.setBarrierAfter(true);
BatchPlanStep batchPlanStep2 = new BatchPlanStep(BatchStepType.FLOW_ADD);
batchPlanStep1.setBarrierAfter(false);
Mockito.when(batchStepJob1.getPlanStep()).thenReturn(batchPlanStep1);
Mockito.when(batchStepJob2.getPlanStep()).thenReturn(batchPlanStep2);
final ListenableFuture<RpcResult<ProcessFlatBatchOutput>> succeededChainOutput = FlatBatchUtil.createEmptyRpcBatchResultFuture(true);
final ListenableFuture<RpcResult<ProcessFlatBatchOutput>> failedChainOutput = RpcResultBuilder.<ProcessFlatBatchOutput>failed().withError(RpcError.ErrorType.APPLICATION, "ut-chainError").withResult(createFlatBatchOutput(createFlowBatchFailure(0, "f1"), createFlowBatchFailure(1, "f2"))).buildFuture();
Mockito.when(batchStepJob1.getStepFunction().apply(Matchers.<RpcResult<ProcessFlatBatchOutput>>any())).thenReturn(succeededChainOutput);
Mockito.when(batchStepJob2.getStepFunction().apply(Matchers.<RpcResult<ProcessFlatBatchOutput>>any())).thenReturn(failedChainOutput);
final List<BatchStepJob> batchChainElements = Lists.newArrayList(batchStepJob1, batchStepJob2);
final Future<RpcResult<ProcessFlatBatchOutput>> rpcResultFuture = salFlatBatchService.executeBatchPlan(batchChainElements);
Assert.assertTrue(rpcResultFuture.isDone());
final RpcResult<ProcessFlatBatchOutput> rpcResult = rpcResultFuture.get();
Assert.assertFalse(rpcResult.isSuccessful());
Assert.assertEquals(1, rpcResult.getErrors().size());
Assert.assertEquals(2, rpcResult.getResult().getBatchFailure().size());
Assert.assertEquals("f2", ((FlatBatchFailureFlowIdCase) rpcResult.getResult().getBatchFailure().get(1).getBatchItemIdChoice()).getFlowId().getValue());
}
use of org.opendaylight.openflowplugin.impl.services.batch.BatchStepJob in project openflowplugin by opendaylight.
the class SalFlatBatchServiceImpl method executeBatchPlan.
@VisibleForTesting
Future<RpcResult<ProcessFlatBatchOutput>> executeBatchPlan(final List<BatchStepJob> batchJobsChain) {
BatchStepJob batchJob;
final List<ListenableFuture<RpcResult<ProcessFlatBatchOutput>>> firedJobs = new ArrayList<>();
ListenableFuture<RpcResult<ProcessFlatBatchOutput>> chainSummaryResult = FlatBatchUtil.createEmptyRpcBatchResultFuture(true);
for (int i = 0; i < batchJobsChain.size(); i++) {
batchJob = batchJobsChain.get(i);
// wire actual job with chain
firedJobs.add(Futures.transformAsync(chainSummaryResult, batchJob.getStepFunction(), MoreExecutors.directExecutor()));
// if barrier after actual job is needed or it is the last job -> merge fired job results with chain result
if ((batchJob.getPlanStep().isBarrierAfter()) || (i == batchJobsChain.size() - 1)) {
firedJobs.add(0, chainSummaryResult);
chainSummaryResult = FlatBatchUtil.mergeJobsResultsFutures(firedJobs);
firedJobs.clear();
}
}
return chainSummaryResult;
}
use of org.opendaylight.openflowplugin.impl.services.batch.BatchStepJob in project openflowplugin by opendaylight.
the class SalFlatBatchServiceImpl method prepareBatchChain.
@VisibleForTesting
List<BatchStepJob> prepareBatchChain(final List<BatchPlanStep> batchPlan, final NodeRef node, final boolean exitOnFirstError) {
// create batch API calls based on plan steps
final List<BatchStepJob> chainJobs = new ArrayList<>();
int stepOffset = 0;
for (final BatchPlanStep planStep : batchPlan) {
final int currentOffset = stepOffset;
chainJobs.add(new BatchStepJob(planStep, chainInput -> {
if (exitOnFirstError && !chainInput.isSuccessful()) {
LOG.debug("error on flat batch chain occurred -> skipping step {}", planStep.getStepType());
return FlatBatchUtil.createEmptyRpcBatchResultFuture(false);
}
LOG.trace("batch progressing on step type {}, previous steps result: {}", planStep.getStepType(), chainInput.isSuccessful());
return getChainOutput(node, planStep, currentOffset);
}));
stepOffset += planStep.getTaskBag().size();
}
return chainJobs;
}
use of org.opendaylight.openflowplugin.impl.services.batch.BatchStepJob in project openflowplugin by opendaylight.
the class SalFlatBatchServiceImplTest method testPrepareBatchPlan_failure.
@Test
public void testPrepareBatchPlan_failure() throws Exception {
final FlatBatchAddFlow flatBatchAddFlow = new FlatBatchAddFlowBuilder().setFlowId(new FlowId("f1")).build();
final BatchPlanStep batchPlanStep = new BatchPlanStep(BatchStepType.FLOW_ADD);
batchPlanStep.getTaskBag().addAll(Lists.newArrayList(flatBatchAddFlow, flatBatchAddFlow));
final List<BatchPlanStep> batchPlan = Lists.newArrayList(batchPlanStep, batchPlanStep);
final List<BatchStepJob> batchChain = salFlatBatchService.prepareBatchChain(batchPlan, NODE_REF, true);
Assert.assertEquals(2, batchChain.size());
Mockito.when(salFlowsBatchService.addFlowsBatch(Matchers.<AddFlowsBatchInput>any())).thenReturn(RpcResultBuilder.<AddFlowsBatchOutput>failed().withResult(new AddFlowsBatchOutputBuilder().setBatchFailedFlowsOutput(Lists.newArrayList(new BatchFailedFlowsOutputBuilder().setBatchOrder(0).setFlowId(new FlowId("f1")).build(), new BatchFailedFlowsOutputBuilder().setBatchOrder(1).setFlowId(new FlowId("f2")).build())).build()).withError(RpcError.ErrorType.APPLICATION, "ut-addFlowBatchError").buildFuture());
final Future<RpcResult<ProcessFlatBatchOutput>> rpcResultFuture = salFlatBatchService.executeBatchPlan(batchChain);
Assert.assertTrue(rpcResultFuture.isDone());
final RpcResult<ProcessFlatBatchOutput> rpcResult = rpcResultFuture.get();
Assert.assertFalse(rpcResult.isSuccessful());
Assert.assertEquals(2, rpcResult.getErrors().size());
Assert.assertEquals(4, rpcResult.getResult().getBatchFailure().size());
Mockito.verify(salFlowsBatchService, Mockito.times(2)).addFlowsBatch(addFlowsBatchInputCpt.capture());
Assert.assertEquals(2, addFlowsBatchInputCpt.getValue().getBatchAddFlows().size());
}
use of org.opendaylight.openflowplugin.impl.services.batch.BatchStepJob in project openflowplugin by opendaylight.
the class SalFlatBatchServiceImplTest method testPrepareBatchPlan_success.
@Test
public void testPrepareBatchPlan_success() throws Exception {
final FlatBatchAddFlow flatBatchAddFlow = new FlatBatchAddFlowBuilder().setFlowId(new FlowId("f1")).build();
final BatchPlanStep batchPlanStep = new BatchPlanStep(BatchStepType.FLOW_ADD);
batchPlanStep.getTaskBag().addAll(Lists.newArrayList(flatBatchAddFlow, flatBatchAddFlow));
final List<BatchPlanStep> batchPlan = Lists.newArrayList(batchPlanStep);
final List<BatchStepJob> batchChain = salFlatBatchService.prepareBatchChain(batchPlan, NODE_REF, true);
Assert.assertEquals(1, batchChain.size());
Mockito.when(salFlowsBatchService.addFlowsBatch(Matchers.<AddFlowsBatchInput>any())).thenReturn(RpcResultBuilder.success(new AddFlowsBatchOutputBuilder().build()).buildFuture());
final Future<RpcResult<ProcessFlatBatchOutput>> rpcResultFuture = salFlatBatchService.executeBatchPlan(batchChain);
Assert.assertTrue(rpcResultFuture.isDone());
final RpcResult<ProcessFlatBatchOutput> rpcResult = rpcResultFuture.get();
Assert.assertTrue(rpcResult.isSuccessful());
Assert.assertEquals(0, rpcResult.getErrors().size());
Assert.assertEquals(0, rpcResult.getResult().getBatchFailure().size());
Mockito.verify(salFlowsBatchService).addFlowsBatch(Matchers.<AddFlowsBatchInput>any());
}
Aggregations