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.rfc7950.stmt;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.base.Verify;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Iterables;
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.LinkedHashMap;
24 import java.util.LinkedHashSet;
25 import java.util.List;
27 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import org.eclipse.jdt.annotation.NonNull;
31 import org.opendaylight.yangtools.concepts.SemVer;
32 import org.opendaylight.yangtools.openconfig.model.api.OpenConfigVersionEffectiveStatement;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.Revision;
35 import org.opendaylight.yangtools.yang.common.YangVersion;
36 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.Deviation;
40 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
41 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
42 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
43 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
46 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
47 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
48 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
50 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.UsesNode;
52 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
53 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
54 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
55 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToEffectiveStatement;
56 import org.opendaylight.yangtools.yang.model.api.stmt.ContactEffectiveStatement;
57 import org.opendaylight.yangtools.yang.model.api.stmt.OrganizationEffectiveStatement;
58 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
59 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
60 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
61 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
62 import org.opendaylight.yangtools.yang.model.api.stmt.YangVersionEffectiveStatement;
63 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
64 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
65 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
66 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
67 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
70 public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
71 AbstractSchemaEffectiveDocumentedNode<String, D> implements Module, MutableStatement {
72 private final String name;
73 private final String prefix;
74 private final YangVersion yangVersion;
75 private final String organization;
76 private final String contact;
77 private final Set<ModuleImport> imports;
78 private final Set<FeatureDefinition> features;
79 private final Set<NotificationDefinition> notifications;
80 private final Set<AugmentationSchemaNode> augmentations;
81 private final Set<RpcDefinition> rpcs;
82 private final Set<Deviation> deviations;
83 private final List<ExtensionDefinition> extensionNodes;
84 private final Set<IdentitySchemaNode> identities;
85 private final @NonNull List<UnknownSchemaNode> unknownNodes;
86 private final Map<QName, DataSchemaNode> childNodes;
87 private final Set<GroupingDefinition> groupings;
88 private final Set<UsesNode> uses;
89 private final Set<TypeDefinition<?>> typeDefinitions;
90 private final Set<DataSchemaNode> publicChildNodes;
91 private final SemVer semanticVersion;
93 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
94 submoduleContextsToBuild;
95 private Set<Module> submodules;
96 private boolean sealed;
98 protected AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
101 this.name = argument();
103 final EffectiveStatement<?, ?> parentOfPrefix;
104 if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
105 final Optional<BelongsToEffectiveStatement> optParent =
106 findFirstEffectiveSubstatement(BelongsToEffectiveStatement.class);
107 SourceException.throwIf(!optParent.isPresent(), ctx.getStatementSourceReference(),
108 "Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
109 parentOfPrefix = optParent.get();
111 parentOfPrefix = this;
114 final Optional<@NonNull PrefixEffectiveStatement> prefixStmt = parentOfPrefix.findFirstEffectiveSubstatement(
115 PrefixEffectiveStatement.class);
116 SourceException.throwIf(!prefixStmt.isPresent(), ctx.getStatementSourceReference(),
117 "Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
118 this.prefix = prefixStmt.get().argument();
119 this.yangVersion = findFirstEffectiveSubstatementArgument(YangVersionEffectiveStatement.class)
120 .orElse(YangVersion.VERSION_1);
121 this.semanticVersion = findFirstEffectiveSubstatementArgument(OpenConfigVersionEffectiveStatement.class)
123 this.organization = findFirstEffectiveSubstatementArgument(OrganizationEffectiveStatement.class)
125 this.contact = findFirstEffectiveSubstatementArgument(ContactEffectiveStatement.class)
128 // init submodules and substatements of submodules
129 final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
130 final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx
131 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
133 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
134 this.submodules = ImmutableSet.of();
135 this.submoduleContextsToBuild = ImmutableSet.of();
136 substatementsOfSubmodules = ImmutableList.of();
137 } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
139 * Aggregation of substatements from submodules should be done only
140 * for modules. In case of submodules it does not make sense because
141 * of possible circular chains of includes between submodules.
143 final Set<Module> submodulesInit = new HashSet<>();
144 final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
145 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
146 final EffectiveStatement<?, ?> submodule = submoduleCtx.buildEffective();
147 Verify.verify(submodule instanceof SubmoduleEffectiveStatement);
148 Verify.verify(submodule instanceof Module, "Submodule statement %s is not a Module", submodule);
149 submodulesInit.add((Module) submodule);
150 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
151 .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
152 .collect(Collectors.toList()));
155 this.submodules = ImmutableSet.copyOf(submodulesInit);
156 this.submoduleContextsToBuild = ImmutableSet.of();
157 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
160 * Because of possible circular chains of includes between submodules we can
161 * collect only submodule contexts here and then build them during
162 * sealing of this statement.
164 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
165 submoduleContextsInit = new HashSet<>();
166 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
167 submoduleContextsInit.add(
168 (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
171 this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
172 substatementsOfSubmodules = ImmutableList.of();
175 if (!submoduleContextsToBuild.isEmpty()) {
176 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
182 // init substatements collections
183 final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
184 effectiveSubstatements.addAll(effectiveSubstatements());
185 effectiveSubstatements.addAll(substatementsOfSubmodules);
187 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
188 final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
189 final Set<ModuleImport> importsInit = new HashSet<>();
190 final Set<NotificationDefinition> notificationsInit = new HashSet<>();
191 final Set<RpcDefinition> rpcsInit = new HashSet<>();
192 final Set<Deviation> deviationsInit = new HashSet<>();
193 final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
194 final Set<FeatureDefinition> featuresInit = new HashSet<>();
195 final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
197 final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
198 final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
199 final Set<UsesNode> mutableUses = new HashSet<>();
200 final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
201 final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
203 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
204 if (effectiveStatement instanceof UnknownSchemaNode) {
205 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
207 if (effectiveStatement instanceof AugmentationSchemaNode) {
208 augmentationsInit.add((AugmentationSchemaNode) effectiveStatement);
210 if (effectiveStatement instanceof ModuleImport) {
211 importsInit.add((ModuleImport) effectiveStatement);
213 if (effectiveStatement instanceof NotificationDefinition) {
214 notificationsInit.add((NotificationDefinition) effectiveStatement);
216 if (effectiveStatement instanceof RpcDefinition) {
217 rpcsInit.add((RpcDefinition) effectiveStatement);
219 if (effectiveStatement instanceof Deviation) {
220 deviationsInit.add((Deviation) effectiveStatement);
222 if (effectiveStatement instanceof IdentitySchemaNode) {
223 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
225 if (effectiveStatement instanceof FeatureDefinition) {
226 featuresInit.add((FeatureDefinition) effectiveStatement);
228 if (effectiveStatement instanceof ExtensionDefinition) {
229 extensionNodesInit.add((ExtensionDefinition) effectiveStatement);
231 if (effectiveStatement instanceof DataSchemaNode) {
232 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
233 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
234 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
235 mutablePublicChildNodes.add(dataSchemaNode);
237 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
240 if (effectiveStatement instanceof UsesNode) {
241 final UsesNode usesNode = (UsesNode) effectiveStatement;
242 if (!mutableUses.contains(usesNode)) {
243 mutableUses.add(usesNode);
245 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
248 if (effectiveStatement instanceof TypedefEffectiveStatement) {
249 final TypedefEffectiveStatement typeDef = (TypedefEffectiveStatement) effectiveStatement;
250 final TypeDefinition<?> type = typeDef.getTypeDefinition();
251 if (!mutableTypeDefinitions.contains(type)) {
252 mutableTypeDefinitions.add(type);
254 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
257 if (effectiveStatement instanceof GroupingDefinition) {
258 final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
259 if (!mutableGroupings.contains(grp)) {
260 mutableGroupings.add(grp);
262 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
267 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
268 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
269 this.imports = ImmutableSet.copyOf(importsInit);
270 this.notifications = ImmutableSet.copyOf(notificationsInit);
271 this.rpcs = ImmutableSet.copyOf(rpcsInit);
272 this.deviations = ImmutableSet.copyOf(deviationsInit);
273 this.identities = ImmutableSet.copyOf(identitiesInit);
274 this.features = ImmutableSet.copyOf(featuresInit);
275 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
277 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
278 this.groupings = ImmutableSet.copyOf(mutableGroupings);
279 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
280 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
281 this.uses = ImmutableSet.copyOf(mutableUses);
286 public URI getNamespace() {
287 return getQNameModule().getNamespace();
291 public String getName() {
296 public Optional<Revision> getRevision() {
297 return getQNameModule().getRevision();
301 public String getPrefix() {
306 public YangVersion getYangVersion() {
311 public Optional<String> getOrganization() {
312 return Optional.ofNullable(organization);
316 public Optional<String> getContact() {
317 return Optional.ofNullable(contact);
321 public Set<ModuleImport> getImports() {
326 public Set<Module> getSubmodules() {
327 checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
333 public Set<FeatureDefinition> getFeatures() {
338 public Set<NotificationDefinition> getNotifications() {
339 return notifications;
343 public Set<AugmentationSchemaNode> getAugmentations() {
344 return augmentations;
348 public Set<RpcDefinition> getRpcs() {
353 public Set<Deviation> getDeviations() {
358 public List<ExtensionDefinition> getExtensionSchemaNodes() {
359 return extensionNodes;
363 public Set<IdentitySchemaNode> getIdentities() {
368 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
373 public final Set<TypeDefinition<?>> getTypeDefinitions() {
374 return typeDefinitions;
378 public final Set<DataSchemaNode> getChildNodes() {
379 return publicChildNodes;
383 public final Set<GroupingDefinition> getGroupings() {
388 @SuppressWarnings("checkstyle:hiddenField")
389 public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
390 // Child nodes are keyed by their container name, so we can do a direct lookup
391 return Optional.ofNullable(childNodes.get(requireNonNull(name)));
395 public Set<UsesNode> getUses() {
400 public Optional<SemVer> getSemanticVersion() {
401 return Optional.ofNullable(semanticVersion);
405 public String toString() {
406 return MoreObjects.toStringHelper(this).omitNullValues()
408 .add("namespace", getNamespace())
409 .add("revision", getRevision().orElse(null))
410 .add("prefix", prefix)
411 .add("yangVersion", yangVersion)
418 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
419 ctx -> (Module) ctx.buildEffective()));
420 submoduleContextsToBuild = ImmutableSet.of();