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.rfc7950.stmt.module;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import com.google.common.collect.Maps;
16 import java.util.Collection;
18 import java.util.Map.Entry;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.Submodule;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatementNamespace;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatementNamespace;
33 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatementNamespace;
36 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
39 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
40 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
41 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
42 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.FeatureNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
47 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
48 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
50 final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleStatement, ModuleEffectiveStatement>
51 implements Module, ModuleEffectiveStatement {
52 private final ImmutableMap<String, SubmoduleEffectiveStatement> nameToSubmodule;
53 private final ImmutableMap<QName, ExtensionEffectiveStatement> qnameToExtension;
54 private final ImmutableMap<QName, FeatureEffectiveStatement> qnameToFeature;
55 private final ImmutableMap<QName, IdentityEffectiveStatement> qnameToIdentity;
56 private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
57 private final ImmutableMap<QNameModule, String> namespaceToPrefix;
58 private final @NonNull QNameModule qnameModule;
59 private final ImmutableList<Submodule> submodules;
61 ModuleEffectiveStatementImpl(final Current<UnqualifiedQName, ModuleStatement> stmt,
62 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
63 final Collection<? extends Submodule> submodules) {
64 super(stmt, substatements, findPrefix(stmt.caerbannog(), "module",
65 stmt.caerbannog().coerceRawStatementArgument()));
67 qnameModule = verifyNotNull(stmt.getFromNamespace(ModuleCtxToModuleQName.class, stmt.caerbannog()));
68 this.submodules = ImmutableList.copyOf(submodules);
70 final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
71 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
72 prefixToModuleBuilder.put(localPrefix, this);
73 appendPrefixes(stmt, prefixToModuleBuilder);
74 prefixToModule = prefixToModuleBuilder.build();
76 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size() + 1);
77 tmp.put(qnameModule, localPrefix);
78 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
79 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
81 namespaceToPrefix = ImmutableMap.copyOf(tmp);
83 final Map<String, StmtContext<?, ?, ?>> includedSubmodules =
84 stmt.getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
85 nameToSubmodule = includedSubmodules == null ? ImmutableMap.of()
86 : ImmutableMap.copyOf(Maps.transformValues(includedSubmodules,
87 submodule -> (SubmoduleEffectiveStatement) submodule.buildEffective()));
89 final Map<QName, StmtContext<?, ExtensionStatement, ExtensionEffectiveStatement>> extensions =
90 stmt.getAllFromCurrentStmtCtxNamespace(ExtensionNamespace.class);
91 qnameToExtension = extensions == null ? ImmutableMap.of()
92 : ImmutableMap.copyOf(Maps.transformValues(extensions, StmtContext::buildEffective));
93 final Map<QName, StmtContext<?, FeatureStatement, FeatureEffectiveStatement>> features =
94 stmt.getAllFromCurrentStmtCtxNamespace(FeatureNamespace.class);
95 qnameToFeature = features == null ? ImmutableMap.of()
96 : ImmutableMap.copyOf(Maps.transformValues(features, StmtContext::buildEffective));
97 final Map<QName, StmtContext<?, IdentityStatement, IdentityEffectiveStatement>> identities =
98 stmt.getAllFromCurrentStmtCtxNamespace(IdentityNamespace.class);
99 qnameToIdentity = identities == null ? ImmutableMap.of()
100 : ImmutableMap.copyOf(Maps.transformValues(identities, StmtContext::buildEffective));
104 public @NonNull QNameModule localQNameModule() {
109 public @NonNull QNameModule getQNameModule() {
114 public Collection<? extends @NonNull Submodule> getSubmodules() {
119 public ModuleEffectiveStatement asEffectiveStatement() {
124 @SuppressWarnings("unchecked")
125 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
126 final @NonNull Class<N> namespace) {
127 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
128 return Optional.of((Map<K, V>) prefixToModule);
130 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
131 return Optional.of((Map<K, V>) namespaceToPrefix);
133 if (NameToEffectiveSubmoduleNamespace.class.equals(namespace)) {
134 return Optional.of((Map<K, V>) nameToSubmodule);
136 if (ExtensionEffectiveStatementNamespace.class.equals(namespace)) {
137 return Optional.of((Map<K, V>) qnameToExtension);
139 if (FeatureEffectiveStatementNamespace.class.equals(namespace)) {
140 return Optional.of((Map<K, V>) qnameToFeature);
142 if (IdentityEffectiveStatementNamespace.class.equals(namespace)) {
143 return Optional.of((Map<K, V>) qnameToIdentity);
145 return super.getNamespaceContents(namespace);