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.AbstractSimpleIdentifiable;
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.stmt.SchemaTreeAwareEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
25 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
28 * Definition / implementation of specific Identifier Namespace behaviour. A namespace behaviour is built on top
29 * of a tree of {@link NamespaceStorageNode} which represents local context of one of types defined
30 * n {@link StorageNodeType}.
33 * For common behaviour models please use static factories {@link #global(Class)}, {@link #sourceLocal(Class)} and
34 * {@link #treeScoped(Class)}.
37 * @param <V> Value type
38 * @param <N> Namespace Type
40 public abstract class NamespaceBehaviour<K, V, N extends ParserNamespace<K, V>>
41 extends AbstractSimpleIdentifiable<Class<N>> {
43 public enum StorageNodeType {
45 * Global storage, visible from all sources.
49 * Storage of the root statement of a particular source and any sources it is importing.
51 // FIXME: 7.0.0: this is a misnomer and should be renamed
54 * Storage of a single statement.
58 * Storage of the root statement of a particular source.
63 public interface Registry {
65 * Get a namespace behavior.
67 * @param type Namespace type class
69 * @param <V> value type
70 * @param <N> namespace type
71 * @return Namespace behaviour
72 * @throws NamespaceNotAvailableException when the namespace is not available
74 <K, V, N extends ParserNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> type);
77 public interface NamespaceStorageNode {
79 * Return local namespace behaviour type.
81 * @return local namespace behaviour type {@link NamespaceBehaviour}
83 StorageNodeType getStorageNodeType();
85 @Nullable NamespaceStorageNode getParentNamespaceStorage();
87 <K, V, N extends ParserNamespace<K, V>> @Nullable V getFromLocalStorage(Class<N> type, K key);
89 <K, V, N extends ParserNamespace<K, V>> @Nullable Map<K, V> getAllFromLocalStorage(Class<N> type);
92 * Populate specified namespace with a key/value pair, overwriting previous contents. Similar to
93 * {@link Map#put(Object, Object)}.
95 * @param type Namespace identifier
98 * @return Previously-stored value, or null if the key was not present
100 <K, V, N extends ParserNamespace<K, V>> @Nullable V putToLocalStorage(Class<N> type, K key, V value);
103 * Populate specified namespace with a key/value pair unless the key is already associated with a value. Similar
104 * to {@link Map#putIfAbsent(Object, Object)}.
106 * @param type Namespace identifier
109 * @return Preexisting value or null if there was no previous mapping
111 <K, V, N extends ParserNamespace<K, V>> @Nullable V putToLocalStorageIfAbsent(Class<N> type, K key, V value);
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 tractable. 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.
132 * The results produced by this method are expected to be consistent with
133 * {@link SchemaTreeAwareEffectiveStatement#findSchemaTreeNode(QName)} and
134 * {@link SchemaTreeNamespace#getFrom(NamespaceStorageNode, QName)}.
136 * @param qname node identifier of the child being requested
137 * @return Requested child, if it is present.
138 * @throws NullPointerException in {@code qname} is null
140 <D extends DeclaredStatement<QName>, E extends SchemaTreeEffectiveStatement<D>>
141 @Nullable StmtContext<QName, D, E> requestSchemaTreeChild(QName qname);
144 protected NamespaceBehaviour(final Class<N> identifier) {
149 * Creates a global namespace behaviour for supplied namespace type. Global behaviour stores and loads all values
150 * from root {@link NamespaceStorageNode} with type of {@link StorageNodeType#GLOBAL}.
152 * @param identifier Namespace identifier.
153 * @param <K> type parameter
154 * @param <V> type parameter
155 * @param <N> type parameter
156 * @return global namespace behaviour for supplied namespace type.
158 public static <K, V, N extends ParserNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> global(
159 final Class<N> identifier) {
160 return new StorageSpecific<>(identifier, StorageNodeType.GLOBAL);
164 * Creates source-local namespace behaviour for supplied namespace type. Source-local namespace behaviour stores
165 * and loads all values from closest {@link NamespaceStorageNode} ancestor with type
166 * of {@link StorageNodeType#SOURCE_LOCAL_SPECIAL}.
168 * @param identifier Namespace identifier.
169 * @param <K> type parameter
170 * @param <V> type parameter
171 * @param <N> type parameter
172 * @return source-local namespace behaviour for supplied namespace type.
174 public static <K, V, N extends ParserNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> sourceLocal(
175 final Class<N> identifier) {
176 return new StorageSpecific<>(identifier, StorageNodeType.SOURCE_LOCAL_SPECIAL);
179 public static <K, V, N extends ParserNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> statementLocal(
180 final Class<N> identifier) {
181 return new StatementLocal<>(identifier);
185 * Creates a root-statement-local namespace behaviour for supplied namespace type. Root-statement-local namespace
186 * behaviour stores and loads all values from closest {@link NamespaceStorageNode} ancestor with type
187 * of {@link StorageNodeType#ROOT_STATEMENT_LOCAL}.
189 * @param identifier Namespace identifier.
190 * @param <K> type parameter
191 * @param <V> type parameter
192 * @param <N> type parameter
193 * @return root-statement-local namespace behaviour for supplied namespace type.
195 public static <K, V, N extends ParserNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> rootStatementLocal(
196 final Class<N> identifier) {
197 return new StorageSpecific<>(identifier, StorageNodeType.ROOT_STATEMENT_LOCAL);
201 * Creates tree-scoped namespace behaviour for supplied namespace type. Tree-scoped namespace behaviour searches
202 * for value in all storage nodes up to the root and stores values in supplied node.
205 * Namespace identifier.
206 * @param <K> type parameter
207 * @param <V> type parameter
208 * @param <N> type parameter
209 * @return tree-scoped namespace behaviour for supplied namespace type.
211 public static <K, V, N extends ParserNamespace<K, V>> @NonNull NamespaceBehaviour<K, V, N> treeScoped(
212 final Class<N> identifier) {
213 return new TreeScoped<>(identifier);
217 * Returns a value from model namespace storage according to key param class.
219 * @param storage namespace storage
220 * @param key type parameter
221 * @return value from model namespace storage according to key param class
223 public abstract V getFrom(NamespaceStorageNode storage, K key);
226 * Returns the key/value mapping best matching specified criterion.
228 * @param storage namespace storage
229 * @param criterion selection criterion
230 * @return Selected mapping, if available.
232 public final Optional<Entry<K, V>> getFrom(final NamespaceStorageNode storage,
233 final NamespaceKeyCriterion<K> criterion) {
234 final Map<K, V> mappings = getAllFrom(storage);
235 if (mappings == null) {
236 return Optional.empty();
239 Entry<K, V> match = null;
240 for (Entry<K, V> entry : mappings.entrySet()) {
241 final K key = entry.getKey();
242 if (criterion.match(key)) {
244 final K selected = criterion.select(match.getKey(), key);
245 if (selected.equals(match.getKey())) {
249 Verify.verify(selected == key, "Criterion %s selected invalid key %s from candidates [%s %s]",
250 selected, match.getKey(), key);
257 return Optional.ofNullable(match);
261 * Returns all values of a keys of param class from model namespace storage.
263 * @param storage namespace storage
264 * @return all values of keys of param class from model namespace storage
266 public abstract Map<K, V> getAllFrom(NamespaceStorageNode storage);
269 * Adds a key/value to corresponding namespace storage according to param class.
271 * @param storage namespace storage
272 * @param key type parameter
273 * @param value type parameter
275 public abstract void addTo(NamespaceStorageNode storage, K key, V value);
277 protected final V getFromLocalStorage(final NamespaceStorageNode storage, final K key) {
278 return storage.getFromLocalStorage(getIdentifier(), key);
281 protected final Map<K, V> getAllFromLocalStorage(final NamespaceStorageNode storage) {
282 return storage.getAllFromLocalStorage(getIdentifier());
285 protected final void addToStorage(final NamespaceStorageNode storage, final K key, final V value) {
286 storage.putToLocalStorage(getIdentifier(), key, value);
289 abstract static class AbstractSpecific<K, V, N extends ParserNamespace<K, V>>
290 extends NamespaceBehaviour<K, V, N> {
291 AbstractSpecific(final Class<N> identifier) {
296 public final V getFrom(final NamespaceStorageNode storage, final K key) {
297 return getFromLocalStorage(findStorageNode(storage), key);
301 public final Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
302 return getAllFromLocalStorage(findStorageNode(storage));
306 public final void addTo(final NamespaceStorageNode storage, final K key, final V value) {
307 addToStorage(findStorageNode(storage), key, value);
310 abstract NamespaceStorageNode findStorageNode(NamespaceStorageNode storage);
313 static final class StatementLocal<K, V, N extends ParserNamespace<K, V>> extends AbstractSpecific<K, V, N> {
314 StatementLocal(final Class<N> identifier) {
319 NamespaceStorageNode findStorageNode(final NamespaceStorageNode storage) {
324 static final class StorageSpecific<K, V, N extends ParserNamespace<K, V>> extends AbstractSpecific<K, V, N> {
325 private final StorageNodeType storageType;
327 StorageSpecific(final Class<N> identifier, final StorageNodeType type) {
329 storageType = requireNonNull(type);
333 NamespaceStorageNode findStorageNode(final NamespaceStorageNode storage) {
334 return findClosestTowardsRoot(storage, storageType);
338 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
339 return super.addToStringAttributes(helper.add("type", storageType));
343 static final class TreeScoped<K, V, N extends ParserNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
344 TreeScoped(final Class<N> identifier) {
349 public V getFrom(final NamespaceStorageNode storage, final K key) {
350 NamespaceStorageNode current = storage;
351 while (current != null) {
352 final V val = getFromLocalStorage(current, key);
356 current = current.getParentNamespaceStorage();
362 public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
363 NamespaceStorageNode current = storage;
364 while (current != null) {
365 final Map<K, V> val = getAllFromLocalStorage(current);
369 current = current.getParentNamespaceStorage();
375 public void addTo(final NamespaceStorageNode storage, final K key, final V value) {
376 addToStorage(storage, key, value);
381 protected static NamespaceStorageNode findClosestTowardsRoot(final NamespaceStorageNode storage,
382 final StorageNodeType type) {
383 NamespaceStorageNode current = storage;
384 while (current != null && current.getStorageNodeType() != type) {
385 current = current.getParentNamespaceStorage();
391 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
392 return helper.add("identifier", getIdentifier().getName());