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.Date;
12 import java.util.HashMap;
14 import java.util.Map.Entry;
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 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 abstract class NamespaceStorageSupport implements NamespaceStorageNode {
29 private Map<Class<?>, Map<?,?>> namespaces = ImmutableMap.of();
32 public abstract NamespaceStorageNode getParentNamespaceStorage();
35 * @return registry of source context
37 public abstract NamespaceBehaviour.Registry getBehaviourRegistry();
39 protected void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
44 * Occurs when an item is added to model namespace.
46 * @throws SourceException instance of SourceException
48 protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
54 public final <K,V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(final Class<N> type,
55 final KT key) throws NamespaceNotAvailableException {
56 return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this,key);
59 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type){
60 return getBehaviourRegistry().getNamespaceBehaviour(type).getAllFrom(this);
63 @SuppressWarnings("unchecked")
64 public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
65 final Class<N> type) {
66 return (Map<K, V>) namespaces.get(type);
69 public final <K,V, KT extends K, VT extends V,N extends IdentifierNamespace<K, V>> void addToNs(final Class<N> type,
70 final KT key, final VT value) throws NamespaceNotAvailableException {
71 getBehaviourRegistry().getNamespaceBehaviour(type).addTo(this,key,value);
74 @SuppressWarnings({ "unchecked", "rawtypes" })
75 public final <K, N extends StatementNamespace<K, ?,?>> void addContextToNamespace(final Class<N> type, final K key,
76 final StmtContext<?, ?, ?> value) throws NamespaceNotAvailableException {
77 getBehaviourRegistry().getNamespaceBehaviour((Class)type).addTo(this, key, value);
80 @SuppressWarnings("unchecked")
82 public <K, V, N extends IdentifierNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
83 final Map<K, V> localNamespace = (Map<K,V>) namespaces.get(type);
86 if (localNamespace != null) {
87 potential = localNamespace.get(key);
90 if (potential == null && isModuleIdentifierWithoutSpecifiedRevision(key)) {
91 potential = getRegardlessOfRevision((ModuleIdentifier)key,(Map<ModuleIdentifier,V>)localNamespace);
97 private static boolean isModuleIdentifierWithoutSpecifiedRevision(final Object obj) {
98 if (!(obj instanceof ModuleIdentifier)) {
102 final Date rev = ((ModuleIdentifier) obj).getRevision();
103 return rev == SimpleDateFormatUtil.DEFAULT_DATE_IMP || rev == SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE;
106 private static <K, V, N extends IdentifierNamespace<K, V>> V getRegardlessOfRevision(final ModuleIdentifier key,
107 final Map<ModuleIdentifier, V> localNamespace) {
109 if (localNamespace == null) {
113 final Set<Entry<ModuleIdentifier, V>> entrySet = localNamespace.entrySet();
114 for (final Entry<ModuleIdentifier, V> entry : entrySet) {
115 final ModuleIdentifier moduleIdentifierInMap = entry.getKey();
116 if (moduleIdentifierInMap.getName().equals(key.getName())) {
117 return entry.getValue();
125 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
126 @SuppressWarnings("unchecked")
127 final Map<K, V> localNamespace = (Map<K, V>) namespaces.get(type);
128 return localNamespace;
131 private <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> ensureLocalNamespace(final Class<N> type) {
132 @SuppressWarnings("unchecked")
133 Map<K, V> ret = (Map<K,V>) namespaces.get(type);
135 checkLocalNamespaceAllowed(type);
136 ret = new HashMap<>(1);
138 if (namespaces.isEmpty()) {
139 namespaces = new HashMap<>(1);
141 namespaces.put(type, ret);
148 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
150 final V ret = ensureLocalNamespace(type).put(key, value);
151 onNamespaceElementAdded(type, key, value);
156 public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorageIfAbsent(final Class<N> type, final K key,
158 final V ret = ensureLocalNamespace(type).putIfAbsent(key, value);
160 onNamespaceElementAdded(type, key, value);