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.controller.md.sal.dom.broker.impl;
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.EnumMap;
13 import java.util.HashMap;
15 import java.util.Map.Entry;
16 import java.util.TreeMap;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeListener;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeProducer;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeService;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeShard;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeShardingConflictException;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeShardingService;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTreeShardingService {
31 private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTree.class);
32 private final Map<LogicalDatastoreType, ShardingTableEntry> shardingTables = new EnumMap<>(LogicalDatastoreType.class);
34 private final Map<DOMDataTreeIdentifier, DOMDataTreeProducer> idToProducer = new TreeMap<>();
37 private ShardingTableEntry lookupShard(final DOMDataTreeIdentifier prefix) {
38 final ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
43 return t.lookup(prefix.getRootIdentifier());
47 private void storeShard(final DOMDataTreeIdentifier prefix, final ShardRegistration<?> reg) {
48 ShardingTableEntry t = shardingTables.computeIfAbsent(prefix.getDatastoreType(), k -> new ShardingTableEntry());
50 t.store(prefix.getRootIdentifier(), reg);
53 void removeShard(final ShardRegistration<?> reg) {
54 final DOMDataTreeIdentifier prefix = reg.getPrefix();
55 final ShardRegistration<?> parentReg;
58 final ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
60 LOG.warn("Shard registration {} points to non-existent table", reg);
64 t.remove(prefix.getRootIdentifier());
65 parentReg = lookupShard(prefix).getRegistration();
68 * FIXME: adjust all producers. This is tricky, as we need different locking strategy,
69 * simply because we risk AB/BA deadlock with a producer being split off from
75 if (parentReg != null) {
76 parentReg.getInstance().onChildDetached(prefix, reg.getInstance());
81 public <T extends DOMDataTreeShard> ListenerRegistration<T> registerDataTreeShard(final DOMDataTreeIdentifier prefix, final T shard) throws DOMDataTreeShardingConflictException {
82 final ShardRegistration<T> reg;
83 final ShardRegistration<?> parentReg;
87 * Lookup the parent shard (e.g. the one which currently matches the prefix),
88 * and if it exists, check if its registration prefix does not collide with
91 final ShardingTableEntry parent = lookupShard(prefix);
92 parentReg = parent.getRegistration();
93 if (parentReg != null && prefix.equals(parentReg.getPrefix())) {
94 throw new DOMDataTreeShardingConflictException(String.format("Prefix %s is already occupied by shard %s", prefix, parentReg.getInstance()));
97 // FIXME: wrap the shard in a proper adaptor based on implemented interface
99 reg = new ShardRegistration<>(this, prefix, shard);
101 storeShard(prefix, reg);
103 // FIXME: update any producers/registrations
106 // Notify the parent shard
107 if (parentReg != null) {
108 parentReg.getInstance().onChildAttached(prefix, shard);
115 private DOMDataTreeProducer findProducer(final DOMDataTreeIdentifier subtree) {
116 for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : idToProducer.entrySet()) {
117 if (e.getKey().contains(subtree)) {
125 synchronized void destroyProducer(final ShardedDOMDataTreeProducer producer) {
126 for (final DOMDataTreeIdentifier s : producer.getSubtrees()) {
127 final DOMDataTreeProducer r = idToProducer.remove(s);
128 if (!producer.equals(r)) {
129 LOG.error("Removed producer {} on subtree {} while removing {}", r, s, producer);
135 private DOMDataTreeProducer createProducer(final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap) {
136 // Record the producer's attachment points
137 final DOMDataTreeProducer ret = ShardedDOMDataTreeProducer.create(this, shardMap);
138 for (DOMDataTreeIdentifier s : shardMap.keySet()) {
139 idToProducer.put(s, ret);
146 public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
147 Preconditions.checkArgument(!subtrees.isEmpty(), "Subtrees may not be empty");
149 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
150 for (final DOMDataTreeIdentifier s : subtrees) {
151 // Attempting to create a disconnected producer -- all subtrees have to be unclaimed
152 final DOMDataTreeProducer producer = findProducer(s);
153 Preconditions.checkArgument(producer == null, "Subtree %s is attached to producer %s", s, producer);
155 shardMap.put(s, lookupShard(s).getRegistration().getInstance());
158 return createProducer(shardMap);
161 synchronized DOMDataTreeProducer createProducer(final ShardedDOMDataTreeProducer parent, final Collection<DOMDataTreeIdentifier> subtrees) {
162 Preconditions.checkNotNull(parent);
164 final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
165 for (final DOMDataTreeIdentifier s : subtrees) {
166 shardMap.put(s, lookupShard(s).getRegistration().getInstance());
169 return createProducer(shardMap);
173 public synchronized <T extends DOMDataTreeListener> ListenerRegistration<T> registerListener(final T listener, final Collection<DOMDataTreeIdentifier> subtrees, final boolean allowRxMerges, final Collection<DOMDataTreeProducer> producers) {
174 // FIXME Implement this.
175 throw new UnsupportedOperationException("Not implemented yet.");