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.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.MoreExecutors;
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.dom.api.DOMDataTreeCursorAwareTransaction;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
37 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
39 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.CreatePrefixShardInput;
40 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardInput;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
48 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class PrefixShardHandler {
56 private static final Logger LOG = LoggerFactory.getLogger(PrefixShardHandler.class);
57 private static final int MAX_PREFIX = 4;
58 private static final String PREFIX_TEMPLATE = "prefix-";
60 private final DistributedShardFactory shardFactory;
61 private final DOMDataTreeService domDataTreeService;
62 private final BindingNormalizedNodeSerializer serializer;
64 private final Map<YangInstanceIdentifier, DistributedShardRegistration> registrations =
65 Collections.synchronizedMap(new HashMap<>());
67 public PrefixShardHandler(final DistributedShardFactory shardFactory,
68 final DOMDataTreeService domDataTreeService,
69 final BindingNormalizedNodeSerializer serializer) {
71 this.shardFactory = shardFactory;
72 this.domDataTreeService = domDataTreeService;
73 this.serializer = serializer;
76 public ListenableFuture<RpcResult<Void>> onCreatePrefixShard(final CreatePrefixShardInput input) {
78 final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
80 final CompletionStage<DistributedShardRegistration> completionStage;
81 final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
84 completionStage = shardFactory.createDistributedShard(
85 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, identifier),
86 input.getReplicas().stream().map(MemberName::forName).collect(Collectors.toList()));
88 completionStage.thenAccept(registration -> {
89 LOG.debug("Shard[{}] created successfully.", identifier);
90 registrations.put(identifier, registration);
92 final ListenableFuture<Void> ensureFuture = ensureListExists();
93 Futures.addCallback(ensureFuture, new FutureCallback<Void>() {
95 public void onSuccess(@Nullable final Void result) {
96 LOG.debug("Initial list write successful.");
97 future.set(RpcResultBuilder.<Void>success().build());
101 public void onFailure(final Throwable throwable) {
102 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
104 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION,
105 "create-shard-failed", "Shard creation failed", "cluster-test-app", "", throwable);
106 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
108 }, MoreExecutors.directExecutor());
110 completionStage.exceptionally(throwable -> {
111 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
113 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
114 "Shard creation failed", "cluster-test-app", "", throwable);
115 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
118 } catch (final DOMDataTreeShardingConflictException e) {
119 LOG.warn("Unable to register shard for: {}.", identifier);
121 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
122 "Sharding conflict", "cluster-test-app", "", e);
123 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
129 public ListenableFuture<RpcResult<Void>> onRemovePrefixShard(final RemovePrefixShardInput input) {
131 final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
132 final DistributedShardRegistration registration = registrations.get(identifier);
134 if (registration == null) {
135 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "registration-missing",
136 "No shard registered at this prefix.");
137 return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
140 final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
142 final CompletionStage<Void> close = registration.close();
143 close.thenRun(() -> future.set(RpcResultBuilder.<Void>success().build()));
144 close.exceptionally(throwable -> {
145 LOG.warn("Shard[{}] removal failed:", identifier, throwable);
147 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "remove-shard-failed",
148 "Shard removal failed", "cluster-test-app", "", throwable);
149 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
156 private ListenableFuture<Void> ensureListExists() {
158 final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ID_INT);
160 // hardcoded initial list population for parallel produce-transactions testing on multiple nodes
161 for (int i = 1; i < MAX_PREFIX; i++) {
162 mapBuilder.withChild(
163 ImmutableNodes.mapEntryBuilder(ID_INT, ID, PREFIX_TEMPLATE + i)
164 .withChild(ImmutableNodes.mapNodeBuilder(ITEM).build())
167 final MapNode mapNode = mapBuilder.build();
169 final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
170 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ID_INTS))
174 final DOMDataTreeProducer producer = domDataTreeService.createProducer(Collections.singleton(
175 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY)));
177 final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
179 final DOMDataTreeWriteCursor cursor =
180 tx.createCursor(new DOMDataTreeIdentifier(
181 LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY));
183 cursor.merge(containerNode.getIdentifier(), containerNode);
186 final ListenableFuture<Void> future = tx.submit();
187 Futures.addCallback(future, new FutureCallback<Void>() {
189 public void onSuccess(@Nullable final Void result) {
191 LOG.debug("Closing producer for initial list.");
193 } catch (DOMDataTreeProducerException e) {
194 LOG.warn("Error while closing producer.", e);
199 public void onFailure(final Throwable throwable) {
200 //NOOP handled by the caller of this method.
202 }, MoreExecutors.directExecutor());