BUG-5280: Add use-tell-based-protocol config knob
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / DistributedShardFrontend.java
1 /*
2  * Copyright (c) 2016 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 com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import javax.annotation.Nonnull;
18 import javax.annotation.concurrent.GuardedBy;
19 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
20 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
24 import org.opendaylight.mdsal.dom.spi.shard.ChildShardContext;
25 import org.opendaylight.mdsal.dom.spi.shard.DOMDataTreeShardProducer;
26 import org.opendaylight.mdsal.dom.spi.shard.ForeignShardModificationContext;
27 import org.opendaylight.mdsal.dom.spi.shard.ReadableWriteableDOMDataTreeShard;
28 import org.opendaylight.mdsal.dom.spi.shard.SubshardProducerSpecification;
29 import org.opendaylight.mdsal.dom.spi.shard.WriteableDOMDataTreeShard;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Proxy implementation of a shard that creates forwarding producers to the backend shard.
38  */
39 class DistributedShardFrontend implements ReadableWriteableDOMDataTreeShard {
40
41     private static final Logger LOG = LoggerFactory.getLogger(DistributedShardFrontend.class);
42
43     private final DataStoreClient client;
44     private final DOMDataTreeIdentifier shardRoot;
45     @GuardedBy("this")
46     private final Map<DOMDataTreeIdentifier, ChildShardContext> childShards = new HashMap<>();
47     @GuardedBy("this")
48     private final List<ShardProxyProducer> producers = new ArrayList<>();
49     private final DistributedDataStore distributedDataStore;
50
51     DistributedShardFrontend(final DistributedDataStore distributedDataStore,
52                              final DataStoreClient client,
53                              final DOMDataTreeIdentifier shardRoot) {
54         this.distributedDataStore = Preconditions.checkNotNull(distributedDataStore);
55         this.client = Preconditions.checkNotNull(client);
56         this.shardRoot = Preconditions.checkNotNull(shardRoot);
57     }
58
59     @Override
60     public synchronized DOMDataTreeShardProducer createProducer(final Collection<DOMDataTreeIdentifier> paths) {
61         for (final DOMDataTreeIdentifier prodPrefix : paths) {
62             Preconditions.checkArgument(paths.contains(prodPrefix), "Prefix %s is not contained under shard root",
63                     prodPrefix, paths);
64         }
65
66         final ShardProxyProducer ret =
67                 new ShardProxyProducer(shardRoot, paths, client, createModificationFactory(paths));
68         producers.add(ret);
69         return ret;
70     }
71
72     @Override
73     public synchronized void onChildAttached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
74         LOG.debug("{} : Child shard attached at {}", shardRoot, prefix);
75         Preconditions.checkArgument(child != this, "Attempted to attach child %s onto self", this);
76         addChildShard(prefix, child);
77         updateProducers();
78     }
79
80     @Override
81     public synchronized void onChildDetached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
82         LOG.debug("{} : Child shard detached at {}", shardRoot, prefix);
83         childShards.remove(prefix);
84         updateProducers();
85         // TODO we should grab the dataTreeSnapshot that's in the shard and apply it to this shard
86     }
87
88     private void addChildShard(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
89         Preconditions.checkArgument(child instanceof WriteableDOMDataTreeShard);
90         childShards.put(prefix, new ChildShardContext(prefix, (WriteableDOMDataTreeShard) child));
91     }
92
93     DistributedShardModificationFactory createModificationFactory(final Collection<DOMDataTreeIdentifier> prefixes) {
94         // TODO this could be abstract
95         final Map<DOMDataTreeIdentifier, SubshardProducerSpecification> affectedSubshards = new HashMap<>();
96
97         for (final DOMDataTreeIdentifier producerPrefix : prefixes) {
98             for (final ChildShardContext maybeAffected : childShards.values()) {
99                 final DOMDataTreeIdentifier bindPath;
100                 if (producerPrefix.contains(maybeAffected.getPrefix())) {
101                     bindPath = maybeAffected.getPrefix();
102                 } else if (maybeAffected.getPrefix().contains(producerPrefix)) {
103                     // Bound path is inside subshard
104                     bindPath = producerPrefix;
105                 } else {
106                     continue;
107                 }
108
109                 SubshardProducerSpecification spec = affectedSubshards.get(maybeAffected.getPrefix());
110                 if (spec == null) {
111                     spec = new SubshardProducerSpecification(maybeAffected);
112                     affectedSubshards.put(maybeAffected.getPrefix(), spec);
113                 }
114                 spec.addPrefix(bindPath);
115             }
116         }
117
118         final DistributedShardModificationFactoryBuilder builder =
119                 new DistributedShardModificationFactoryBuilder(shardRoot);
120         for (final SubshardProducerSpecification spec : affectedSubshards.values()) {
121             final ForeignShardModificationContext foreignContext =
122                     new ForeignShardModificationContext(spec.getPrefix(), spec.createProducer());
123             builder.addSubshard(foreignContext);
124             builder.addSubshard(spec.getPrefix(), foreignContext);
125         }
126
127         return builder.build();
128     }
129
130     private void updateProducers() {
131         for (final ShardProxyProducer producer : producers) {
132             producer.setModificationFactory(createModificationFactory(producer.getPrefixes()));
133         }
134     }
135
136     @Nonnull
137     @Override
138     @SuppressWarnings("unchecked")
139     public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
140             final YangInstanceIdentifier treeId, final L listener) {
141
142         final List<PathArgument> toStrip = new ArrayList<>(shardRoot.getRootIdentifier().getPathArguments());
143         final List<PathArgument> stripFrom = new ArrayList<>(treeId.getPathArguments());
144
145         while (!toStrip.isEmpty()) {
146             stripFrom.remove(0);
147             toStrip.remove(0);
148         }
149
150         return (ListenerRegistration<L>) new ProxyRegistration(distributedDataStore
151                 .registerProxyListener(treeId, YangInstanceIdentifier.create(stripFrom), listener), listener);
152     }
153
154     private static class ProxyRegistration implements ListenerRegistration<DOMDataTreeChangeListener> {
155
156         private ListenerRegistration<org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener> proxy;
157         private DOMDataTreeChangeListener listener;
158
159         private ProxyRegistration(
160                 final ListenerRegistration<
161                         org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener> proxy,
162                 final DOMDataTreeChangeListener listener) {
163             this.proxy = proxy;
164             this.listener = listener;
165         }
166
167         @Override
168         public DOMDataTreeChangeListener getInstance() {
169             return listener;
170         }
171
172         @Override
173         public void close() {
174             proxy.close();
175         }
176     }
177
178 }