2 * Copyright (c) 2017 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.controller.clustering.it.provider.impl;
11 import static org.opendaylight.controller.clustering.it.provider.impl.AbstractTransactionHandler.ID;
12 import static org.opendaylight.controller.clustering.it.provider.impl.AbstractTransactionHandler.ID_INT;
13 import static org.opendaylight.controller.clustering.it.provider.impl.AbstractTransactionHandler.ID_INTS;
14 import static org.opendaylight.controller.clustering.it.provider.impl.AbstractTransactionHandler.ITEM;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.SettableFuture;
21 import java.util.Collections;
22 import java.util.HashMap;
24 import java.util.concurrent.CompletionStage;
25 import java.util.stream.Collectors;
26 import javax.annotation.Nullable;
27 import org.opendaylight.controller.cluster.access.concepts.MemberName;
28 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory;
29 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration;
30 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
39 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
40 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.CreatePrefixShardInput;
41 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardInput;
42 import org.opendaylight.yangtools.yang.common.RpcError;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
49 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class PrefixShardHandler {
57 private static final Logger LOG = LoggerFactory.getLogger(PrefixShardHandler.class);
58 private static final int MAX_PREFIX = 4;
59 private static final String PREFIX_TEMPLATE = "prefix-";
61 private final DistributedShardFactory shardFactory;
62 private final DOMDataTreeService domDataTreeService;
63 private final BindingNormalizedNodeSerializer serializer;
65 private final Map<YangInstanceIdentifier, DistributedShardRegistration> registrations =
66 Collections.synchronizedMap(new HashMap<>());
68 public PrefixShardHandler(final DistributedShardFactory shardFactory,
69 final DOMDataTreeService domDataTreeService,
70 final BindingNormalizedNodeSerializer serializer) {
72 this.shardFactory = shardFactory;
73 this.domDataTreeService = domDataTreeService;
74 this.serializer = serializer;
77 public ListenableFuture<RpcResult<Void>> onCreatePrefixShard(final CreatePrefixShardInput input) {
79 final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
81 final CompletionStage<DistributedShardRegistration> completionStage;
82 final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
85 completionStage = shardFactory.createDistributedShard(
86 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, identifier),
87 input.getReplicas().stream().map(MemberName::forName).collect(Collectors.toList()));
89 completionStage.thenAccept(registration -> {
90 LOG.debug("Shard[{}] created successfully.", identifier);
91 registrations.put(identifier, registration);
93 final CheckedFuture<Void, TransactionCommitFailedException> ensureFuture = ensureListExists();
94 Futures.addCallback(ensureFuture, new FutureCallback<Void>() {
96 public void onSuccess(@Nullable final Void result) {
97 LOG.debug("Initial list write successful.");
98 future.set(RpcResultBuilder.<Void>success().build());
102 public void onFailure(final Throwable throwable) {
103 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
105 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
106 "Shard creation failed", "cluster-test-app", "", throwable);
107 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
112 completionStage.exceptionally(throwable -> {
113 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
115 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
116 "Shard creation failed", "cluster-test-app", "", throwable);
117 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
120 } catch (final DOMDataTreeShardingConflictException e) {
121 LOG.warn("Unable to register shard for: {}.", identifier);
123 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
124 "Sharding conflict", "cluster-test-app", "", e);
125 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
131 public ListenableFuture<RpcResult<Void>> onRemovePrefixShard(final RemovePrefixShardInput input) {
133 final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
134 final DistributedShardRegistration registration = registrations.get(identifier);
136 if (registration == null) {
137 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "registration-missing",
138 "No shard registered at this prefix.");
139 return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
142 final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
144 final CompletionStage<Void> close = registration.close();
145 close.thenRun(() -> future.set(RpcResultBuilder.<Void>success().build()));
146 close.exceptionally(throwable -> {
147 LOG.warn("Shard[{}] removal failed:", identifier, throwable);
149 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "remove-shard-failed",
150 "Shard removal failed", "cluster-test-app", "", throwable);
151 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
158 private CheckedFuture<Void, TransactionCommitFailedException> ensureListExists() {
160 final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ID_INT);
162 // hardcoded initial list population for parallel produce-transactions testing on multiple nodes
163 for (int i = 1; i < MAX_PREFIX; i++) {
164 mapBuilder.withChild(
165 ImmutableNodes.mapEntryBuilder(ID_INT, ID, PREFIX_TEMPLATE + i)
166 .withChild(ImmutableNodes.mapNodeBuilder(ITEM).build())
169 final MapNode mapNode = mapBuilder.build();
171 final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
172 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ID_INTS))
176 final DOMDataTreeProducer producer = domDataTreeService.createProducer(Collections.singleton(
177 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY)));
179 final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
181 final DOMDataTreeWriteCursor cursor =
182 tx.createCursor(new DOMDataTreeIdentifier(
183 LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY));
185 cursor.merge(containerNode.getIdentifier(), containerNode);
188 final CheckedFuture<Void, TransactionCommitFailedException> future = tx.submit();
189 Futures.addCallback(future, new FutureCallback<Void>() {
191 public void onSuccess(@Nullable final Void result) {
193 LOG.debug("Closing producer for initial list.");
195 } catch (DOMDataTreeProducerException e) {
196 LOG.warn("Error while closing producer.", e);
201 public void onFailure(final Throwable throwable) {
202 //NOOP handled by the caller of this method.