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.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;
37 * Proxy implementation of a shard that creates forwarding producers to the backend shard.
39 class DistributedShardFrontend implements ReadableWriteableDOMDataTreeShard {
41 private static final Logger LOG = LoggerFactory.getLogger(DistributedShardFrontend.class);
43 private final DataStoreClient client;
44 private final DOMDataTreeIdentifier shardRoot;
46 private final Map<DOMDataTreeIdentifier, ChildShardContext> childShards = new HashMap<>();
48 private final List<ShardProxyProducer> producers = new ArrayList<>();
49 private final DistributedDataStore distributedDataStore;
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);
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",
66 final ShardProxyProducer ret =
67 new ShardProxyProducer(shardRoot, paths, client, createModificationFactory(paths));
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);
81 public synchronized void onChildDetached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
82 LOG.debug("{} : Child shard detached at {}", shardRoot, prefix);
83 childShards.remove(prefix);
85 // TODO we should grab the dataTreeSnapshot that's in the shard and apply it to this shard
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));
93 DistributedShardModificationFactory createModificationFactory(final Collection<DOMDataTreeIdentifier> prefixes) {
94 // TODO this could be abstract
95 final Map<DOMDataTreeIdentifier, SubshardProducerSpecification> affectedSubshards = new HashMap<>();
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;
109 SubshardProducerSpecification spec = affectedSubshards.get(maybeAffected.getPrefix());
111 spec = new SubshardProducerSpecification(maybeAffected);
112 affectedSubshards.put(maybeAffected.getPrefix(), spec);
114 spec.addPrefix(bindPath);
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);
127 return builder.build();
130 private void updateProducers() {
131 for (final ShardProxyProducer producer : producers) {
132 producer.setModificationFactory(createModificationFactory(producer.getPrefixes()));
138 @SuppressWarnings("unchecked")
139 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
140 final YangInstanceIdentifier treeId, final L listener) {
142 final List<PathArgument> toStrip = new ArrayList<>(shardRoot.getRootIdentifier().getPathArguments());
143 final List<PathArgument> stripFrom = new ArrayList<>(treeId.getPathArguments());
145 while (!toStrip.isEmpty()) {
150 return (ListenerRegistration<L>) new ProxyRegistration(distributedDataStore
151 .registerProxyListener(treeId, YangInstanceIdentifier.create(stripFrom), listener), listener);
154 private static class ProxyRegistration implements ListenerRegistration<DOMDataTreeChangeListener> {
156 private ListenerRegistration<org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener> proxy;
157 private DOMDataTreeChangeListener listener;
159 private ProxyRegistration(
160 final ListenerRegistration<
161 org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener> proxy,
162 final DOMDataTreeChangeListener listener) {
164 this.listener = listener;
168 public DOMDataTreeChangeListener getInstance() {
173 public void close() {