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
8 package org.opendaylight.openflowplugin.impl.services;
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 java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.Future;
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;
53 * default implementation of {@link SalFlowsBatchService} - delegates work to {@link SalFlowService}
55 public class SalFlowsBatchServiceImpl implements SalFlowsBatchService {
56 private static final Logger LOG = LoggerFactory.getLogger(SalFlowsBatchServiceImpl.class);
58 private final SalFlowService salFlowService;
59 private final FlowCapableTransactionService transactionService;
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 = Preconditions.checkNotNull(transactionService, "delegate transaction service must not be null");
68 public Future<RpcResult<RemoveFlowsBatchOutput>> removeFlowsBatch(final RemoveFlowsBatchInput input) {
69 LOG.trace("Removing flows @ {} : {}", PathUtil.extractNodeId(input.getNode()), input.getBatchRemoveFlows().size());
70 final ArrayList<ListenableFuture<RpcResult<RemoveFlowOutput>>> resultsLot = new ArrayList<>();
71 for (BatchFlowInputGrouping batchFlow : input.getBatchRemoveFlows()) {
72 final RemoveFlowInput removeFlowInput = new RemoveFlowInputBuilder(batchFlow)
73 .setFlowRef(createFlowRef(input.getNode(), batchFlow))
74 .setNode(input.getNode())
76 resultsLot.add(JdkFutureAdapters.listenInPoolThread(salFlowService.removeFlow(removeFlowInput)));
79 final ListenableFuture<RpcResult<List<BatchFailedFlowsOutput>>> commonResult =
80 Futures.transform(Futures.successfulAsList(resultsLot),
81 FlowUtil.<RemoveFlowOutput>createCumulatingFunction(input.getBatchRemoveFlows()));
83 ListenableFuture<RpcResult<RemoveFlowsBatchOutput>> removeFlowsBulkFuture = Futures.transform(commonResult, FlowUtil.FLOW_REMOVE_TRANSFORM);
85 if (input.isBarrierAfter()) {
86 removeFlowsBulkFuture = BarrierUtil.chainBarrier(removeFlowsBulkFuture, input.getNode(),
87 transactionService, FlowUtil.FLOW_REMOVE_COMPOSING_TRANSFORM);
90 return removeFlowsBulkFuture;
94 public Future<RpcResult<AddFlowsBatchOutput>> addFlowsBatch(final AddFlowsBatchInput input) {
95 LOG.trace("Adding flows @ {} : {}", PathUtil.extractNodeId(input.getNode()), input.getBatchAddFlows().size());
96 final ArrayList<ListenableFuture<RpcResult<AddFlowOutput>>> resultsLot = new ArrayList<>();
97 for (BatchFlowInputGrouping batchFlow : input.getBatchAddFlows()) {
98 final AddFlowInput addFlowInput = new AddFlowInputBuilder(batchFlow)
99 .setFlowRef(createFlowRef(input.getNode(), batchFlow))
100 .setNode(input.getNode())
102 resultsLot.add(JdkFutureAdapters.listenInPoolThread(salFlowService.addFlow(addFlowInput)));
105 final ListenableFuture<RpcResult<List<BatchFailedFlowsOutput>>> commonResult =
106 Futures.transform(Futures.successfulAsList(resultsLot),
107 FlowUtil.<AddFlowOutput>createCumulatingFunction(input.getBatchAddFlows()));
109 ListenableFuture<RpcResult<AddFlowsBatchOutput>> addFlowsBulkFuture =
110 Futures.transform(commonResult, FlowUtil.FLOW_ADD_TRANSFORM);
112 if (input.isBarrierAfter()) {
113 addFlowsBulkFuture = BarrierUtil.chainBarrier(addFlowsBulkFuture, input.getNode(),
114 transactionService, FlowUtil.FLOW_ADD_COMPOSING_TRANSFORM);
117 return addFlowsBulkFuture;
120 private static FlowRef createFlowRef(final NodeRef nodeRef, final BatchFlowInputGrouping batchFlow) {
121 return FlowUtil.buildFlowPath((InstanceIdentifier<Node>) nodeRef.getValue(),
122 batchFlow.getTableId(), batchFlow.getFlowId());
125 private static FlowRef createFlowRef(final NodeRef nodeRef, final BatchFlowInputUpdateGrouping batchFlow) {
126 return FlowUtil.buildFlowPath((InstanceIdentifier<Node>) nodeRef.getValue(),
127 batchFlow.getOriginalBatchedFlow().getTableId(), batchFlow.getFlowId());
131 public Future<RpcResult<UpdateFlowsBatchOutput>> updateFlowsBatch(final UpdateFlowsBatchInput input) {
132 LOG.trace("Updating flows @ {} : {}", PathUtil.extractNodeId(input.getNode()), input.getBatchUpdateFlows().size());
133 final ArrayList<ListenableFuture<RpcResult<UpdateFlowOutput>>> resultsLot = new ArrayList<>();
134 for (BatchUpdateFlows batchFlow : input.getBatchUpdateFlows()) {
135 final UpdateFlowInput updateFlowInput = new UpdateFlowInputBuilder(input)
136 .setOriginalFlow(new OriginalFlowBuilder(batchFlow.getOriginalBatchedFlow()).build())
137 .setUpdatedFlow(new UpdatedFlowBuilder(batchFlow.getUpdatedBatchedFlow()).build())
138 .setFlowRef(createFlowRef(input.getNode(), batchFlow))
139 .setNode(input.getNode())
141 resultsLot.add(JdkFutureAdapters.listenInPoolThread(salFlowService.updateFlow(updateFlowInput)));
144 final ListenableFuture<RpcResult<List<BatchFailedFlowsOutput>>> commonResult =
145 Futures.transform(Futures.successfulAsList(resultsLot), FlowUtil.<UpdateFlowOutput>createCumulatingFunction(input.getBatchUpdateFlows()));
147 ListenableFuture<RpcResult<UpdateFlowsBatchOutput>> updateFlowsBulkFuture = Futures.transform(commonResult, FlowUtil.FLOW_UPDATE_TRANSFORM);
149 if (input.isBarrierAfter()) {
150 updateFlowsBulkFuture = BarrierUtil.chainBarrier(updateFlowsBulkFuture, input.getNode(),
151 transactionService, FlowUtil.FLOW_UPDATE_COMPOSING_TRANSFORM);
154 return updateFlowsBulkFuture;