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 java.util.ArrayList;
17 import java.util.List;
18 import java.util.concurrent.Future;
19 import javax.annotation.Nullable;
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 class FlatBatchGroupAdapters {
51 private FlatBatchGroupAdapters() {
52 throw new IllegalStateException("This class should not be instantiated.");
56 * Adapt flat batch add group.
57 * @param planStep batch step containing changes of the same type
58 * @param node pointer for RPC routing
59 * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
60 * .opendaylight.groups.service.rev160315.SalGroupsBatchService#addGroupsBatch(AddGroupsBatchInput)}
62 public static AddGroupsBatchInput adaptFlatBatchAddGroup(final BatchPlanStep planStep, final NodeRef node) {
63 final List<BatchAddGroups> batchGroups = new ArrayList<>();
64 for (FlatBatchAddGroup batchAddGroup : planStep.<FlatBatchAddGroup>getTaskBag()) {
65 final BatchAddGroups addGroups = new BatchAddGroupsBuilder(batchAddGroup)
66 .setGroupId(batchAddGroup.getGroupId())
68 batchGroups.add(addGroups);
71 return new AddGroupsBatchInputBuilder()
72 .setBarrierAfter(planStep.isBarrierAfter())
74 .setBatchAddGroups(batchGroups)
79 * Adapt flat batch remove group.
80 * @param planStep batch step containing changes of the same type
81 * @param node pointer for RPC routing
82 * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
83 * .opendaylight.groups.service.rev160315.SalGroupsBatchService#removeGroupsBatch(RemoveGroupsBatchInput)}
85 public static RemoveGroupsBatchInput adaptFlatBatchRemoveGroup(final BatchPlanStep planStep, final NodeRef node) {
86 final List<BatchRemoveGroups> batchGroups = new ArrayList<>();
87 for (FlatBatchRemoveGroup batchRemoveGroup : planStep.<FlatBatchRemoveGroup>getTaskBag()) {
88 final BatchRemoveGroups removeGroups = new BatchRemoveGroupsBuilder(batchRemoveGroup)
89 .setGroupId(batchRemoveGroup.getGroupId())
91 batchGroups.add(removeGroups);
94 return new RemoveGroupsBatchInputBuilder()
95 .setBarrierAfter(planStep.isBarrierAfter())
97 .setBatchRemoveGroups(batchGroups)
102 * Adapt flat batch update group.
103 * @param planStep batch step containing changes of the same type
104 * @param node pointer for RPC routing
105 * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
106 * .opendaylight.groups.service.rev160315.SalGroupsBatchService#updateGroupsBatch(UpdateGroupsBatchInput)}
108 public static UpdateGroupsBatchInput adaptFlatBatchUpdateGroup(final BatchPlanStep planStep, final NodeRef node) {
109 final List<BatchUpdateGroups> batchGroups = new ArrayList<>();
110 for (FlatBatchUpdateGroup batchUpdateGroup : planStep.<FlatBatchUpdateGroup>getTaskBag()) {
111 final BatchUpdateGroups updateGroups = new BatchUpdateGroupsBuilder(batchUpdateGroup)
113 batchGroups.add(updateGroups);
116 return new UpdateGroupsBatchInputBuilder()
117 .setBarrierAfter(planStep.isBarrierAfter())
119 .setBatchUpdateGroups(batchGroups)
124 * Convert batch group result.
125 * @param stepOffset offset of current batch plan step
126 * @return converted {@link ProcessFlatBatchOutput} RPC result
129 static <T extends BatchGroupOutputListGrouping> Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>
130 convertBatchGroupResult(final int stepOffset) {
131 return new Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>() {
134 public RpcResult<ProcessFlatBatchOutput> apply(@Nullable final RpcResult<T> input) {
135 List<BatchFailure> batchFailures = wrapBatchGroupFailuresForFlat(input, stepOffset);
136 ProcessFlatBatchOutputBuilder outputBuilder =
137 new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures);
138 return RpcResultBuilder.<ProcessFlatBatchOutput>status(input.isSuccessful())
139 .withRpcErrors(input.getErrors())
140 .withResult(outputBuilder.build())
146 private static <T extends BatchGroupOutputListGrouping> List<BatchFailure> wrapBatchGroupFailuresForFlat(
147 final RpcResult<T> input, final int stepOffset) {
148 final List<BatchFailure> batchFailures = new ArrayList<>();
149 if (input.getResult().getBatchFailedGroupsOutput() != null) {
150 for (BatchFailedGroupsOutput stepOutput : input.getResult().getBatchFailedGroupsOutput()) {
151 final BatchFailure batchFailure = new BatchFailureBuilder()
152 .setBatchOrder(stepOffset + stepOutput.getBatchOrder())
153 .setBatchItemIdChoice(new FlatBatchFailureGroupIdCaseBuilder()
154 .setGroupId(stepOutput.getGroupId())
157 batchFailures.add(batchFailure);
160 return batchFailures;
164 * Shortcut for {@link #convertBatchGroupResult(int)} with conversion {@link ListenableFuture}.
166 * @param <T> exact type of batch flow output
167 * @param resultUpdateGroupFuture batch group rpc-result (add/remove/update)
168 * @param currentOffset offset of current batch plan step with respect to entire chain of steps
169 * @return ListenableFuture with converted result {@link ProcessFlatBatchOutput}
171 public static <T extends BatchGroupOutputListGrouping> ListenableFuture<RpcResult<ProcessFlatBatchOutput>>
172 convertGroupBatchFutureForChain(final Future<RpcResult<T>> resultUpdateGroupFuture,
173 final int currentOffset) {
174 return Futures.transform(JdkFutureAdapters.listenInPoolThread(resultUpdateGroupFuture),
175 FlatBatchGroupAdapters.<T>convertBatchGroupResult(currentOffset));