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 {
39 abstract <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> type);
43 public interface NamespaceStorageNode {
45 StorageNodeType getStorageNodeType();
47 @Nullable NamespaceStorageNode getParentNamespaceStorage();
49 @Nullable <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key);
51 @Nullable <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value);
55 private final Class<N> identifier;
58 protected NamespaceBehaviour(Class<N> identifier) {
59 this.identifier = Preconditions.checkNotNull(identifier);
64 * Creates global namespace behaviour for supplied namespace type.
66 * Global behaviour stores and loads all values from root {@link NamespaceStorageNode}
67 * with type of {@link StorageNodeType#Global}.
69 * @param identifier Namespace identifier.
70 * @return global namespace behaviour for supplied namespace type.
72 public static @Nonnull <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> global(Class<N> identifier) {
73 return new StorageSpecific<>(identifier, StorageNodeType.Global);
78 * Creates source-local namespace behaviour for supplied namespace type.
80 * Source-local namespace behaviour stores and loads all values from closest
81 * {@link NamespaceStorageNode} ancestor with type of
82 * {@link StorageNodeType#SourceLocalSpecial}.
84 * @param identifier Namespace identifier.
85 * @return source-local namespace behaviour for supplied namespace type.
87 public static <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> sourceLocal(Class<N> identifier) {
88 return new StorageSpecific<>(identifier, StorageNodeType.SourceLocalSpecial);
93 * Creates tree-scoped namespace behaviour for supplied namespace type.
95 * Tree-scoped namespace behaviour search for value in all storage nodes
96 * up to the root and stores values in supplied node.
98 * @param identifier Namespace identifier.
99 * @return tree-scoped namespace behaviour for supplied namespace type.
101 public static <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> treeScoped(Class<N> identifier) {
102 return new TreeScoped<>(identifier);
105 public abstract V getFrom(NamespaceStorageNode storage, K key);
106 public abstract void addTo(NamespaceStorageNode storage,K key,V value);
110 public Class<N> getIdentifier() {
114 protected final V getFromLocalStorage(NamespaceStorageNode storage, K key) {
115 return storage.getFromLocalStorage(getIdentifier(), key);
118 protected final void addToStorage(NamespaceStorageNode storage,K key,V value) {
119 storage.addToLocalStorage(getIdentifier(),key,value);
122 static class StorageSpecific<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
124 StorageNodeType storageType;
126 public StorageSpecific(Class<N> identifier, StorageNodeType type) {
128 storageType = Preconditions.checkNotNull(type);
132 public V getFrom(final NamespaceStorageNode storage, final K key) {
133 NamespaceStorageNode current = storage;
134 while(current.getParentNamespaceStorage() != null) {
135 current = current.getParentNamespaceStorage();
137 return getFromLocalStorage(current,key);
141 public void addTo(NamespaceBehaviour.NamespaceStorageNode storage, K key, V value) {
142 NamespaceStorageNode current = storage;
143 while(current.getStorageNodeType() != storageType) {
144 current = current.getParentNamespaceStorage();
146 addToStorage(current, key, value);
151 static class TreeScoped<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
153 public TreeScoped(Class<N> identifier) {
158 public V getFrom(final NamespaceStorageNode storage, final K key) {
159 NamespaceStorageNode current = storage;
160 while(current != null) {
161 final V val = getFromLocalStorage(current, key);
165 current = current.getParentNamespaceStorage();
171 public void addTo(NamespaceStorageNode storage,K key, V value) {
172 addToStorage(storage, key, value);