2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.util;
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;
43 * provides flat batch util methods
45 public final class FlatBatchUtil {
47 private static final Logger LOG = LoggerFactory.getLogger(FlatBatchUtil.class);
49 private FlatBatchUtil() {
50 throw new IllegalStateException("This class should not be instantiated.");
53 public static void markBarriersWhereNeeded(final List<BatchPlanStep> batchPlan) {
54 final EnumSet<BatchStepType> previousTypes = EnumSet.noneOf(BatchStepType.class);
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();
63 previousTypes.add(type);
64 previousPlanStep = planStep;
69 static boolean decideBarrier(final EnumSet<BatchStepType> previousTypes, final BatchStepType type) {
70 final boolean needBarrier;
74 needBarrier = previousTypes.contains(BatchStepType.GROUP_ADD)
75 || previousTypes.contains(BatchStepType.METER_ADD);
78 needBarrier = previousTypes.contains(BatchStepType.GROUP_ADD)
79 || previousTypes.contains(BatchStepType.GROUP_UPDATE);
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);
88 needBarrier = previousTypes.contains(BatchStepType.FLOW_REMOVE)
89 || previousTypes.contains(BatchStepType.FLOW_UPDATE);
97 public static List<BatchPlanStep> assembleBatchPlan(List<Batch> batches) {
98 final List<BatchPlanStep> plan = new ArrayList<>();
100 BatchPlanStep planStep;
101 for (Batch batch : batches) {
102 final BatchStepType nextStepType = detectBatchStepType(batch.getBatchChoice());
104 planStep = new BatchPlanStep(nextStepType);
105 planStep.getTaskBag().addAll(extractBatchData(planStep.getStepType(), batch.getBatchChoice()));
106 if (!planStep.isEmpty()) {
114 private static List<? extends BatchOrderGrouping> extractBatchData(final BatchStepType batchStepType,
115 final BatchChoice batchChoice) {
116 final List<? extends BatchOrderGrouping> batchData;
117 switch (batchStepType) {
119 batchData = ((FlatBatchAddFlowCase) batchChoice).getFlatBatchAddFlow();
122 batchData = ((FlatBatchRemoveFlowCase) batchChoice).getFlatBatchRemoveFlow();
125 batchData = ((FlatBatchUpdateFlowCase) batchChoice).getFlatBatchUpdateFlow();
128 batchData = ((FlatBatchAddGroupCase) batchChoice).getFlatBatchAddGroup();
131 batchData = ((FlatBatchRemoveGroupCase) batchChoice).getFlatBatchRemoveGroup();
134 batchData = ((FlatBatchUpdateGroupCase) batchChoice).getFlatBatchUpdateGroup();
137 batchData = ((FlatBatchAddMeterCase) batchChoice).getFlatBatchAddMeter();
140 batchData = ((FlatBatchRemoveMeterCase) batchChoice).getFlatBatchRemoveMeter();
143 batchData = ((FlatBatchUpdateMeterCase) batchChoice).getFlatBatchUpdateMeter();
146 throw new IllegalArgumentException("Unsupported batch step type obtained: " + batchStepType);
152 static <T extends BatchChoice> BatchStepType detectBatchStepType(final T batchCase) {
153 final BatchStepType type;
154 final Class<? extends DataContainer> implementedInterface = batchCase.getImplementedInterface();
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;
175 throw new IllegalArgumentException("Unsupported batch obtained: " + implementedInterface);
181 * @return RPC result incorporating partial results (state, errors, batch failures)
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<>();
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());
198 return RpcResultBuilder.<ProcessFlatBatchOutput>status(isSuccessful)
199 .withRpcErrors(rpcErrors)
200 .withResult(new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures).build())
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
211 public static ListenableFuture<RpcResult<ProcessFlatBatchOutput>> mergeJobsResultsFutures(
212 final List<ListenableFuture<RpcResult<ProcessFlatBatchOutput>>> firedJobs) {
213 return Futures.transform(Futures.successfulAsList(firedJobs), mergeRpcResults());
218 * @param status RPC result status
219 * @return ListenableFuture of RPC result with empty list of errors and batch failures
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())