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.Maps;
16 import java.util.Map.Entry;
17 import java.util.Objects;
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.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
24 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatementNamespace;
26 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
33 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
36 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
37 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
39 final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleStatement>
40 implements ModuleEffectiveStatement {
41 private final Map<String, SubmoduleEffectiveStatement> nameToSubmodule;
42 private final Map<QName, IdentityEffectiveStatement> qnameToIdentity;
43 private final Map<String, ModuleEffectiveStatement> prefixToModule;
44 private final Map<QNameModule, String> namespaceToPrefix;
45 private final @NonNull QNameModule qnameModule;
47 ModuleEffectiveStatementImpl(
48 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
50 qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx));
52 final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
53 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
54 prefixToModuleBuilder.put(localPrefix, this);
56 streamEffectiveSubstatements(ImportEffectiveStatement.class)
57 .map(imp -> imp.findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get())
59 final StmtContext<?, ?, ?> importedCtx =
60 verifyNotNull(ctx.getFromNamespace(ImportPrefixToModuleCtx.class, prefix),
61 "Failed to resolve prefix %s", prefix);
62 prefixToModuleBuilder.put(prefix, (ModuleEffectiveStatement) importedCtx.buildEffective());
64 prefixToModule = prefixToModuleBuilder.build();
66 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size() + 1);
67 tmp.put(qnameModule, localPrefix);
68 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
69 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
71 namespaceToPrefix = ImmutableMap.copyOf(tmp);
73 final Map<String, StmtContext<?, ?, ?>> submodules =
74 ctx.getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
75 nameToSubmodule = submodules == null ? ImmutableMap.of()
76 : ImmutableMap.copyOf(Maps.transformValues(submodules,
77 submodule -> (SubmoduleEffectiveStatement) submodule.buildEffective()));
79 final Map<QName, StmtContext<?, IdentityStatement, EffectiveStatement<QName, IdentityStatement>>> identities =
80 ctx.getAllFromCurrentStmtCtxNamespace(IdentityNamespace.class);
81 qnameToIdentity = identities == null ? ImmutableMap.of()
82 : ImmutableMap.copyOf(Maps.transformValues(identities,
83 stmt -> (IdentityEffectiveStatement) stmt.buildEffective()));
87 public @NonNull QNameModule localQNameModule() {
92 public @NonNull QNameModule getQNameModule() {
97 @SuppressWarnings("unchecked")
98 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
99 final @NonNull Class<N> namespace) {
100 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
101 return Optional.of((Map<K, V>) prefixToModule);
103 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
104 return Optional.of((Map<K, V>) namespaceToPrefix);
106 if (NameToEffectiveSubmoduleNamespace.class.equals(namespace)) {
107 return Optional.of((Map<K, V>) nameToSubmodule);
109 if (IdentityEffectiveStatementNamespace.class.equals(namespace)) {
110 return Optional.of((Map<K, V>) qnameToIdentity);
112 return super.getNamespaceContents(namespace);
116 public int hashCode() {
117 final int prime = 31;
119 result = prime * result + Objects.hashCode(getName());
120 result = prime * result + Objects.hashCode(getYangVersion());
121 result = prime * result + Objects.hashCode(qnameModule);
126 public boolean equals(final Object obj) {
130 if (!(obj instanceof ModuleEffectiveStatementImpl)) {
133 ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
134 if (!Objects.equals(getName(), other.getName())) {
137 if (!qnameModule.equals(other.qnameModule)) {
140 if (!Objects.equals(getYangVersion(), other.getYangVersion())) {