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.yangtools.yang.parser.spi.meta;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.base.Verify;
16 import java.util.Map.Entry;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.yangtools.concepts.AbstractIdentifiable;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
27 * Definition / implementation of specific Identifier Namespace behaviour. A namespace behaviour is built on top
28 * of a tree of {@link NamespaceStorageNode} which represents local context of one of types defined
29 * n {@link StorageNodeType}.
32 * For common behaviour models please use static factories {@link #global(Class)}, {@link #sourceLocal(Class)} and
33 * {@link #treeScoped(Class)}.
36 * @param <V> Value type
37 * @param <N> Namespace Type
39 public abstract class NamespaceBehaviour<K, V, N extends IdentifierNamespace<K, V>>
40 extends AbstractIdentifiable<Class<N>> {
42 public enum StorageNodeType {
44 * Global storage, visible from all sources.
48 * Storage of the root statement of a particular source and any sources it is importing.
50 // FIXME: 7.0.0: this is a misnomer and should be renamed
53 * Storage of a single statement.
57 * Storage of the root statement of a particular source.
62 public interface Registry {
64 * Get a namespace behavior.
66 * @param type Namespace type class
68 * @param <V> value type
69 * @param <N> namespace type
70 * @return Namespace behaviour
71 * @throws NamespaceNotAvailableException when the namespace is not available
73 <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> type);
76 public interface NamespaceStorageNode {
78 * Return local namespace behaviour type.
80 * @return local namespace behaviour type {@link NamespaceBehaviour}
82 StorageNodeType getStorageNodeType();
84 @Nullable NamespaceStorageNode getParentNamespaceStorage();
86 <K, V, N extends IdentifierNamespace<K, V>> @Nullable V getFromLocalStorage(Class<N> type, K key);
88 <K, V, N extends IdentifierNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(Class<N> type);
91 * Populate specified namespace with a key/value pair, overwriting previous contents. Similar to
92 * {@link Map#put(Object, Object)}.
94 * @param type Namespace identifier
97 * @return Previously-stored value, or null if the key was not present
99 <K, V, N extends IdentifierNamespace<K, V>> @Nullable V putToLocalStorage(Class<N> type, K key, V value);
102 * Populate specified namespace with a key/value pair unless the key is already associated with a value. Similar
103 * to {@link Map#putIfAbsent(Object, Object)}.
105 * @param type Namespace identifier
108 * @return Preexisting value or null if there was no previous mapping
110 <K, V, N extends IdentifierNamespace<K, V>> @Nullable V putToLocalStorageIfAbsent(Class<N> type, K key,
115 * Interface implemented by {@link NamespaceStorageNode}s which support dynamic addition of child elements as they
116 * are requested. This means that such a node can, defer creation of child namespace storage nodes, in effect lazily
117 * expanding this namespace on an if-needed basis.
120 public interface OnDemandSchemaTreeStorageNode extends NamespaceStorageNode {
122 * Request that a new member of this node's schema tree statement be added. Implementations are required to
123 * perform lookup in their internal structure and create a child if tracktable. Resulting node is expected to
124 * have been registered with local storage, so that it is accessible through
125 * {@link #getFromLocalStorage(Class, Object)}.
128 * This method must not change its mind about a child's presence -- once it returns non-present, it has to be
129 * always returning non-present.
131 * @param qname node identifier of the child being requested
132 * @return Requested child, if it is present.
134 <D extends DeclaredStatement<QName>, E extends EffectiveStatement<QName, D>>
135 @Nullable StmtContext<QName, D, E> requestSchemaTreeChild(QName qname);
138 protected NamespaceBehaviour(final Class<N> identifier) {
143 * Creates a global namespace behaviour for supplied namespace type. Global behaviour stores and loads all values
144 * from root {@link NamespaceStorageNode} with type of {@link StorageNodeType#GLOBAL}.
146 * @param identifier Namespace identifier.
147 * @param <K> type parameter
148 * @param <V> type parameter
149 * @param <N> type parameter
150 * @return global namespace behaviour for supplied namespace type.
152 public static <K, V, N extends IdentifierNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> global(
153 final Class<N> identifier) {
154 return new StorageSpecific<>(identifier, StorageNodeType.GLOBAL);
158 * Creates source-local namespace behaviour for supplied namespace type. Source-local namespace behaviour stores
159 * and loads all values from closest {@link NamespaceStorageNode} ancestor with type
160 * of {@link StorageNodeType#SOURCE_LOCAL_SPECIAL}.
162 * @param identifier Namespace identifier.
163 * @param <K> type parameter
164 * @param <V> type parameter
165 * @param <N> type parameter
166 * @return source-local namespace behaviour for supplied namespace type.
168 public static <K, V, N extends IdentifierNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> sourceLocal(
169 final Class<N> identifier) {
170 return new StorageSpecific<>(identifier, StorageNodeType.SOURCE_LOCAL_SPECIAL);
173 public static <K, V, N extends IdentifierNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> statementLocal(
174 final Class<N> identifier) {
175 return new StorageSpecific<>(identifier, StorageNodeType.STATEMENT_LOCAL);
179 * Creates a root-statement-local namespace behaviour for supplied namespace type. Root-statement-local namespace
180 * behaviour stores and loads all values from closest {@link NamespaceStorageNode} ancestor with type
181 * of {@link StorageNodeType#ROOT_STATEMENT_LOCAL}.
183 * @param identifier Namespace identifier.
184 * @param <K> type parameter
185 * @param <V> type parameter
186 * @param <N> type parameter
187 * @return root-statement-local namespace behaviour for supplied namespace type.
189 public static <K, V, N extends IdentifierNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> rootStatementLocal(
190 final Class<N> identifier) {
191 return new StorageSpecific<>(identifier, StorageNodeType.ROOT_STATEMENT_LOCAL);
195 * Creates tree-scoped namespace behaviour for supplied namespace type. Tree-scoped namespace behaviour searches
196 * for value in all storage nodes up to the root and stores values in supplied node.
199 * Namespace identifier.
200 * @param <K> type parameter
201 * @param <V> type parameter
202 * @param <N> type parameter
203 * @return tree-scoped namespace behaviour for supplied namespace type.
205 public static <K, V, N extends IdentifierNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> treeScoped(
206 final Class<N> identifier) {
207 return new TreeScoped<>(identifier);
211 * Returns a value from model namespace storage according to key param class.
213 * @param storage namespace storage
214 * @param key type parameter
215 * @return value from model namespace storage according to key param class
217 public abstract V getFrom(NamespaceStorageNode storage, K key);
220 * Returns the key/value mapping best matching specified criterion.
222 * @param storage namespace storage
223 * @param criterion selection criterion
224 * @return Selected mapping, if available.
226 public final Optional<Entry<K, V>> getFrom(final NamespaceStorageNode storage,
227 final NamespaceKeyCriterion<K> criterion) {
228 final Map<K, V> mappings = getAllFrom(storage);
229 if (mappings == null) {
230 return Optional.empty();
233 Entry<K, V> match = null;
234 for (Entry<K, V> entry : mappings.entrySet()) {
235 final K key = entry.getKey();
236 if (criterion.match(key)) {
238 final K selected = criterion.select(match.getKey(), key);
239 if (selected.equals(match.getKey())) {
243 Verify.verify(selected == key, "Criterion %s selected invalid key %s from candidates [%s %s]",
244 selected, match.getKey(), key);
251 return Optional.ofNullable(match);
255 * Returns all values of a keys of param class from model namespace storage.
257 * @param storage namespace storage
258 * @return all values of keys of param class from model namespace storage
260 public abstract Map<K, V> getAllFrom(NamespaceStorageNode storage);
263 * Adds a key/value to corresponding namespace storage according to param class.
265 * @param storage namespace storage
266 * @param key type parameter
267 * @param value type parameter
269 public abstract void addTo(NamespaceStorageNode storage, K key, V value);
271 protected final V getFromLocalStorage(final NamespaceStorageNode storage, final K key) {
272 return storage.getFromLocalStorage(getIdentifier(), key);
275 protected final Map<K, V> getAllFromLocalStorage(final NamespaceStorageNode storage) {
276 return storage.getAllFromLocalStorage(getIdentifier());
279 protected final void addToStorage(final NamespaceStorageNode storage, final K key, final V value) {
280 storage.putToLocalStorage(getIdentifier(), key, value);
283 static final class StorageSpecific<K, V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
284 StorageNodeType storageType;
286 StorageSpecific(final Class<N> identifier, final StorageNodeType type) {
288 storageType = requireNonNull(type);
292 public V getFrom(final NamespaceStorageNode storage, final K key) {
293 NamespaceStorageNode current = findClosestTowardsRoot(storage, storageType);
294 return getFromLocalStorage(current, key);
298 public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
299 NamespaceStorageNode current = storage;
300 while (current.getStorageNodeType() != storageType) {
301 current = current.getParentNamespaceStorage();
304 return getAllFromLocalStorage(current);
308 public void addTo(final NamespaceBehaviour.NamespaceStorageNode storage, final K key, final V value) {
309 NamespaceStorageNode current = findClosestTowardsRoot(storage, storageType);
310 addToStorage(current, key, value);
314 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
315 return super.addToStringAttributes(helper.add("type", storageType));
319 static final class TreeScoped<K, V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
321 TreeScoped(final Class<N> identifier) {
326 public V getFrom(final NamespaceStorageNode storage, final K key) {
327 NamespaceStorageNode current = storage;
328 while (current != null) {
329 final V val = getFromLocalStorage(current, key);
333 current = current.getParentNamespaceStorage();
339 public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
340 NamespaceStorageNode current = storage;
341 while (current != null) {
342 final Map<K, V> val = getAllFromLocalStorage(current);
346 current = current.getParentNamespaceStorage();
352 public void addTo(final NamespaceStorageNode storage, final K key, final V value) {
353 addToStorage(storage, key, value);
358 protected static NamespaceStorageNode findClosestTowardsRoot(final NamespaceStorageNode storage,
359 final StorageNodeType type) {
360 NamespaceStorageNode current = storage;
361 while (current != null && current.getStorageNodeType() != type) {
362 current = current.getParentNamespaceStorage();
368 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
369 return helper.add("identifier", getIdentifier().getName());