Update MRI projects for Aluminium
[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.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;
43
44 /**
45  * Transform between FlatBatch API and group batch API.
46  */
47 public final class FlatBatchGroupAdapters {
48
49     private FlatBatchGroupAdapters() {
50     }
51
52     /**
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)}
58      */
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())
64                     .build();
65             batchGroups.add(addGroups);
66         }
67
68         return new AddGroupsBatchInputBuilder()
69                 .setBarrierAfter(planStep.isBarrierAfter())
70                 .setNode(node)
71                 .setBatchAddGroups(batchGroups)
72                 .build();
73     }
74
75     /**
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)}
81      */
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())
87                     .build();
88             batchGroups.add(removeGroups);
89         }
90
91         return new RemoveGroupsBatchInputBuilder()
92                 .setBarrierAfter(planStep.isBarrierAfter())
93                 .setNode(node)
94                 .setBatchRemoveGroups(batchGroups)
95                 .build();
96     }
97
98     /**
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)}
104      */
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)
109                     .build();
110             batchGroups.add(updateGroups);
111         }
112
113         return new UpdateGroupsBatchInputBuilder()
114                 .setBarrierAfter(planStep.isBarrierAfter())
115                 .setNode(node)
116                 .setBatchUpdateGroups(batchGroups)
117                 .build();
118     }
119
120     /**
121      * Convert batch group result.
122      * @param stepOffset offset of current batch plan step
123      * @return converted {@link ProcessFlatBatchOutput} RPC result
124      */
125     @VisibleForTesting
126     static <T extends BatchGroupOutputListGrouping> Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>
127         convertBatchGroupResult(final int stepOffset) {
128         return input -> {
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())
135                                    .build();
136         };
137     }
138
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())
148                                 .build())
149                         .build();
150                 batchFailures.add(batchFailure);
151             }
152         }
153         return batchFailures;
154     }
155
156     /**
157      * Shortcut for {@link #convertBatchGroupResult(int)} with conversion {@link ListenableFuture}.
158      *
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}
163      */
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());
169     }
170 }