cd9f7bd37991bb03ba7861a73371bde03badc4c8
[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 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);
33     @GuardedBy("this")
34     private final Map<DOMDataTreeIdentifier, DOMDataTreeProducer> idToProducer = new TreeMap<>();
35
36     @GuardedBy("this")
37     private ShardingTableEntry lookupShard(final DOMDataTreeIdentifier prefix) {
38         final ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
39         if (t == null) {
40             return null;
41         }
42
43         return t.lookup(prefix.getRootIdentifier());
44     }
45
46     @GuardedBy("this")
47     private void storeShard(final DOMDataTreeIdentifier prefix, final ShardRegistration<?> reg) {
48         ShardingTableEntry t = shardingTables.computeIfAbsent(prefix.getDatastoreType(), k -> new ShardingTableEntry());
49
50         t.store(prefix.getRootIdentifier(), reg);
51     }
52
53     void removeShard(final ShardRegistration<?> reg) {
54         final DOMDataTreeIdentifier prefix = reg.getPrefix();
55         final ShardRegistration<?> parentReg;
56
57         synchronized (this) {
58             final ShardingTableEntry t = shardingTables.get(prefix.getDatastoreType());
59             if (t == null) {
60                 LOG.warn("Shard registration {} points to non-existent table", reg);
61                 return;
62             }
63
64             t.remove(prefix.getRootIdentifier());
65             parentReg = lookupShard(prefix).getRegistration();
66
67             /*
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
70              *        a producer.
71              *
72              */
73         }
74
75         if (parentReg != null) {
76             parentReg.getInstance().onChildDetached(prefix, reg.getInstance());
77         }
78     }
79
80     @Override
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;
84
85         synchronized (this) {
86             /*
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
89              * this registration.
90              */
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()));
95             }
96
97             // FIXME: wrap the shard in a proper adaptor based on implemented interface
98
99             reg = new ShardRegistration<>(this, prefix, shard);
100
101             storeShard(prefix, reg);
102
103             // FIXME: update any producers/registrations
104         }
105
106         // Notify the parent shard
107         if (parentReg != null) {
108             parentReg.getInstance().onChildAttached(prefix, shard);
109         }
110
111         return reg;
112     }
113
114     @GuardedBy("this")
115     private DOMDataTreeProducer findProducer(final DOMDataTreeIdentifier subtree) {
116         for (final Entry<DOMDataTreeIdentifier, DOMDataTreeProducer> e : idToProducer.entrySet()) {
117             if (e.getKey().contains(subtree)) {
118                 return e.getValue();
119             }
120         }
121
122         return null;
123     }
124
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);
130             }
131         }
132     }
133
134     @GuardedBy("this")
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);
140         }
141
142         return ret;
143     }
144
145     @Override
146     public synchronized DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
147         Preconditions.checkArgument(!subtrees.isEmpty(), "Subtrees may not be empty");
148
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);
154
155             shardMap.put(s, lookupShard(s).getRegistration().getInstance());
156         }
157
158         return createProducer(shardMap);
159     }
160
161     synchronized DOMDataTreeProducer createProducer(final ShardedDOMDataTreeProducer parent, final Collection<DOMDataTreeIdentifier> subtrees) {
162         Preconditions.checkNotNull(parent);
163
164         final Map<DOMDataTreeIdentifier, DOMDataTreeShard> shardMap = new HashMap<>();
165         for (final DOMDataTreeIdentifier s : subtrees) {
166             shardMap.put(s, lookupShard(s).getRegistration().getInstance());
167         }
168
169         return createProducer(shardMap);
170     }
171
172     @Override
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.");
176     }
177 }