Remove redundant type specifiers
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / batch / FlatBatchGroupAdapters.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.services.batch;
10
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 javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutputBuilder;
24 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;
25 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;
26 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;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailureBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.batch.failure.batch.item.id.choice.FlatBatchFailureGroupIdCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.AddGroupsBatchInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.AddGroupsBatchInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.BatchGroupOutputListGrouping;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.RemoveGroupsBatchInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.RemoveGroupsBatchInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.UpdateGroupsBatchInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.UpdateGroupsBatchInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.add.groups.batch.input.BatchAddGroups;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.add.groups.batch.input.BatchAddGroupsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.batch.group.output.list.grouping.BatchFailedGroupsOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.remove.groups.batch.input.BatchRemoveGroups;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.remove.groups.batch.input.BatchRemoveGroupsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.update.groups.batch.input.BatchUpdateGroups;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.update.groups.batch.input.BatchUpdateGroupsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47
48 /**
49  * Transform between FlatBatch API and group batch API.
50  */
51 public final class FlatBatchGroupAdapters {
52
53     private FlatBatchGroupAdapters() {
54     }
55
56     /**
57      * Adapt flat batch add group.
58      * @param planStep batch step containing changes of the same type
59      * @param node     pointer for RPC routing
60      * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
61      * .opendaylight.groups.service.rev160315.SalGroupsBatchService#addGroupsBatch(AddGroupsBatchInput)}
62      */
63     public static AddGroupsBatchInput adaptFlatBatchAddGroup(final BatchPlanStep planStep, final NodeRef node) {
64         final List<BatchAddGroups> batchGroups = new ArrayList<>();
65         for (FlatBatchAddGroup batchAddGroup : planStep.<FlatBatchAddGroup>getTaskBag()) {
66             final BatchAddGroups addGroups = new BatchAddGroupsBuilder(batchAddGroup)
67                     .setGroupId(batchAddGroup.getGroupId())
68                     .build();
69             batchGroups.add(addGroups);
70         }
71
72         return new AddGroupsBatchInputBuilder()
73                 .setBarrierAfter(planStep.isBarrierAfter())
74                 .setNode(node)
75                 .setBatchAddGroups(batchGroups)
76                 .build();
77     }
78
79     /**
80      * Adapt flat batch remove group.
81      * @param planStep batch step containing changes of the same type
82      * @param node     pointer for RPC routing
83      * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
84      * .opendaylight.groups.service.rev160315.SalGroupsBatchService#removeGroupsBatch(RemoveGroupsBatchInput)}
85      */
86     public static RemoveGroupsBatchInput adaptFlatBatchRemoveGroup(final BatchPlanStep planStep, final NodeRef node) {
87         final List<BatchRemoveGroups> batchGroups = new ArrayList<>();
88         for (FlatBatchRemoveGroup batchRemoveGroup : planStep.<FlatBatchRemoveGroup>getTaskBag()) {
89             final BatchRemoveGroups removeGroups = new BatchRemoveGroupsBuilder(batchRemoveGroup)
90                     .setGroupId(batchRemoveGroup.getGroupId())
91                     .build();
92             batchGroups.add(removeGroups);
93         }
94
95         return new RemoveGroupsBatchInputBuilder()
96                 .setBarrierAfter(planStep.isBarrierAfter())
97                 .setNode(node)
98                 .setBatchRemoveGroups(batchGroups)
99                 .build();
100     }
101
102     /**
103      * Adapt flat batch update group.
104      * @param planStep batch step containing changes of the same type
105      * @param node     pointer for RPC routing
106      * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn
107      * .opendaylight.groups.service.rev160315.SalGroupsBatchService#updateGroupsBatch(UpdateGroupsBatchInput)}
108      */
109     public static UpdateGroupsBatchInput adaptFlatBatchUpdateGroup(final BatchPlanStep planStep, final NodeRef node) {
110         final List<BatchUpdateGroups> batchGroups = new ArrayList<>();
111         for (FlatBatchUpdateGroup batchUpdateGroup : planStep.<FlatBatchUpdateGroup>getTaskBag()) {
112             final BatchUpdateGroups updateGroups = new BatchUpdateGroupsBuilder(batchUpdateGroup)
113                     .build();
114             batchGroups.add(updateGroups);
115         }
116
117         return new UpdateGroupsBatchInputBuilder()
118                 .setBarrierAfter(planStep.isBarrierAfter())
119                 .setNode(node)
120                 .setBatchUpdateGroups(batchGroups)
121                 .build();
122     }
123
124     /**
125      * Convert batch group result.
126      * @param stepOffset offset of current batch plan step
127      * @return converted {@link ProcessFlatBatchOutput} RPC result
128      */
129     @VisibleForTesting
130     static <T extends BatchGroupOutputListGrouping> Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>
131         convertBatchGroupResult(final int stepOffset) {
132         return new Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>() {
133             @Nullable
134             @Override
135             public RpcResult<ProcessFlatBatchOutput> apply(@Nonnull final RpcResult<T> input) {
136                 List<BatchFailure> batchFailures = wrapBatchGroupFailuresForFlat(input, stepOffset);
137                 ProcessFlatBatchOutputBuilder outputBuilder =
138                         new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures);
139                 return RpcResultBuilder.<ProcessFlatBatchOutput>status(input.isSuccessful())
140                                        .withRpcErrors(input.getErrors())
141                                        .withResult(outputBuilder.build())
142                                        .build();
143             }
144         };
145     }
146
147     private static <T extends BatchGroupOutputListGrouping> List<BatchFailure> wrapBatchGroupFailuresForFlat(
148             final RpcResult<T> input, final int stepOffset) {
149         final List<BatchFailure> batchFailures = new ArrayList<>();
150         if (input.getResult().getBatchFailedGroupsOutput() != null) {
151             for (BatchFailedGroupsOutput stepOutput : input.getResult().getBatchFailedGroupsOutput()) {
152                 final BatchFailure batchFailure = new BatchFailureBuilder()
153                         .setBatchOrder(stepOffset + stepOutput.getBatchOrder())
154                         .setBatchItemIdChoice(new FlatBatchFailureGroupIdCaseBuilder()
155                                 .setGroupId(stepOutput.getGroupId())
156                                 .build())
157                         .build();
158                 batchFailures.add(batchFailure);
159             }
160         }
161         return batchFailures;
162     }
163
164     /**
165      * Shortcut for {@link #convertBatchGroupResult(int)} with conversion {@link ListenableFuture}.
166      *
167      * @param <T>                     exact type of batch flow output
168      * @param resultUpdateGroupFuture batch group rpc-result (add/remove/update)
169      * @param currentOffset           offset of current batch plan step with respect to entire chain of steps
170      * @return ListenableFuture with converted result {@link ProcessFlatBatchOutput}
171      */
172     public static <T extends BatchGroupOutputListGrouping> ListenableFuture<RpcResult<ProcessFlatBatchOutput>>
173         convertGroupBatchFutureForChain(final Future<RpcResult<T>> resultUpdateGroupFuture,
174                                     final int currentOffset) {
175         return Futures.transform(JdkFutureAdapters.listenInPoolThread(resultUpdateGroupFuture),
176                 FlatBatchGroupAdapters.convertBatchGroupResult(currentOffset),
177                 MoreExecutors.directExecutor());
178     }
179 }