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);
103 public abstract void addTo(NamespaceStorageNode storage,K key,V value);
106 public Class<N> getIdentifier() {
110 protected final V getFromLocalStorage(NamespaceStorageNode storage, K key) {
111 return storage.getFromLocalStorage(getIdentifier(), key);
114 protected final void addToStorage(NamespaceStorageNode storage,K key,V value) {
115 storage.addToLocalStorage(getIdentifier(),key,value);
118 static class StorageSpecific<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
120 StorageNodeType storageType;
122 public StorageSpecific(Class<N> identifier, StorageNodeType type) {
124 storageType = Preconditions.checkNotNull(type);
128 public V getFrom(final NamespaceStorageNode storage, final K key) {
129 NamespaceStorageNode current = storage;
130 while(current.getParentNamespaceStorage() != null) {
131 current = current.getParentNamespaceStorage();
133 return getFromLocalStorage(current,key);
137 public void addTo(NamespaceBehaviour.NamespaceStorageNode storage, K key, V value) {
138 NamespaceStorageNode current = storage;
139 while(current.getStorageNodeType() != storageType) {
140 current = current.getParentNamespaceStorage();
142 addToStorage(current, key, value);
147 static class TreeScoped<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
149 public TreeScoped(Class<N> identifier) {
154 public V getFrom(final NamespaceStorageNode storage, final K key) {
155 NamespaceStorageNode current = storage;
156 while(current != null) {
157 final V val = getFromLocalStorage(current, key);
161 current = current.getParentNamespaceStorage();
167 public void addTo(NamespaceStorageNode storage,K key, V value) {
168 addToStorage(storage, key, value);