Convert sal-distributed-datastore to OSGi DS
[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 class DistributedShardFrontend implements ReadableWriteableDOMDataTreeShard {
39
40     private static final Logger LOG = LoggerFactory.getLogger(DistributedShardFrontend.class);
41
42     private final DataStoreClient client;
43     private final DOMDataTreeIdentifier shardRoot;
44     @GuardedBy("this")
45     private final Map<DOMDataTreeIdentifier, ChildShardContext> childShards = new HashMap<>();
46     @GuardedBy("this")
47     private final List<ShardProxyProducer> producers = new ArrayList<>();
48
49     private final DistributedShardChangePublisher publisher;
50
51     DistributedShardFrontend(final DistributedDataStoreInterface distributedDataStore,
52                              final DataStoreClient client,
53                              final DOMDataTreeIdentifier shardRoot) {
54         this.client = requireNonNull(client);
55         this.shardRoot = requireNonNull(shardRoot);
56
57         publisher = new DistributedShardChangePublisher(client, requireNonNull(distributedDataStore), shardRoot,
58             childShards);
59     }
60
61     @Override
62     public synchronized DOMDataTreeShardProducer createProducer(final Collection<DOMDataTreeIdentifier> paths) {
63         for (final DOMDataTreeIdentifier prodPrefix : paths) {
64             checkArgument(shardRoot.contains(prodPrefix), "Prefix %s is not contained under shard root", prodPrefix,
65                 paths);
66         }
67
68         final ShardProxyProducer ret =
69                 new ShardProxyProducer(shardRoot, paths, client, createModificationFactory(paths));
70         producers.add(ret);
71         return ret;
72     }
73
74     @Override
75     public synchronized void onChildAttached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
76         LOG.debug("{} : Child shard attached at {}", shardRoot, prefix);
77         checkArgument(child != this, "Attempted to attach child %s onto self", this);
78         addChildShard(prefix, child);
79         updateProducers();
80     }
81
82     @Override
83     public synchronized void onChildDetached(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
84         LOG.debug("{} : Child shard detached at {}", shardRoot, prefix);
85         childShards.remove(prefix);
86         updateProducers();
87         // TODO we should grab the dataTreeSnapshot that's in the shard and apply it to this shard
88     }
89
90     private void addChildShard(final DOMDataTreeIdentifier prefix, final DOMDataTreeShard child) {
91         checkArgument(child instanceof WriteableDOMDataTreeShard);
92         childShards.put(prefix, new ChildShardContext(prefix, (WriteableDOMDataTreeShard) child));
93     }
94
95     DistributedShardModificationFactory createModificationFactory(final Collection<DOMDataTreeIdentifier> prefixes) {
96         // TODO this could be abstract
97         final Map<DOMDataTreeIdentifier, SubshardProducerSpecification> affectedSubshards = new HashMap<>();
98
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;
107                 } else {
108                     continue;
109                 }
110
111                 SubshardProducerSpecification spec = affectedSubshards.computeIfAbsent(maybeAffected.getPrefix(),
112                     k -> new SubshardProducerSpecification(maybeAffected));
113                 spec.addPrefix(bindPath);
114             }
115         }
116
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);
124         }
125
126         return builder.build();
127     }
128
129     private void updateProducers() {
130         for (final ShardProxyProducer producer : producers) {
131             producer.setModificationFactory(createModificationFactory(producer.getPrefixes()));
132         }
133     }
134
135     @Override
136     public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
137             final YangInstanceIdentifier treeId, final L listener) {
138         return publisher.registerTreeChangeListener(treeId, listener);
139     }
140 }

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