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.model.api.stmt.compat.NotificationNodeContainerCompat;
64 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
65 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
66 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
67 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
68 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
71 public abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
72 AbstractSchemaEffectiveDocumentedNode<String, D> implements Module, MutableStatement,
73 NotificationNodeContainerCompat<String, D> {
74 private final String name;
75 private final String prefix;
76 private final YangVersion yangVersion;
77 private final String organization;
78 private final String contact;
79 private final ImmutableSet<ModuleImport> imports;
80 private final ImmutableSet<FeatureDefinition> features;
81 private final @NonNull ImmutableSet<NotificationDefinition> notifications;
82 private final ImmutableSet<AugmentationSchemaNode> augmentations;
83 private final ImmutableSet<RpcDefinition> rpcs;
84 private final ImmutableSet<Deviation> deviations;
85 private final ImmutableList<ExtensionDefinition> extensionNodes;
86 private final ImmutableSet<IdentitySchemaNode> identities;
87 private final @NonNull ImmutableList<UnknownSchemaNode> unknownNodes;
88 private final ImmutableMap<QName, DataSchemaNode> childNodes;
89 private final ImmutableSet<GroupingDefinition> groupings;
90 private final ImmutableSet<UsesNode> uses;
91 private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
92 private final ImmutableSet<DataSchemaNode> publicChildNodes;
93 private final SemVer semanticVersion;
95 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
96 submoduleContextsToBuild;
97 private ImmutableSet<Module> submodules;
98 private boolean sealed;
100 protected AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
103 this.name = argument();
105 final EffectiveStatement<?, ?> parentOfPrefix;
106 if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
107 final Optional<BelongsToEffectiveStatement> optParent =
108 findFirstEffectiveSubstatement(BelongsToEffectiveStatement.class);
109 SourceException.throwIf(!optParent.isPresent(), ctx.getStatementSourceReference(),
110 "Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
111 parentOfPrefix = optParent.get();
113 parentOfPrefix = this;
116 final Optional<@NonNull PrefixEffectiveStatement> prefixStmt = parentOfPrefix.findFirstEffectiveSubstatement(
117 PrefixEffectiveStatement.class);
118 SourceException.throwIf(!prefixStmt.isPresent(), ctx.getStatementSourceReference(),
119 "Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
120 this.prefix = prefixStmt.get().argument();
121 this.yangVersion = findFirstEffectiveSubstatementArgument(YangVersionEffectiveStatement.class)
122 .orElse(YangVersion.VERSION_1);
123 this.semanticVersion = findFirstEffectiveSubstatementArgument(OpenConfigVersionEffectiveStatement.class)
125 this.organization = findFirstEffectiveSubstatementArgument(OrganizationEffectiveStatement.class)
127 this.contact = findFirstEffectiveSubstatementArgument(ContactEffectiveStatement.class)
130 // init submodules and substatements of submodules
131 final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
132 final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx
133 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
135 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
136 this.submodules = ImmutableSet.of();
137 this.submoduleContextsToBuild = ImmutableSet.of();
138 substatementsOfSubmodules = ImmutableList.of();
139 } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
141 * Aggregation of substatements from submodules should be done only
142 * for modules. In case of submodules it does not make sense because
143 * of possible circular chains of includes between submodules.
145 final Set<Module> submodulesInit = new HashSet<>();
146 final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
147 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
148 final EffectiveStatement<?, ?> submodule = submoduleCtx.buildEffective();
149 Verify.verify(submodule instanceof SubmoduleEffectiveStatement);
150 Verify.verify(submodule instanceof Module, "Submodule statement %s is not a Module", submodule);
151 submodulesInit.add((Module) submodule);
152 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
153 .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
154 .collect(Collectors.toList()));
157 this.submodules = ImmutableSet.copyOf(submodulesInit);
158 this.submoduleContextsToBuild = ImmutableSet.of();
159 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
162 * Because of possible circular chains of includes between submodules we can
163 * collect only submodule contexts here and then build them during
164 * sealing of this statement.
166 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
167 submoduleContextsInit = new HashSet<>();
168 for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
169 submoduleContextsInit.add(
170 (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
173 this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
174 substatementsOfSubmodules = ImmutableList.of();
177 if (!submoduleContextsToBuild.isEmpty()) {
178 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
184 // init substatements collections
185 final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
186 effectiveSubstatements.addAll(effectiveSubstatements());
187 effectiveSubstatements.addAll(substatementsOfSubmodules);
189 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
190 final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
191 final Set<ModuleImport> importsInit = new HashSet<>();
192 final Set<NotificationDefinition> notificationsInit = new HashSet<>();
193 final Set<RpcDefinition> rpcsInit = new HashSet<>();
194 final Set<Deviation> deviationsInit = new HashSet<>();
195 final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
196 final Set<FeatureDefinition> featuresInit = new HashSet<>();
197 final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
199 final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
200 final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
201 final Set<UsesNode> mutableUses = new HashSet<>();
202 final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
203 final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
205 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
206 if (effectiveStatement instanceof UnknownSchemaNode) {
207 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
209 if (effectiveStatement instanceof AugmentationSchemaNode) {
210 augmentationsInit.add((AugmentationSchemaNode) effectiveStatement);
212 if (effectiveStatement instanceof ModuleImport) {
213 importsInit.add((ModuleImport) effectiveStatement);
215 if (effectiveStatement instanceof NotificationDefinition) {
216 notificationsInit.add((NotificationDefinition) effectiveStatement);
218 if (effectiveStatement instanceof RpcDefinition) {
219 rpcsInit.add((RpcDefinition) effectiveStatement);
221 if (effectiveStatement instanceof Deviation) {
222 deviationsInit.add((Deviation) effectiveStatement);
224 if (effectiveStatement instanceof IdentitySchemaNode) {
225 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
227 if (effectiveStatement instanceof FeatureDefinition) {
228 featuresInit.add((FeatureDefinition) effectiveStatement);
230 if (effectiveStatement instanceof ExtensionDefinition) {
231 extensionNodesInit.add((ExtensionDefinition) effectiveStatement);
233 if (effectiveStatement instanceof DataSchemaNode) {
234 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
235 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
236 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
237 mutablePublicChildNodes.add(dataSchemaNode);
239 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
242 if (effectiveStatement instanceof UsesNode) {
243 final UsesNode usesNode = (UsesNode) effectiveStatement;
244 if (!mutableUses.contains(usesNode)) {
245 mutableUses.add(usesNode);
247 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
250 if (effectiveStatement instanceof TypedefEffectiveStatement) {
251 final TypedefEffectiveStatement typeDef = (TypedefEffectiveStatement) effectiveStatement;
252 final TypeDefinition<?> type = typeDef.getTypeDefinition();
253 if (!mutableTypeDefinitions.contains(type)) {
254 mutableTypeDefinitions.add(type);
256 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
259 if (effectiveStatement instanceof GroupingDefinition) {
260 final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
261 if (!mutableGroupings.contains(grp)) {
262 mutableGroupings.add(grp);
264 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
269 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
270 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
271 this.imports = ImmutableSet.copyOf(importsInit);
272 this.notifications = ImmutableSet.copyOf(notificationsInit);
273 this.rpcs = ImmutableSet.copyOf(rpcsInit);
274 this.deviations = ImmutableSet.copyOf(deviationsInit);
275 this.identities = ImmutableSet.copyOf(identitiesInit);
276 this.features = ImmutableSet.copyOf(featuresInit);
277 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
279 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
280 this.groupings = ImmutableSet.copyOf(mutableGroupings);
281 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
282 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
283 this.uses = ImmutableSet.copyOf(mutableUses);
288 public URI getNamespace() {
289 return getQNameModule().getNamespace();
293 public String getName() {
298 public Optional<Revision> getRevision() {
299 return getQNameModule().getRevision();
303 public String getPrefix() {
308 public YangVersion getYangVersion() {
313 public Optional<String> getOrganization() {
314 return Optional.ofNullable(organization);
318 public Optional<String> getContact() {
319 return Optional.ofNullable(contact);
323 public Set<ModuleImport> getImports() {
328 public Set<Module> getSubmodules() {
329 checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
335 public Set<FeatureDefinition> getFeatures() {
340 public Set<NotificationDefinition> getNotifications() {
341 return notifications;
345 public Set<AugmentationSchemaNode> getAugmentations() {
346 return augmentations;
350 public Set<RpcDefinition> getRpcs() {
355 public Set<Deviation> getDeviations() {
360 public List<ExtensionDefinition> getExtensionSchemaNodes() {
361 return extensionNodes;
365 public Set<IdentitySchemaNode> getIdentities() {
370 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
375 public final Set<TypeDefinition<?>> getTypeDefinitions() {
376 return typeDefinitions;
380 public final Set<DataSchemaNode> getChildNodes() {
381 return publicChildNodes;
385 public final Set<GroupingDefinition> getGroupings() {
390 @SuppressWarnings("checkstyle:hiddenField")
391 public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
392 // Child nodes are keyed by their container name, so we can do a direct lookup
393 return Optional.ofNullable(childNodes.get(requireNonNull(name)));
397 public Set<UsesNode> getUses() {
402 public Optional<SemVer> getSemanticVersion() {
403 return Optional.ofNullable(semanticVersion);
407 public String toString() {
408 return MoreObjects.toStringHelper(this).omitNullValues()
410 .add("namespace", getNamespace())
411 .add("revision", getRevision().orElse(null))
412 .add("prefix", prefix)
413 .add("yangVersion", yangVersion)
420 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
421 ctx -> (Module) ctx.buildEffective()));
422 submoduleContextsToBuild = ImmutableSet.of();