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 javax.annotation.Nonnull;
26 import org.opendaylight.yangtools.concepts.SemVer;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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.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.TypeDefinition;
44 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.UsesNode;
46 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
47 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
48 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
49 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
50 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
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.DeclarationInTextSource;
55 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
56 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
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 sourcePath;
62 private final String prefix;
63 private final YangVersion yangVersion;
64 private final String organization;
65 private final String contact;
66 private final Set<ModuleImport> imports;
67 private final Set<FeatureDefinition> features;
68 private final Set<NotificationDefinition> notifications;
69 private final Set<AugmentationSchema> augmentations;
70 private final Set<RpcDefinition> rpcs;
71 private final Set<Deviation> deviations;
72 private final List<ExtensionDefinition> extensionNodes;
73 private final Set<IdentitySchemaNode> identities;
74 private final List<UnknownSchemaNode> unknownNodes;
75 private final Map<QName, DataSchemaNode> childNodes;
76 private final Set<GroupingDefinition> groupings;
77 private final Set<UsesNode> uses;
78 private final Set<TypeDefinition<?>> typeDefinitions;
79 private final Set<DataSchemaNode> publicChildNodes;
80 private final SemVer semanticVersion;
81 private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> 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 final PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
91 this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
93 final YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
94 this.yangVersion = (yangVersionStmt == null) ? YangVersion.VERSION_1 : yangVersionStmt.argument();
96 final OpenconfigVersionEffectiveStatementImpl semanticVersionStmt = firstEffective(OpenconfigVersionEffectiveStatementImpl.class);
97 this.semanticVersion = (semanticVersionStmt == null) ? DEFAULT_OPENCONFIG_VERSION : semanticVersionStmt.argument();
99 final OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
100 this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
102 final ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
103 this.contact = (contactStmt == null) ? null : contactStmt.argument();
105 if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
106 this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
108 this.sourcePath = null;
111 // init submodules and substatements of submodules
112 final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
113 final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
114 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
116 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
117 this.submodules = ImmutableSet.of();
118 this.submoduleContextsToBuild = ImmutableSet.of();
119 substatementsOfSubmodules = ImmutableList.of();
120 } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
122 * Aggregation of substatements from submodules should be done only
123 * for modules. In case of submodules it does not make sense because
124 * of possible circular chains of includes between submodules.
126 final Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
127 final Set<Module> submodulesInit = new HashSet<>();
128 final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
129 for (final ModuleIdentifier submoduleIdentifier : includedSubmodules) {
130 @SuppressWarnings("unchecked")
131 final Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
132 .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
133 final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
135 submodulesInit.add(submodule);
136 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
139 this.submodules = ImmutableSet.copyOf(submodulesInit);
140 this.submoduleContextsToBuild = ImmutableSet.of();
141 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
144 * Because of possible circular chains of includes between submodules we can
145 * collect only submodule contexts here and then build them during
146 * sealing of this statement.
148 final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>> submoduleContextsInit = new HashSet<>();
149 for (final ModuleIdentifier submoduleIdentifier : includedSubmodulesMap.values()) {
150 final StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = ctx
151 .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
152 submoduleContextsInit.add(submoduleCtx);
155 this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
156 substatementsOfSubmodules = ImmutableList.of();
159 if (!submoduleContextsToBuild.isEmpty()) {
160 ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
166 // init substatements collections
167 final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
168 effectiveSubstatements.addAll(effectiveSubstatements());
169 effectiveSubstatements.addAll(substatementsOfSubmodules);
171 final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
172 final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
173 final Set<ModuleImport> importsInit = new HashSet<>();
174 final Set<NotificationDefinition> notificationsInit = new HashSet<>();
175 final Set<RpcDefinition> rpcsInit = new HashSet<>();
176 final Set<Deviation> deviationsInit = new HashSet<>();
177 final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
178 final Set<FeatureDefinition> featuresInit = new HashSet<>();
179 final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
181 final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
182 final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
183 final Set<UsesNode> mutableUses = new HashSet<>();
184 final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
185 final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
187 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
188 if (effectiveStatement instanceof UnknownSchemaNode) {
189 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
191 if (effectiveStatement instanceof AugmentationSchema) {
192 augmentationsInit.add((AugmentationSchema) effectiveStatement);
194 if (effectiveStatement instanceof ModuleImport) {
195 importsInit.add((ModuleImport) effectiveStatement);
197 if (effectiveStatement instanceof NotificationDefinition) {
198 notificationsInit.add((NotificationDefinition) effectiveStatement);
200 if (effectiveStatement instanceof RpcDefinition) {
201 rpcsInit.add((RpcDefinition) effectiveStatement);
203 if (effectiveStatement instanceof Deviation) {
204 deviationsInit.add((Deviation) effectiveStatement);
206 if (effectiveStatement instanceof IdentitySchemaNode) {
207 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
209 if (effectiveStatement instanceof FeatureDefinition) {
210 featuresInit.add((FeatureDefinition) effectiveStatement);
212 if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
213 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
215 if (effectiveStatement instanceof DataSchemaNode) {
216 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
217 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
218 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
219 mutablePublicChildNodes.add(dataSchemaNode);
221 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
224 if (effectiveStatement instanceof UsesNode) {
225 final UsesNode usesNode = (UsesNode) effectiveStatement;
226 if (!mutableUses.contains(usesNode)) {
227 mutableUses.add(usesNode);
229 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
232 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
233 final TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
234 final TypeDefinition<?> type = typeDef.getTypeDefinition();
235 if (!mutableTypeDefinitions.contains(type)) {
236 mutableTypeDefinitions.add(type);
238 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
241 if (effectiveStatement instanceof GroupingDefinition) {
242 final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
243 if (!mutableGroupings.contains(grp)) {
244 mutableGroupings.add(grp);
246 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
251 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
252 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
253 if (ctx.isEnabledOpenconfigVersioning()) {
254 this.imports = ImmutableSet.copyOf(importsInit);
256 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
258 this.notifications = ImmutableSet.copyOf(notificationsInit);
259 this.rpcs = ImmutableSet.copyOf(rpcsInit);
260 this.deviations = ImmutableSet.copyOf(deviationsInit);
261 this.identities = ImmutableSet.copyOf(identitiesInit);
262 this.features = ImmutableSet.copyOf(featuresInit);
263 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
265 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
266 this.groupings = ImmutableSet.copyOf(mutableGroupings);
267 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
268 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
269 this.uses = ImmutableSet.copyOf(mutableUses);
273 private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
274 final StmtContext<String, ? extends DeclaredStatement<String>, ? extends EffectiveStatement<String, ?>> ctx) {
275 final Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
276 for (final ModuleImport moduleImport : importsInit) {
277 if (moduleImport.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
278 final QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, moduleImport.getPrefix());
279 final ModuleImport resolvedModuleImport = new ModuleImportImpl(moduleImport.getModuleName(),
280 impModuleQName.getRevision(), moduleImport.getPrefix());
281 resolvedModuleImports.add(resolvedModuleImport);
283 resolvedModuleImports.add(moduleImport);
286 return resolvedModuleImports;
290 public String getModuleSourcePath() {
295 public String getSource() {
300 public URI getNamespace() {
301 return getQNameModule().getNamespace();
305 public String getName() {
310 public Date getRevision() {
311 return getQNameModule().getRevision();
315 public String getPrefix() {
320 public String getYangVersion() {
321 return yangVersion.toString();
325 public String getOrganization() {
330 public String getContact() {
335 public Set<ModuleImport> getImports() {
340 public Set<Module> getSubmodules() {
341 Preconditions.checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
347 public Set<FeatureDefinition> getFeatures() {
352 public Set<NotificationDefinition> getNotifications() {
353 return notifications;
357 public Set<AugmentationSchema> getAugmentations() {
358 return augmentations;
362 public Set<RpcDefinition> getRpcs() {
367 public Set<Deviation> getDeviations() {
372 public List<ExtensionDefinition> getExtensionSchemaNodes() {
373 return extensionNodes;
377 public Set<IdentitySchemaNode> getIdentities() {
383 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
388 public final Set<TypeDefinition<?>> getTypeDefinitions() {
389 return typeDefinitions;
393 public final Set<DataSchemaNode> getChildNodes() {
394 return publicChildNodes;
398 public final Set<GroupingDefinition> getGroupings() {
403 public final DataSchemaNode getDataChildByName(final QName name) {
404 // Child nodes are keyed by their container name, so we can do a direct
406 return childNodes.get(name);
410 public Set<UsesNode> getUses() {
415 public SemVer getOpenconfigVersion() {
416 return semanticVersion;
420 public String toString() {
421 return this.getClass().getSimpleName() + "[" +
423 ", namespace=" + getNamespace() +
424 ", revision=" + getQNameModule().getFormattedRevision() +
425 ", prefix=" + prefix +
426 ", yangVersion=" + yangVersion +
433 submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
434 ctx -> (Module) ctx.buildEffective()));
435 submoduleContextsToBuild = ImmutableSet.of();