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.HashSet;
19 import java.util.LinkedHashMap;
20 import java.util.LinkedHashSet;
21 import java.util.List;
23 import java.util.Optional;
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.Revision;
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.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Deviation;
35 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
36 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
37 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
38 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Module;
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.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.IncludedSubmoduleNameToModuleCtx;
55 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
57 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
58 AbstractEffectiveDocumentedNode<String, D> implements Module, MutableStatement {
59 private final String name;
60 private final String prefix;
61 private final YangVersion yangVersion;
62 private final String organization;
63 private final String contact;
64 private final Set<ModuleImport> imports;
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 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
81 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 EffectiveStatementBase<?, ?> parentOfPrefix = this;
91 if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
92 parentOfPrefix = firstEffective(BelongsToEffectiveStatementImpl.class);
93 SourceException.throwIfNull(parentOfPrefix, ctx.getStatementSourceReference(),
94 "Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
97 final PrefixEffectiveStatementImpl prefixStmt = parentOfPrefix
98 .firstEffective(PrefixEffectiveStatementImpl.class);
99 SourceException.throwIfNull(prefixStmt, ctx.getStatementSourceReference(),
100 "Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
101 this.prefix = prefixStmt.argument();
103 final YangVersionEffectiveStatementImpl yangVersionStmt =
104 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 ? null : semanticVersionStmt.argument();
111 final OrganizationEffectiveStatementImpl organizationStmt =
112 firstEffective(OrganizationEffectiveStatementImpl.class);
113 this.organization = organizationStmt == null ? null : organizationStmt.argument();
115 final ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
116 this.contact = contactStmt == null ? null : contactStmt.argument();
118 // init submodules and substatements of submodules
119 final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
120 final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx
121 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
123 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
124 this.submodules = ImmutableSet.of();
125 this.submoduleContextsToBuild = ImmutableSet.of();
126 substatementsOfSubmodules = ImmutableList.of();
127 } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
129 * Aggregation of substatements from submodules should be done only
130 * for modules. In case of submodules it does not make sense because
131 * of possible circular chains of includes between submodules.
133 final Collection<StmtContext<?, ?, ?>> includedSubmodules = includedSubmodulesMap.values();
134 final Set<Module> submodulesInit = new HashSet<>();
135 final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
136 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodules) {
137 final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
139 submodulesInit.add(submodule);
140 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
141 .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
142 .collect(Collectors.toList()));
145 this.submodules = ImmutableSet.copyOf(submodulesInit);
146 this.submoduleContextsToBuild = ImmutableSet.of();
147 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
150 * Because of possible circular chains of includes between submodules we can
151 * collect only submodule contexts here and then build them during
152 * sealing of this statement.
154 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
155 submoduleContextsInit = new HashSet<>();
156 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
157 submoduleContextsInit.add(
158 (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
161 this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
162 substatementsOfSubmodules = ImmutableList.of();
165 if (!submoduleContextsToBuild.isEmpty()) {
166 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
172 // init substatements collections
173 final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
174 effectiveSubstatements.addAll(effectiveSubstatements());
175 effectiveSubstatements.addAll(substatementsOfSubmodules);
177 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
178 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
179 final Set<ModuleImport> importsInit = new HashSet<>();
180 final Set<NotificationDefinition> notificationsInit = new HashSet<>();
181 final Set<RpcDefinition> rpcsInit = new HashSet<>();
182 final Set<Deviation> deviationsInit = new HashSet<>();
183 final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
184 final Set<FeatureDefinition> featuresInit = new HashSet<>();
185 final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
187 final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
188 final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
189 final Set<UsesNode> mutableUses = new HashSet<>();
190 final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
191 final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
193 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
194 if (effectiveStatement instanceof UnknownSchemaNode) {
195 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
197 if (effectiveStatement instanceof AugmentationSchema) {
198 augmentationsInit.add((AugmentationSchema) effectiveStatement);
200 if (effectiveStatement instanceof ModuleImport) {
201 importsInit.add((ModuleImport) effectiveStatement);
203 if (effectiveStatement instanceof NotificationDefinition) {
204 notificationsInit.add((NotificationDefinition) effectiveStatement);
206 if (effectiveStatement instanceof RpcDefinition) {
207 rpcsInit.add((RpcDefinition) effectiveStatement);
209 if (effectiveStatement instanceof Deviation) {
210 deviationsInit.add((Deviation) effectiveStatement);
212 if (effectiveStatement instanceof IdentitySchemaNode) {
213 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
215 if (effectiveStatement instanceof FeatureDefinition) {
216 featuresInit.add((FeatureDefinition) effectiveStatement);
218 if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
219 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
221 if (effectiveStatement instanceof DataSchemaNode) {
222 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
223 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
224 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
225 mutablePublicChildNodes.add(dataSchemaNode);
227 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
230 if (effectiveStatement instanceof UsesNode) {
231 final UsesNode usesNode = (UsesNode) effectiveStatement;
232 if (!mutableUses.contains(usesNode)) {
233 mutableUses.add(usesNode);
235 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
238 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
239 final TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
240 final TypeDefinition<?> type = typeDef.getTypeDefinition();
241 if (!mutableTypeDefinitions.contains(type)) {
242 mutableTypeDefinitions.add(type);
244 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
247 if (effectiveStatement instanceof GroupingDefinition) {
248 final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
249 if (!mutableGroupings.contains(grp)) {
250 mutableGroupings.add(grp);
252 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
257 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
258 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
259 this.imports = ImmutableSet.copyOf(importsInit);
260 this.notifications = ImmutableSet.copyOf(notificationsInit);
261 this.rpcs = ImmutableSet.copyOf(rpcsInit);
262 this.deviations = ImmutableSet.copyOf(deviationsInit);
263 this.identities = ImmutableSet.copyOf(identitiesInit);
264 this.features = ImmutableSet.copyOf(featuresInit);
265 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
267 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
268 this.groupings = ImmutableSet.copyOf(mutableGroupings);
269 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
270 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
271 this.uses = ImmutableSet.copyOf(mutableUses);
276 public URI getNamespace() {
277 return getQNameModule().getNamespace();
281 public String getName() {
286 public Optional<Revision> getRevision() {
287 return getQNameModule().getRevision();
291 public String getPrefix() {
296 public YangVersion getYangVersion() {
301 public String getOrganization() {
306 public String getContact() {
311 public Set<ModuleImport> getImports() {
316 public Set<Module> getSubmodules() {
317 Preconditions.checkState(sealed,
318 "Attempt to get base submodules from unsealed submodule effective statement %s", getQNameModule());
323 public Set<FeatureDefinition> getFeatures() {
328 public Set<NotificationDefinition> getNotifications() {
329 return notifications;
333 public Set<AugmentationSchema> getAugmentations() {
334 return augmentations;
338 public Set<RpcDefinition> getRpcs() {
343 public Set<Deviation> getDeviations() {
348 public List<ExtensionDefinition> getExtensionSchemaNodes() {
349 return extensionNodes;
353 public Set<IdentitySchemaNode> getIdentities() {
359 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
364 public final Set<TypeDefinition<?>> getTypeDefinitions() {
365 return typeDefinitions;
369 public final Set<DataSchemaNode> getChildNodes() {
370 return publicChildNodes;
374 public final Set<GroupingDefinition> getGroupings() {
379 public final DataSchemaNode getDataChildByName(final QName name) {
380 // Child nodes are keyed by their container name, so we can do a direct
382 return childNodes.get(name);
386 public Set<UsesNode> getUses() {
391 public Optional<SemVer> getSemanticVersion() {
392 return Optional.ofNullable(semanticVersion);
396 public String toString() {
397 return this.getClass().getSimpleName() + "["
399 + ", namespace=" + getNamespace()
400 + ", revision=" + getQNameModule().getFormattedRevision()
401 + ", prefix=" + prefix
402 + ", yangVersion=" + yangVersion
409 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
410 ctx -> (Module) ctx.buildEffective()));
411 submoduleContextsToBuild = ImmutableSet.of();