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