735bcb9ae002780284395adf450510f3c7dc4565
[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 java.util.Map;
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;
16
17 /**
18  * Definition / implementation of specific Identifier Namespace behaviour.
19  *
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}.
22  *
23  * For common behaviour models please use static factories {@link #global(Class)}, {@link #sourceLocal(Class)} and
24  * {@link #treeScoped(Class)}.
25  *
26  * @param <K>
27  *            Key type
28  * @param <V>
29  *            Value type
30  * @param <N>
31  *            Namespace Type
32  */
33 public abstract class NamespaceBehaviour<K, V, N extends IdentifierNamespace<K, V>> implements Identifiable<Class<N>> {
34
35     public enum StorageNodeType {
36         GLOBAL, SOURCE_LOCAL_SPECIAL, STATEMENT_LOCAL,
37     }
38
39     public interface Registry {
40         <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(Class<N> type);
41     }
42
43     public interface NamespaceStorageNode {
44
45         StorageNodeType getStorageNodeType();
46
47         @Nullable
48         NamespaceStorageNode getParentNamespaceStorage();
49
50         @Nullable
51         <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(Class<N> type, K key);
52
53         @Nullable
54         <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(Class<N> type);
55
56         @Nullable
57         <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(Class<N> type, K key, V value);
58
59     }
60
61     private final Class<N> identifier;
62
63     protected NamespaceBehaviour(Class<N> identifier) {
64         this.identifier = Preconditions.checkNotNull(identifier);
65     }
66
67     /**
68      *
69      * Creates global namespace behaviour for supplied namespace type.
70      *
71      * Global behaviour stores and loads all values from root {@link NamespaceStorageNode} with type of
72      * {@link StorageNodeType#GLOBAL}.
73      *
74      * @param identifier
75      *            Namespace identifier.
76      * @param <K> type parameter
77      * @param <V> type parameter
78      * @param <N> type parameter
79      *
80      * @return global namespace behaviour for supplied namespace type.
81      */
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);
85     }
86
87     /**
88      *
89      * Creates source-local namespace behaviour for supplied namespace type.
90      *
91      * Source-local namespace behaviour stores and loads all values from closest {@link NamespaceStorageNode} ancestor
92      * with type of {@link StorageNodeType#SOURCE_LOCAL_SPECIAL}.
93      *
94      * @param identifier
95      *            Namespace identifier.
96      * @param <K> type parameter
97      * @param <V> type parameter
98      * @param <N> type parameter
99      *
100      * @return source-local namespace behaviour for supplied namespace type.
101      */
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);
105     }
106
107     /**
108      *
109      * Creates tree-scoped namespace behaviour for supplied namespace type.
110      *
111      * Tree-scoped namespace behaviour search for value in all storage nodes up to the root and stores values in
112      * supplied node.
113      *
114      * @param identifier
115      *            Namespace identifier.     *
116      * @param <K> type parameter
117      * @param <V> type parameter
118      * @param <N> type parameter
119      *
120      * @return tree-scoped namespace behaviour for supplied namespace type.
121      */
122     public static <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> treeScoped(Class<N> identifier) {
123         return new TreeScoped<>(identifier);
124     }
125
126     /**
127      * returns value from model namespace storage according to key param class
128      *
129      * @param storage namespace storage
130      * @param key type parameter
131      *
132      * @return value from model namespace storage according to key param class
133      */
134     public abstract V getFrom(NamespaceStorageNode storage, K key);
135
136     /**
137      * returns all values of a keys of param class from model namespace storage
138      *
139      * @param storage namespace storage
140      *
141      * @return all values of keys of param class from model namespace storage
142      */
143     public abstract Map<K, V> getAllFrom(NamespaceStorageNode storage);
144
145     /**
146      * adds key and value to corresponding namespace storage according to param class
147      *
148      * @param storage namespace storage
149      * @param key type parameter
150      * @param value type parameter
151      */
152     public abstract void addTo(NamespaceStorageNode storage, K key, V value);
153
154     @Override
155     public Class<N> getIdentifier() {
156         return identifier;
157     }
158
159     protected final V getFromLocalStorage(NamespaceStorageNode storage, K key) {
160         return storage.getFromLocalStorage(getIdentifier(), key);
161     }
162
163     protected final Map<K, V> getAllFromLocalStorage(NamespaceStorageNode storage) {
164         return storage.getAllFromLocalStorage(getIdentifier());
165     }
166
167     protected final void addToStorage(NamespaceStorageNode storage, K key, V value) {
168         storage.addToLocalStorage(getIdentifier(), key, value);
169     }
170
171     static class StorageSpecific<K, V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
172
173         StorageNodeType storageType;
174
175         public StorageSpecific(Class<N> identifier, StorageNodeType type) {
176             super(identifier);
177             storageType = Preconditions.checkNotNull(type);
178         }
179
180         @Override
181         public V getFrom(final NamespaceStorageNode storage, final K key) {
182             NamespaceStorageNode current = storage;
183             while (current.getStorageNodeType() != storageType) {
184                 current = current.getParentNamespaceStorage();
185             }
186             return getFromLocalStorage(current, key);
187         }
188
189         @Override
190         public Map<K, V> getAllFrom(final NamespaceStorageNode storage) {
191             NamespaceStorageNode current = storage;
192             while (current.getStorageNodeType() != storageType) {
193                 current = current.getParentNamespaceStorage();
194             }
195
196             return getAllFromLocalStorage(current);
197         }
198
199         @Override
200         public void addTo(NamespaceBehaviour.NamespaceStorageNode storage, K key, V value) {
201             NamespaceStorageNode current = storage;
202             while (current.getStorageNodeType() != storageType) {
203                 current = current.getParentNamespaceStorage();
204             }
205             addToStorage(current, key, value);
206         }
207
208     }
209
210     static class TreeScoped<K, V, N extends IdentifierNamespace<K, V>> extends NamespaceBehaviour<K, V, N> {
211
212         public TreeScoped(Class<N> identifier) {
213             super(identifier);
214         }
215
216         @Override
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);
221                 if (val != null) {
222                     return val;
223                 }
224                 current = current.getParentNamespaceStorage();
225             }
226             return null;
227         }
228
229         @Override
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);
234                 if (val != null) {
235                     return val;
236                 }
237                 current = current.getParentNamespaceStorage();
238             }
239             return null;
240         }
241
242         @Override
243         public void addTo(NamespaceStorageNode storage, K key, V value) {
244             addToStorage(storage, key, value);
245         }
246
247     }
248 }