Bug 3670 (part 3/5): Use of new statement parser in yang-maven-plugin
[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 java.util.Map.Entry;
11
12 import java.util.Set;
13 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
14 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
15 import java.util.HashMap;
16 import java.util.Map;
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
24 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
25
26     private final Map<Class<?>,Map<?,?>> namespaces = new HashMap<>();
27
28
29     @Override
30     public abstract NamespaceStorageNode getParentNamespaceStorage();
31
32     public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
33
34     protected void checkLocalNamespaceAllowed(Class<? extends IdentifierNamespace<?, ?>> type) {
35         // NOOP
36     }
37
38     protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(Class<N> type, K key, V value) {
39         // NOOP
40     }
41
42     //<K,V,N extends IdentifierNamespace<K, V>> V
43     //public final <K, VT, V extends VT ,N extends IdentifierNamespace<K, V>> VT getFromNamespace(Class<N> type, K key)
44     public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, KT key)
45             throws NamespaceNotAvailableException {
46         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
47     }
48
49     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type){
50         return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
51     }
52
53     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(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(Class<N> type, KT key, 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(Class<N> type, K key, 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(Class<N> type, K key) {
71         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 <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(ModuleIdentifier key,
86             Map<ModuleIdentifier, V> localNamespace) {
87
88         if(localNamespace == null) {
89             return null;
90         }
91
92         Set<Entry<ModuleIdentifier, V>> entrySet = localNamespace.entrySet();
93         for (Entry<ModuleIdentifier, V> entry : entrySet) {
94             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(Class<N> type) {
105         @SuppressWarnings("unchecked")
106         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(Class<N> type, K key, 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<>();
117             namespaces.put(type, localNamespace);
118         }
119         localNamespace.put(key,value);
120         onNamespaceElementAdded(type,key,value);
121     }
122
123 }