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;
11 import javax.annotation.Nonnull;
12 import javax.annotation.Nullable;
13 import org.opendaylight.yangtools.concepts.Identifiable;
14 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
17 * Definition / implementation of specific Identifier Namespace behaviour.
19 * Namespace behaviour is build on top of tree of {@link NamespaceStorageNode}
20 * which represents local context of one of types defined in {@link StorageNodeType}.
22 * For common behaviour models please use static factories {@link #global(Class)},
23 * {@link #sourceLocal(Class)} and {@link #treeScoped(Class)}.
26 * @param <V> Value type
27 * @param <N> Namespace Type
29 public abstract class NamespaceBehaviour<K,V, N extends IdentifierNamespace<K, V>> implements Identifiable<Class<N>>{
31 public enum StorageNodeType {
37 public interface Registry {
38 <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> type);
41 public interface NamespaceStorageNode {
43 StorageNodeType getStorageNodeType();
45 @Nullable NamespaceStorageNode getParentNamespaceStorage();
47 @Nullable <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key);
49 @Nullable <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value);
53 private final Class<N> identifier;
55 protected NamespaceBehaviour(Class<N> identifier) {
56 this.identifier = Preconditions.checkNotNull(identifier);
61 * Creates global namespace behaviour for supplied namespace type.
63 * Global behaviour stores and loads all values from root {@link NamespaceStorageNode}
64 * with type of {@link StorageNodeType#GLOBAL}.
66 * @param identifier Namespace identifier.
67 * @return global namespace behaviour for supplied namespace type.
69 public static @Nonnull <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> global(Class<N> identifier) {
70 return new StorageSpecific<>(identifier, StorageNodeType.GLOBAL);
75 * Creates source-local namespace behaviour for supplied namespace type.
77 * Source-local namespace behaviour stores and loads all values from closest
78 * {@link NamespaceStorageNode} ancestor with type of
79 * {@link StorageNodeType#SOURCE_LOCAL_SPECIAL}.
81 * @param identifier Namespace identifier.
82 * @return source-local namespace behaviour for supplied namespace type.
84 public static <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> sourceLocal(Class<N> identifier) {
85 return new StorageSpecific<>(identifier, StorageNodeType.SOURCE_LOCAL_SPECIAL);
90 * Creates tree-scoped namespace behaviour for supplied namespace type.
92 * Tree-scoped namespace behaviour search for value in all storage nodes
93 * up to the root and stores values in supplied node.
95 * @param identifier Namespace identifier.
96 * @return tree-scoped namespace behaviour for supplied namespace type.
98 public static <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> treeScoped(Class<N> identifier) {
99 return new TreeScoped<>(identifier);
102 public abstract V getFrom(NamespaceStorageNode storage, K key);
104 public abstract void addTo(NamespaceStorageNode storage,K key,V value);
107 public Class<N> getIdentifier() {
111 protected final V getFromLocalStorage(NamespaceStorageNode storage, K key) {
112 return storage.getFromLocalStorage(getIdentifier(), key);
115 protected final void addToStorage(NamespaceStorageNode storage,K key,V value) {
116 storage.addToLocalStorage(getIdentifier(),key,value);
119 static class StorageSpecific<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
121 StorageNodeType storageType;
123 public StorageSpecific(Class<N> identifier, StorageNodeType type) {
125 storageType = Preconditions.checkNotNull(type);
129 public V getFrom(final NamespaceStorageNode storage, final K key) {
130 NamespaceStorageNode current = storage;
131 while(current.getStorageNodeType() != storageType) {
132 current = current.getParentNamespaceStorage();
134 return getFromLocalStorage(current,key);
138 public void addTo(NamespaceBehaviour.NamespaceStorageNode storage, K key, V value) {
139 NamespaceStorageNode current = storage;
140 while(current.getStorageNodeType() != storageType) {
141 current = current.getParentNamespaceStorage();
143 addToStorage(current, key, value);
148 static class TreeScoped<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
150 public TreeScoped(Class<N> identifier) {
155 public V getFrom(final NamespaceStorageNode storage, final K key) {
156 NamespaceStorageNode current = storage;
157 while(current != null) {
158 final V val = getFromLocalStorage(current, key);
162 current = current.getParentNamespaceStorage();
168 // public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
169 // NamespaceStorageNode current = storage;
170 // while(current != null) {
171 // final Map<K, V> val = getAllFromLocalStorage(current);
175 // current = current.getParentNamespaceStorage();
181 public void addTo(NamespaceStorageNode storage,K key, V value) {
182 addToStorage(storage, key, value);