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 java.util.stream.Collectors;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yangtools.concepts.SemVer;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.YangVersion;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
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.SchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.UsesNode;
47 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
48 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
49 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
50 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
51 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
54 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
55 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
56 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
57 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
59 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
60 AbstractEffectiveDocumentedNode<String, D> implements Module, MutableStatement {
61 private final String name;
62 private final String sourcePath;
63 private final String prefix;
64 private final YangVersion yangVersion;
65 private final String organization;
66 private final String contact;
67 private final Set<ModuleImport> imports;
68 private final Set<FeatureDefinition> features;
69 private final Set<NotificationDefinition> notifications;
70 private final Set<AugmentationSchema> augmentations;
71 private final Set<RpcDefinition> rpcs;
72 private final Set<Deviation> deviations;
73 private final List<ExtensionDefinition> extensionNodes;
74 private final Set<IdentitySchemaNode> identities;
75 private final List<UnknownSchemaNode> unknownNodes;
76 private final Map<QName, DataSchemaNode> childNodes;
77 private final Set<GroupingDefinition> groupings;
78 private final Set<UsesNode> uses;
79 private final Set<TypeDefinition<?>> typeDefinitions;
80 private final Set<DataSchemaNode> publicChildNodes;
81 private final SemVer semanticVersion;
82 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContextsToBuild;
83 private Set<Module> submodules;
84 private boolean sealed;
86 AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
89 this.name = argument();
91 EffectiveStatementBase<?, ?> parentOfPrefix = this;
92 if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
93 parentOfPrefix = firstEffective(BelongsToEffectiveStatementImpl.class);
94 SourceException.throwIfNull(parentOfPrefix, ctx.getStatementSourceReference(),
95 "Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
98 final PrefixEffectiveStatementImpl prefixStmt = parentOfPrefix
99 .firstEffective(PrefixEffectiveStatementImpl.class);
100 SourceException.throwIfNull(prefixStmt, ctx.getStatementSourceReference(),
101 "Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
102 this.prefix = prefixStmt.argument();
104 final YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
105 this.yangVersion = (yangVersionStmt == null) ? YangVersion.VERSION_1 : yangVersionStmt.argument();
107 final OpenconfigVersionEffectiveStatementImpl semanticVersionStmt =
108 firstEffective(OpenconfigVersionEffectiveStatementImpl.class);
109 this.semanticVersion = (semanticVersionStmt == null) ? DEFAULT_SEMANTIC_VERSION : semanticVersionStmt.argument();
111 final OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
112 this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
114 final ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
115 this.contact = (contactStmt == null) ? null : contactStmt.argument();
117 if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
118 this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
120 this.sourcePath = null;
123 // init submodules and substatements of submodules
124 final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
125 final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
126 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
128 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
129 this.submodules = ImmutableSet.of();
130 this.submoduleContextsToBuild = ImmutableSet.of();
131 substatementsOfSubmodules = ImmutableList.of();
132 } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
134 * Aggregation of substatements from submodules should be done only
135 * for modules. In case of submodules it does not make sense because
136 * of possible circular chains of includes between submodules.
138 final Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
139 final Set<Module> submodulesInit = new HashSet<>();
140 final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
141 for (final ModuleIdentifier submoduleIdentifier : includedSubmodules) {
142 @SuppressWarnings("unchecked")
143 final Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
144 .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
145 final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
147 submodulesInit.add(submodule);
148 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
149 .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
150 .collect(Collectors.toList()));
153 this.submodules = ImmutableSet.copyOf(submodulesInit);
154 this.submoduleContextsToBuild = ImmutableSet.of();
155 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
158 * Because of possible circular chains of includes between submodules we can
159 * collect only submodule contexts here and then build them during
160 * sealing of this statement.
162 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContextsInit = new HashSet<>();
163 for (final ModuleIdentifier submoduleIdentifier : includedSubmodulesMap.values()) {
164 final StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = ctx
165 .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
166 submoduleContextsInit.add(submoduleCtx);
169 this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
170 substatementsOfSubmodules = ImmutableList.of();
173 if (!submoduleContextsToBuild.isEmpty()) {
174 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
180 // init substatements collections
181 final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
182 effectiveSubstatements.addAll(effectiveSubstatements());
183 effectiveSubstatements.addAll(substatementsOfSubmodules);
185 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
186 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
187 final Set<ModuleImport> importsInit = new HashSet<>();
188 final Set<NotificationDefinition> notificationsInit = new HashSet<>();
189 final Set<RpcDefinition> rpcsInit = new HashSet<>();
190 final Set<Deviation> deviationsInit = new HashSet<>();
191 final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
192 final Set<FeatureDefinition> featuresInit = new HashSet<>();
193 final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
195 final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
196 final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
197 final Set<UsesNode> mutableUses = new HashSet<>();
198 final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
199 final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
201 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
202 if (effectiveStatement instanceof UnknownSchemaNode) {
203 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
205 if (effectiveStatement instanceof AugmentationSchema) {
206 augmentationsInit.add((AugmentationSchema) effectiveStatement);
208 if (effectiveStatement instanceof ModuleImport) {
209 importsInit.add((ModuleImport) effectiveStatement);
211 if (effectiveStatement instanceof NotificationDefinition) {
212 notificationsInit.add((NotificationDefinition) effectiveStatement);
214 if (effectiveStatement instanceof RpcDefinition) {
215 rpcsInit.add((RpcDefinition) effectiveStatement);
217 if (effectiveStatement instanceof Deviation) {
218 deviationsInit.add((Deviation) effectiveStatement);
220 if (effectiveStatement instanceof IdentitySchemaNode) {
221 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
223 if (effectiveStatement instanceof FeatureDefinition) {
224 featuresInit.add((FeatureDefinition) effectiveStatement);
226 if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
227 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
229 if (effectiveStatement instanceof DataSchemaNode) {
230 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
231 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
232 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
233 mutablePublicChildNodes.add(dataSchemaNode);
235 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
238 if (effectiveStatement instanceof UsesNode) {
239 final UsesNode usesNode = (UsesNode) effectiveStatement;
240 if (!mutableUses.contains(usesNode)) {
241 mutableUses.add(usesNode);
243 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
246 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
247 final TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
248 final TypeDefinition<?> type = typeDef.getTypeDefinition();
249 if (!mutableTypeDefinitions.contains(type)) {
250 mutableTypeDefinitions.add(type);
252 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
255 if (effectiveStatement instanceof GroupingDefinition) {
256 final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
257 if (!mutableGroupings.contains(grp)) {
258 mutableGroupings.add(grp);
260 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
265 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
266 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
267 this.imports = ImmutableSet.copyOf(importsInit);
268 this.notifications = ImmutableSet.copyOf(notificationsInit);
269 this.rpcs = ImmutableSet.copyOf(rpcsInit);
270 this.deviations = ImmutableSet.copyOf(deviationsInit);
271 this.identities = ImmutableSet.copyOf(identitiesInit);
272 this.features = ImmutableSet.copyOf(featuresInit);
273 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
275 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
276 this.groupings = ImmutableSet.copyOf(mutableGroupings);
277 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
278 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
279 this.uses = ImmutableSet.copyOf(mutableUses);
284 public String getModuleSourcePath() {
289 public String getSource() {
294 public URI getNamespace() {
295 return getQNameModule().getNamespace();
299 public String getName() {
304 public Date getRevision() {
305 return getQNameModule().getRevision();
309 public String getPrefix() {
314 public String getYangVersion() {
315 return yangVersion.toString();
319 public String getOrganization() {
324 public String getContact() {
329 public Set<ModuleImport> getImports() {
334 public Set<Module> getSubmodules() {
335 Preconditions.checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
341 public Set<FeatureDefinition> getFeatures() {
346 public Set<NotificationDefinition> getNotifications() {
347 return notifications;
351 public Set<AugmentationSchema> getAugmentations() {
352 return augmentations;
356 public Set<RpcDefinition> getRpcs() {
361 public Set<Deviation> getDeviations() {
366 public List<ExtensionDefinition> getExtensionSchemaNodes() {
367 return extensionNodes;
371 public Set<IdentitySchemaNode> getIdentities() {
377 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
382 public final Set<TypeDefinition<?>> getTypeDefinitions() {
383 return typeDefinitions;
387 public final Set<DataSchemaNode> getChildNodes() {
388 return publicChildNodes;
392 public final Set<GroupingDefinition> getGroupings() {
397 public final DataSchemaNode getDataChildByName(final QName name) {
398 // Child nodes are keyed by their container name, so we can do a direct
400 return childNodes.get(name);
404 public Set<UsesNode> getUses() {
409 public SemVer getSemanticVersion() {
410 return semanticVersion;
414 public String toString() {
415 return this.getClass().getSimpleName() + "[" +
417 ", namespace=" + getNamespace() +
418 ", revision=" + getQNameModule().getFormattedRevision() +
419 ", prefix=" + prefix +
420 ", yangVersion=" + yangVersion +
427 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
428 ctx -> (Module) ctx.buildEffective()));
429 submoduleContextsToBuild = ImmutableSet.of();