2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import com.google.common.collect.ImmutableMap;
11 import java.util.HashMap;
13 import java.util.Map.Entry;
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;
25 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
27 private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
30 public abstract NamespaceStorageNode getParentNamespaceStorage();
32 public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
34 protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
38 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key, final V value) {
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);
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);
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);
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);
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);
68 @SuppressWarnings("unchecked")
70 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
71 Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
74 if (localNamespace != null) {
75 potential = localNamespace.get(key);
78 if (potential == null && Utils.isModuleIdentifierWithoutSpecifiedRevision(key)) {
79 potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
85 private static <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(final ModuleIdentifier key,
86 final Map<ModuleIdentifier, V> localNamespace) {
88 if (localNamespace == null) {
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();
104 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
105 @SuppressWarnings("unchecked")
106 Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
107 return localNamespace;
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);
118 if (namespaces.isEmpty()) {
119 namespaces = new HashMap<>(1);
121 namespaces.put(type, localNamespace);
123 localNamespace.put(key,value);
124 onNamespaceElementAdded(type,key,value);