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 java.util.HashMap;
12 import java.util.Map.Entry;
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;
23 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
25 private final Map<Class<?>,Map<?,?>> namespaces = new HashMap<>();
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 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(final Class<N> type){
51 return (Map<K, V>) namespaces.get(type);
54 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)
55 throws NamespaceNotAvailableException {
56 getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
59 @SuppressWarnings({ "unchecked", "rawtypes" })
60 public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key, final StmtContext<?, ?, ?> value)
61 throws NamespaceNotAvailableException {
62 getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
65 @SuppressWarnings("unchecked")
67 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
68 Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
71 if(localNamespace != null) {
72 potential = localNamespace.get(key);
75 if(potential == null && Utils.isModuleIdentifierWithoutSpecifiedRevision(key)) {
76 potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
82 private static <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(final ModuleIdentifier key,
83 final Map<ModuleIdentifier, V> localNamespace) {
85 if (localNamespace == null) {
89 Set<Entry<ModuleIdentifier, V>> entrySet = localNamespace.entrySet();
90 for (Entry<ModuleIdentifier, V> entry : entrySet) {
91 ModuleIdentifier moduleIdentifierInMap = entry.getKey();
92 if (moduleIdentifierInMap.getName().equals(key.getName())) {
93 return entry.getValue();
101 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
102 @SuppressWarnings("unchecked")
103 Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
104 return localNamespace;
108 public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key, final V value) {
109 @SuppressWarnings("unchecked")
110 Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
111 if(localNamespace == null) {
112 checkLocalNamespaceAllowed(type);
113 localNamespace = new HashMap<>();
114 namespaces.put(type, localNamespace);
116 localNamespace.put(key,value);
117 onNamespaceElementAdded(type,key,value);