2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.sharding;
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;
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.AbstractDataStore;
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;
36 * Proxy implementation of a shard that creates forwarding producers to the backend shard.
38 class DistributedShardFrontend implements ReadableWriteableDOMDataTreeShard {
40 private static final Logger LOG = LoggerFactory.getLogger(DistributedShardFrontend.class);
42 private final DataStoreClient client;
43 private final DOMDataTreeIdentifier shardRoot;
45 private final Map<DOMDataTreeIdentifier, ChildShardContext> childShards = new HashMap<>();
47 private final List<ShardProxyProducer> producers = new ArrayList<>();
49 private final DistributedShardChangePublisher publisher;
51 DistributedShardFrontend(final AbstractDataStore distributedDataStore,
52 final DataStoreClient client,
53 final DOMDataTreeIdentifier shardRoot) {
54 this.client = Preconditions.checkNotNull(client);
55 this.shardRoot = Preconditions.checkNotNull(shardRoot);
57 publisher = new DistributedShardChangePublisher(client, Preconditions.checkNotNull(distributedDataStore),
58 shardRoot, childShards);
62 public synchronized DOMDataTreeShardProducer createProducer(final Collection<DOMDataTreeIdentifier> paths) {
63 for (final DOMDataTreeIdentifier prodPrefix : paths) {
64 Preconditions.checkArgument(shardRoot.contains(prodPrefix), "Prefix %s is not contained under shard root",
68 final ShardProxyProducer ret =
69 new ShardProxyProducer(shardRoot, paths, client, createModificationFactory(paths));
75 public synchronized void onChildAttached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
76 LOG.debug("{} : Child shard attached at {}", shardRoot, prefix);
77 Preconditions.checkArgument(child != this, "Attempted to attach child %s onto self", this);
78 addChildShard(prefix, child);
83 public synchronized void onChildDetached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
84 LOG.debug("{} : Child shard detached at {}", shardRoot, prefix);
85 childShards.remove(prefix);
87 // TODO we should grab the dataTreeSnapshot that's in the shard and apply it to this shard
90 private void addChildShard(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
91 Preconditions.checkArgument(child instanceof WriteableDOMDataTreeShard);
92 childShards.put(prefix, new ChildShardContext(prefix, (WriteableDOMDataTreeShard) child));
95 DistributedShardModificationFactory createModificationFactory(final Collection<DOMDataTreeIdentifier> prefixes) {
96 // TODO this could be abstract
97 final Map<DOMDataTreeIdentifier, SubshardProducerSpecification> affectedSubshards = new HashMap<>();
99 for (final DOMDataTreeIdentifier producerPrefix : prefixes) {
100 for (final ChildShardContext maybeAffected : childShards.values()) {
101 final DOMDataTreeIdentifier bindPath;
102 if (producerPrefix.contains(maybeAffected.getPrefix())) {
103 bindPath = maybeAffected.getPrefix();
104 } else if (maybeAffected.getPrefix().contains(producerPrefix)) {
105 // Bound path is inside subshard
106 bindPath = producerPrefix;
111 SubshardProducerSpecification spec = affectedSubshards.computeIfAbsent(maybeAffected.getPrefix(),
112 k -> new SubshardProducerSpecification(maybeAffected));
113 spec.addPrefix(bindPath);
117 final DistributedShardModificationFactoryBuilder builder =
118 new DistributedShardModificationFactoryBuilder(shardRoot);
119 for (final SubshardProducerSpecification spec : affectedSubshards.values()) {
120 final ForeignShardModificationContext foreignContext =
121 new ForeignShardModificationContext(spec.getPrefix(), spec.createProducer());
122 builder.addSubshard(foreignContext);
123 builder.addSubshard(spec.getPrefix(), foreignContext);
126 return builder.build();
129 private void updateProducers() {
130 for (final ShardProxyProducer producer : producers) {
131 producer.setModificationFactory(createModificationFactory(producer.getPrefixes()));
137 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
138 final YangInstanceIdentifier treeId, final L listener) {
139 return publisher.registerTreeChangeListener(treeId, listener);