Bug 6867: Extend yang statement parser to support different yang versions
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / NamespaceStorageSupport.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.stmt.reactor;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.Set;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
24
25 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
26
27     private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
28
29     @Override
30     public abstract NamespaceStorageNode getParentNamespaceStorage();
31
32     public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
33
34     protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
35         // NOOP
36     }
37
38     protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key, final V value) {
39         // NOOP
40     }
41
42     @Nonnull
43     public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type, final KT key)
44             throws NamespaceNotAvailableException {
45         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
46     }
47
48     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type){
49         return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
50     }
51
52     @SuppressWarnings("unchecked")
53     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(final Class<N> type){
54         return (Map<K, V>) namespaces.get(type);
55     }
56
57     public final <K,V, KT extends K, VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(final Class<N> type, final KT key, final VT value)
58             throws NamespaceNotAvailableException {
59         getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
60     }
61
62     @SuppressWarnings({ "unchecked", "rawtypes" })
63     public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key, final StmtContext<?, ?, ?> value)
64             throws NamespaceNotAvailableException {
65         getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
66     }
67
68     @SuppressWarnings("unchecked")
69     @Override
70     public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
71         final Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
72
73         V potential = null;
74         if (localNamespace != null) {
75             potential = localNamespace.get(key);
76         }
77
78         if (potential == null && Utils.isModuleIdentifierWithoutSpecifiedRevision(key)) {
79             potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
80         }
81
82         return potential;
83     }
84
85     private static <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(final ModuleIdentifier key,
86             final Map<ModuleIdentifier, V> localNamespace) {
87
88         if (localNamespace == null) {
89             return null;
90         }
91
92         final Set<Entry<ModuleIdentifier, V>> entrySet = localNamespace.entrySet();
93         for (final Entry<ModuleIdentifier, V> entry : entrySet) {
94             final ModuleIdentifier moduleIdentifierInMap = entry.getKey();
95             if (moduleIdentifierInMap.getName().equals(key.getName())) {
96                 return entry.getValue();
97             }
98         }
99
100         return null;
101     }
102
103     @Override
104     public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
105         @SuppressWarnings("unchecked")
106         final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
107         return localNamespace;
108     }
109
110     @Override
111     public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key, final V value) {
112         @SuppressWarnings("unchecked")
113         Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
114         if (localNamespace == null) {
115             checkLocalNamespaceAllowed(type);
116             localNamespace = new HashMap<>(1);
117
118             if (namespaces.isEmpty()) {
119                 namespaces = new HashMap<>(1);
120             }
121             namespaces.put(type, localNamespace);
122         }
123         localNamespace.put(key,value);
124         onNamespaceElementAdded(type,key,value);
125     }
126
127 }