Adjust to mdsal DOM read/exists FluentFuture change
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / PrefixedShardConfigWriter.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.cluster.sharding;
10
11 import com.google.common.util.concurrent.AsyncFunction;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Collection;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.controller.cluster.access.concepts.MemberName;
18 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
19 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
20 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
21 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
22 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetEntryNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Writes and removes prefix-based shards' configuration
44  * to prefix-shard-configuration. This classed is meant to be utilized
45  * by {@link DistributedShardedDOMDataTree} for updating
46  * prefix-shard-configuration upon creating and de-spawning prefix-based shards.
47  */
48 class PrefixedShardConfigWriter {
49
50     private static final Logger LOG = LoggerFactory.getLogger(PrefixedShardConfigWriter.class);
51
52     private final ClientLocalHistory history;
53
54     PrefixedShardConfigWriter(final DataStoreClient client) {
55         history = client.createLocalHistory();
56         writeInitialParent();
57     }
58
59     ListenableFuture<Void> writeConfig(final YangInstanceIdentifier path, final Collection<MemberName> replicas) {
60         LOG.debug("Writing config for {}, replicas {}", path, replicas);
61
62         return doSubmit(doWrite(path, replicas));
63     }
64
65     ListenableFuture<Void> removeConfig(final YangInstanceIdentifier path) {
66         LOG.debug("Removing config for {}.", path);
67
68         return doSubmit(doDelete(path));
69     }
70
71     private void writeInitialParent() {
72         final ClientTransaction tx = history.createTransaction();
73
74         final DOMDataTreeWriteCursor cursor = tx.openCursor();
75
76         final ContainerNode root = ImmutableContainerNodeBuilder.create()
77                 .withNodeIdentifier(new NodeIdentifier(ClusterUtils.PREFIX_SHARDS_QNAME))
78                 .withChild(ImmutableMapNodeBuilder.create()
79                         .withNodeIdentifier(new NodeIdentifier(ClusterUtils.SHARD_LIST_QNAME))
80                         .build())
81                 .build();
82
83         cursor.merge(ClusterUtils.PREFIX_SHARDS_PATH.getLastPathArgument(), root);
84         cursor.close();
85
86         final DOMStoreThreePhaseCommitCohort cohort = tx.ready();
87
88         submitBlocking(cohort);
89     }
90
91     private static void submitBlocking(final DOMStoreThreePhaseCommitCohort cohort) {
92         try {
93             doSubmit(cohort).get();
94         } catch (final InterruptedException | ExecutionException e) {
95             LOG.error("Unable to write initial shard config parent.", e);
96         }
97     }
98
99     private static ListenableFuture<Void> doSubmit(final DOMStoreThreePhaseCommitCohort cohort) {
100         final AsyncFunction<Boolean, Void> validateFunction = input -> cohort.preCommit();
101         final AsyncFunction<Void, Void> prepareFunction = input -> cohort.commit();
102
103         final ListenableFuture<Void> prepareFuture = Futures.transformAsync(cohort.canCommit(), validateFunction,
104             MoreExecutors.directExecutor());
105         return Futures.transformAsync(prepareFuture, prepareFunction, MoreExecutors.directExecutor());
106     }
107
108     boolean checkDefaultIsPresent() {
109         final NodeIdentifierWithPredicates pag =
110                 new NodeIdentifierWithPredicates(ClusterUtils.SHARD_LIST_QNAME, ClusterUtils.SHARD_PREFIX_QNAME,
111                 YangInstanceIdentifier.EMPTY);
112
113         final YangInstanceIdentifier defaultId = ClusterUtils.SHARD_LIST_PATH.node(pag);
114
115         final ClientSnapshot snapshot = history.takeSnapshot();
116         try {
117             return snapshot.exists(defaultId).get();
118         } catch (InterruptedException | ExecutionException e) {
119             LOG.error("Presence check of default shard in configuration failed.", e);
120             return false;
121         } finally {
122             snapshot.abort();
123         }
124     }
125
126     private DOMStoreThreePhaseCommitCohort doWrite(final YangInstanceIdentifier path,
127                                                    final Collection<MemberName> replicas) {
128
129         final ListNodeBuilder<Object, LeafSetEntryNode<Object>> replicaListBuilder =
130                 ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(
131                         new NodeIdentifier(ClusterUtils.SHARD_REPLICA_QNAME));
132
133         replicas.forEach(name -> replicaListBuilder.withChild(
134                 ImmutableLeafSetEntryNodeBuilder.create()
135                         .withNodeIdentifier(new NodeWithValue<>(ClusterUtils.SHARD_REPLICA_QNAME, name.getName()))
136                         .withValue(name.getName())
137                         .build()));
138
139         final MapEntryNode newEntry = ImmutableMapEntryNodeBuilder.create()
140                 .withNodeIdentifier(
141                         new NodeIdentifierWithPredicates(ClusterUtils.SHARD_LIST_QNAME, ClusterUtils.SHARD_PREFIX_QNAME,
142                                 path))
143                 .withChild(ImmutableLeafNodeBuilder.create()
144                         .withNodeIdentifier(new NodeIdentifier(ClusterUtils.SHARD_PREFIX_QNAME))
145                         .withValue(path)
146                         .build())
147                 .withChild(ImmutableContainerNodeBuilder.create()
148                         .withNodeIdentifier(new NodeIdentifier(ClusterUtils.SHARD_REPLICAS_QNAME))
149                         .withChild(replicaListBuilder.build())
150                         .build())
151                 .build();
152
153         final ClientTransaction tx = history.createTransaction();
154         final DOMDataTreeWriteCursor cursor = tx.openCursor();
155
156         ClusterUtils.SHARD_LIST_PATH.getPathArguments().forEach(cursor::enter);
157
158         cursor.write(newEntry.getIdentifier(), newEntry);
159         cursor.close();
160
161         return tx.ready();
162     }
163
164     private DOMStoreThreePhaseCommitCohort doDelete(final YangInstanceIdentifier path) {
165
166         final ClientTransaction tx = history.createTransaction();
167         final DOMDataTreeWriteCursor cursor = tx.openCursor();
168
169         ClusterUtils.SHARD_LIST_PATH.getPathArguments().forEach(cursor::enter);
170
171         cursor.delete(
172                 new NodeIdentifierWithPredicates(ClusterUtils.SHARD_LIST_QNAME, ClusterUtils.SHARD_PREFIX_QNAME, path));
173         cursor.close();
174
175         return tx.ready();
176     }
177 }