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.ImportEffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
39 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.FeatureNamespace;
41 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
43 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
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>
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 ImmutableSet<Module> submodules;
58 private ModuleEffectiveStatementImpl(final @NonNull ModuleStmtContext ctx) {
59 super(ctx, findPrefix(ctx.delegate(), "module", ctx.getStatementArgument()));
60 submodules = ctx.getSubmodules();
62 qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.delegate()));
64 final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
65 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
66 prefixToModuleBuilder.put(localPrefix, this);
68 streamEffectiveSubstatements(ImportEffectiveStatement.class)
69 .map(imp -> imp.findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get())
71 final StmtContext<?, ?, ?> importedCtx =
72 verifyNotNull(ctx.getFromNamespace(ImportPrefixToModuleCtx.class, prefix),
73 "Failed to resolve prefix %s", prefix);
74 prefixToModuleBuilder.put(prefix, (ModuleEffectiveStatement) importedCtx.buildEffective());
76 prefixToModule = prefixToModuleBuilder.build();
78 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size() + 1);
79 tmp.put(qnameModule, localPrefix);
80 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
81 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
83 namespaceToPrefix = ImmutableMap.copyOf(tmp);
85 final Map<String, StmtContext<?, ?, ?>> includedSubmodules =
86 ctx.getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
87 nameToSubmodule = includedSubmodules == null ? ImmutableMap.of()
88 : ImmutableMap.copyOf(Maps.transformValues(includedSubmodules,
89 submodule -> (SubmoduleEffectiveStatement) submodule.buildEffective()));
91 final Map<QName, StmtContext<?, ExtensionStatement, ExtensionEffectiveStatement>> extensions =
92 ctx.getAllFromCurrentStmtCtxNamespace(ExtensionNamespace.class);
93 qnameToExtension = extensions == null ? ImmutableMap.of()
94 : ImmutableMap.copyOf(Maps.transformValues(extensions, StmtContext::buildEffective));
95 final Map<QName, StmtContext<?, FeatureStatement, FeatureEffectiveStatement>> features =
96 ctx.getAllFromCurrentStmtCtxNamespace(FeatureNamespace.class);
97 qnameToFeature = features == null ? ImmutableMap.of()
98 : ImmutableMap.copyOf(Maps.transformValues(features, StmtContext::buildEffective));
99 final Map<QName, StmtContext<?, IdentityStatement, IdentityEffectiveStatement>> identities =
100 ctx.getAllFromCurrentStmtCtxNamespace(IdentityNamespace.class);
101 qnameToIdentity = identities == null ? ImmutableMap.of()
102 : ImmutableMap.copyOf(Maps.transformValues(identities, StmtContext::buildEffective));
105 ModuleEffectiveStatementImpl(final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> ctx) {
106 this(ModuleStmtContext.create(ctx));
110 public @NonNull QNameModule localQNameModule() {
115 public @NonNull QNameModule getQNameModule() {
120 public ImmutableSet<Module> getSubmodules() {
125 @SuppressWarnings("unchecked")
126 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
127 final @NonNull Class<N> namespace) {
128 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
129 return Optional.of((Map<K, V>) prefixToModule);
131 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
132 return Optional.of((Map<K, V>) namespaceToPrefix);
134 if (NameToEffectiveSubmoduleNamespace.class.equals(namespace)) {
135 return Optional.of((Map<K, V>) nameToSubmodule);
137 if (ExtensionEffectiveStatementNamespace.class.equals(namespace)) {
138 return Optional.of((Map<K, V>) qnameToExtension);
140 if (FeatureEffectiveStatementNamespace.class.equals(namespace)) {
141 return Optional.of((Map<K, V>) qnameToFeature);
143 if (IdentityEffectiveStatementNamespace.class.equals(namespace)) {
144 return Optional.of((Map<K, V>) qnameToIdentity);
146 return super.getNamespaceContents(namespace);