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

©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.