re-activate FindBugs
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / SalFlowsBatchServiceImpl.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 package org.opendaylight.openflowplugin.impl.services.sal;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.JdkFutureAdapters;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.openflowplugin.impl.util.BarrierUtil;
18 import org.opendaylight.openflowplugin.impl.util.FlowUtil;
19 import org.opendaylight.openflowplugin.impl.util.PathUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowInputGrouping;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.BatchFlowInputUpdateGrouping;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.SalFlowsBatchService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.output.list.grouping.BatchFailedFlowsOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.update.flows.batch.input.BatchUpdateFlows;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * Default implementation of {@link SalFlowsBatchService} - delegates work to {@link SalFlowService}.
54  */
55 public class SalFlowsBatchServiceImpl implements SalFlowsBatchService {
56     private static final Logger LOG = LoggerFactory.getLogger(SalFlowsBatchServiceImpl.class);
57
58     private final SalFlowService salFlowService;
59     private final FlowCapableTransactionService transactionService;
60
61     public SalFlowsBatchServiceImpl(final SalFlowService salFlowService,
62                                     final FlowCapableTransactionService transactionService) {
63         this.salFlowService = Preconditions.checkNotNull(salFlowService, "delegate flow service must not be null");
64         this.transactionService =
65                 Preconditions.checkNotNull(transactionService, "delegate transaction service must not be null");
66     }
67
68     @Override
69     public ListenableFuture<RpcResult<RemoveFlowsBatchOutput>> removeFlowsBatch(final RemoveFlowsBatchInput input) {
70         LOG.trace("Removing flows @ {} : {}",
71                   PathUtil.extractNodeId(input.getNode()),
72                   input.getBatchRemoveFlows().size());
73         final ArrayList<ListenableFuture<RpcResult<RemoveFlowOutput>>> resultsLot = new ArrayList<>();
74         for (BatchFlowInputGrouping batchFlow : input.getBatchRemoveFlows()) {
75             final RemoveFlowInput removeFlowInput = new RemoveFlowInputBuilder(batchFlow)
76                     .setFlowRef(createFlowRef(input.getNode(), batchFlow))
77                     .setNode(input.getNode())
78                     .build();
79             resultsLot.add(JdkFutureAdapters.listenInPoolThread(salFlowService.removeFlow(removeFlowInput)));
80         }
81
82         final ListenableFuture<RpcResult<List<BatchFailedFlowsOutput>>> commonResult =
83                 Futures.transform(Futures.successfulAsList(resultsLot),
84                         FlowUtil.<RemoveFlowOutput>createCumulatingFunction(input.getBatchRemoveFlows()),
85                         MoreExecutors.directExecutor());
86
87         ListenableFuture<RpcResult<RemoveFlowsBatchOutput>> removeFlowsBulkFuture =
88                 Futures.transform(commonResult, FlowUtil.FLOW_REMOVE_TRANSFORM, MoreExecutors.directExecutor());
89
90         if (input.isBarrierAfter()) {
91             removeFlowsBulkFuture = BarrierUtil.chainBarrier(removeFlowsBulkFuture, input.getNode(),
92                     transactionService, FlowUtil.FLOW_REMOVE_COMPOSING_TRANSFORM);
93         }
94
95         return removeFlowsBulkFuture;
96     }
97
98     @Override
99     public ListenableFuture<RpcResult<AddFlowsBatchOutput>> addFlowsBatch(final AddFlowsBatchInput input) {
100         LOG.trace("Adding flows @ {} : {}", PathUtil.extractNodeId(input.getNode()), input.getBatchAddFlows().size());
101         final ArrayList<ListenableFuture<RpcResult<AddFlowOutput>>> resultsLot = new ArrayList<>();
102         for (BatchFlowInputGrouping batchFlow : input.getBatchAddFlows()) {
103             final AddFlowInput addFlowInput = new AddFlowInputBuilder(batchFlow)
104                     .setFlowRef(createFlowRef(input.getNode(), batchFlow))
105                     .setNode(input.getNode())
106                     .build();
107             resultsLot.add(JdkFutureAdapters.listenInPoolThread(salFlowService.addFlow(addFlowInput)));
108         }
109
110         final ListenableFuture<RpcResult<List<BatchFailedFlowsOutput>>> commonResult =
111                 Futures.transform(Futures.successfulAsList(resultsLot),
112                         FlowUtil.<AddFlowOutput>createCumulatingFunction(input.getBatchAddFlows()),
113                         MoreExecutors.directExecutor());
114
115         ListenableFuture<RpcResult<AddFlowsBatchOutput>> addFlowsBulkFuture =
116                 Futures.transform(commonResult, FlowUtil.FLOW_ADD_TRANSFORM, MoreExecutors.directExecutor());
117
118         if (input.isBarrierAfter()) {
119             addFlowsBulkFuture = BarrierUtil.chainBarrier(addFlowsBulkFuture, input.getNode(),
120                     transactionService, FlowUtil.FLOW_ADD_COMPOSING_TRANSFORM);
121         }
122
123         return addFlowsBulkFuture;
124     }
125
126     private static FlowRef createFlowRef(final NodeRef nodeRef, final BatchFlowInputGrouping batchFlow) {
127         return FlowUtil.buildFlowPath((InstanceIdentifier<Node>) nodeRef.getValue(),
128                 batchFlow.getTableId(), batchFlow.getFlowId());
129     }
130
131     private static FlowRef createFlowRef(final NodeRef nodeRef, final BatchFlowInputUpdateGrouping batchFlow) {
132         return FlowUtil.buildFlowPath((InstanceIdentifier<Node>) nodeRef.getValue(),
133                 batchFlow.getOriginalBatchedFlow().getTableId(), batchFlow.getFlowId());
134     }
135
136     @Override
137     public ListenableFuture<RpcResult<UpdateFlowsBatchOutput>> updateFlowsBatch(final UpdateFlowsBatchInput input) {
138         LOG.trace("Updating flows @ {} : {}",
139                   PathUtil.extractNodeId(input.getNode()),
140                   input.getBatchUpdateFlows().size());
141         final ArrayList<ListenableFuture<RpcResult<UpdateFlowOutput>>> resultsLot = new ArrayList<>();
142         for (BatchUpdateFlows batchFlow : input.getBatchUpdateFlows()) {
143             final UpdateFlowInput updateFlowInput = new UpdateFlowInputBuilder(input)
144                     .setOriginalFlow(new OriginalFlowBuilder(batchFlow.getOriginalBatchedFlow()).build())
145                     .setUpdatedFlow(new UpdatedFlowBuilder(batchFlow.getUpdatedBatchedFlow()).build())
146                     .setFlowRef(createFlowRef(input.getNode(), batchFlow))
147                     .setNode(input.getNode())
148                     .build();
149             resultsLot.add(JdkFutureAdapters.listenInPoolThread(salFlowService.updateFlow(updateFlowInput)));
150         }
151
152         final ListenableFuture<RpcResult<List<BatchFailedFlowsOutput>>> commonResult =
153                 Futures.transform(Futures.successfulAsList(resultsLot),
154                                   FlowUtil.<UpdateFlowOutput>createCumulatingFunction(input.getBatchUpdateFlows()),
155                         MoreExecutors.directExecutor());
156
157         ListenableFuture<RpcResult<UpdateFlowsBatchOutput>> updateFlowsBulkFuture =
158                 Futures.transform(commonResult, FlowUtil.FLOW_UPDATE_TRANSFORM, MoreExecutors.directExecutor());
159
160         if (input.isBarrierAfter()) {
161             updateFlowsBulkFuture = BarrierUtil.chainBarrier(updateFlowsBulkFuture, input.getNode(),
162                     transactionService, FlowUtil.FLOW_UPDATE_COMPOSING_TRANSFORM);
163         }
164
165         return updateFlowsBulkFuture;
166     }
167 }