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.services.sal;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Iterables;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.JdkFutureAdapters;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.concurrent.Future;
20 import java.util.stream.Collectors;
21 import javax.annotation.Nullable;
22 import org.opendaylight.openflowplugin.impl.util.BarrierUtil;
23 import org.opendaylight.openflowplugin.impl.util.GroupUtil;
24 import org.opendaylight.openflowplugin.impl.util.PathUtil;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.group.update.OriginalGroupBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.group.update.UpdatedGroupBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.AddGroupsBatchInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.AddGroupsBatchOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.BatchGroupInputUpdateGrouping;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.RemoveGroupsBatchInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.RemoveGroupsBatchOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.SalGroupsBatchService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.UpdateGroupsBatchInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.UpdateGroupsBatchOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.add.groups.batch.input.BatchAddGroups;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.batch.group.output.list.grouping.BatchFailedGroupsOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.remove.groups.batch.input.BatchRemoveGroups;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.update.groups.batch.input.BatchUpdateGroups;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * Default implementation of {@link SalGroupsBatchService} - delegates work to {@link SalGroupService}.
62 public class SalGroupsBatchServiceImpl implements SalGroupsBatchService {
64 private static final Logger LOG = LoggerFactory.getLogger(SalGroupsBatchServiceImpl.class);
66 private final SalGroupService salGroupService;
67 private final FlowCapableTransactionService transactionService;
69 public SalGroupsBatchServiceImpl(final SalGroupService salGroupService,
70 final FlowCapableTransactionService transactionService) {
71 this.salGroupService = Preconditions.checkNotNull(salGroupService);
72 this.transactionService = Preconditions.checkNotNull(transactionService);
76 public Future<RpcResult<UpdateGroupsBatchOutput>> updateGroupsBatch(final UpdateGroupsBatchInput input) {
77 final List<BatchUpdateGroups> batchUpdateGroups = input.getBatchUpdateGroups();
78 LOG.trace("Updating groups @ {} : {}", PathUtil.extractNodeId(input.getNode()), batchUpdateGroups.size());
80 final ArrayList<ListenableFuture<RpcResult<UpdateGroupOutput>>> resultsLot = new ArrayList<>();
81 for (BatchUpdateGroups batchGroup : batchUpdateGroups) {
82 final UpdateGroupInput updateGroupInput = new UpdateGroupInputBuilder(input)
83 .setOriginalGroup(new OriginalGroupBuilder(batchGroup.getOriginalBatchedGroup()).build())
84 .setUpdatedGroup(new UpdatedGroupBuilder(batchGroup.getUpdatedBatchedGroup()).build())
85 .setGroupRef(createGroupRef(input.getNode(), batchGroup))
86 .setNode(input.getNode())
88 resultsLot.add(JdkFutureAdapters.listenInPoolThread(salGroupService.updateGroup(updateGroupInput)));
91 final Iterable<Group> groups = batchUpdateGroups.stream()
92 .map(BatchGroupInputUpdateGrouping::getUpdatedBatchedGroup)
93 .collect(Collectors.toList());
95 final ListenableFuture<RpcResult<List<BatchFailedGroupsOutput>>> commonResult =
96 Futures.transform(Futures.allAsList(resultsLot), GroupUtil.<UpdateGroupOutput>createCumulatingFunction(
97 groups, batchUpdateGroups.size()));
99 ListenableFuture<RpcResult<UpdateGroupsBatchOutput>> updateGroupsBulkFuture = Futures.transform(
100 commonResult, GroupUtil.GROUP_UPDATE_TRANSFORM);
102 if (input.isBarrierAfter()) {
103 updateGroupsBulkFuture = BarrierUtil.chainBarrier(updateGroupsBulkFuture, input.getNode(),
104 transactionService, GroupUtil.GROUP_UPDATE_COMPOSING_TRANSFORM);
107 return updateGroupsBulkFuture;
111 public Future<RpcResult<AddGroupsBatchOutput>> addGroupsBatch(final AddGroupsBatchInput input) {
112 LOG.trace("Adding groups @ {} : {}", PathUtil.extractNodeId(input.getNode()), input.getBatchAddGroups().size());
113 final ArrayList<ListenableFuture<RpcResult<AddGroupOutput>>> resultsLot = new ArrayList<>();
114 for (BatchAddGroups addGroup : input.getBatchAddGroups()) {
115 final AddGroupInput addGroupInput = new AddGroupInputBuilder(addGroup)
116 .setGroupRef(createGroupRef(input.getNode(), addGroup))
117 .setNode(input.getNode())
119 resultsLot.add(JdkFutureAdapters.listenInPoolThread(salGroupService.addGroup(addGroupInput)));
122 final ListenableFuture<RpcResult<List<BatchFailedGroupsOutput>>> commonResult =
123 Futures.transform(Futures.allAsList(resultsLot),
124 GroupUtil.<AddGroupOutput>createCumulatingFunction(input.getBatchAddGroups()));
126 ListenableFuture<RpcResult<AddGroupsBatchOutput>> addGroupsBulkFuture =
127 Futures.transform(commonResult, GroupUtil.GROUP_ADD_TRANSFORM);
129 if (input.isBarrierAfter()) {
130 addGroupsBulkFuture = BarrierUtil.chainBarrier(addGroupsBulkFuture, input.getNode(),
131 transactionService, GroupUtil.GROUP_ADD_COMPOSING_TRANSFORM);
134 return addGroupsBulkFuture;
138 public Future<RpcResult<RemoveGroupsBatchOutput>> removeGroupsBatch(final RemoveGroupsBatchInput input) {
139 LOG.trace("Removing groups @ {} : {}",
140 PathUtil.extractNodeId(input.getNode()),
141 input.getBatchRemoveGroups().size());
142 final ArrayList<ListenableFuture<RpcResult<RemoveGroupOutput>>> resultsLot = new ArrayList<>();
143 for (BatchRemoveGroups addGroup : input.getBatchRemoveGroups()) {
144 final RemoveGroupInput removeGroupInput = new RemoveGroupInputBuilder(addGroup)
145 .setGroupRef(createGroupRef(input.getNode(), addGroup))
146 .setNode(input.getNode())
148 resultsLot.add(JdkFutureAdapters.listenInPoolThread(salGroupService.removeGroup(removeGroupInput)));
151 final ListenableFuture<RpcResult<List<BatchFailedGroupsOutput>>> commonResult =
152 Futures.transform(Futures.allAsList(resultsLot),
153 GroupUtil.<RemoveGroupOutput>createCumulatingFunction(input.getBatchRemoveGroups()));
155 ListenableFuture<RpcResult<RemoveGroupsBatchOutput>> removeGroupsBulkFuture =
156 Futures.transform(commonResult, GroupUtil.GROUP_REMOVE_TRANSFORM);
158 if (input.isBarrierAfter()) {
159 removeGroupsBulkFuture = BarrierUtil.chainBarrier(removeGroupsBulkFuture, input.getNode(),
160 transactionService, GroupUtil.GROUP_REMOVE_COMPOSING_TRANSFORM);
163 return removeGroupsBulkFuture;
166 private static GroupRef createGroupRef(final NodeRef nodeRef, final Group batchGroup) {
167 return GroupUtil.buildGroupPath((InstanceIdentifier<Node>) nodeRef.getValue(), batchGroup.getGroupId());
170 private static GroupRef createGroupRef(final NodeRef nodeRef, final BatchUpdateGroups batchGroup) {
171 return GroupUtil.buildGroupPath((InstanceIdentifier<Node>) nodeRef.getValue(),
172 batchGroup.getUpdatedBatchedGroup().getGroupId());