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