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
8 package org.opendaylight.controller.cluster.sharding;
10 import static akka.actor.ActorRef.noSender;
11 import static java.util.Objects.requireNonNull;
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 java.util.Collection;
18 import java.util.EnumMap;
19 import java.util.List;
20 import java.util.stream.Collectors;
21 import org.opendaylight.controller.cluster.access.concepts.MemberName;
22 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
23 import org.opendaylight.controller.cluster.datastore.config.PrefixShardConfiguration;
24 import org.opendaylight.controller.cluster.datastore.shardstrategy.PrefixShardStrategy;
25 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
26 import org.opendaylight.controller.cluster.sharding.messages.PrefixShardCreated;
27 import org.opendaylight.controller.cluster.sharding.messages.PrefixShardRemoved;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Listens on changes on prefix-shard-configuration. Resolves the changes and
46 * notifies handling actor with {@link PrefixShardCreated} and
47 * {@link PrefixShardRemoved} messages.
49 public class PrefixedShardConfigUpdateHandler {
51 private static final Logger LOG = LoggerFactory.getLogger(PrefixedShardConfigUpdateHandler.class);
52 private final ActorRef handlingActor;
53 private final MemberName memberName;
55 private final EnumMap<LogicalDatastoreType,ListenerRegistration<DOMDataTreeChangeListener>> registrations =
56 new EnumMap<>(LogicalDatastoreType.class);
58 public PrefixedShardConfigUpdateHandler(final ActorRef handlingActor, final MemberName memberName) {
59 this.handlingActor = requireNonNull(handlingActor);
60 this.memberName = requireNonNull(memberName);
63 public void initListener(final DistributedDataStoreInterface dataStore, final LogicalDatastoreType type) {
64 registrations.put(type, dataStore.registerShardConfigListener(
65 ClusterUtils.SHARD_LIST_PATH, new ShardConfigHandler(memberName, type, handlingActor)));
69 registrations.values().forEach(ListenerRegistration::close);
70 registrations.clear();
73 public static final class ShardConfigHandler implements ClusteredDOMDataTreeChangeListener {
75 private final MemberName memberName;
76 private final LogicalDatastoreType type;
77 private final ActorRef handlingActor;
78 private final String logName;
80 public ShardConfigHandler(final MemberName memberName,
81 final LogicalDatastoreType type,
82 final ActorRef handlingActor) {
83 this.memberName = memberName;
85 this.handlingActor = handlingActor;
86 logName = memberName.getName() + "-" + type;
90 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
91 changes.forEach(this::resolveChange);
94 private void resolveChange(final DataTreeCandidate candidate) {
95 switch (candidate.getRootNode().getModificationType()) {
101 case SUBTREE_MODIFIED:
103 resolveModifiedRoot(candidate.getRootNode());
110 private void resolveModifiedRoot(final DataTreeCandidateNode rootNode) {
112 LOG.debug("{}: New config received {}", logName, rootNode);
113 LOG.debug("{}: Data after: {}", logName, rootNode.getDataAfter());
115 // were in the shards list, iter children and resolve
116 for (final DataTreeCandidateNode childNode : rootNode.getChildNodes()) {
117 switch (childNode.getModificationType()) {
120 case SUBTREE_MODIFIED:
123 resolveWrittenShard(childNode);
127 resolveDeletedShard(childNode);
135 @SuppressWarnings("unchecked")
136 private void resolveWrittenShard(final DataTreeCandidateNode childNode) {
137 final MapEntryNode entryNode = (MapEntryNode) childNode.getDataAfter().get();
138 final LeafNode<YangInstanceIdentifier> prefix =
139 (LeafNode<YangInstanceIdentifier>) entryNode.getChild(new NodeIdentifier(SHARD_PREFIX_QNAME)).get();
141 final YangInstanceIdentifier identifier = prefix.getValue();
143 LOG.debug("{}: Deserialized {} from datastore", logName, identifier);
145 final ContainerNode replicas =
146 (ContainerNode) entryNode.getChild(new NodeIdentifier(SHARD_REPLICAS_QNAME)).get();
148 final LeafSetNode<String> replicaList =
149 (LeafSetNode<String>) replicas.getChild(new NodeIdentifier(SHARD_REPLICA_QNAME)).get();
151 final List<MemberName> retReplicas = replicaList.getValue().stream()
152 .map(child -> MemberName.forName(child.getValue()))
153 .collect(Collectors.toList());
155 LOG.debug("{}: Replicas read from ds {}", logName, retReplicas.toString());
157 final PrefixShardConfiguration newConfig =
158 new PrefixShardConfiguration(new DOMDataTreeIdentifier(type, identifier),
159 PrefixShardStrategy.NAME, retReplicas);
161 LOG.debug("{}: Resulting config {} - sending PrefixShardCreated to {}", logName, newConfig, handlingActor);
163 handlingActor.tell(new PrefixShardCreated(newConfig), noSender());
166 private void resolveDeletedShard(final DataTreeCandidateNode childNode) {
168 final MapEntryNode entryNode = (MapEntryNode) childNode.getDataBefore().get();
170 final LeafNode<YangInstanceIdentifier> prefix =
171 (LeafNode<YangInstanceIdentifier>) entryNode.getChild(new NodeIdentifier(SHARD_PREFIX_QNAME)).get();
173 final YangInstanceIdentifier deleted = prefix.getValue();
174 LOG.debug("{}: Removing shard at {}.", memberName, deleted);
176 final DOMDataTreeIdentifier domDataTreeIdentifier = new DOMDataTreeIdentifier(type, deleted);
177 final PrefixShardRemoved message = new PrefixShardRemoved(domDataTreeIdentifier);
179 handlingActor.tell(message, noSender());
183 public String toString() {
184 return "ShardConfigHandler [logName=" + logName + ", handlingActor=" + handlingActor + "]";