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 com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.Collections;
15 import java.util.HashMap;
17 import java.util.concurrent.CompletionStage;
18 import java.util.stream.Collectors;
19 import org.opendaylight.controller.cluster.access.concepts.MemberName;
20 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory;
21 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
26 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.CreatePrefixShardInput;
27 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardInput;
28 import org.opendaylight.yangtools.yang.common.RpcError;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class PrefixShardHandler {
37 private static final Logger LOG = LoggerFactory.getLogger(PrefixShardHandler.class);
39 private final DistributedShardFactory shardFactory;
40 private final BindingNormalizedNodeSerializer serializer;
42 private final Map<YangInstanceIdentifier, DistributedShardRegistration> registrations =
43 Collections.synchronizedMap(new HashMap<>());
45 public PrefixShardHandler(final DistributedShardFactory shardFactory,
46 final BindingNormalizedNodeSerializer serializer) {
48 this.shardFactory = shardFactory;
49 this.serializer = serializer;
52 public ListenableFuture<RpcResult<Void>> onCreatePrefixShard(final CreatePrefixShardInput input) {
54 final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
56 final CompletionStage<DistributedShardRegistration> completionStage;
57 final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
60 completionStage = shardFactory.createDistributedShard(
61 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, identifier),
62 input.getReplicas().stream().map(MemberName::forName).collect(Collectors.toList()));
64 completionStage.thenAccept(registration -> {
65 LOG.debug("Shard[{}] created successfully.", identifier);
66 registrations.put(identifier, registration);
67 future.set(RpcResultBuilder.<Void>success().build());
69 completionStage.exceptionally(throwable -> {
70 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
72 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
73 "Shard creation failed", "cluster-test-app", "", throwable);
74 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
77 } catch (final DOMDataTreeShardingConflictException e) {
78 LOG.warn("Unable to register shard for: {}.", identifier);
80 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
81 "Sharding conflict", "cluster-test-app", "", e);
82 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
88 public ListenableFuture<RpcResult<Void>> onRemovePrefixShard(final RemovePrefixShardInput input) {
90 final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
91 final DistributedShardRegistration registration = registrations.get(identifier);
93 if (registration == null) {
94 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "registration-missing",
95 "No shard registered at this prefix.");
96 return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
99 final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
101 final CompletionStage<Void> close = registration.close();
102 close.thenRun(() -> future.set(RpcResultBuilder.<Void>success().build()));
103 close.exceptionally(throwable -> {
104 LOG.warn("Shard[{}] removal failed:", identifier, throwable);
106 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "remove-shard-failed",
107 "Shard removal failed", "cluster-test-app", "", throwable);
108 future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());