SONAR TD - StatisticsContextImpl, StatisticsManagerImpl
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / FlatBatchUtil.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.util;
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 java.util.ArrayList;
16 import java.util.EnumSet;
17 import java.util.List;
18 import org.opendaylight.openflowplugin.impl.services.batch.BatchPlanStep;
19 import org.opendaylight.openflowplugin.impl.services.batch.BatchStepType;
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;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.BatchChoice;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddFlowCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddGroupCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddMeterCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveFlowCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveGroupCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveMeterCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateFlowCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateGroupCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateMeterCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.service.batch.common.rev160322.BatchOrderGrouping;
35 import org.opendaylight.yangtools.yang.binding.DataContainer;
36 import org.opendaylight.yangtools.yang.common.RpcError;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * provides flat batch util methods
44  */
45 public final class FlatBatchUtil {
46
47     private static final Logger LOG = LoggerFactory.getLogger(FlatBatchUtil.class);
48
49     private FlatBatchUtil() {
50         throw new IllegalStateException("This class should not be instantiated.");
51     }
52
53     public static void markBarriersWhereNeeded(final List<BatchPlanStep> batchPlan) {
54         final EnumSet<BatchStepType> previousTypes = EnumSet.noneOf(BatchStepType.class);
55
56         BatchPlanStep previousPlanStep = null;
57         for (BatchPlanStep planStep : batchPlan) {
58             final BatchStepType type = planStep.getStepType();
59             if (!previousTypes.isEmpty() && decideBarrier(previousTypes, type)) {
60                 previousPlanStep.setBarrierAfter(true);
61                 previousTypes.clear();
62             }
63             previousTypes.add(type);
64             previousPlanStep = planStep;
65         }
66     }
67
68     @VisibleForTesting
69     static boolean decideBarrier(final EnumSet<BatchStepType> previousTypes, final BatchStepType type) {
70         final boolean needBarrier;
71         switch (type) {
72             case FLOW_ADD:
73             case FLOW_UPDATE:
74                 needBarrier = previousTypes.contains(BatchStepType.GROUP_ADD)
75                         || previousTypes.contains(BatchStepType.METER_ADD);
76                 break;
77             case GROUP_ADD:
78                 needBarrier = previousTypes.contains(BatchStepType.GROUP_ADD)
79                         || previousTypes.contains(BatchStepType.GROUP_UPDATE);
80                 break;
81             case GROUP_REMOVE:
82                 needBarrier = previousTypes.contains(BatchStepType.FLOW_REMOVE)
83                         || previousTypes.contains(BatchStepType.FLOW_UPDATE)
84                         || previousTypes.contains(BatchStepType.GROUP_REMOVE)
85                         || previousTypes.contains(BatchStepType.GROUP_UPDATE);
86                 break;
87             case METER_REMOVE:
88                 needBarrier = previousTypes.contains(BatchStepType.FLOW_REMOVE)
89                         || previousTypes.contains(BatchStepType.FLOW_UPDATE);
90                 break;
91             default:
92                 needBarrier = false;
93         }
94         return needBarrier;
95     }
96
97     public static List<BatchPlanStep> assembleBatchPlan(List<Batch> batches) {
98         final List<BatchPlanStep> plan = new ArrayList<>();
99
100         BatchPlanStep planStep;
101         for (Batch batch : batches) {
102             final BatchStepType nextStepType = detectBatchStepType(batch.getBatchChoice());
103
104             planStep = new BatchPlanStep(nextStepType);
105             planStep.getTaskBag().addAll(extractBatchData(planStep.getStepType(), batch.getBatchChoice()));
106             if (!planStep.isEmpty()) {
107                 plan.add(planStep);
108             }
109         }
110
111         return plan;
112     }
113
114     private static List<? extends BatchOrderGrouping> extractBatchData(final BatchStepType batchStepType,
115                                                                        final BatchChoice batchChoice) {
116         final List<? extends BatchOrderGrouping> batchData;
117         switch (batchStepType) {
118             case FLOW_ADD:
119                 batchData = ((FlatBatchAddFlowCase) batchChoice).getFlatBatchAddFlow();
120                 break;
121             case FLOW_REMOVE:
122                 batchData = ((FlatBatchRemoveFlowCase) batchChoice).getFlatBatchRemoveFlow();
123                 break;
124             case FLOW_UPDATE:
125                 batchData = ((FlatBatchUpdateFlowCase) batchChoice).getFlatBatchUpdateFlow();
126                 break;
127             case GROUP_ADD:
128                 batchData = ((FlatBatchAddGroupCase) batchChoice).getFlatBatchAddGroup();
129                 break;
130             case GROUP_REMOVE:
131                 batchData = ((FlatBatchRemoveGroupCase) batchChoice).getFlatBatchRemoveGroup();
132                 break;
133             case GROUP_UPDATE:
134                 batchData = ((FlatBatchUpdateGroupCase) batchChoice).getFlatBatchUpdateGroup();
135                 break;
136             case METER_ADD:
137                 batchData = ((FlatBatchAddMeterCase) batchChoice).getFlatBatchAddMeter();
138                 break;
139             case METER_REMOVE:
140                 batchData = ((FlatBatchRemoveMeterCase) batchChoice).getFlatBatchRemoveMeter();
141                 break;
142             case METER_UPDATE:
143                 batchData = ((FlatBatchUpdateMeterCase) batchChoice).getFlatBatchUpdateMeter();
144                 break;
145             default:
146                 throw new IllegalArgumentException("Unsupported batch step type obtained: " + batchStepType);
147         }
148         return batchData;
149     }
150
151     @VisibleForTesting
152     static <T extends BatchChoice> BatchStepType detectBatchStepType(final T batchCase) {
153         final BatchStepType type;
154         final Class<? extends DataContainer> implementedInterface = batchCase.getImplementedInterface();
155
156         if (FlatBatchAddFlowCase.class.equals(implementedInterface)) {
157             type = BatchStepType.FLOW_ADD;
158         } else if (FlatBatchRemoveFlowCase.class.equals(implementedInterface)) {
159             type = BatchStepType.FLOW_REMOVE;
160         } else if (FlatBatchUpdateFlowCase.class.equals(implementedInterface)) {
161             type = BatchStepType.FLOW_UPDATE;
162         } else if (FlatBatchAddGroupCase.class.equals(implementedInterface)) {
163             type = BatchStepType.GROUP_ADD;
164         } else if (FlatBatchRemoveGroupCase.class.equals(implementedInterface)) {
165             type = BatchStepType.GROUP_REMOVE;
166         } else if (FlatBatchUpdateGroupCase.class.equals(implementedInterface)) {
167             type = BatchStepType.GROUP_UPDATE;
168         } else if (FlatBatchAddMeterCase.class.equals(implementedInterface)) {
169             type = BatchStepType.METER_ADD;
170         } else if (FlatBatchRemoveMeterCase.class.equals(implementedInterface)) {
171             type = BatchStepType.METER_REMOVE;
172         } else if (FlatBatchUpdateMeterCase.class.equals(implementedInterface)) {
173             type = BatchStepType.METER_UPDATE;
174         } else {
175             throw new IllegalArgumentException("Unsupported batch obtained: " + implementedInterface);
176         }
177         return type;
178     }
179
180     /**
181      * @return RPC result incorporating partial results (state, errors, batch failures)
182      */
183     @VisibleForTesting
184     static Function<List<RpcResult<ProcessFlatBatchOutput>>, RpcResult<ProcessFlatBatchOutput>> mergeRpcResults() {
185         return jobsResults -> {
186             boolean isSuccessful = true;
187             List<RpcError> rpcErrors = new ArrayList<>();
188             List<BatchFailure> batchFailures = new ArrayList<>();
189
190             for (RpcResult<ProcessFlatBatchOutput> jobResult : jobsResults) {
191                 if (jobResult != null) {
192                     isSuccessful = (isSuccessful && jobResult.isSuccessful());
193                     rpcErrors.addAll(jobResult.getErrors());
194                     batchFailures.addAll(jobResult.getResult().getBatchFailure());
195                 }
196             }
197
198             return RpcResultBuilder.<ProcessFlatBatchOutput>status(isSuccessful)
199                     .withRpcErrors(rpcErrors)
200                     .withResult(new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures).build())
201                     .build();
202         };
203     }
204
205     /**
206      * Merge list of Futures with partial results into one ListenableFuture with single result.
207      * shortcut for {@link #mergeRpcResults()}
208      * @param firedJobs list of ListenableFutures with RPC results {@link ProcessFlatBatchOutput}
209      * @return ListenableFuture of RPC result with combined status and all errors + batch failures
210      */
211     public static ListenableFuture<RpcResult<ProcessFlatBatchOutput>> mergeJobsResultsFutures(
212             final List<ListenableFuture<RpcResult<ProcessFlatBatchOutput>>> firedJobs) {
213         return Futures.transform(Futures.successfulAsList(firedJobs), mergeRpcResults());
214     }
215
216     /**
217      *
218      * @param status RPC result status
219      * @return ListenableFuture of RPC result with empty list of errors and batch failures
220      */
221     public static ListenableFuture<RpcResult<ProcessFlatBatchOutput>> createEmptyRpcBatchResultFuture(final boolean status) {
222         return RpcResultBuilder.<ProcessFlatBatchOutput>status(status)
223                                .withRpcErrors(new ArrayList<>())
224                                .withResult(new ProcessFlatBatchOutputBuilder().setBatchFailure(new ArrayList<>()).build())
225                                .buildFuture();
226     }
227
228 }