Deprecate DOMDataTreeProducer-related classes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / PrefixedShardConfigUpdateHandler.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 package org.opendaylight.controller.cluster.sharding;
9
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;
15
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;
43
44 /**
45  * Listens on changes on prefix-shard-configuration. Resolves the changes and
46  * notifies handling actor with {@link PrefixShardCreated} and
47  * {@link PrefixShardRemoved} messages.
48  */
49 @Deprecated(forRemoval = true)
50 public class PrefixedShardConfigUpdateHandler {
51
52     private static final Logger LOG = LoggerFactory.getLogger(PrefixedShardConfigUpdateHandler.class);
53     private final ActorRef handlingActor;
54     private final MemberName memberName;
55
56     private final EnumMap<LogicalDatastoreType,ListenerRegistration<DOMDataTreeChangeListener>> registrations =
57             new EnumMap<>(LogicalDatastoreType.class);
58
59     public PrefixedShardConfigUpdateHandler(final ActorRef handlingActor, final MemberName memberName) {
60         this.handlingActor = requireNonNull(handlingActor);
61         this.memberName = requireNonNull(memberName);
62     }
63
64     public void initListener(final DistributedDataStoreInterface dataStore, final LogicalDatastoreType type) {
65         registrations.put(type, dataStore.registerShardConfigListener(
66                 ClusterUtils.SHARD_LIST_PATH, new ShardConfigHandler(memberName, type, handlingActor)));
67     }
68
69     public void close() {
70         registrations.values().forEach(ListenerRegistration::close);
71         registrations.clear();
72     }
73
74     public static final class ShardConfigHandler implements ClusteredDOMDataTreeChangeListener {
75
76         private final MemberName memberName;
77         private final LogicalDatastoreType type;
78         private final ActorRef handlingActor;
79         private final String logName;
80
81         public ShardConfigHandler(final MemberName memberName,
82                            final LogicalDatastoreType type,
83                            final ActorRef handlingActor) {
84             this.memberName = memberName;
85             this.type = type;
86             this.handlingActor = handlingActor;
87             logName = memberName.getName() + "-" + type;
88         }
89
90         @Override
91         public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
92             changes.forEach(this::resolveChange);
93         }
94
95         private void resolveChange(final DataTreeCandidate candidate) {
96             switch (candidate.getRootNode().getModificationType()) {
97                 case UNMODIFIED:
98                     break;
99                 case APPEARED:
100                 case DELETE:
101                 case DISAPPEARED:
102                 case SUBTREE_MODIFIED:
103                 case WRITE:
104                     resolveModifiedRoot(candidate.getRootNode());
105                     break;
106                 default:
107                     break;
108             }
109         }
110
111         private void resolveModifiedRoot(final DataTreeCandidateNode rootNode) {
112
113             LOG.debug("{}: New config received {}", logName, rootNode);
114             LOG.debug("{}: Data after: {}", logName, rootNode.getDataAfter());
115
116             // were in the shards list, iter children and resolve
117             for (final DataTreeCandidateNode childNode : rootNode.getChildNodes()) {
118                 switch (childNode.getModificationType()) {
119                     case UNMODIFIED:
120                         break;
121                     case SUBTREE_MODIFIED:
122                     case APPEARED:
123                     case WRITE:
124                         resolveWrittenShard(childNode);
125                         break;
126                     case DELETE:
127                     case DISAPPEARED:
128                         resolveDeletedShard(childNode);
129                         break;
130                     default:
131                         break;
132                 }
133             }
134         }
135
136         @SuppressWarnings("unchecked")
137         private void resolveWrittenShard(final DataTreeCandidateNode childNode) {
138             final MapEntryNode entryNode = (MapEntryNode) childNode.getDataAfter().get();
139             final LeafNode<YangInstanceIdentifier> prefix =
140                     (LeafNode<YangInstanceIdentifier>) entryNode.getChild(new NodeIdentifier(SHARD_PREFIX_QNAME)).get();
141
142             final YangInstanceIdentifier identifier = prefix.getValue();
143
144             LOG.debug("{}: Deserialized {} from datastore", logName, identifier);
145
146             final ContainerNode replicas =
147                     (ContainerNode) entryNode.getChild(new NodeIdentifier(SHARD_REPLICAS_QNAME)).get();
148
149             final LeafSetNode<String> replicaList =
150                     (LeafSetNode<String>) replicas.getChild(new NodeIdentifier(SHARD_REPLICA_QNAME)).get();
151
152             final List<MemberName> retReplicas = replicaList.getValue().stream()
153                     .map(child -> MemberName.forName(child.getValue()))
154                     .collect(Collectors.toList());
155
156             LOG.debug("{}: Replicas read from ds {}", logName, retReplicas.toString());
157
158             final PrefixShardConfiguration newConfig =
159                     new PrefixShardConfiguration(new DOMDataTreeIdentifier(type, identifier),
160                             PrefixShardStrategy.NAME, retReplicas);
161
162             LOG.debug("{}: Resulting config {} - sending PrefixShardCreated to {}", logName, newConfig, handlingActor);
163
164             handlingActor.tell(new PrefixShardCreated(newConfig), noSender());
165         }
166
167         private void resolveDeletedShard(final DataTreeCandidateNode childNode) {
168
169             final MapEntryNode entryNode = (MapEntryNode) childNode.getDataBefore().get();
170
171             final LeafNode<YangInstanceIdentifier> prefix =
172                     (LeafNode<YangInstanceIdentifier>) entryNode.getChild(new NodeIdentifier(SHARD_PREFIX_QNAME)).get();
173
174             final YangInstanceIdentifier deleted = prefix.getValue();
175             LOG.debug("{}: Removing shard at {}.", memberName, deleted);
176
177             final DOMDataTreeIdentifier domDataTreeIdentifier = new DOMDataTreeIdentifier(type, deleted);
178             final PrefixShardRemoved message = new PrefixShardRemoved(domDataTreeIdentifier);
179
180             handlingActor.tell(message, noSender());
181         }
182
183         @Override
184         public String toString() {
185             return "ShardConfigHandler [logName=" + logName + ", handlingActor=" + handlingActor + "]";
186         }
187     }
188 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.