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.CreatePrefixShardOutput;
41 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.CreatePrefixShardOutputBuilder;
42 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardInput;
43 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardOutput;
44 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardOutputBuilder;
45 import org.opendaylight.yangtools.yang.common.RpcError;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
52 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
53 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
54 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 public class PrefixShardHandler {
60 private static final Logger LOG = LoggerFactory.getLogger(PrefixShardHandler.class);
61 private static final int MAX_PREFIX = 4;
62 private static final String PREFIX_TEMPLATE = "prefix-";
64 private final DistributedShardFactory shardFactory;
65 private final DOMDataTreeService domDataTreeService;
66 private final BindingNormalizedNodeSerializer serializer;
68 private final Map<YangInstanceIdentifier, DistributedShardRegistration> registrations =
69 Collections.synchronizedMap(new HashMap<>());
71 public PrefixShardHandler(final DistributedShardFactory shardFactory,
72 final DOMDataTreeService domDataTreeService,
73 final BindingNormalizedNodeSerializer serializer) {
75 this.shardFactory = shardFactory;
76 this.domDataTreeService = domDataTreeService;
77 this.serializer = serializer;
80 public ListenableFuture<RpcResult<CreatePrefixShardOutput>> onCreatePrefixShard(
81 final CreatePrefixShardInput input) {
83 final SettableFuture<RpcResult<CreatePrefixShardOutput>> future = SettableFuture.create();
85 final CompletionStage<DistributedShardRegistration> completionStage;
86 final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
89 completionStage = shardFactory.createDistributedShard(
90 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, identifier),
91 input.getReplicas().stream().map(MemberName::forName).collect(Collectors.toList()));
93 completionStage.thenAccept(registration -> {
94 LOG.debug("Shard[{}] created successfully.", identifier);
95 registrations.put(identifier, registration);
97 final ListenableFuture<Void> ensureFuture = ensureListExists();
98 Futures.addCallback(ensureFuture, new FutureCallback<Void>() {
100 public void onSuccess(@Nullable final Void result) {
101 LOG.debug("Initial list write successful.");
102 future.set(RpcResultBuilder.success(new CreatePrefixShardOutputBuilder().build()).build());
106 public void onFailure(final Throwable throwable) {
107 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
109 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION,
110 "create-shard-failed", "Shard creation failed", "cluster-test-app", "", throwable);
111 future.set(RpcResultBuilder.<CreatePrefixShardOutput>failed().withRpcError(error).build());
113 }, MoreExecutors.directExecutor());
115 completionStage.exceptionally(throwable -> {
116 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
118 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
119 "Shard creation failed", "cluster-test-app", "", throwable);
120 future.set(RpcResultBuilder.<CreatePrefixShardOutput>failed().withRpcError(error).build());
123 } catch (final DOMDataTreeShardingConflictException e) {
124 LOG.warn("Unable to register shard for: {}.", identifier);
126 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
127 "Sharding conflict", "cluster-test-app", "", e);
128 future.set(RpcResultBuilder.<CreatePrefixShardOutput>failed().withRpcError(error).build());
134 public ListenableFuture<RpcResult<RemovePrefixShardOutput>> onRemovePrefixShard(
135 final RemovePrefixShardInput input) {
137 final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
138 final DistributedShardRegistration registration = registrations.get(identifier);
140 if (registration == null) {
141 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "registration-missing",
142 "No shard registered at this prefix.");
143 return Futures.immediateFuture(RpcResultBuilder.<RemovePrefixShardOutput>failed().withRpcError(error)
147 final SettableFuture<RpcResult<RemovePrefixShardOutput>> future = SettableFuture.create();
149 final CompletionStage<Void> close = registration.close();
150 close.thenRun(() -> future.set(RpcResultBuilder.success(new RemovePrefixShardOutputBuilder().build()).build()));
151 close.exceptionally(throwable -> {
152 LOG.warn("Shard[{}] removal failed:", identifier, throwable);
154 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "remove-shard-failed",
155 "Shard removal failed", "cluster-test-app", "", throwable);
156 future.set(RpcResultBuilder.<RemovePrefixShardOutput>failed().withRpcError(error).build());
163 private ListenableFuture<Void> ensureListExists() {
165 final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ID_INT);
167 // hardcoded initial list population for parallel produce-transactions testing on multiple nodes
168 for (int i = 1; i < MAX_PREFIX; i++) {
169 mapBuilder.withChild(
170 ImmutableNodes.mapEntryBuilder(ID_INT, ID, PREFIX_TEMPLATE + i)
171 .withChild(ImmutableNodes.mapNodeBuilder(ITEM).build())
174 final MapNode mapNode = mapBuilder.build();
176 final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
177 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ID_INTS))
181 final DOMDataTreeProducer producer = domDataTreeService.createProducer(Collections.singleton(
182 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY)));
184 final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
186 final DOMDataTreeWriteCursor cursor =
187 tx.createCursor(new DOMDataTreeIdentifier(
188 LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY));
190 cursor.merge(containerNode.getIdentifier(), containerNode);
193 final ListenableFuture<Void> future = tx.submit();
194 Futures.addCallback(future, new FutureCallback<Void>() {
196 public void onSuccess(@Nullable final Void result) {
198 LOG.debug("Closing producer for initial list.");
200 } catch (DOMDataTreeProducerException e) {
201 LOG.warn("Error while closing producer.", e);
206 public void onFailure(final Throwable throwable) {
207 //NOOP handled by the caller of this method.
209 }, MoreExecutors.directExecutor());