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 org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
12 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
13 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
14 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableSet;
17 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
18 import java.util.LinkedHashSet;
19 import java.util.LinkedHashMap;
20 import com.google.common.collect.ImmutableMap;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.UsesNode;
26 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
29 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
30 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
32 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
33 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
35 import java.util.Collection;
36 import java.util.Date;
37 import java.util.HashSet;
38 import java.util.LinkedList;
39 import java.util.List;
41 import org.opendaylight.yangtools.concepts.Immutable;
42 import org.opendaylight.yangtools.yang.common.QNameModule;
43 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
44 import org.opendaylight.yangtools.yang.model.api.Deviation;
45 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
46 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
47 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.Module;
49 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
50 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
51 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
52 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
54 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
56 public class ModuleEffectiveStatementImpl extends
57 AbstractEffectiveDocumentedNode<String, ModuleStatement> implements
60 private final QNameModule qNameModule;
61 private final String name;
62 private String sourcePath;
63 private String prefix;
64 private String yangVersion;
65 private String organization;
66 private String contact;
67 private ImmutableSet<ModuleImport> imports;
68 private ImmutableSet<Module> submodules;
69 private ImmutableSet<FeatureDefinition> features;
70 private ImmutableSet<NotificationDefinition> notifications;
71 private ImmutableSet<AugmentationSchema> augmentations;
72 private ImmutableSet<RpcDefinition> rpcs;
73 private ImmutableSet<Deviation> deviations;
74 private ImmutableList<ExtensionDefinition> extensionNodes;
75 private ImmutableSet<IdentitySchemaNode> identities;
76 private ImmutableList<UnknownSchemaNode> unknownNodes;
77 private String source;
78 private ImmutableList<EffectiveStatement<?, ?>> substatementsOfSubmodules;
80 private ImmutableMap<QName, DataSchemaNode> childNodes;
81 private ImmutableSet<GroupingDefinition> groupings;
82 private ImmutableSet<UsesNode> uses;
83 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
84 private ImmutableSet<DataSchemaNode> publicChildNodes;
86 public ModuleEffectiveStatementImpl(
87 StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
91 QNameModule qNameModuleInit = ctx.getFromNamespace(
92 ModuleCtxToModuleQName.class, ctx);
93 qNameModule = qNameModuleInit.getRevision() == null ? QNameModule
94 .create(qNameModuleInit.getNamespace(),
95 SimpleDateFormatUtil.DEFAULT_DATE_REV)
98 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
99 if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
100 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement)
103 if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
104 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement)
107 if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
108 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement)
111 if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
112 contact = ((ContactEffectiveStatementImpl) effectiveStatement)
117 if (yangVersion == null) {
121 sourcePath = ((DeclarationInTextSource) ctx
122 .getStatementSourceReference()).getSourceName();
127 initSubstatementCollections(ctx);
130 private void initSubmodules(
131 StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
132 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
133 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
135 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
136 this.submodules = ImmutableSet.of();
137 this.substatementsOfSubmodules = ImmutableList.of();
141 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap
144 Set<Module> submodulesInit = new HashSet<>();
145 List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
146 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
147 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
148 .getFromNamespace(SubmoduleNamespace.class,
149 submoduleIdentifier);
150 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
152 submodulesInit.add(submodule);
153 substatementsOfSubmodulesInit.addAll(submodule
154 .effectiveSubstatements());
157 this.submodules = ImmutableSet.copyOf(submodulesInit);
158 this.substatementsOfSubmodules = ImmutableList
159 .copyOf(substatementsOfSubmodulesInit);
162 private void initSubstatementCollections(
163 StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
164 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
166 effectiveSubstatements.addAll(effectiveSubstatements());
167 effectiveSubstatements.addAll(substatementsOfSubmodules);
169 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
170 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
171 Set<ModuleImport> importsInit = new HashSet<>();
172 Set<NotificationDefinition> notificationsInit = new HashSet<>();
173 Set<RpcDefinition> rpcsInit = new HashSet<>();
174 Set<Deviation> deviationsInit = new HashSet<>();
175 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
176 Set<FeatureDefinition> featuresInit = new HashSet<>();
177 List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
179 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
180 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
181 Set<UsesNode> mutableUses = new HashSet<>();
182 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
183 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
185 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
186 if (effectiveStatement instanceof UnknownSchemaNode) {
187 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
189 if (effectiveStatement instanceof AugmentationSchema) {
190 augmentationsInit.add((AugmentationSchema) effectiveStatement);
192 if (effectiveStatement instanceof ModuleImport) {
193 importsInit.add((ModuleImport) effectiveStatement);
195 if (effectiveStatement instanceof NotificationDefinition) {
197 .add((NotificationDefinition) effectiveStatement);
199 if (effectiveStatement instanceof RpcDefinition) {
200 rpcsInit.add((RpcDefinition) effectiveStatement);
202 if (effectiveStatement instanceof Deviation) {
203 deviationsInit.add((Deviation) effectiveStatement);
205 if (effectiveStatement instanceof IdentitySchemaNode) {
206 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
208 if (effectiveStatement instanceof FeatureDefinition) {
209 featuresInit.add((FeatureDefinition) effectiveStatement);
211 if (effectiveStatement instanceof ExtensionDefinition) {
213 .add((ExtensionDefinition) effectiveStatement);
215 if (effectiveStatement instanceof DataSchemaNode) {
216 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
217 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
218 mutableChildNodes.put(dataSchemaNode.getQName(),
220 mutablePublicChildNodes.add(dataSchemaNode);
222 throw EffectiveStmtUtils
223 .createNameCollisionSourceException(ctx,
227 if (effectiveStatement instanceof UsesNode) {
228 UsesNode usesNode = (UsesNode) effectiveStatement;
229 if (!mutableUses.contains(usesNode)) {
230 mutableUses.add(usesNode);
232 throw EffectiveStmtUtils
233 .createNameCollisionSourceException(ctx,
237 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
238 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
239 ExtendedType extendedType = typeDef.buildType();
240 if (!mutableTypeDefinitions.contains(extendedType)) {
241 mutableTypeDefinitions.add(extendedType);
243 throw EffectiveStmtUtils
244 .createNameCollisionSourceException(ctx,
248 if (effectiveStatement instanceof GroupingDefinition) {
249 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
250 if (!mutableGroupings.contains(grp)) {
251 mutableGroupings.add(grp);
253 throw EffectiveStmtUtils
254 .createNameCollisionSourceException(ctx,
260 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
261 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
262 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit,
264 this.notifications = ImmutableSet.copyOf(notificationsInit);
265 this.rpcs = ImmutableSet.copyOf(rpcsInit);
266 this.deviations = ImmutableSet.copyOf(deviationsInit);
267 this.identities = ImmutableSet.copyOf(identitiesInit);
268 this.features = ImmutableSet.copyOf(featuresInit);
269 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
271 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
272 this.groupings = ImmutableSet.copyOf(mutableGroupings);
273 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
274 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
275 this.uses = ImmutableSet.copyOf(mutableUses);
278 private Set<ModuleImport> resolveModuleImports(
279 Set<ModuleImport> importsInit,
280 StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
281 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
282 for (ModuleImport moduleImport : importsInit) {
283 if (moduleImport.getRevision().equals(
284 SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
285 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
286 moduleImport.getPrefix());
287 if (!impModuleQName.getRevision().equals(
288 SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
289 ModuleImport resolvedModuleImport = new ModuleImportImpl(
290 moduleImport.getModuleName(),
291 impModuleQName.getRevision(),
292 moduleImport.getPrefix());
293 resolvedModuleImports.add(resolvedModuleImport);
296 resolvedModuleImports.add(moduleImport);
299 return resolvedModuleImports;
303 public String getModuleSourcePath() {
308 public URI getNamespace() {
309 return qNameModule.getNamespace();
313 public String getName() {
318 public Date getRevision() {
319 return qNameModule.getRevision();
323 public String getPrefix() {
328 public String getYangVersion() {
333 public String getOrganization() {
338 public String getContact() {
343 public Set<ModuleImport> getImports() {
348 public Set<Module> getSubmodules() {
353 public Set<FeatureDefinition> getFeatures() {
358 public Set<NotificationDefinition> getNotifications() {
359 return notifications;
363 public Set<AugmentationSchema> getAugmentations() {
364 return augmentations;
368 public Set<RpcDefinition> getRpcs() {
373 public Set<Deviation> getDeviations() {
378 public List<ExtensionDefinition> getExtensionSchemaNodes() {
379 return extensionNodes;
383 public Set<IdentitySchemaNode> getIdentities() {
388 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
393 public final Set<TypeDefinition<?>> getTypeDefinitions() {
394 return typeDefinitions;
398 public final Set<DataSchemaNode> getChildNodes() {
399 return publicChildNodes;
403 public final Set<GroupingDefinition> getGroupings() {
408 public final DataSchemaNode getDataChildByName(final QName name) {
409 // Child nodes are keyed by their container name, so we can do a direct
411 return childNodes.get(name);
415 public final DataSchemaNode getDataChildByName(final String name) {
416 for (DataSchemaNode node : childNodes.values()) {
417 if (node.getQName().getLocalName().equals(name)) {
425 public Set<UsesNode> getUses() {
430 public String getSource() {
435 public int hashCode() {
436 final int prime = 31;
438 result = prime * result + ((name == null) ? 0 : name.hashCode());
439 result = prime * result
440 + ((yangVersion == null) ? 0 : yangVersion.hashCode());
441 result = prime * result + qNameModule.hashCode();
446 public boolean equals(final Object obj) {
453 if (getClass() != obj.getClass()) {
456 ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
458 if (other.name != null) {
461 } else if (!name.equals(other.name)) {
464 if (!qNameModule.equals(other.qNameModule)) {
467 if (yangVersion == null) {
468 if (other.yangVersion != null) {
471 } else if (!yangVersion.equals(other.yangVersion)) {
478 public String toString() {
479 StringBuilder sb = new StringBuilder(
480 ModuleEffectiveStatementImpl.class.getSimpleName());
482 sb.append("name=").append(name);
483 sb.append(", namespace=").append(getNamespace());
484 sb.append(", revision=").append(getRevision());
485 sb.append(", prefix=").append(prefix);
486 sb.append(", yangVersion=").append(yangVersion);
488 return sb.toString();
492 public QNameModule getQNameModule() {