Add Case/Extension/Feature/IdentityEffectiveStatementNamespace
[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.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;
38
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;
46
47     ModuleEffectiveStatementImpl(
48             final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
49         super(ctx);
50         qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx));
51
52         final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
53         final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
54         prefixToModuleBuilder.put(localPrefix, this);
55
56         streamEffectiveSubstatements(ImportEffectiveStatement.class)
57                 .map(imp -> imp.findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get())
58                 .forEach(prefix -> {
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());
63                 });
64         prefixToModule = prefixToModuleBuilder.build();
65
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());
70         }
71         namespaceToPrefix = ImmutableMap.copyOf(tmp);
72
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()));
78
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()));
84     }
85
86     @Override
87     public @NonNull QNameModule localQNameModule() {
88         return qnameModule;
89     }
90
91     @Override
92     public @NonNull QNameModule getQNameModule() {
93         return qnameModule;
94     }
95
96     @Override
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);
102         }
103         if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
104             return Optional.of((Map<K, V>) namespaceToPrefix);
105         }
106         if (NameToEffectiveSubmoduleNamespace.class.equals(namespace)) {
107             return Optional.of((Map<K, V>) nameToSubmodule);
108         }
109         if (IdentityEffectiveStatementNamespace.class.equals(namespace)) {
110             return Optional.of((Map<K, V>) qnameToIdentity);
111         }
112         return super.getNamespaceContents(namespace);
113     }
114
115     @Override
116     public int hashCode() {
117         final int prime = 31;
118         int result = 1;
119         result = prime * result + Objects.hashCode(getName());
120         result = prime * result + Objects.hashCode(getYangVersion());
121         result = prime * result + Objects.hashCode(qnameModule);
122         return result;
123     }
124
125     @Override
126     public boolean equals(final Object obj) {
127         if (this == obj) {
128             return true;
129         }
130         if (!(obj instanceof ModuleEffectiveStatementImpl)) {
131             return false;
132         }
133         ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
134         if (!Objects.equals(getName(), other.getName())) {
135             return false;
136         }
137         if (!qnameModule.equals(other.qnameModule)) {
138             return false;
139         }
140         if (!Objects.equals(getYangVersion(), other.getYangVersion())) {
141             return false;
142         }
143         return true;
144     }
145 }