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