Fix enum members' name
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / NamespaceBehaviour.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
9
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;
15
16 /**
17  * Definition / implementation of specific Identifier Namespace behaviour.
18  *
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}.
21  *
22  * For common behaviour models please use static factories {@link #global(Class)},
23  * {@link #sourceLocal(Class)} and {@link #treeScoped(Class)}.
24  *
25  * @param <K> Key type
26  * @param <V> Value type
27  * @param <N> Namespace Type
28  */
29 public abstract class NamespaceBehaviour<K,V, N extends IdentifierNamespace<K, V>> implements Identifiable<Class<N>>{
30
31     public enum StorageNodeType {
32         GLOBAL,
33         SOURCE_LOCAL_SPECIAL,
34         STATEMENT_LOCAL,
35     }
36
37     public interface Registry {
38         <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> type);
39     }
40
41     public interface NamespaceStorageNode {
42
43         StorageNodeType getStorageNodeType();
44
45         @Nullable NamespaceStorageNode getParentNamespaceStorage();
46
47         @Nullable  <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key);
48
49         @Nullable  <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value);
50
51     }
52
53     private final Class<N> identifier;
54
55     protected NamespaceBehaviour(Class<N> identifier) {
56         this.identifier = Preconditions.checkNotNull(identifier);
57     }
58
59     /**
60      *
61      * Creates global namespace behaviour for supplied namespace type.
62      *
63      * Global behaviour stores and loads all values from root {@link NamespaceStorageNode}
64      * with type of {@link StorageNodeType#GLOBAL}.
65      *
66      * @param identifier Namespace identifier.
67      * @return global namespace behaviour for supplied namespace type.
68      */
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);
71     }
72
73     /**
74      *
75      * Creates source-local namespace behaviour for supplied namespace type.
76      *
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}.
80      *
81      * @param identifier Namespace identifier.
82      * @return source-local namespace behaviour for supplied namespace type.
83      */
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);
86     }
87
88     /**
89     *
90     * Creates tree-scoped namespace behaviour for supplied namespace type.
91     *
92     * Tree-scoped namespace behaviour search for value in all storage nodes
93     * up to the root and stores values in supplied node.
94     *
95     * @param identifier Namespace identifier.
96     * @return tree-scoped namespace behaviour for supplied namespace type.
97     */
98     public static <K,V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K,V,N> treeScoped(Class<N> identifier) {
99         return new TreeScoped<>(identifier);
100     }
101
102     public abstract V getFrom(NamespaceStorageNode storage, K key);
103     public abstract void addTo(NamespaceStorageNode storage,K key,V value);
104
105     @Override
106     public Class<N> getIdentifier() {
107         return identifier;
108     }
109
110     protected final V getFromLocalStorage(NamespaceStorageNode storage, K key) {
111         return storage.getFromLocalStorage(getIdentifier(), key);
112     }
113
114     protected final void addToStorage(NamespaceStorageNode storage,K key,V value) {
115         storage.addToLocalStorage(getIdentifier(),key,value);
116     }
117
118     static class StorageSpecific<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
119
120         StorageNodeType storageType;
121
122         public StorageSpecific(Class<N> identifier, StorageNodeType type) {
123             super(identifier);
124             storageType = Preconditions.checkNotNull(type);
125         }
126
127         @Override
128         public V getFrom(final NamespaceStorageNode storage, final K key) {
129             NamespaceStorageNode current = storage;
130             while(current.getParentNamespaceStorage() != null) {
131                 current = current.getParentNamespaceStorage();
132             }
133             return getFromLocalStorage(current,key);
134         }
135
136         @Override
137         public void addTo(NamespaceBehaviour.NamespaceStorageNode storage, K key, V value) {
138             NamespaceStorageNode current = storage;
139             while(current.getStorageNodeType() != storageType) {
140                 current = current.getParentNamespaceStorage();
141             }
142             addToStorage(current, key, value);
143         }
144
145     }
146
147     static class TreeScoped<K,V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
148
149         public TreeScoped(Class<N> identifier) {
150             super(identifier);
151         }
152
153         @Override
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);
158                 if(val != null) {
159                     return val;
160                 }
161                 current = current.getParentNamespaceStorage();
162             }
163             return null;
164         }
165
166         @Override
167         public void addTo(NamespaceStorageNode storage,K key, V value) {
168             addToStorage(storage, key, value);
169         }
170
171     }
172 }