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.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatementNamespace;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatementNamespace;
31 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatementNamespace;
34 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
39 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
40 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
41 import org.opendaylight.yangtools.yang.parser.spi.FeatureNamespace;
42 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
44 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
45 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
47 final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleStatement, ModuleEffectiveStatement>
48 implements ModuleEffectiveStatement {
49 private final ImmutableMap<String, SubmoduleEffectiveStatement> nameToSubmodule;
50 private final ImmutableMap<QName, ExtensionEffectiveStatement> qnameToExtension;
51 private final ImmutableMap<QName, FeatureEffectiveStatement> qnameToFeature;
52 private final ImmutableMap<QName, IdentityEffectiveStatement> qnameToIdentity;
53 private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
54 private final ImmutableMap<QNameModule, String> namespaceToPrefix;
55 private final @NonNull QNameModule qnameModule;
56 private final ImmutableList<Module> submodules;
58 ModuleEffectiveStatementImpl(final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> ctx,
59 final ModuleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
60 final Collection<? extends Module> submodules) {
61 super(declared, ctx, substatements, findPrefix(ctx, "module", ctx.getStatementArgument()));
63 qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx));
64 this.submodules = ImmutableList.copyOf(submodules);
66 final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
67 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
68 prefixToModuleBuilder.put(localPrefix, this);
69 appendPrefixes(ctx, prefixToModuleBuilder);
70 prefixToModule = prefixToModuleBuilder.build();
72 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size() + 1);
73 tmp.put(qnameModule, localPrefix);
74 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
75 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
77 namespaceToPrefix = ImmutableMap.copyOf(tmp);
79 final Map<String, StmtContext<?, ?, ?>> includedSubmodules =
80 ctx.getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
81 nameToSubmodule = includedSubmodules == null ? ImmutableMap.of()
82 : ImmutableMap.copyOf(Maps.transformValues(includedSubmodules,
83 submodule -> (SubmoduleEffectiveStatement) submodule.buildEffective()));
85 final Map<QName, StmtContext<?, ExtensionStatement, ExtensionEffectiveStatement>> extensions =
86 ctx.getAllFromCurrentStmtCtxNamespace(ExtensionNamespace.class);
87 qnameToExtension = extensions == null ? ImmutableMap.of()
88 : ImmutableMap.copyOf(Maps.transformValues(extensions, StmtContext::buildEffective));
89 final Map<QName, StmtContext<?, FeatureStatement, FeatureEffectiveStatement>> features =
90 ctx.getAllFromCurrentStmtCtxNamespace(FeatureNamespace.class);
91 qnameToFeature = features == null ? ImmutableMap.of()
92 : ImmutableMap.copyOf(Maps.transformValues(features, StmtContext::buildEffective));
93 final Map<QName, StmtContext<?, IdentityStatement, IdentityEffectiveStatement>> identities =
94 ctx.getAllFromCurrentStmtCtxNamespace(IdentityNamespace.class);
95 qnameToIdentity = identities == null ? ImmutableMap.of()
96 : ImmutableMap.copyOf(Maps.transformValues(identities, StmtContext::buildEffective));
100 public @NonNull QNameModule localQNameModule() {
105 public @NonNull QNameModule getQNameModule() {
110 public Collection<? extends Module> getSubmodules() {
115 @SuppressWarnings("unchecked")
116 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
117 final @NonNull Class<N> namespace) {
118 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
119 return Optional.of((Map<K, V>) prefixToModule);
121 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
122 return Optional.of((Map<K, V>) namespaceToPrefix);
124 if (NameToEffectiveSubmoduleNamespace.class.equals(namespace)) {
125 return Optional.of((Map<K, V>) nameToSubmodule);
127 if (ExtensionEffectiveStatementNamespace.class.equals(namespace)) {
128 return Optional.of((Map<K, V>) qnameToExtension);
130 if (FeatureEffectiveStatementNamespace.class.equals(namespace)) {
131 return Optional.of((Map<K, V>) qnameToFeature);
133 if (IdentityEffectiveStatementNamespace.class.equals(namespace)) {
134 return Optional.of((Map<K, V>) qnameToIdentity);
136 return super.getNamespaceContents(namespace);