2 * Copyright (c) 2015 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
8 package org.opendaylight.mdsal.dom.broker;
10 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
12 import com.google.common.base.Preconditions;
13 import java.util.Collection;
14 import java.util.EnumMap;
15 import java.util.HashMap;
17 import java.util.Map.Entry;
18 import java.util.TreeMap;
19 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingService;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTreeShardingService {
32 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTree.class);
33 private final Map<LogicalDatastoreType, ShardingTableEntry> shardingTables = new EnumMap<>(LogicalDatastoreType.class);
35 private final Map<DOMDataTreeIdentifier, DOMDataTreeProducer> idToProducer = new TreeMap<>();
38 private ShardingTableEntry lookupShard(final DOMDataTreeIdentifier prefix) {
39 final ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
44 return t.lookup(prefix.getRootIdentifier());
48 private void storeShard(final DOMDataTreeIdentifier prefix, final ShardRegistration<?> reg) {
49 ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
51 t = new ShardingTableEntry();
52 shardingTables.put(prefix.getDatastoreType(), t);
55 t.store(prefix.getRootIdentifier(), reg);
58 void removeShard(final ShardRegistration<?> reg) {
59 final DOMDataTreeIdentifier prefix = reg.getPrefix();
60 final ShardRegistration<?> parentReg;
63 final ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
65 LOG.warn("Shard registration {} points to non-existent table", reg);
69 t.remove(prefix.getRootIdentifier());
70 parentReg = lookupShard(prefix).getRegistration();
73 * FIXME: adjust all producers. This is tricky, as we need different locking strategy,
74 * simply because we risk AB/BA deadlock with a producer being split off from
80 if (parentReg != null) {
81 parentReg.getInstance().onChildDetached(prefix, reg.getInstance());
86 public <T extends DOMDataTreeShard> ListenerRegistration<T> registerDataTreeShard(final DOMDataTreeIdentifier prefix, final T shard) throws DOMDataTreeShardingConflictException {
87 final ShardRegistration<T> reg;
88 final ShardRegistration<?> parentReg;
92 * Lookup the parent shard (e.g. the one which currently matches the prefix),
93 * and if it exists, check if its registration prefix does not collide with
96 final ShardingTableEntry parent = lookupShard(prefix);
98 parentReg = parent.getRegistration();
99 if (parentReg != null && prefix.equals(parentReg.getPrefix())) {
100 throw new DOMDataTreeShardingConflictException(String.format(
101 "Prefix %s is already occupied by shard %s", prefix, parentReg.getInstance()));
107 // FIXME: wrap the shard in a proper adaptor based on implemented interface
109 reg = new ShardRegistration<T>(this, prefix, shard);
111 storeShard(prefix, reg);
113 // FIXME: update any producers/registrations
116 // Notify the parent shard
117 if (parentReg != null) {
118 parentReg.getInstance().onChildAttached(prefix, shard);
125 private DOMDataTreeProducer findProducer(final DOMDataTreeIdentifier subtree) {
126 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : idToProducer.entrySet()) {
127 if (e.getKey().contains(subtree)) {
135 synchronized void destroyProducer(final ShardedDOMDataTreeProducer producer) {
136 for (final DOMDataTreeIdentifier s : producer.getSubtrees()) {
137 final DOMDataTreeProducer r = idToProducer.remove(s);
138 if (!producer.equals(r)) {
139 LOG.error("Removed producer {} on subtree {} while removing {}", r, s, producer);
145 private DOMDataTreeProducer createProducer(final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
146 // Record the producer's attachment points
147 final DOMDataTreeProducer ret = ShardedDOMDataTreeProducer.create(this, shardMap);
148 for (final DOMDataTreeIdentifier s : shardMap.keySet()) {
149 idToProducer.put(s, ret);
156 public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
157 Preconditions.checkArgument(!subtrees.isEmpty(), "Subtrees may not be empty");
159 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
160 for (final DOMDataTreeIdentifier s : subtrees) {
161 // Attempting to create a disconnected producer -- all subtrees have to be unclaimed
162 final DOMDataTreeProducer producer = findProducer(s);
163 Preconditions.checkArgument(producer == null, "Subtree %s is attached to producer %s", s, producer);
165 shardMap.put(s, lookupShard(s).getRegistration().getInstance());
168 return createProducer(shardMap);
171 synchronized DOMDataTreeProducer createProducer(final ShardedDOMDataTreeProducer parent, final Collection<DOMDataTreeIdentifier> subtrees) {
172 Preconditions.checkNotNull(parent);
174 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
175 for (final DOMDataTreeIdentifier s : subtrees) {
176 shardMap.put(s, lookupShard(s).getRegistration().getInstance());
179 return createProducer(shardMap);
183 public synchronized <T extends DOMDataTreeListener> ListenerRegistration<T> registerListener(final T listener, final Collection<DOMDataTreeIdentifier> subtrees, final boolean allowRxMerges, final Collection<DOMDataTreeProducer> producers) {
184 // FIXME Implement this.
185 throw new UnsupportedOperationException("Not implemented yet.");