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.IdentityStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
32 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
35 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
36 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
38 final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleStatement>
39 implements ModuleEffectiveStatement {
40 private final Map<String, SubmoduleEffectiveStatement> nameToSubmodule;
41 private final Map<QName, IdentityEffectiveStatement> qnameToIdentity;
42 private final Map<String, ModuleEffectiveStatement> prefixToModule;
43 private final Map<QNameModule, String> namespaceToPrefix;
44 private final @NonNull QNameModule qnameModule;
46 ModuleEffectiveStatementImpl(
47 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
49 qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx));
51 final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
52 final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
53 prefixToModuleBuilder.put(localPrefix, this);
55 streamEffectiveSubstatements(ImportEffectiveStatement.class)
56 .map(imp -> imp.findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get())
58 final StmtContext<?, ?, ?> importedCtx =
59 verifyNotNull(ctx.getFromNamespace(ImportPrefixToModuleCtx.class, prefix),
60 "Failed to resolve prefix %s", prefix);
61 prefixToModuleBuilder.put(prefix, (ModuleEffectiveStatement) importedCtx.buildEffective());
63 prefixToModule = prefixToModuleBuilder.build();
65 final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size() + 1);
66 tmp.put(qnameModule, localPrefix);
67 for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
68 tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
70 namespaceToPrefix = ImmutableMap.copyOf(tmp);
72 final Map<String, StmtContext<?, ?, ?>> submodules =
73 ctx.getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
74 nameToSubmodule = submodules == null ? ImmutableMap.of()
75 : ImmutableMap.copyOf(Maps.transformValues(submodules,
76 submodule -> (SubmoduleEffectiveStatement) submodule.buildEffective()));
78 final Map<QName, StmtContext<?, IdentityStatement, EffectiveStatement<QName, IdentityStatement>>> identities =
79 ctx.getAllFromCurrentStmtCtxNamespace(IdentityNamespace.class);
80 qnameToIdentity = identities == null ? ImmutableMap.of()
81 : ImmutableMap.copyOf(Maps.transformValues(identities,
82 stmt -> (IdentityEffectiveStatement) stmt.buildEffective()));
86 public @NonNull QNameModule localQNameModule() {
91 public @NonNull QNameModule getQNameModule() {
96 @SuppressWarnings("unchecked")
97 public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
98 final @NonNull Class<N> namespace) {
99 if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
100 return Optional.of((Map<K, V>) prefixToModule);
102 if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
103 return Optional.of((Map<K, V>) namespaceToPrefix);
105 if (NameToEffectiveSubmoduleNamespace.class.equals(namespace)) {
106 return Optional.of((Map<K, V>) nameToSubmodule);
108 if (QNameToEffectiveIdentityNamespace.class.equals(namespace)) {
109 return Optional.of((Map<K, V>) qnameToIdentity);
111 return super.getNamespaceContents(namespace);
115 public int hashCode() {
116 final int prime = 31;
118 result = prime * result + Objects.hashCode(getName());
119 result = prime * result + Objects.hashCode(getYangVersion());
120 result = prime * result + Objects.hashCode(qnameModule);
125 public boolean equals(final Object obj) {
129 if (!(obj instanceof ModuleEffectiveStatementImpl)) {
132 ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
133 if (!Objects.equals(getName(), other.getName())) {
136 if (!qnameModule.equals(other.qnameModule)) {
139 if (!Objects.equals(getYangVersion(), other.getYangVersion())) {