Deprecate DOMDataTreeProducer-related classes
[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 package org.opendaylight.controller.cluster.sharding;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import org.checkerframework.checker.lock.qual.GuardedBy;
19 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
20 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
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.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Proxy implementation of a shard that creates forwarding producers to the backend shard.
37  */
38 @Deprecated(forRemoval = true)
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
50     private final DistributedShardChangePublisher publisher;
51
52     DistributedShardFrontend(final DistributedDataStoreInterface distributedDataStore,
53                              final DataStoreClient client,
54                              final DOMDataTreeIdentifier shardRoot) {
55         this.client = requireNonNull(client);
56         this.shardRoot = requireNonNull(shardRoot);
57
58         publisher = new DistributedShardChangePublisher(client, requireNonNull(distributedDataStore), shardRoot,
59             childShards);
60     }
61
62     @Override
63     public synchronized DOMDataTreeShardProducer createProducer(final Collection<DOMDataTreeIdentifier> paths) {
64         for (final DOMDataTreeIdentifier prodPrefix : paths) {
65             checkArgument(shardRoot.contains(prodPrefix), "Prefix %s is not contained under shard root", prodPrefix,
66                 paths);
67         }
68
69         final ShardProxyProducer ret =
70                 new ShardProxyProducer(shardRoot, paths, client, createModificationFactory(paths));
71         producers.add(ret);
72         return ret;
73     }
74
75     @Override
76     public synchronized void onChildAttached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
77         LOG.debug("{} : Child shard attached at {}", shardRoot, prefix);
78         checkArgument(child != this, "Attempted to attach child %s onto self", this);
79         addChildShard(prefix, child);
80         updateProducers();
81     }
82
83     @Override
84     public synchronized void onChildDetached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
85         LOG.debug("{} : Child shard detached at {}", shardRoot, prefix);
86         childShards.remove(prefix);
87         updateProducers();
88         // TODO we should grab the dataTreeSnapshot that's in the shard and apply it to this shard
89     }
90
91     private void addChildShard(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
92         checkArgument(child instanceof WriteableDOMDataTreeShard);
93         childShards.put(prefix, new ChildShardContext(prefix, (WriteableDOMDataTreeShard) child));
94     }
95
96     DistributedShardModificationFactory createModificationFactory(final Collection<DOMDataTreeIdentifier> prefixes) {
97         // TODO this could be abstract
98         final Map<DOMDataTreeIdentifier, SubshardProducerSpecification> affectedSubshards = new HashMap<>();
99
100         for (final DOMDataTreeIdentifier producerPrefix : prefixes) {
101             for (final ChildShardContext maybeAffected : childShards.values()) {
102                 final DOMDataTreeIdentifier bindPath;
103                 if (producerPrefix.contains(maybeAffected.getPrefix())) {
104                     bindPath = maybeAffected.getPrefix();
105                 } else if (maybeAffected.getPrefix().contains(producerPrefix)) {
106                     // Bound path is inside subshard
107                     bindPath = producerPrefix;
108                 } else {
109                     continue;
110                 }
111
112                 SubshardProducerSpecification spec = affectedSubshards.computeIfAbsent(maybeAffected.getPrefix(),
113                     k -> new SubshardProducerSpecification(maybeAffected));
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     @Override
137     public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
138             final YangInstanceIdentifier treeId, final L listener) {
139         return publisher.registerTreeChangeListener(treeId, listener);
140     }
141 }

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