Update comments around flat-batch service
[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 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;
45
46 /**
47  * Transform between FlatBatch API and group batch API.
48  */
49 public class FlatBatchGroupAdapters {
50
51     private FlatBatchGroupAdapters() {
52         throw new IllegalStateException("This class should not be instantiated.");
53     }
54
55     /**
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.opendaylight.groups.service.rev160315.SalGroupsBatchService#addGroupsBatch(AddGroupsBatchInput)}
59      */
60     public static AddGroupsBatchInput adaptFlatBatchAddGroup(final BatchPlanStep planStep, final NodeRef node) {
61         final List<BatchAddGroups> batchGroups = new ArrayList<>();
62         for (FlatBatchAddGroup batchAddGroup : planStep.<FlatBatchAddGroup>getTaskBag()) {
63             final BatchAddGroups addGroups = new BatchAddGroupsBuilder(batchAddGroup)
64                     .setGroupId(batchAddGroup.getGroupId())
65                     .build();
66             batchGroups.add(addGroups);
67         }
68
69         return new AddGroupsBatchInputBuilder()
70                 .setBarrierAfter(planStep.isBarrierAfter())
71                 .setNode(node)
72                 .setBatchAddGroups(batchGroups)
73                 .build();
74     }
75
76     /**
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.opendaylight.groups.service.rev160315.SalGroupsBatchService#removeGroupsBatch(RemoveGroupsBatchInput)}
80      */
81     public static RemoveGroupsBatchInput adaptFlatBatchRemoveGroup(final BatchPlanStep planStep, final NodeRef node) {
82         final List<BatchRemoveGroups> batchGroups = new ArrayList<>();
83         for (FlatBatchRemoveGroup batchRemoveGroup : planStep.<FlatBatchRemoveGroup>getTaskBag()) {
84             final BatchRemoveGroups removeGroups = new BatchRemoveGroupsBuilder(batchRemoveGroup)
85                     .setGroupId(batchRemoveGroup.getGroupId())
86                     .build();
87             batchGroups.add(removeGroups);
88         }
89
90         return new RemoveGroupsBatchInputBuilder()
91                 .setBarrierAfter(planStep.isBarrierAfter())
92                 .setNode(node)
93                 .setBatchRemoveGroups(batchGroups)
94                 .build();
95     }
96
97     /**
98      * @param planStep batch step containing changes of the same type
99      * @param node     pointer for RPC routing
100      * @return input suitable for {@link org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.SalGroupsBatchService#updateGroupsBatch(UpdateGroupsBatchInput)}
101      */
102     public static UpdateGroupsBatchInput adaptFlatBatchUpdateGroup(final BatchPlanStep planStep, final NodeRef node) {
103         final List<BatchUpdateGroups> batchGroups = new ArrayList<>();
104         for (FlatBatchUpdateGroup batchUpdateGroup : planStep.<FlatBatchUpdateGroup>getTaskBag()) {
105             final BatchUpdateGroups updateGroups = new BatchUpdateGroupsBuilder(batchUpdateGroup)
106                     .build();
107             batchGroups.add(updateGroups);
108         }
109
110         return new UpdateGroupsBatchInputBuilder()
111                 .setBarrierAfter(planStep.isBarrierAfter())
112                 .setNode(node)
113                 .setBatchUpdateGroups(batchGroups)
114                 .build();
115     }
116
117     /**
118      * @param stepOffset offset of current batch plan step
119      * @return converted {@link ProcessFlatBatchOutput} RPC result
120      */
121     @VisibleForTesting
122     static <T extends BatchGroupOutputListGrouping> Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>
123     convertBatchGroupResult(final int stepOffset) {
124         return new Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>() {
125             @Nullable
126             @Override
127             public RpcResult<ProcessFlatBatchOutput> apply(@Nullable final RpcResult<T> input) {
128                 List<BatchFailure> batchFailures = wrapBatchGroupFailuresForFlat(input, stepOffset);
129                 ProcessFlatBatchOutputBuilder outputBuilder = new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures);
130                 return RpcResultBuilder.<ProcessFlatBatchOutput>status(input.isSuccessful())
131                                        .withRpcErrors(input.getErrors())
132                                        .withResult(outputBuilder.build())
133                                        .build();
134             }
135         };
136     }
137
138     private static <T extends BatchGroupOutputListGrouping> List<BatchFailure> wrapBatchGroupFailuresForFlat(
139             final RpcResult<T> input, final int stepOffset) {
140         final List<BatchFailure> batchFailures = new ArrayList<>();
141         if (input.getResult().getBatchFailedGroupsOutput() != null) {
142             for (BatchFailedGroupsOutput stepOutput : input.getResult().getBatchFailedGroupsOutput()) {
143                 final BatchFailure batchFailure = new BatchFailureBuilder()
144                         .setBatchOrder(stepOffset + stepOutput.getBatchOrder())
145                         .setBatchItemIdChoice(new FlatBatchFailureGroupIdCaseBuilder()
146                                 .setGroupId(stepOutput.getGroupId())
147                                 .build())
148                         .build();
149                 batchFailures.add(batchFailure);
150             }
151         }
152         return batchFailures;
153     }
154
155     /**
156      * shortcut for {@link #convertBatchGroupResult(int)} with conversion {@link ListenableFuture}
157      *
158      * @param <T>                     exact type of batch flow output
159      * @param resultUpdateGroupFuture batch group rpc-result (add/remove/update)
160      * @param currentOffset           offset of current batch plan step with respect to entire chain of steps
161      * @return ListenableFuture with converted result {@link ProcessFlatBatchOutput}
162      */
163     public static <T extends BatchGroupOutputListGrouping> ListenableFuture<RpcResult<ProcessFlatBatchOutput>>
164     convertGroupBatchFutureForChain(final Future<RpcResult<T>> resultUpdateGroupFuture,
165                                     final int currentOffset) {
166         return Futures.transform(JdkFutureAdapters.listenInPoolThread(resultUpdateGroupFuture),
167                 FlatBatchGroupAdapters.<T>convertBatchGroupResult(currentOffset));
168     }
169 }