9e7596f5561821dc2078c1bd4b080640bf6edd79
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / ShardedDOMDataTree.java
1 /*
2  * Copyright (c) 2015 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.md.sal.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.EnumMap;
13 import java.util.HashMap;
14 import java.util.Map;
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;
29
30 /**
31  * Shared DOM data tree.
32  *
33  * @deprecated Use {@link org.opendaylight.mdsal.dom.broker.ShardedDOMDataTree} instead.
34  */
35 @Deprecated
36 public final class ShardedDOMDataTree implements DOMDataTreeService, DOMDataTreeShardingService {
37     private static final Logger LOG = LoggerFactory.getLogger(ShardedDOMDataTree.class);
38     private final Map<LogicalDatastoreType, ShardingTableEntry> shardingTables = new EnumMap<>(
39             LogicalDatastoreType.class);
40     @GuardedBy("this")
41     private final Map<DOMDataTreeIdentifier, DOMDataTreeProducer> idToProducer = new TreeMap<>();
42
43     @GuardedBy("this")
44     private ShardingTableEntry lookupShard(final DOMDataTreeIdentifier prefix) {
45         final ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
46         if (t == null) {
47             return null;
48         }
49
50         return t.lookup(prefix.getRootIdentifier());
51     }
52
53     @GuardedBy("this")
54     private void storeShard(final DOMDataTreeIdentifier prefix, final ShardRegistration<?> reg) {
55         ShardingTableEntry shardingTableEntry = shardingTables
56                 .computeIfAbsent(prefix.getDatastoreType(), k -> new ShardingTableEntry());
57
58         shardingTableEntry.store(prefix.getRootIdentifier(), reg);
59     }
60
61     void removeShard(final ShardRegistration<?> reg) {
62         final DOMDataTreeIdentifier prefix = reg.getPrefix();
63         final ShardRegistration<?> parentReg;
64
65         synchronized (this) {
66             final ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
67             if (t == null) {
68                 LOG.warn("Shard registration {} points to non-existent table", reg);
69                 return;
70             }
71
72             t.remove(prefix.getRootIdentifier());
73             parentReg = lookupShard(prefix).getRegistration();
74
75             /*
76              * FIXME: adjust all producers. This is tricky, as we need different locking strategy,
77              *        simply because we risk AB/BA deadlock with a producer being split off from
78              *        a producer.
79              *
80              */
81         }
82
83         if (parentReg != null) {
84             parentReg.getInstance().onChildDetached(prefix, reg.getInstance());
85         }
86     }
87
88     @Override
89     public <T extends DOMDataTreeShard> ListenerRegistration<T> registerDataTreeShard(
90             final DOMDataTreeIdentifier prefix, final T shard) throws DOMDataTreeShardingConflictException {
91         final ShardRegistration<T> reg;
92         final ShardRegistration<?> parentReg;
93
94         synchronized (this) {
95             /*
96              * Lookup the parent shard (e.g. the one which currently matches the prefix),
97              * and if it exists, check if its registration prefix does not collide with
98              * this registration.
99              */
100             final ShardingTableEntry parent = lookupShard(prefix);
101             parentReg = parent.getRegistration();
102             if (parentReg != null && prefix.equals(parentReg.getPrefix())) {
103                 throw new DOMDataTreeShardingConflictException(
104                         String.format("Prefix %s is already occupied by shard %s", prefix, parentReg.getInstance()));
105             }
106
107             // FIXME: wrap the shard in a proper adaptor based on implemented interface
108
109             reg = new ShardRegistration<>(this, prefix, shard);
110
111             storeShard(prefix, reg);
112
113             // FIXME: update any producers/registrations
114         }
115
116         // Notify the parent shard
117         if (parentReg != null) {
118             parentReg.getInstance().onChildAttached(prefix, shard);
119         }
120
121         return reg;
122     }
123
124     @GuardedBy("this")
125     private DOMDataTreeProducer findProducer(final DOMDataTreeIdentifier subtree) {
126         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : idToProducer.entrySet()) {
127             if (e.getKey().contains(subtree)) {
128                 return e.getValue();
129             }
130         }
131
132         return null;
133     }
134
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);
140             }
141         }
142     }
143
144     @GuardedBy("this")
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 (DOMDataTreeIdentifier s : shardMap.keySet()) {
149             idToProducer.put(s, ret);
150         }
151
152         return ret;
153     }
154
155     @Override
156     public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
157         Preconditions.checkArgument(!subtrees.isEmpty(), "Subtrees may not be empty");
158
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);
164
165             shardMap.put(s, lookupShard(s).getRegistration().getInstance());
166         }
167
168         return createProducer(shardMap);
169     }
170
171     synchronized DOMDataTreeProducer createProducer(final ShardedDOMDataTreeProducer parent,
172                                                     final Collection<DOMDataTreeIdentifier> subtrees) {
173         Preconditions.checkNotNull(parent);
174
175         final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
176         for (final DOMDataTreeIdentifier s : subtrees) {
177             shardMap.put(s, lookupShard(s).getRegistration().getInstance());
178         }
179
180         return createProducer(shardMap);
181     }
182
183     @Override
184     public synchronized <T extends DOMDataTreeListener> ListenerRegistration<T>
185         registerListener(final T listener,
186                          final Collection<DOMDataTreeIdentifier> subtrees,
187                          final boolean allowRxMerges,
188                          final Collection<DOMDataTreeProducer> producers) {
189         // FIXME Implement this.
190         throw new UnsupportedOperationException("Not implemented yet.");
191     }
192 }