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.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutputBuilder;
20 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;
21 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;
22 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;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailureBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.batch.failure.batch.item.id.choice.FlatBatchFailureGroupIdCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.AddGroupsBatchInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.AddGroupsBatchInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.BatchGroupOutputListGrouping;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.RemoveGroupsBatchInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.RemoveGroupsBatchInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.UpdateGroupsBatchInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.UpdateGroupsBatchInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.add.groups.batch.input.BatchAddGroups;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.add.groups.batch.input.BatchAddGroupsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.batch.group.output.list.grouping.BatchFailedGroupsOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.remove.groups.batch.input.BatchRemoveGroups;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.remove.groups.batch.input.BatchRemoveGroupsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.update.groups.batch.input.BatchUpdateGroups;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.update.groups.batch.input.BatchUpdateGroupsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 * Transform between FlatBatch API and group batch API.
47 public final class FlatBatchGroupAdapters {
49 private FlatBatchGroupAdapters() {
53 * Adapt flat batch add group.
54 * @param planStep batch step containing changes of the same type
55 * @param node pointer for RPC routing
56 * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
57 * .opendaylight.groups.service.rev160315.SalGroupsBatchService#addGroupsBatch(AddGroupsBatchInput)}
59 public static AddGroupsBatchInput adaptFlatBatchAddGroup(final BatchPlanStep planStep, final NodeRef node) {
60 final List<BatchAddGroups> batchGroups = new ArrayList<>();
61 for (FlatBatchAddGroup batchAddGroup : planStep.<FlatBatchAddGroup>getTaskBag()) {
62 final BatchAddGroups addGroups = new BatchAddGroupsBuilder(batchAddGroup)
63 .setGroupId(batchAddGroup.getGroupId())
65 batchGroups.add(addGroups);
68 return new AddGroupsBatchInputBuilder()
69 .setBarrierAfter(planStep.isBarrierAfter())
71 .setBatchAddGroups(batchGroups)
76 * Adapt flat batch remove group.
77 * @param planStep batch step containing changes of the same type
78 * @param node pointer for RPC routing
79 * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
80 * .opendaylight.groups.service.rev160315.SalGroupsBatchService#removeGroupsBatch(RemoveGroupsBatchInput)}
82 public static RemoveGroupsBatchInput adaptFlatBatchRemoveGroup(final BatchPlanStep planStep, final NodeRef node) {
83 final List<BatchRemoveGroups> batchGroups = new ArrayList<>();
84 for (FlatBatchRemoveGroup batchRemoveGroup : planStep.<FlatBatchRemoveGroup>getTaskBag()) {
85 final BatchRemoveGroups removeGroups = new BatchRemoveGroupsBuilder(batchRemoveGroup)
86 .setGroupId(batchRemoveGroup.getGroupId())
88 batchGroups.add(removeGroups);
91 return new RemoveGroupsBatchInputBuilder()
92 .setBarrierAfter(planStep.isBarrierAfter())
94 .setBatchRemoveGroups(batchGroups)
99 * Adapt flat batch update group.
100 * @param planStep batch step containing changes of the same type
101 * @param node pointer for RPC routing
102 * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
103 * .opendaylight.groups.service.rev160315.SalGroupsBatchService#updateGroupsBatch(UpdateGroupsBatchInput)}
105 public static UpdateGroupsBatchInput adaptFlatBatchUpdateGroup(final BatchPlanStep planStep, final NodeRef node) {
106 final List<BatchUpdateGroups> batchGroups = new ArrayList<>();
107 for (FlatBatchUpdateGroup batchUpdateGroup : planStep.<FlatBatchUpdateGroup>getTaskBag()) {
108 final BatchUpdateGroups updateGroups = new BatchUpdateGroupsBuilder(batchUpdateGroup)
110 batchGroups.add(updateGroups);
113 return new UpdateGroupsBatchInputBuilder()
114 .setBarrierAfter(planStep.isBarrierAfter())
116 .setBatchUpdateGroups(batchGroups)
121 * Convert batch group result.
122 * @param stepOffset offset of current batch plan step
123 * @return converted {@link ProcessFlatBatchOutput} RPC result
126 static <T extends BatchGroupOutputListGrouping> Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>
127 convertBatchGroupResult(final int stepOffset) {
129 List<BatchFailure> batchFailures = wrapBatchGroupFailuresForFlat(input, stepOffset);
130 ProcessFlatBatchOutputBuilder outputBuilder =
131 new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures);
132 return RpcResultBuilder.<ProcessFlatBatchOutput>status(input.isSuccessful())
133 .withRpcErrors(input.getErrors())
134 .withResult(outputBuilder.build())
139 private static <T extends BatchGroupOutputListGrouping> List<BatchFailure> wrapBatchGroupFailuresForFlat(
140 final RpcResult<T> input, final int stepOffset) {
141 final List<BatchFailure> batchFailures = new ArrayList<>();
142 if (input.getResult().getBatchFailedGroupsOutput() != null) {
143 for (BatchFailedGroupsOutput stepOutput : input.getResult().nonnullBatchFailedGroupsOutput().values()) {
144 final BatchFailure batchFailure = new BatchFailureBuilder()
145 .setBatchOrder(stepOffset + stepOutput.getBatchOrder().toJava())
146 .setBatchItemIdChoice(new FlatBatchFailureGroupIdCaseBuilder()
147 .setGroupId(stepOutput.getGroupId())
150 batchFailures.add(batchFailure);
153 return batchFailures;
157 * Shortcut for {@link #convertBatchGroupResult(int)} with conversion {@link ListenableFuture}.
159 * @param <T> exact type of batch flow output
160 * @param resultUpdateGroupFuture batch group rpc-result (add/remove/update)
161 * @param currentOffset offset of current batch plan step with respect to entire chain of steps
162 * @return ListenableFuture with converted result {@link ProcessFlatBatchOutput}
164 public static <T extends BatchGroupOutputListGrouping> ListenableFuture<RpcResult<ProcessFlatBatchOutput>>
165 convertGroupBatchFutureForChain(final ListenableFuture<RpcResult<T>> resultUpdateGroupFuture,
166 final int currentOffset) {
167 return Futures.transform(resultUpdateGroupFuture,
168 FlatBatchGroupAdapters.convertBatchGroupResult(currentOffset), MoreExecutors.directExecutor());