22e7700d5cf6166984facd393398eec83b0fb7f8
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / PrefixShardHandler.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.clustering.it.provider.impl;
10
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;
16 import java.util.Map;
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;
34
35 public class PrefixShardHandler {
36
37     private static final Logger LOG = LoggerFactory.getLogger(PrefixShardHandler.class);
38
39     private final DistributedShardFactory shardFactory;
40     private final BindingNormalizedNodeSerializer serializer;
41
42     private final Map<YangInstanceIdentifier, DistributedShardRegistration> registrations =
43             Collections.synchronizedMap(new HashMap<>());
44
45     public PrefixShardHandler(final DistributedShardFactory shardFactory,
46                               final BindingNormalizedNodeSerializer serializer) {
47
48         this.shardFactory = shardFactory;
49         this.serializer = serializer;
50     }
51
52     public ListenableFuture<RpcResult<Void>> onCreatePrefixShard(final CreatePrefixShardInput input) {
53
54         final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
55
56         final CompletionStage<DistributedShardRegistration> completionStage;
57         final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
58
59         try {
60              completionStage = shardFactory.createDistributedShard(
61                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, identifier),
62                     input.getReplicas().stream().map(MemberName::forName).collect(Collectors.toList()));
63
64             completionStage.thenAccept(registration -> {
65                 LOG.debug("Shard[{}] created successfully.", identifier);
66                 registrations.put(identifier, registration);
67                 future.set(RpcResultBuilder.<Void>success().build());
68             });
69             completionStage.exceptionally(throwable -> {
70                 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
71
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());
75                 return null;
76             });
77         } catch (final DOMDataTreeShardingConflictException e) {
78             LOG.warn("Unable to register shard for: {}.", identifier);
79
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());
83         }
84
85         return future;
86     }
87
88     public ListenableFuture<RpcResult<Void>> onRemovePrefixShard(final RemovePrefixShardInput input) {
89
90         final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
91         final DistributedShardRegistration registration = registrations.get(identifier);
92
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());
97         }
98
99         final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
100
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);
105
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());
109             return null;
110         });
111
112         return future;
113     }
114 }