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 com.google.common.base.Preconditions;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
18 * Definition / implementation of specific Identifier Namespace behaviour.
20 * Namespace behaviour is build on top of tree of {@link NamespaceStorageNode} which represents local context of one of
21 * types defined in {@link StorageNodeType}.
23 * For common behaviour models please use static factories {@link #global(Class)}, {@link #sourceLocal(Class)} and
24 * {@link #treeScoped(Class)}.
33 public abstract class NamespaceBehaviour<K, V, N extends IdentifierNamespace<K, V>> implements Identifiable<Class<N>> {
35 public enum StorageNodeType {
36 GLOBAL, SOURCE_LOCAL_SPECIAL, STATEMENT_LOCAL,
39 public interface Registry {
40 <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> type);
43 public interface NamespaceStorageNode {
45 StorageNodeType getStorageNodeType();
48 NamespaceStorageNode getParentNamespaceStorage();
51 <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key);
54 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(Class<N> type);
57 <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value);
61 private final Class<N> identifier;
63 protected NamespaceBehaviour(Class<N> identifier) {
64 this.identifier = Preconditions.checkNotNull(identifier);
69 * Creates global namespace behaviour for supplied namespace type.
71 * Global behaviour stores and loads all values from root {@link NamespaceStorageNode} with type of
72 * {@link StorageNodeType#GLOBAL}.
75 * Namespace identifier.
76 * @param <K> type parameter
77 * @param <V> type parameter
78 * @param <N> type parameter
80 * @return global namespace behaviour for supplied namespace type.
82 public static @Nonnull <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> global(
83 Class<N> identifier) {
84 return new StorageSpecific<>(identifier, StorageNodeType.GLOBAL);
89 * Creates source-local namespace behaviour for supplied namespace type.
91 * Source-local namespace behaviour stores and loads all values from closest {@link NamespaceStorageNode} ancestor
92 * with type of {@link StorageNodeType#SOURCE_LOCAL_SPECIAL}.
95 * Namespace identifier.
96 * @param <K> type parameter
97 * @param <V> type parameter
98 * @param <N> type parameter
100 * @return source-local namespace behaviour for supplied namespace type.
102 public static <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> sourceLocal(
103 Class<N> identifier) {
104 return new StorageSpecific<>(identifier, StorageNodeType.SOURCE_LOCAL_SPECIAL);
109 * Creates tree-scoped namespace behaviour for supplied namespace type.
111 * Tree-scoped namespace behaviour search for value in all storage nodes up to the root and stores values in
115 * Namespace identifier. *
116 * @param <K> type parameter
117 * @param <V> type parameter
118 * @param <N> type parameter
120 * @return tree-scoped namespace behaviour for supplied namespace type.
122 public static <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> treeScoped(Class<N> identifier) {
123 return new TreeScoped<>(identifier);
127 * returns value from model namespace storage according to key param class
129 * @param storage namespace storage
130 * @param key type parameter
132 * @return value from model namespace storage according to key param class
134 public abstract V getFrom(NamespaceStorageNode storage, K key);
137 * returns all values of a keys of param class from model namespace storage
139 * @param storage namespace storage
141 * @return all values of keys of param class from model namespace storage
143 public abstract Map<K, V> getAllFrom(NamespaceStorageNode storage);
146 * adds key and value to corresponding namespace storage according to param class
148 * @param storage namespace storage
149 * @param key type parameter
150 * @param value type parameter
152 public abstract void addTo(NamespaceStorageNode storage, K key, V value);
155 public Class<N> getIdentifier() {
159 protected final V getFromLocalStorage(NamespaceStorageNode storage, K key) {
160 return storage.getFromLocalStorage(getIdentifier(), key);
163 protected final Map<K, V> getAllFromLocalStorage(NamespaceStorageNode storage) {
164 return storage.getAllFromLocalStorage(getIdentifier());
167 protected final void addToStorage(NamespaceStorageNode storage, K key, V value) {
168 storage.addToLocalStorage(getIdentifier(), key, value);
171 static class StorageSpecific<K, V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
173 StorageNodeType storageType;
175 public StorageSpecific(Class<N> identifier, StorageNodeType type) {
177 storageType = Preconditions.checkNotNull(type);
181 public V getFrom(final NamespaceStorageNode storage, final K key) {
182 NamespaceStorageNode current = storage;
183 while (current.getStorageNodeType() != storageType) {
184 current = current.getParentNamespaceStorage();
186 return getFromLocalStorage(current, key);
190 public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
191 NamespaceStorageNode current = storage;
192 while (current.getStorageNodeType() != storageType) {
193 current = current.getParentNamespaceStorage();
196 return getAllFromLocalStorage(current);
200 public void addTo(NamespaceBehaviour.NamespaceStorageNode storage, K key, V value) {
201 NamespaceStorageNode current = storage;
202 while (current.getStorageNodeType() != storageType) {
203 current = current.getParentNamespaceStorage();
205 addToStorage(current, key, value);
210 static class TreeScoped<K, V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
212 public TreeScoped(Class<N> identifier) {
217 public V getFrom(final NamespaceStorageNode storage, final K key) {
218 NamespaceStorageNode current = storage;
219 while (current != null) {
220 final V val = getFromLocalStorage(current, key);
224 current = current.getParentNamespaceStorage();
230 public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
231 NamespaceStorageNode current = storage;
232 while (current != null) {
233 final Map<K, V> val = getAllFromLocalStorage(current);
237 current = current.getParentNamespaceStorage();
243 public void addTo(NamespaceStorageNode storage, K key, V value) {
244 addToStorage(storage, key, value);