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.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.Iterables;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Date;
19 import java.util.HashSet;
20 import java.util.LinkedHashMap;
21 import java.util.LinkedHashSet;
22 import java.util.List;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.yangtools.concepts.SemVer;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
30 import org.opendaylight.yangtools.yang.common.YangVersion;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Deviation;
34 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
35 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
36 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
37 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
40 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
41 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
42 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
43 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.UsesNode;
46 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
47 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
48 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
49 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
50 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
54 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
55 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
56 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
58 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
59 AbstractEffectiveDocumentedNode<String, D> implements Module, MutableStatement {
60 private final String name;
61 private final String sourcePath;
62 private final String prefix;
63 private final YangVersion yangVersion;
64 private final String organization;
65 private final String contact;
66 private final Set<ModuleImport> imports;
67 private final Set<FeatureDefinition> features;
68 private final Set<NotificationDefinition> notifications;
69 private final Set<AugmentationSchema> augmentations;
70 private final Set<RpcDefinition> rpcs;
71 private final Set<Deviation> deviations;
72 private final List<ExtensionDefinition> extensionNodes;
73 private final Set<IdentitySchemaNode> identities;
74 private final List<UnknownSchemaNode> unknownNodes;
75 private final Map<QName, DataSchemaNode> childNodes;
76 private final Set<GroupingDefinition> groupings;
77 private final Set<UsesNode> uses;
78 private final Set<TypeDefinition<?>> typeDefinitions;
79 private final Set<DataSchemaNode> publicChildNodes;
80 private final SemVer semanticVersion;
81 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContextsToBuild;
82 private Set<Module> submodules;
83 private boolean sealed;
85 AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
88 this.name = argument();
90 final PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
91 this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
93 final YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
94 this.yangVersion = (yangVersionStmt == null) ? YangVersion.VERSION_1 : yangVersionStmt.argument();
96 final OpenconfigVersionEffectiveStatementImpl semanticVersionStmt =
97 firstEffective(OpenconfigVersionEffectiveStatementImpl.class);
98 this.semanticVersion = (semanticVersionStmt == null) ? DEFAULT_SEMANTIC_VERSION : semanticVersionStmt.argument();
100 final OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
101 this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
103 final ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
104 this.contact = (contactStmt == null) ? null : contactStmt.argument();
106 if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
107 this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
109 this.sourcePath = null;
112 // init submodules and substatements of submodules
113 final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
114 final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
115 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
117 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
118 this.submodules = ImmutableSet.of();
119 this.submoduleContextsToBuild = ImmutableSet.of();
120 substatementsOfSubmodules = ImmutableList.of();
121 } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
123 * Aggregation of substatements from submodules should be done only
124 * for modules. In case of submodules it does not make sense because
125 * of possible circular chains of includes between submodules.
127 final Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
128 final Set<Module> submodulesInit = new HashSet<>();
129 final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
130 for (final ModuleIdentifier submoduleIdentifier : includedSubmodules) {
131 @SuppressWarnings("unchecked")
132 final Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
133 .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
134 final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
136 submodulesInit.add(submodule);
137 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
140 this.submodules = ImmutableSet.copyOf(submodulesInit);
141 this.submoduleContextsToBuild = ImmutableSet.of();
142 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
145 * Because of possible circular chains of includes between submodules we can
146 * collect only submodule contexts here and then build them during
147 * sealing of this statement.
149 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContextsInit = new HashSet<>();
150 for (final ModuleIdentifier submoduleIdentifier : includedSubmodulesMap.values()) {
151 final StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = ctx
152 .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
153 submoduleContextsInit.add(submoduleCtx);
156 this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
157 substatementsOfSubmodules = ImmutableList.of();
160 if (!submoduleContextsToBuild.isEmpty()) {
161 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
167 // init substatements collections
168 final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
169 effectiveSubstatements.addAll(effectiveSubstatements());
170 effectiveSubstatements.addAll(substatementsOfSubmodules);
172 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
173 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
174 final Set<ModuleImport> importsInit = new HashSet<>();
175 final Set<NotificationDefinition> notificationsInit = new HashSet<>();
176 final Set<RpcDefinition> rpcsInit = new HashSet<>();
177 final Set<Deviation> deviationsInit = new HashSet<>();
178 final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
179 final Set<FeatureDefinition> featuresInit = new HashSet<>();
180 final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
182 final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
183 final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
184 final Set<UsesNode> mutableUses = new HashSet<>();
185 final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
186 final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
188 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
189 if (effectiveStatement instanceof UnknownSchemaNode) {
190 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
192 if (effectiveStatement instanceof AugmentationSchema) {
193 augmentationsInit.add((AugmentationSchema) effectiveStatement);
195 if (effectiveStatement instanceof ModuleImport) {
196 importsInit.add((ModuleImport) effectiveStatement);
198 if (effectiveStatement instanceof NotificationDefinition) {
199 notificationsInit.add((NotificationDefinition) effectiveStatement);
201 if (effectiveStatement instanceof RpcDefinition) {
202 rpcsInit.add((RpcDefinition) effectiveStatement);
204 if (effectiveStatement instanceof Deviation) {
205 deviationsInit.add((Deviation) effectiveStatement);
207 if (effectiveStatement instanceof IdentitySchemaNode) {
208 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
210 if (effectiveStatement instanceof FeatureDefinition) {
211 featuresInit.add((FeatureDefinition) effectiveStatement);
213 if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
214 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
216 if (effectiveStatement instanceof DataSchemaNode) {
217 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
218 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
219 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
220 mutablePublicChildNodes.add(dataSchemaNode);
222 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
225 if (effectiveStatement instanceof UsesNode) {
226 final UsesNode usesNode = (UsesNode) effectiveStatement;
227 if (!mutableUses.contains(usesNode)) {
228 mutableUses.add(usesNode);
230 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
233 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
234 final TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
235 final TypeDefinition<?> type = typeDef.getTypeDefinition();
236 if (!mutableTypeDefinitions.contains(type)) {
237 mutableTypeDefinitions.add(type);
239 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
242 if (effectiveStatement instanceof GroupingDefinition) {
243 final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
244 if (!mutableGroupings.contains(grp)) {
245 mutableGroupings.add(grp);
247 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
252 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
253 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
254 if (ctx.isEnabledSemanticVersioning()) {
255 this.imports = ImmutableSet.copyOf(importsInit);
257 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
259 this.notifications = ImmutableSet.copyOf(notificationsInit);
260 this.rpcs = ImmutableSet.copyOf(rpcsInit);
261 this.deviations = ImmutableSet.copyOf(deviationsInit);
262 this.identities = ImmutableSet.copyOf(identitiesInit);
263 this.features = ImmutableSet.copyOf(featuresInit);
264 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
266 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
267 this.groupings = ImmutableSet.copyOf(mutableGroupings);
268 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
269 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
270 this.uses = ImmutableSet.copyOf(mutableUses);
274 private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
275 final StmtContext<String, ? extends DeclaredStatement<String>, ? extends EffectiveStatement<String, ?>> ctx) {
276 final Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
277 for (final ModuleImport moduleImport : importsInit) {
278 if (moduleImport.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
279 final QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, moduleImport.getPrefix());
280 final ModuleImport resolvedModuleImport = new ModuleImportImpl(moduleImport.getModuleName(),
281 impModuleQName.getRevision(), moduleImport.getPrefix());
282 resolvedModuleImports.add(resolvedModuleImport);
284 resolvedModuleImports.add(moduleImport);
287 return resolvedModuleImports;
291 public String getModuleSourcePath() {
296 public String getSource() {
301 public URI getNamespace() {
302 return getQNameModule().getNamespace();
306 public String getName() {
311 public Date getRevision() {
312 return getQNameModule().getRevision();
316 public String getPrefix() {
321 public String getYangVersion() {
322 return yangVersion.toString();
326 public String getOrganization() {
331 public String getContact() {
336 public Set<ModuleImport> getImports() {
341 public Set<Module> getSubmodules() {
342 Preconditions.checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
348 public Set<FeatureDefinition> getFeatures() {
353 public Set<NotificationDefinition> getNotifications() {
354 return notifications;
358 public Set<AugmentationSchema> getAugmentations() {
359 return augmentations;
363 public Set<RpcDefinition> getRpcs() {
368 public Set<Deviation> getDeviations() {
373 public List<ExtensionDefinition> getExtensionSchemaNodes() {
374 return extensionNodes;
378 public Set<IdentitySchemaNode> getIdentities() {
384 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
389 public final Set<TypeDefinition<?>> getTypeDefinitions() {
390 return typeDefinitions;
394 public final Set<DataSchemaNode> getChildNodes() {
395 return publicChildNodes;
399 public final Set<GroupingDefinition> getGroupings() {
404 public final DataSchemaNode getDataChildByName(final QName name) {
405 // Child nodes are keyed by their container name, so we can do a direct
407 return childNodes.get(name);
411 public Set<UsesNode> getUses() {
416 public SemVer getSemanticVersion() {
417 return semanticVersion;
421 public String toString() {
422 return this.getClass().getSimpleName() + "[" +
424 ", namespace=" + getNamespace() +
425 ", revision=" + getQNameModule().getFormattedRevision() +
426 ", prefix=" + prefix +
427 ", yangVersion=" + yangVersion +
434 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
435 ctx -> (Module) ctx.buildEffective()));
436 submoduleContextsToBuild = ImmutableSet.of();