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.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.Date;
16 import java.util.HashSet;
17 import java.util.LinkedHashMap;
18 import java.util.LinkedHashSet;
19 import java.util.LinkedList;
20 import java.util.List;
22 import java.util.Objects;
24 import org.opendaylight.yangtools.concepts.Immutable;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Deviation;
31 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
32 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
33 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
34 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
38 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
39 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
40 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.UsesNode;
43 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
46 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
47 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
48 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
51 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
52 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
53 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
56 public class ModuleEffectiveStatementImpl extends
57 AbstractEffectiveDocumentedNode<String, ModuleStatement> implements
60 private final QNameModule qNameModule;
61 private final String name;
62 private final 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 final 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 final 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).argument();
102 if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
103 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement).argument();
105 if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
106 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement).argument();
108 if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
109 contact = ((ContactEffectiveStatementImpl) effectiveStatement).argument();
113 if (yangVersion == null) {
117 DeclarationInTextSource sourceReference = (DeclarationInTextSource) ctx.getStatementSourceReference();
118 sourcePath = sourceReference.getSourceName();
119 source = sourceReference.getSourceText();
122 initSubstatementCollections(ctx);
125 private void initSubmodules(
126 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
127 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
128 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
130 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
131 this.submodules = ImmutableSet.of();
132 this.substatementsOfSubmodules = ImmutableList.of();
136 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
138 Set<Module> submodulesInit = new HashSet<>();
139 List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
140 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
141 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
142 .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
143 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
145 submodulesInit.add(submodule);
146 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
149 this.submodules = ImmutableSet.copyOf(submodulesInit);
150 this.substatementsOfSubmodules = ImmutableList
151 .copyOf(substatementsOfSubmodulesInit);
154 private void initSubstatementCollections(
155 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
156 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
158 effectiveSubstatements.addAll(effectiveSubstatements());
159 effectiveSubstatements.addAll(substatementsOfSubmodules);
161 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
162 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
163 Set<ModuleImport> importsInit = new HashSet<>();
164 Set<NotificationDefinition> notificationsInit = new HashSet<>();
165 Set<RpcDefinition> rpcsInit = new HashSet<>();
166 Set<Deviation> deviationsInit = new HashSet<>();
167 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
168 Set<FeatureDefinition> featuresInit = new HashSet<>();
169 List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
171 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
172 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
173 Set<UsesNode> mutableUses = new HashSet<>();
174 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
175 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
177 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
178 if (effectiveStatement instanceof UnknownSchemaNode) {
179 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
181 if (effectiveStatement instanceof AugmentationSchema) {
182 augmentationsInit.add((AugmentationSchema) effectiveStatement);
184 if (effectiveStatement instanceof ModuleImport) {
185 importsInit.add((ModuleImport) effectiveStatement);
187 if (effectiveStatement instanceof NotificationDefinition) {
188 notificationsInit.add((NotificationDefinition) effectiveStatement);
190 if (effectiveStatement instanceof RpcDefinition) {
191 rpcsInit.add((RpcDefinition) effectiveStatement);
193 if (effectiveStatement instanceof Deviation) {
194 deviationsInit.add((Deviation) effectiveStatement);
196 if (effectiveStatement instanceof IdentitySchemaNode) {
197 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
199 if (effectiveStatement instanceof FeatureDefinition) {
200 featuresInit.add((FeatureDefinition) effectiveStatement);
202 if (effectiveStatement instanceof ExtensionDefinition) {
203 extensionNodesInit.add((ExtensionDefinition) effectiveStatement);
205 if (effectiveStatement instanceof DataSchemaNode) {
206 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
207 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
208 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
209 mutablePublicChildNodes.add(dataSchemaNode);
211 throw EffectiveStmtUtils
212 .createNameCollisionSourceException(ctx,
216 if (effectiveStatement instanceof UsesNode) {
217 UsesNode usesNode = (UsesNode) effectiveStatement;
218 if (!mutableUses.contains(usesNode)) {
219 mutableUses.add(usesNode);
221 throw EffectiveStmtUtils
222 .createNameCollisionSourceException(ctx,
226 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
227 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
228 ExtendedType extendedType = typeDef.buildType();
229 if (!mutableTypeDefinitions.contains(extendedType)) {
230 mutableTypeDefinitions.add(extendedType);
232 throw EffectiveStmtUtils
233 .createNameCollisionSourceException(ctx,
237 if (effectiveStatement instanceof GroupingDefinition) {
238 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
239 if (!mutableGroupings.contains(grp)) {
240 mutableGroupings.add(grp);
242 throw EffectiveStmtUtils
243 .createNameCollisionSourceException(ctx,
249 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
250 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
251 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit,
253 this.notifications = ImmutableSet.copyOf(notificationsInit);
254 this.rpcs = ImmutableSet.copyOf(rpcsInit);
255 this.deviations = ImmutableSet.copyOf(deviationsInit);
256 this.identities = ImmutableSet.copyOf(identitiesInit);
257 this.features = ImmutableSet.copyOf(featuresInit);
258 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
260 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
261 this.groupings = ImmutableSet.copyOf(mutableGroupings);
262 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
263 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
264 this.uses = ImmutableSet.copyOf(mutableUses);
267 private static Set<ModuleImport> resolveModuleImports(
268 final Set<ModuleImport> importsInit,
269 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
270 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
271 for (ModuleImport moduleImport : importsInit) {
272 if (moduleImport.getRevision().equals(
273 SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
274 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
275 moduleImport.getPrefix());
276 if (!impModuleQName.getRevision().equals(
277 SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
278 ModuleImport resolvedModuleImport = new ModuleImportImpl(
279 moduleImport.getModuleName(),
280 impModuleQName.getRevision(),
281 moduleImport.getPrefix());
282 resolvedModuleImports.add(resolvedModuleImport);
285 resolvedModuleImports.add(moduleImport);
288 return resolvedModuleImports;
292 public String getModuleSourcePath() {
297 public URI getNamespace() {
298 return qNameModule.getNamespace();
302 public String getName() {
307 public Date getRevision() {
308 return qNameModule.getRevision();
312 public String getPrefix() {
317 public String getYangVersion() {
322 public String getOrganization() {
327 public String getContact() {
332 public Set<ModuleImport> getImports() {
337 public Set<Module> getSubmodules() {
342 public Set<FeatureDefinition> getFeatures() {
347 public Set<NotificationDefinition> getNotifications() {
348 return notifications;
352 public Set<AugmentationSchema> getAugmentations() {
353 return augmentations;
357 public Set<RpcDefinition> getRpcs() {
362 public Set<Deviation> getDeviations() {
367 public List<ExtensionDefinition> getExtensionSchemaNodes() {
368 return extensionNodes;
372 public Set<IdentitySchemaNode> getIdentities() {
377 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
382 public final Set<TypeDefinition<?>> getTypeDefinitions() {
383 return typeDefinitions;
387 public final Set<DataSchemaNode> getChildNodes() {
388 return publicChildNodes;
392 public final Set<GroupingDefinition> getGroupings() {
397 public final DataSchemaNode getDataChildByName(final QName name) {
398 // Child nodes are keyed by their container name, so we can do a direct
400 return childNodes.get(name);
404 public final DataSchemaNode getDataChildByName(final String name) {
405 for (DataSchemaNode node : childNodes.values()) {
406 if (node.getQName().getLocalName().equals(name)) {
414 public Set<UsesNode> getUses() {
419 public String getSource() {
424 public int hashCode() {
425 final int prime = 31;
427 result = prime * result + Objects.hashCode(name);
428 result = prime * result + Objects.hashCode(yangVersion);
429 result = prime * result + qNameModule.hashCode();
434 public boolean equals(final Object obj) {
441 if (getClass() != obj.getClass()) {
444 ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
446 if (other.name != null) {
449 } else if (!name.equals(other.name)) {
452 if (!qNameModule.equals(other.qNameModule)) {
455 if (yangVersion == null) {
456 if (other.yangVersion != null) {
459 } else if (!yangVersion.equals(other.yangVersion)) {
466 public String toString() {
467 StringBuilder sb = new StringBuilder(
468 ModuleEffectiveStatementImpl.class.getSimpleName());
470 sb.append("name=").append(name);
471 sb.append(", namespace=").append(getNamespace());
472 sb.append(", revision=").append(getRevision());
473 sb.append(", prefix=").append(prefix);
474 sb.append(", yangVersion=").append(yangVersion);
476 return sb.toString();
480 public QNameModule getQNameModule() {