Fixup collections return implementations
[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.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import com.google.common.collect.Maps;
16 import java.util.Collection;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.Submodule;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatementNamespace;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatementNamespace;
33 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
34 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatementNamespace;
36 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
37 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
38 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
39 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
40 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
41 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
42 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.FeatureNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
46 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
47 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
48
49 final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleStatement, ModuleEffectiveStatement>
50         implements Module, ModuleEffectiveStatement {
51     private final ImmutableMap<String, SubmoduleEffectiveStatement> nameToSubmodule;
52     private final ImmutableMap<QName, ExtensionEffectiveStatement> qnameToExtension;
53     private final ImmutableMap<QName, FeatureEffectiveStatement> qnameToFeature;
54     private final ImmutableMap<QName, IdentityEffectiveStatement> qnameToIdentity;
55     private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
56     private final ImmutableMap<QNameModule, String> namespaceToPrefix;
57     private final @NonNull QNameModule qnameModule;
58     private final ImmutableList<Submodule> submodules;
59
60     ModuleEffectiveStatementImpl(final StmtContext<UnqualifiedQName, ModuleStatement, ModuleEffectiveStatement> ctx,
61             final ModuleStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
62             final Collection<? extends Submodule> submodules) {
63         super(declared, ctx, substatements, findPrefix(ctx, "module", ctx.coerceRawStatementArgument()));
64
65         qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx));
66         this.submodules = ImmutableList.copyOf(submodules);
67
68         final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
69         final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
70         prefixToModuleBuilder.put(localPrefix, this);
71         appendPrefixes(ctx, prefixToModuleBuilder);
72         prefixToModule = prefixToModuleBuilder.build();
73
74         final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size() + 1);
75         tmp.put(qnameModule, localPrefix);
76         for (Entry<String, ModuleEffectiveStatement> e : prefixToModule.entrySet()) {
77             tmp.putIfAbsent(e.getValue().localQNameModule(), e.getKey());
78         }
79         namespaceToPrefix = ImmutableMap.copyOf(tmp);
80
81         final Map<String, StmtContext<?, ?, ?>> includedSubmodules =
82                 ctx.getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
83         nameToSubmodule = includedSubmodules == null ? ImmutableMap.of()
84                 : ImmutableMap.copyOf(Maps.transformValues(includedSubmodules,
85                     submodule -> (SubmoduleEffectiveStatement) submodule.buildEffective()));
86
87         final Map<QName, StmtContext<?, ExtensionStatement, ExtensionEffectiveStatement>> extensions =
88                 ctx.getAllFromCurrentStmtCtxNamespace(ExtensionNamespace.class);
89         qnameToExtension = extensions == null ? ImmutableMap.of()
90                 : ImmutableMap.copyOf(Maps.transformValues(extensions, StmtContext::buildEffective));
91         final Map<QName, StmtContext<?, FeatureStatement, FeatureEffectiveStatement>> features =
92                 ctx.getAllFromCurrentStmtCtxNamespace(FeatureNamespace.class);
93         qnameToFeature = features == null ? ImmutableMap.of()
94                 : ImmutableMap.copyOf(Maps.transformValues(features, StmtContext::buildEffective));
95         final Map<QName, StmtContext<?, IdentityStatement, IdentityEffectiveStatement>> identities =
96                 ctx.getAllFromCurrentStmtCtxNamespace(IdentityNamespace.class);
97         qnameToIdentity = identities == null ? ImmutableMap.of()
98                 : ImmutableMap.copyOf(Maps.transformValues(identities, StmtContext::buildEffective));
99     }
100
101     @Override
102     public @NonNull QNameModule localQNameModule() {
103         return qnameModule;
104     }
105
106     @Override
107     public @NonNull QNameModule getQNameModule() {
108         return qnameModule;
109     }
110
111     @Override
112     public Collection<? extends @NonNull Submodule> getSubmodules() {
113         return submodules;
114     }
115
116     @Override
117     public ModuleEffectiveStatement asEffectiveStatement() {
118         return this;
119     }
120
121     @Override
122     @SuppressWarnings("unchecked")
123     public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
124             final @NonNull Class<N> namespace) {
125         if (PrefixToEffectiveModuleNamespace.class.equals(namespace)) {
126             return Optional.of((Map<K, V>) prefixToModule);
127         }
128         if (QNameModuleToPrefixNamespace.class.equals(namespace)) {
129             return Optional.of((Map<K, V>) namespaceToPrefix);
130         }
131         if (NameToEffectiveSubmoduleNamespace.class.equals(namespace)) {
132             return Optional.of((Map<K, V>) nameToSubmodule);
133         }
134         if (ExtensionEffectiveStatementNamespace.class.equals(namespace)) {
135             return Optional.of((Map<K, V>) qnameToExtension);
136         }
137         if (FeatureEffectiveStatementNamespace.class.equals(namespace)) {
138             return Optional.of((Map<K, V>) qnameToFeature);
139         }
140         if (IdentityEffectiveStatementNamespace.class.equals(namespace)) {
141             return Optional.of((Map<K, V>) qnameToIdentity);
142         }
143         return super.getNamespaceContents(namespace);
144     }
145 }