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