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.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Maps;
17 import java.util.Map.Entry;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatementNamespace;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatementNamespace;
29 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatementNamespace;
32 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
38 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
39 import org.opendaylight.yangtools.yang.parser.spi.FeatureNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
42 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
43 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
45 final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleStatement, ModuleEffectiveStatement>
46 implements ModuleEffectiveStatement {
47 private final ImmutableMap<String, SubmoduleEffectiveStatement> nameToSubmodule;
48 private final ImmutableMap<QName, ExtensionEffectiveStatement> qnameToExtension;
49 private final ImmutableMap<QName, FeatureEffectiveStatement> qnameToFeature;
50 private final ImmutableMap<QName, IdentityEffectiveStatement> qnameToIdentity;
51 private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
52 private final ImmutableMap<QNameModule, String> namespaceToPrefix;
53 private final @NonNull QNameModule qnameModule;
54 private final ImmutableSet<Module> submodules;
56 private ModuleEffectiveStatementImpl(final @NonNull ModuleStmtContext ctx) {
57 super(ctx, findPrefix(ctx.delegate(), "module", ctx.getStatementArgument()));
58 submodules = ctx.getSubmodules();
60 qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.delegate()));
62 final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
63 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
64 prefixToModuleBuilder.put(localPrefix, this);
65 appendPrefixes(ctx, prefixToModuleBuilder);
66 prefixToModule = prefixToModuleBuilder.build();
68 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size() + 1);
69 tmp.put(qnameModule, localPrefix);
70 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
71 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
73 namespaceToPrefix = ImmutableMap.copyOf(tmp);
75 final Map<String, StmtContext<?, ?, ?>> includedSubmodules =
76 ctx.getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
77 nameToSubmodule = includedSubmodules == null ? ImmutableMap.of()
78 : ImmutableMap.copyOf(Maps.transformValues(includedSubmodules,
79 submodule -> (SubmoduleEffectiveStatement) submodule.buildEffective()));
81 final Map<QName, StmtContext<?, ExtensionStatement, ExtensionEffectiveStatement>> extensions =
82 ctx.getAllFromCurrentStmtCtxNamespace(ExtensionNamespace.class);
83 qnameToExtension = extensions == null ? ImmutableMap.of()
84 : ImmutableMap.copyOf(Maps.transformValues(extensions, StmtContext::buildEffective));
85 final Map<QName, StmtContext<?, FeatureStatement, FeatureEffectiveStatement>> features =
86 ctx.getAllFromCurrentStmtCtxNamespace(FeatureNamespace.class);
87 qnameToFeature = features == null ? ImmutableMap.of()
88 : ImmutableMap.copyOf(Maps.transformValues(features, StmtContext::buildEffective));
89 final Map<QName, StmtContext<?, IdentityStatement, IdentityEffectiveStatement>> identities =
90 ctx.getAllFromCurrentStmtCtxNamespace(IdentityNamespace.class);
91 qnameToIdentity = identities == null ? ImmutableMap.of()
92 : ImmutableMap.copyOf(Maps.transformValues(identities, StmtContext::buildEffective));
95 ModuleEffectiveStatementImpl(final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> ctx) {
96 this(ModuleStmtContext.create(ctx));
100 public @NonNull QNameModule localQNameModule() {
105 public @NonNull QNameModule getQNameModule() {
110 public ImmutableSet<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);