Merge remote-tracking branch 'liblldp/master'
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / batch / FlatBatchFlowAdapters.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.flow._case.FlatBatchAddFlow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.flow._case.FlatBatchRemoveFlow;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.flow._case.FlatBatchUpdateFlow;
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.FlatBatchFailureFlowIdCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowOutputListGrouping;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.add.flows.batch.input.BatchAddFlows;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.add.flows.batch.input.BatchAddFlowsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.remove.flows.batch.input.BatchRemoveFlows;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.remove.flows.batch.input.BatchRemoveFlowsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.update.flows.batch.input.BatchUpdateFlows;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.update.flows.batch.input.BatchUpdateFlowsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46
47 /**
48  * Transform between FlatBatch API and flow batch API.
49  */
50 public final class FlatBatchFlowAdapters {
51
52     private FlatBatchFlowAdapters() {
53     }
54
55     /**
56      * Adapt flat batch add flow.
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.flows.service.rev160314.SalFlowsBatchService#addFlowsBatch(AddFlowsBatchInput)}
61      */
62     public static AddFlowsBatchInput adaptFlatBatchAddFlow(final BatchPlanStep planStep, final NodeRef node) {
63         final List<BatchAddFlows> batchFlows = new ArrayList<>();
64         for (FlatBatchAddFlow batchAddFlows : planStep.<FlatBatchAddFlow>getTaskBag()) {
65             final BatchAddFlows addFlows = new BatchAddFlowsBuilder((Flow) batchAddFlows)
66                     .setFlowId(batchAddFlows.getFlowId())
67                     .build();
68             batchFlows.add(addFlows);
69         }
70
71         return new AddFlowsBatchInputBuilder()
72                 .setBarrierAfter(planStep.isBarrierAfter())
73                 .setNode(node)
74                 .setBatchAddFlows(batchFlows)
75                 .build();
76     }
77
78     /**
79      * Adapt flat batch remove flow.
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.flows.service.rev160314.SalFlowsBatchService#removeFlowsBatch(RemoveFlowsBatchInput)}
84      */
85     public static RemoveFlowsBatchInput adaptFlatBatchRemoveFlow(final BatchPlanStep planStep, final NodeRef node) {
86         final List<BatchRemoveFlows> batchFlows = new ArrayList<>();
87         for (FlatBatchRemoveFlow batchRemoveFlow : planStep.<FlatBatchRemoveFlow>getTaskBag()) {
88             final BatchRemoveFlows removeFlows = new BatchRemoveFlowsBuilder((Flow) batchRemoveFlow)
89                     .setFlowId(batchRemoveFlow.getFlowId())
90                     .build();
91             batchFlows.add(removeFlows);
92         }
93
94         return new RemoveFlowsBatchInputBuilder()
95                 .setBarrierAfter(planStep.isBarrierAfter())
96                 .setNode(node)
97                 .setBatchRemoveFlows(batchFlows)
98                 .build();
99     }
100
101     /**
102      * Adapt flat batch update flow.
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.flows.service.rev160314.SalFlowsBatchService#updateFlowsBatch(UpdateFlowsBatchInput)}
107      */
108     public static UpdateFlowsBatchInput adaptFlatBatchUpdateFlow(final BatchPlanStep planStep, final NodeRef node) {
109         final List<BatchUpdateFlows> batchFlows = new ArrayList<>();
110         for (FlatBatchUpdateFlow batchUpdateFlow : planStep.<FlatBatchUpdateFlow>getTaskBag()) {
111             final BatchUpdateFlows updateFlows = new BatchUpdateFlowsBuilder(batchUpdateFlow)
112                     .build();
113             batchFlows.add(updateFlows);
114         }
115
116         return new UpdateFlowsBatchInputBuilder()
117                 .setBarrierAfter(planStep.isBarrierAfter())
118                 .setNode(node)
119                 .setBatchUpdateFlows(batchFlows)
120                 .build();
121     }
122
123     /**
124      * Convert batch result.
125      * @param stepOffset offset of current batch plan step
126      * @return converted {@link ProcessFlatBatchOutput} RPC result
127      */
128     @VisibleForTesting
129     static <T extends BatchFlowOutputListGrouping> Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>
130         convertBatchFlowResult(final int stepOffset) {
131         return new Function<RpcResult<T>, RpcResult<ProcessFlatBatchOutput>>() {
132             @Nullable
133             @Override
134             public RpcResult<ProcessFlatBatchOutput> apply(@Nullable final RpcResult<T> input) {
135                 List<BatchFailure> batchFailures = wrapBatchFlowFailuresForFlat(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())
141                                        .build();
142             }
143         };
144     }
145
146     private static <T extends BatchFlowOutputListGrouping> List<BatchFailure> wrapBatchFlowFailuresForFlat(
147             final RpcResult<T> input, final int stepOffset) {
148         final List<BatchFailure> batchFailures = new ArrayList<>();
149         if (input.getResult().getBatchFailedFlowsOutput() != null) {
150             for (BatchFailedFlowsOutput stepOutput : input.getResult().getBatchFailedFlowsOutput()) {
151                 final BatchFailure batchFailure = new BatchFailureBuilder()
152                         .setBatchOrder(stepOffset + stepOutput.getBatchOrder())
153                         .setBatchItemIdChoice(new FlatBatchFailureFlowIdCaseBuilder()
154                                 .setFlowId(stepOutput.getFlowId())
155                                 .build())
156                         .build();
157                 batchFailures.add(batchFailure);
158             }
159         }
160         return batchFailures;
161     }
162
163     /**
164      * Shortcut for {@link #convertBatchFlowResult(int)} with conversion {@link ListenableFuture}.
165      *
166      * @param <T>                    exact type of batch flow output
167      * @param resultUpdateFlowFuture batch flow 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}
170      */
171     public static <T extends BatchFlowOutputListGrouping> ListenableFuture<RpcResult<ProcessFlatBatchOutput>>
172         convertFlowBatchFutureForChain(final Future<RpcResult<T>> resultUpdateFlowFuture,
173                                    final int currentOffset) {
174         return Futures.transform(JdkFutureAdapters.listenInPoolThread(resultUpdateFlowFuture),
175                 FlatBatchFlowAdapters.<T>convertBatchFlowResult(currentOffset));
176     }
177 }