2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.services.batch;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Function;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.concurrent.Future;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.group._case.FlatBatchAddGroup;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.group._case.FlatBatchRemoveGroup;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.group._case.FlatBatchUpdateGroup;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailureBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.batch.failure.batch.item.id.choice.FlatBatchFailureGroupIdCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.AddGroupsBatchInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.AddGroupsBatchInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.BatchGroupOutputListGrouping;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.RemoveGroupsBatchInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.RemoveGroupsBatchInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.UpdateGroupsBatchInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.UpdateGroupsBatchInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.add.groups.batch.input.BatchAddGroups;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.add.groups.batch.input.BatchAddGroupsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.batch.group.output.list.grouping.BatchFailedGroupsOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.remove.groups.batch.input.BatchRemoveGroups;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.remove.groups.batch.input.BatchRemoveGroupsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.update.groups.batch.input.BatchUpdateGroups;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.update.groups.batch.input.BatchUpdateGroupsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 * Transform between FlatBatch API and group batch API.
49 public final class FlatBatchGroupAdapters {
51 private FlatBatchGroupAdapters() {
55 * Adapt flat batch add group.
56 * @param planStep batch step containing changes of the same type
57 * @param node pointer for RPC routing
58 * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
59 * .opendaylight.groups.service.rev160315.SalGroupsBatchService#addGroupsBatch(AddGroupsBatchInput)}
61 public static AddGroupsBatchInput adaptFlatBatchAddGroup(final BatchPlanStep planStep, final NodeRef node) {
62 final List<BatchAddGroups> batchGroups = new ArrayList<>();
63 for (FlatBatchAddGroup batchAddGroup : planStep.<FlatBatchAddGroup>getTaskBag()) {
64 final BatchAddGroups addGroups = new BatchAddGroupsBuilder(batchAddGroup)
65 .setGroupId(batchAddGroup.getGroupId())
67 batchGroups.add(addGroups);
70 return new AddGroupsBatchInputBuilder()
71 .setBarrierAfter(planStep.isBarrierAfter())
73 .setBatchAddGroups(batchGroups)
78 * Adapt flat batch remove group.
79 * @param planStep batch step containing changes of the same type
80 * @param node pointer for RPC routing
81 * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
82 * .opendaylight.groups.service.rev160315.SalGroupsBatchService#removeGroupsBatch(RemoveGroupsBatchInput)}
84 public static RemoveGroupsBatchInput adaptFlatBatchRemoveGroup(final BatchPlanStep planStep, final NodeRef node) {
85 final List<BatchRemoveGroups> batchGroups = new ArrayList<>();
86 for (FlatBatchRemoveGroup batchRemoveGroup : planStep.<FlatBatchRemoveGroup>getTaskBag()) {
87 final BatchRemoveGroups removeGroups = new BatchRemoveGroupsBuilder(batchRemoveGroup)
88 .setGroupId(batchRemoveGroup.getGroupId())
90 batchGroups.add(removeGroups);
93 return new RemoveGroupsBatchInputBuilder()
94 .setBarrierAfter(planStep.isBarrierAfter())
96 .setBatchRemoveGroups(batchGroups)
101 * Adapt flat batch update group.
102 * @param planStep batch step containing changes of the same type
103 * @param node pointer for RPC routing
104 * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
105 * .opendaylight.groups.service.rev160315.SalGroupsBatchService#updateGroupsBatch(UpdateGroupsBatchInput)}
107 public static UpdateGroupsBatchInput adaptFlatBatchUpdateGroup(final BatchPlanStep planStep, final NodeRef node) {
108 final List<BatchUpdateGroups> batchGroups = new ArrayList<>();
109 for (FlatBatchUpdateGroup batchUpdateGroup : planStep.<FlatBatchUpdateGroup>getTaskBag()) {
110 final BatchUpdateGroups updateGroups = new BatchUpdateGroupsBuilder(batchUpdateGroup)
112 batchGroups.add(updateGroups);
115 return new UpdateGroupsBatchInputBuilder()
116 .setBarrierAfter(planStep.isBarrierAfter())
118 .setBatchUpdateGroups(batchGroups)
123 * Convert batch group result.
124 * @param stepOffset offset of current batch plan step
125 * @return converted {@link ProcessFlatBatchOutput} RPC result
128 static <T extends BatchGroupOutputListGrouping> Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>
129 convertBatchGroupResult(final int stepOffset) {
130 return new Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>() {
132 public RpcResult<ProcessFlatBatchOutput> apply(final RpcResult<T> input) {
133 List<BatchFailure> batchFailures = wrapBatchGroupFailuresForFlat(input, stepOffset);
134 ProcessFlatBatchOutputBuilder outputBuilder =
135 new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures);
136 return RpcResultBuilder.<ProcessFlatBatchOutput>status(input.isSuccessful())
137 .withRpcErrors(input.getErrors())
138 .withResult(outputBuilder.build())
144 private static <T extends BatchGroupOutputListGrouping> List<BatchFailure> wrapBatchGroupFailuresForFlat(
145 final RpcResult<T> input, final int stepOffset) {
146 final List<BatchFailure> batchFailures = new ArrayList<>();
147 if (input.getResult().getBatchFailedGroupsOutput() != null) {
148 for (BatchFailedGroupsOutput stepOutput : input.getResult().getBatchFailedGroupsOutput()) {
149 final BatchFailure batchFailure = new BatchFailureBuilder()
150 .setBatchOrder(stepOffset + stepOutput.getBatchOrder())
151 .setBatchItemIdChoice(new FlatBatchFailureGroupIdCaseBuilder()
152 .setGroupId(stepOutput.getGroupId())
155 batchFailures.add(batchFailure);
158 return batchFailures;
162 * Shortcut for {@link #convertBatchGroupResult(int)} with conversion {@link ListenableFuture}.
164 * @param <T> exact type of batch flow output
165 * @param resultUpdateGroupFuture batch group rpc-result (add/remove/update)
166 * @param currentOffset offset of current batch plan step with respect to entire chain of steps
167 * @return ListenableFuture with converted result {@link ProcessFlatBatchOutput}
169 public static <T extends BatchGroupOutputListGrouping> ListenableFuture<RpcResult<ProcessFlatBatchOutput>>
170 convertGroupBatchFutureForChain(final Future<RpcResult<T>> resultUpdateGroupFuture,
171 final int currentOffset) {
172 return Futures.transform(JdkFutureAdapters.listenInPoolThread(resultUpdateGroupFuture),
173 FlatBatchGroupAdapters.convertBatchGroupResult(currentOffset),
174 MoreExecutors.directExecutor());