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.cluster.sharding;
11 import static akka.actor.ActorRef.noSender;
12 import static org.opendaylight.controller.cluster.datastore.utils.ClusterUtils.SHARD_PREFIX_QNAME;
13 import static org.opendaylight.controller.cluster.datastore.utils.ClusterUtils.SHARD_REPLICAS_QNAME;
14 import static org.opendaylight.controller.cluster.datastore.utils.ClusterUtils.SHARD_REPLICA_QNAME;
16 import akka.actor.ActorRef;
17 import com.google.common.base.Preconditions;
18 import java.util.Collection;
19 import java.util.EnumMap;
20 import java.util.List;
21 import java.util.stream.Collectors;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.controller.cluster.access.concepts.MemberName;
24 import org.opendaylight.controller.cluster.datastore.AbstractDataStore;
25 import org.opendaylight.controller.cluster.datastore.config.PrefixShardConfiguration;
26 import org.opendaylight.controller.cluster.datastore.shardstrategy.PrefixShardStrategy;
27 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
28 import org.opendaylight.controller.cluster.sharding.messages.PrefixShardCreated;
29 import org.opendaylight.controller.cluster.sharding.messages.PrefixShardRemoved;
30 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Listens on changes on prefix-shard-configuration. Resolves the changes and
47 * notifies handling actor with {@link PrefixShardCreated} and
48 * {@link PrefixShardRemoved} messages.
50 public class PrefixedShardConfigUpdateHandler {
52 private static final Logger LOG = LoggerFactory.getLogger(PrefixedShardConfigUpdateHandler.class);
53 private final ActorRef handlingActor;
54 private final MemberName memberName;
56 private final EnumMap<LogicalDatastoreType,
57 ListenerRegistration<org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener>> registrations =
58 new EnumMap<>(LogicalDatastoreType.class);
60 public PrefixedShardConfigUpdateHandler(final ActorRef handlingActor, final MemberName memberName) {
61 this.handlingActor = Preconditions.checkNotNull(handlingActor);
62 this.memberName = Preconditions.checkNotNull(memberName);
65 public void initListener(final AbstractDataStore dataStore, final LogicalDatastoreType type) {
66 registrations.put(type, dataStore.registerShardConfigListener(
67 ClusterUtils.SHARD_LIST_PATH, new ShardConfigHandler(memberName, type, handlingActor)));
71 registrations.values().forEach(ListenerRegistration::close);
72 registrations.clear();
75 public static final class ShardConfigHandler implements ClusteredDOMDataTreeChangeListener {
77 private final MemberName memberName;
78 private final LogicalDatastoreType type;
79 private final ActorRef handlingActor;
80 private final String logName;
82 public ShardConfigHandler(final MemberName memberName,
83 final LogicalDatastoreType type,
84 final ActorRef handlingActor) {
85 this.memberName = memberName;
87 this.handlingActor = handlingActor;
88 logName = memberName.getName() + "-" + type;
92 public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
93 changes.forEach(this::resolveChange);
96 private void resolveChange(final DataTreeCandidate candidate) {
97 switch (candidate.getRootNode().getModificationType()) {
100 case SUBTREE_MODIFIED:
103 resolveWrite(candidate.getRootNode());
107 resolveDelete(candidate.getRootNode());
114 private void resolveWrite(final DataTreeCandidateNode rootNode) {
116 LOG.debug("{}: New config received {}", logName, rootNode);
117 LOG.debug("{}: Data after: {}", logName, rootNode.getDataAfter());
119 // were in the shards list, iter children and resolve
120 for (final DataTreeCandidateNode childNode : rootNode.getChildNodes()) {
121 switch (childNode.getModificationType()) {
124 case SUBTREE_MODIFIED:
127 resolveWrittenShard(childNode);
131 resolveDeletedShard(childNode);
139 @SuppressWarnings("unchecked")
140 private void resolveWrittenShard(final DataTreeCandidateNode childNode) {
141 final MapEntryNode entryNode = (MapEntryNode) childNode.getDataAfter().get();
142 final LeafNode<YangInstanceIdentifier> prefix =
143 (LeafNode<YangInstanceIdentifier>) entryNode.getChild(new NodeIdentifier(SHARD_PREFIX_QNAME)).get();
145 final YangInstanceIdentifier identifier = prefix.getValue();
147 LOG.debug("{}: Deserialized {} from datastore", logName, identifier);
149 final ContainerNode replicas =
150 (ContainerNode) entryNode.getChild(new NodeIdentifier(SHARD_REPLICAS_QNAME)).get();
152 final LeafSetNode<String> replicaList =
153 (LeafSetNode<String>) replicas.getChild(new NodeIdentifier(SHARD_REPLICA_QNAME)).get();
155 final List<MemberName> retReplicas = replicaList.getValue().stream()
156 .map(child -> MemberName.forName(child.getValue()))
157 .collect(Collectors.toList());
159 LOG.debug("{}: Replicas read from ds {}", logName, retReplicas.toString());
161 final PrefixShardConfiguration newConfig =
162 new PrefixShardConfiguration(new DOMDataTreeIdentifier(type, identifier),
163 PrefixShardStrategy.NAME, retReplicas);
165 LOG.debug("{}: Resulting config {} - sending PrefixShardCreated to {}", logName, newConfig, handlingActor);
167 handlingActor.tell(new PrefixShardCreated(newConfig), noSender());
170 private void resolveDeletedShard(final DataTreeCandidateNode childNode) {
172 final MapEntryNode entryNode = (MapEntryNode) childNode.getDataBefore().get();
174 final LeafNode<YangInstanceIdentifier> prefix =
175 (LeafNode<YangInstanceIdentifier>) entryNode.getChild(new NodeIdentifier(SHARD_PREFIX_QNAME)).get();
177 final YangInstanceIdentifier deleted = prefix.getValue();
178 LOG.debug("{}: Removing shard at {}.", memberName, deleted);
180 final DOMDataTreeIdentifier domDataTreeIdentifier = new DOMDataTreeIdentifier(type, deleted);
181 final PrefixShardRemoved message = new PrefixShardRemoved(domDataTreeIdentifier);
183 handlingActor.tell(message, noSender());
186 private void resolveDelete(final DataTreeCandidateNode rootNode) {
191 public String toString() {
192 return "ShardConfigHandler [logName=" + logName + ", handlingActor=" + handlingActor + "]";