BUG 2138 - Do not fail on module-based default shard
[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
9 package org.opendaylight.controller.cluster.sharding;
10
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;
15
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;
44
45 /**
46  * Listens on changes on prefix-shard-configuration. Resolves the changes and
47  * notifies handling actor with {@link PrefixShardCreated} and
48  * {@link PrefixShardRemoved} messages.
49  */
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,
57             ListenerRegistration<org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener>> registrations =
58             new EnumMap<>(LogicalDatastoreType.class);
59
60     public PrefixedShardConfigUpdateHandler(final ActorRef handlingActor, final MemberName memberName) {
61         this.handlingActor = Preconditions.checkNotNull(handlingActor);
62         this.memberName = Preconditions.checkNotNull(memberName);
63     }
64
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)));
68     }
69
70     public void close() {
71         registrations.values().forEach(ListenerRegistration::close);
72         registrations.clear();
73     }
74
75     public static final class ShardConfigHandler implements ClusteredDOMDataTreeChangeListener {
76
77         private final MemberName memberName;
78         private final LogicalDatastoreType type;
79         private final ActorRef handlingActor;
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         }
88
89         @Override
90         public void onDataTreeChanged(@Nonnull 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 SUBTREE_MODIFIED:
99                 case APPEARED:
100                 case WRITE:
101                     resolveWrite(candidate.getRootNode());
102                     break;
103                 case DELETE:
104                 case DISAPPEARED:
105                     resolveDelete(candidate.getRootNode());
106                     break;
107                 default:
108                     break;
109             }
110         }
111
112         private void resolveWrite(final DataTreeCandidateNode rootNode) {
113
114             LOG.debug("{}: New config received {}", memberName, rootNode);
115             LOG.debug("{}: Data after: {}", memberName, rootNode.getDataAfter());
116
117             // were in the shards list, iter children and resolve
118             for (final DataTreeCandidateNode childNode : rootNode.getChildNodes()) {
119                 switch (childNode.getModificationType()) {
120                     case UNMODIFIED:
121                         break;
122                     case SUBTREE_MODIFIED:
123                     case APPEARED:
124                     case WRITE:
125                         resolveWrittenShard(childNode);
126                         break;
127                     case DELETE:
128                     case DISAPPEARED:
129                         resolveDeletedShard(childNode);
130                         break;
131                     default:
132                         break;
133                 }
134             }
135         }
136
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", memberName, 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 {}", memberName, retReplicas.toString());
157
158             final PrefixShardConfiguration newConfig =
159                     new PrefixShardConfiguration(new DOMDataTreeIdentifier(type, identifier),
160                             PrefixShardStrategy.NAME, retReplicas);
161
162             LOG.debug("{}: Resulting config {}", memberName, newConfig);
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         private void resolveDelete(final DataTreeCandidateNode rootNode) {
184
185         }
186     }
187 }