2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Date;
17 import java.util.HashSet;
18 import java.util.LinkedHashMap;
19 import java.util.LinkedHashSet;
20 import java.util.List;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.yangtools.concepts.Immutable;
25 import org.opendaylight.yangtools.concepts.SemVer;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Deviation;
32 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
33 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
34 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
35 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
38 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
39 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
40 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
41 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.UsesNode;
44 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
45 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
47 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
50 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
51 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
54 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
55 AbstractEffectiveDocumentedNode<String, D> implements Module, Immutable {
57 private final String name;
58 private final String sourcePath;
59 private final String prefix;
60 private final String yangVersion;
61 private final String organization;
62 private final String contact;
63 private final Set<ModuleImport> imports;
64 private final Set<Module> submodules;
65 private final Set<FeatureDefinition> features;
66 private final Set<NotificationDefinition> notifications;
67 private final Set<AugmentationSchema> augmentations;
68 private final Set<RpcDefinition> rpcs;
69 private final Set<Deviation> deviations;
70 private final List<ExtensionDefinition> extensionNodes;
71 private final Set<IdentitySchemaNode> identities;
72 private final List<UnknownSchemaNode> unknownNodes;
73 private final Map<QName, DataSchemaNode> childNodes;
74 private final Set<GroupingDefinition> groupings;
75 private final Set<UsesNode> uses;
76 private final Set<TypeDefinition<?>> typeDefinitions;
77 private final Set<DataSchemaNode> publicChildNodes;
78 private final SemVer semanticVersion;
80 AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
83 this.name = argument();
85 PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
86 this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
88 YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
89 this.yangVersion = (yangVersionStmt == null) ? "1" : yangVersionStmt.argument();
91 SemanticVersionEffectiveStatementImpl semanticVersionStmt = firstEffective(SemanticVersionEffectiveStatementImpl.class);
92 this.semanticVersion = (semanticVersionStmt == null) ? DEFAULT_SEMANTIC_VERSION : semanticVersionStmt.argument();
94 OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
95 this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
97 ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
98 this.contact = (contactStmt == null) ? null : contactStmt.argument();
100 if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
101 this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
103 this.sourcePath = null;
106 // init submodules and substatements of submodules
107 final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
108 final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
109 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
111 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
112 this.submodules = ImmutableSet.of();
113 substatementsOfSubmodules = ImmutableList.of();
115 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
116 Set<Module> submodulesInit = new HashSet<>();
117 List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
118 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
119 @SuppressWarnings("unchecked")
120 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
121 (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)
122 ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
123 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
125 submodulesInit.add(submodule);
126 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
129 this.submodules = ImmutableSet.copyOf(submodulesInit);
130 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
133 // init substatements collections
134 List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
135 effectiveSubstatements.addAll(effectiveSubstatements());
136 effectiveSubstatements.addAll(substatementsOfSubmodules);
138 List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
139 Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
140 Set<ModuleImport> importsInit = new HashSet<>();
141 Set<NotificationDefinition> notificationsInit = new HashSet<>();
142 Set<RpcDefinition> rpcsInit = new HashSet<>();
143 Set<Deviation> deviationsInit = new HashSet<>();
144 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
145 Set<FeatureDefinition> featuresInit = new HashSet<>();
146 List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
148 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
149 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
150 Set<UsesNode> mutableUses = new HashSet<>();
151 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
152 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
154 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
155 if (effectiveStatement instanceof UnknownSchemaNode) {
156 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
158 if (effectiveStatement instanceof AugmentationSchema) {
159 augmentationsInit.add((AugmentationSchema) effectiveStatement);
161 if (effectiveStatement instanceof ModuleImport) {
162 importsInit.add((ModuleImport) effectiveStatement);
164 if (effectiveStatement instanceof NotificationDefinition) {
165 notificationsInit.add((NotificationDefinition) effectiveStatement);
167 if (effectiveStatement instanceof RpcDefinition) {
168 rpcsInit.add((RpcDefinition) effectiveStatement);
170 if (effectiveStatement instanceof Deviation) {
171 deviationsInit.add((Deviation) effectiveStatement);
173 if (effectiveStatement instanceof IdentitySchemaNode) {
174 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
176 if (effectiveStatement instanceof FeatureDefinition) {
177 featuresInit.add((FeatureDefinition) effectiveStatement);
179 if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
180 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
182 if (effectiveStatement instanceof DataSchemaNode) {
183 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
184 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
185 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
186 mutablePublicChildNodes.add(dataSchemaNode);
188 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
191 if (effectiveStatement instanceof UsesNode) {
192 UsesNode usesNode = (UsesNode) effectiveStatement;
193 if (!mutableUses.contains(usesNode)) {
194 mutableUses.add(usesNode);
196 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
199 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
200 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
201 TypeDefinition<?> type = typeDef.getTypeDefinition();
202 if (!mutableTypeDefinitions.contains(type)) {
203 mutableTypeDefinitions.add(type);
205 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
208 if (effectiveStatement instanceof GroupingDefinition) {
209 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
210 if (!mutableGroupings.contains(grp)) {
211 mutableGroupings.add(grp);
213 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
218 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
219 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
220 if (ctx.isEnabledSemanticVersioning()) {
221 this.imports = ImmutableSet.copyOf(importsInit);
223 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
225 this.notifications = ImmutableSet.copyOf(notificationsInit);
226 this.rpcs = ImmutableSet.copyOf(rpcsInit);
227 this.deviations = ImmutableSet.copyOf(deviationsInit);
228 this.identities = ImmutableSet.copyOf(identitiesInit);
229 this.features = ImmutableSet.copyOf(featuresInit);
230 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
232 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
233 this.groupings = ImmutableSet.copyOf(mutableGroupings);
234 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
235 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
236 this.uses = ImmutableSet.copyOf(mutableUses);
240 private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
241 final StmtContext<String, ? extends DeclaredStatement<String>, ? extends EffectiveStatement<String, ?>> ctx) {
242 final Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
243 for (ModuleImport moduleImport : importsInit) {
244 if (moduleImport.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
245 final QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, moduleImport.getPrefix());
246 final ModuleImport resolvedModuleImport = new ModuleImportImpl(moduleImport.getModuleName(),
247 impModuleQName.getRevision(), moduleImport.getPrefix());
248 resolvedModuleImports.add(resolvedModuleImport);
250 resolvedModuleImports.add(moduleImport);
253 return resolvedModuleImports;
257 public String getModuleSourcePath() {
262 public String getSource() {
267 public URI getNamespace() {
268 return getQNameModule().getNamespace();
272 public String getName() {
277 public Date getRevision() {
278 return getQNameModule().getRevision();
282 public String getPrefix() {
287 public String getYangVersion() {
292 public String getOrganization() {
297 public String getContact() {
302 public Set<ModuleImport> getImports() {
307 public Set<Module> getSubmodules() {
312 public Set<FeatureDefinition> getFeatures() {
317 public Set<NotificationDefinition> getNotifications() {
318 return notifications;
322 public Set<AugmentationSchema> getAugmentations() {
323 return augmentations;
327 public Set<RpcDefinition> getRpcs() {
332 public Set<Deviation> getDeviations() {
337 public List<ExtensionDefinition> getExtensionSchemaNodes() {
338 return extensionNodes;
342 public Set<IdentitySchemaNode> getIdentities() {
348 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
353 public final Set<TypeDefinition<?>> getTypeDefinitions() {
354 return typeDefinitions;
358 public final Set<DataSchemaNode> getChildNodes() {
359 return publicChildNodes;
363 public final Set<GroupingDefinition> getGroupings() {
368 public final DataSchemaNode getDataChildByName(final QName name) {
369 // Child nodes are keyed by their container name, so we can do a direct
371 return childNodes.get(name);
375 public Set<UsesNode> getUses() {
380 public SemVer getSemanticVersion() {
381 return semanticVersion;
385 public String toString() {
386 return this.getClass().getSimpleName() + "[" +
388 ", namespace=" + getNamespace() +
389 ", revision=" + getRevision() +
390 ", prefix=" + prefix +
391 ", yangVersion=" + yangVersion +