d57aef083ff907d96c2dc09ca1a055dd3afa4502
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / module / ModuleEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.module;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import com.google.common.collect.Maps;
15 import java.util.Map;
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;
37
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;
45
46     ModuleEffectiveStatementImpl(
47             final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
48         super(ctx);
49         qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx));
50
51         final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
52         final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
53         prefixToModuleBuilder.put(localPrefix, this);
54
55         streamEffectiveSubstatements(ImportEffectiveStatement.class)
56                 .map(imp -> imp.findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get())
57                 .forEach(prefix -> {
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());
62                 });
63         prefixToModule = prefixToModuleBuilder.build();
64
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());
69         }
70         namespaceToPrefix = ImmutableMap.copyOf(tmp);
71
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()));
77
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()));
83     }
84
85     @Override
86     public @NonNull QNameModule localQNameModule() {
87         return qnameModule;
88     }
89
90     @Override
91     public @NonNull QNameModule getQNameModule() {
92         return qnameModule;
93     }
94
95     @Override
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);
101         }
102         if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
103             return Optional.of((Map<K, V>) namespaceToPrefix);
104         }
105         if (NameToEffectiveSubmoduleNamespace.class.equals(namespace)) {
106             return Optional.of((Map<K, V>) nameToSubmodule);
107         }
108         if (QNameToEffectiveIdentityNamespace.class.equals(namespace)) {
109             return Optional.of((Map<K, V>) qnameToIdentity);
110         }
111         return super.getNamespaceContents(namespace);
112     }
113
114     @Override
115     public int hashCode() {
116         final int prime = 31;
117         int result = 1;
118         result = prime * result + Objects.hashCode(getName());
119         result = prime * result + Objects.hashCode(getYangVersion());
120         result = prime * result + Objects.hashCode(qnameModule);
121         return result;
122     }
123
124     @Override
125     public boolean equals(final Object obj) {
126         if (this == obj) {
127             return true;
128         }
129         if (!(obj instanceof ModuleEffectiveStatementImpl)) {
130             return false;
131         }
132         ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
133         if (!Objects.equals(getName(), other.getName())) {
134             return false;
135         }
136         if (!qnameModule.equals(other.qnameModule)) {
137             return false;
138         }
139         if (!Objects.equals(getYangVersion(), other.getYangVersion())) {
140             return false;
141         }
142         return true;
143     }
144 }