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.IncludedSubmoduleNameToIdentifier;
56 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
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 prefix;
62 private final YangVersion yangVersion;
63 private final String organization;
64 private final String contact;
65 private final Set<ModuleImport> imports;
66 private final Set<FeatureDefinition> features;
67 private final Set<NotificationDefinition> notifications;
68 private final Set<AugmentationSchema> augmentations;
69 private final Set<RpcDefinition> rpcs;
70 private final Set<Deviation> deviations;
71 private final List<ExtensionDefinition> extensionNodes;
72 private final Set<IdentitySchemaNode> identities;
73 private final List<UnknownSchemaNode> unknownNodes;
74 private final Map<QName, DataSchemaNode> childNodes;
75 private final Set<GroupingDefinition> groupings;
76 private final Set<UsesNode> uses;
77 private final Set<TypeDefinition<?>> typeDefinitions;
78 private final Set<DataSchemaNode> publicChildNodes;
79 private final SemVer semanticVersion;
81 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
82 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 =
105 firstEffective(YangVersionEffectiveStatementImpl.class);
106 this.yangVersion = yangVersionStmt == null ? YangVersion.VERSION_1 : yangVersionStmt.argument();
108 final OpenconfigVersionEffectiveStatementImpl semanticVersionStmt =
109 firstEffective(OpenconfigVersionEffectiveStatementImpl.class);
110 this.semanticVersion = semanticVersionStmt == null ? DEFAULT_SEMANTIC_VERSION : semanticVersionStmt.argument();
112 final OrganizationEffectiveStatementImpl organizationStmt =
113 firstEffective(OrganizationEffectiveStatementImpl.class);
114 this.organization = organizationStmt == null ? null : organizationStmt.argument();
116 final ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
117 this.contact = contactStmt == null ? null : contactStmt.argument();
119 // init submodules and substatements of submodules
120 final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
121 final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
122 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
124 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
125 this.submodules = ImmutableSet.of();
126 this.submoduleContextsToBuild = ImmutableSet.of();
127 substatementsOfSubmodules = ImmutableList.of();
128 } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
130 * Aggregation of substatements from submodules should be done only
131 * for modules. In case of submodules it does not make sense because
132 * of possible circular chains of includes between submodules.
134 final Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
135 final Set<Module> submodulesInit = new HashSet<>();
136 final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
137 for (final ModuleIdentifier submoduleIdentifier : includedSubmodules) {
138 @SuppressWarnings("unchecked")
139 final Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
140 (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
141 .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
142 final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
144 submodulesInit.add(submodule);
145 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
146 .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
147 .collect(Collectors.toList()));
150 this.submodules = ImmutableSet.copyOf(submodulesInit);
151 this.submoduleContextsToBuild = ImmutableSet.of();
152 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
155 * Because of possible circular chains of includes between submodules we can
156 * collect only submodule contexts here and then build them during
157 * sealing of this statement.
159 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
160 submoduleContextsInit = new HashSet<>();
161 for (final ModuleIdentifier submoduleIdentifier : includedSubmodulesMap.values()) {
162 final StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
163 ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
164 submoduleContextsInit.add(submoduleCtx);
167 this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
168 substatementsOfSubmodules = ImmutableList.of();
171 if (!submoduleContextsToBuild.isEmpty()) {
172 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
178 // init substatements collections
179 final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
180 effectiveSubstatements.addAll(effectiveSubstatements());
181 effectiveSubstatements.addAll(substatementsOfSubmodules);
183 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
184 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
185 final Set<ModuleImport> importsInit = new HashSet<>();
186 final Set<NotificationDefinition> notificationsInit = new HashSet<>();
187 final Set<RpcDefinition> rpcsInit = new HashSet<>();
188 final Set<Deviation> deviationsInit = new HashSet<>();
189 final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
190 final Set<FeatureDefinition> featuresInit = new HashSet<>();
191 final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
193 final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
194 final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
195 final Set<UsesNode> mutableUses = new HashSet<>();
196 final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
197 final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
199 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
200 if (effectiveStatement instanceof UnknownSchemaNode) {
201 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
203 if (effectiveStatement instanceof AugmentationSchema) {
204 augmentationsInit.add((AugmentationSchema) effectiveStatement);
206 if (effectiveStatement instanceof ModuleImport) {
207 importsInit.add((ModuleImport) effectiveStatement);
209 if (effectiveStatement instanceof NotificationDefinition) {
210 notificationsInit.add((NotificationDefinition) effectiveStatement);
212 if (effectiveStatement instanceof RpcDefinition) {
213 rpcsInit.add((RpcDefinition) effectiveStatement);
215 if (effectiveStatement instanceof Deviation) {
216 deviationsInit.add((Deviation) effectiveStatement);
218 if (effectiveStatement instanceof IdentitySchemaNode) {
219 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
221 if (effectiveStatement instanceof FeatureDefinition) {
222 featuresInit.add((FeatureDefinition) effectiveStatement);
224 if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
225 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
227 if (effectiveStatement instanceof DataSchemaNode) {
228 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
229 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
230 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
231 mutablePublicChildNodes.add(dataSchemaNode);
233 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
236 if (effectiveStatement instanceof UsesNode) {
237 final UsesNode usesNode = (UsesNode) effectiveStatement;
238 if (!mutableUses.contains(usesNode)) {
239 mutableUses.add(usesNode);
241 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
244 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
245 final TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
246 final TypeDefinition<?> type = typeDef.getTypeDefinition();
247 if (!mutableTypeDefinitions.contains(type)) {
248 mutableTypeDefinitions.add(type);
250 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
253 if (effectiveStatement instanceof GroupingDefinition) {
254 final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
255 if (!mutableGroupings.contains(grp)) {
256 mutableGroupings.add(grp);
258 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
263 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
264 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
265 this.imports = ImmutableSet.copyOf(importsInit);
266 this.notifications = ImmutableSet.copyOf(notificationsInit);
267 this.rpcs = ImmutableSet.copyOf(rpcsInit);
268 this.deviations = ImmutableSet.copyOf(deviationsInit);
269 this.identities = ImmutableSet.copyOf(identitiesInit);
270 this.features = ImmutableSet.copyOf(featuresInit);
271 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
273 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
274 this.groupings = ImmutableSet.copyOf(mutableGroupings);
275 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
276 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
277 this.uses = ImmutableSet.copyOf(mutableUses);
282 public URI getNamespace() {
283 return getQNameModule().getNamespace();
287 public String getName() {
292 public Date getRevision() {
293 return getQNameModule().getRevision();
297 public String getPrefix() {
302 public String getYangVersion() {
303 return yangVersion.toString();
307 public String getOrganization() {
312 public String getContact() {
317 public Set<ModuleImport> getImports() {
322 public Set<Module> getSubmodules() {
323 Preconditions.checkState(sealed,
324 "Attempt to get base submodules from unsealed submodule effective statement %s", getQNameModule());
329 public Set<FeatureDefinition> getFeatures() {
334 public Set<NotificationDefinition> getNotifications() {
335 return notifications;
339 public Set<AugmentationSchema> getAugmentations() {
340 return augmentations;
344 public Set<RpcDefinition> getRpcs() {
349 public Set<Deviation> getDeviations() {
354 public List<ExtensionDefinition> getExtensionSchemaNodes() {
355 return extensionNodes;
359 public Set<IdentitySchemaNode> getIdentities() {
365 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
370 public final Set<TypeDefinition<?>> getTypeDefinitions() {
371 return typeDefinitions;
375 public final Set<DataSchemaNode> getChildNodes() {
376 return publicChildNodes;
380 public final Set<GroupingDefinition> getGroupings() {
385 public final DataSchemaNode getDataChildByName(final QName name) {
386 // Child nodes are keyed by their container name, so we can do a direct
388 return childNodes.get(name);
392 public Set<UsesNode> getUses() {
397 public SemVer getSemanticVersion() {
398 return semanticVersion;
402 public String toString() {
403 return this.getClass().getSimpleName() + "["
405 + ", namespace=" + getNamespace()
406 + ", revision=" + getQNameModule().getFormattedRevision()
407 + ", prefix=" + prefix
408 + ", yangVersion=" + yangVersion
415 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
416 ctx -> (Module) ctx.buildEffective()));
417 submoduleContextsToBuild = ImmutableSet.of();