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 org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
16 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
24 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
26 private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
29 public abstract NamespaceStorageNode getParentNamespaceStorage();
31 public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
33 protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
37 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key, final V value) {
41 public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type, final KT key)
42 throws NamespaceNotAvailableException {
43 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
46 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type){
47 return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
50 @SuppressWarnings("unchecked")
51 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(final Class<N> type){
52 return (Map<K, V>) namespaces.get(type);
55 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)
56 throws NamespaceNotAvailableException {
57 getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
60 @SuppressWarnings({ "unchecked", "rawtypes" })
61 public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key, final StmtContext<?, ?, ?> value)
62 throws NamespaceNotAvailableException {
63 getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
66 @SuppressWarnings("unchecked")
68 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
69 Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
72 if (localNamespace != null) {
73 potential = localNamespace.get(key);
76 if (potential == null && Utils.isModuleIdentifierWithoutSpecifiedRevision(key)) {
77 potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
83 private static <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(final ModuleIdentifier key,
84 final Map<ModuleIdentifier, V> localNamespace) {
86 if (localNamespace == null) {
90 Set<Entry<ModuleIdentifier, V>> entrySet = localNamespace.entrySet();
91 for (Entry<ModuleIdentifier, V> entry : entrySet) {
92 ModuleIdentifier moduleIdentifierInMap = entry.getKey();
93 if (moduleIdentifierInMap.getName().equals(key.getName())) {
94 return entry.getValue();
102 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
103 @SuppressWarnings("unchecked")
104 Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
105 return localNamespace;
109 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key, final V value) {
110 @SuppressWarnings("unchecked")
111 Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
112 if (localNamespace == null) {
113 checkLocalNamespaceAllowed(type);
114 localNamespace = new HashMap<>(1);
116 if (namespaces.isEmpty()) {
117 namespaces = new HashMap<>(1);
119 namespaces.put(type, localNamespace);
121 localNamespace.put(key,value);
122 onNamespaceElementAdded(type,key,value);