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 DeclarationInTextSource sourceReference = (DeclarationInTextSource) ctx.getStatementSourceReference();
122 sourcePath = sourceReference.getSourceName();
123 source = sourceReference.getSourceText();
126 initSubstatementCollections(ctx);
129 private void initSubmodules(
130 StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
131 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
132 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
134 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
135 this.submodules = ImmutableSet.of();
136 this.substatementsOfSubmodules = ImmutableList.of();
140 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap
143 Set<Module> submodulesInit = new HashSet<>();
144 List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
145 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
146 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
147 .getFromNamespace(SubmoduleNamespace.class,
148 submoduleIdentifier);
149 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
151 submodulesInit.add(submodule);
152 substatementsOfSubmodulesInit.addAll(submodule
153 .effectiveSubstatements());
156 this.submodules = ImmutableSet.copyOf(submodulesInit);
157 this.substatementsOfSubmodules = ImmutableList
158 .copyOf(substatementsOfSubmodulesInit);
161 private void initSubstatementCollections(
162 StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
163 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
165 effectiveSubstatements.addAll(effectiveSubstatements());
166 effectiveSubstatements.addAll(substatementsOfSubmodules);
168 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
169 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
170 Set<ModuleImport> importsInit = new HashSet<>();
171 Set<NotificationDefinition> notificationsInit = new HashSet<>();
172 Set<RpcDefinition> rpcsInit = new HashSet<>();
173 Set<Deviation> deviationsInit = new HashSet<>();
174 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
175 Set<FeatureDefinition> featuresInit = new HashSet<>();
176 List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
178 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
179 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
180 Set<UsesNode> mutableUses = new HashSet<>();
181 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
182 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
184 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
185 if (effectiveStatement instanceof UnknownSchemaNode) {
186 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
188 if (effectiveStatement instanceof AugmentationSchema) {
189 augmentationsInit.add((AugmentationSchema) effectiveStatement);
191 if (effectiveStatement instanceof ModuleImport) {
192 importsInit.add((ModuleImport) effectiveStatement);
194 if (effectiveStatement instanceof NotificationDefinition) {
196 .add((NotificationDefinition) effectiveStatement);
198 if (effectiveStatement instanceof RpcDefinition) {
199 rpcsInit.add((RpcDefinition) effectiveStatement);
201 if (effectiveStatement instanceof Deviation) {
202 deviationsInit.add((Deviation) effectiveStatement);
204 if (effectiveStatement instanceof IdentitySchemaNode) {
205 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
207 if (effectiveStatement instanceof FeatureDefinition) {
208 featuresInit.add((FeatureDefinition) effectiveStatement);
210 if (effectiveStatement instanceof ExtensionDefinition) {
212 .add((ExtensionDefinition) effectiveStatement);
214 if (effectiveStatement instanceof DataSchemaNode) {
215 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
216 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
217 mutableChildNodes.put(dataSchemaNode.getQName(),
219 mutablePublicChildNodes.add(dataSchemaNode);
221 throw EffectiveStmtUtils
222 .createNameCollisionSourceException(ctx,
226 if (effectiveStatement instanceof UsesNode) {
227 UsesNode usesNode = (UsesNode) effectiveStatement;
228 if (!mutableUses.contains(usesNode)) {
229 mutableUses.add(usesNode);
231 throw EffectiveStmtUtils
232 .createNameCollisionSourceException(ctx,
236 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
237 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
238 ExtendedType extendedType = typeDef.buildType();
239 if (!mutableTypeDefinitions.contains(extendedType)) {
240 mutableTypeDefinitions.add(extendedType);
242 throw EffectiveStmtUtils
243 .createNameCollisionSourceException(ctx,
247 if (effectiveStatement instanceof GroupingDefinition) {
248 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
249 if (!mutableGroupings.contains(grp)) {
250 mutableGroupings.add(grp);
252 throw EffectiveStmtUtils
253 .createNameCollisionSourceException(ctx,
259 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
260 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
261 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit,
263 this.notifications = ImmutableSet.copyOf(notificationsInit);
264 this.rpcs = ImmutableSet.copyOf(rpcsInit);
265 this.deviations = ImmutableSet.copyOf(deviationsInit);
266 this.identities = ImmutableSet.copyOf(identitiesInit);
267 this.features = ImmutableSet.copyOf(featuresInit);
268 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
270 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
271 this.groupings = ImmutableSet.copyOf(mutableGroupings);
272 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
273 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
274 this.uses = ImmutableSet.copyOf(mutableUses);
277 private Set<ModuleImport> resolveModuleImports(
278 Set<ModuleImport> importsInit,
279 StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
280 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
281 for (ModuleImport moduleImport : importsInit) {
282 if (moduleImport.getRevision().equals(
283 SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
284 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
285 moduleImport.getPrefix());
286 if (!impModuleQName.getRevision().equals(
287 SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
288 ModuleImport resolvedModuleImport = new ModuleImportImpl(
289 moduleImport.getModuleName(),
290 impModuleQName.getRevision(),
291 moduleImport.getPrefix());
292 resolvedModuleImports.add(resolvedModuleImport);
295 resolvedModuleImports.add(moduleImport);
298 return resolvedModuleImports;
302 public String getModuleSourcePath() {
307 public URI getNamespace() {
308 return qNameModule.getNamespace();
312 public String getName() {
317 public Date getRevision() {
318 return qNameModule.getRevision();
322 public String getPrefix() {
327 public String getYangVersion() {
332 public String getOrganization() {
337 public String getContact() {
342 public Set<ModuleImport> getImports() {
347 public Set<Module> getSubmodules() {
352 public Set<FeatureDefinition> getFeatures() {
357 public Set<NotificationDefinition> getNotifications() {
358 return notifications;
362 public Set<AugmentationSchema> getAugmentations() {
363 return augmentations;
367 public Set<RpcDefinition> getRpcs() {
372 public Set<Deviation> getDeviations() {
377 public List<ExtensionDefinition> getExtensionSchemaNodes() {
378 return extensionNodes;
382 public Set<IdentitySchemaNode> getIdentities() {
387 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
392 public final Set<TypeDefinition<?>> getTypeDefinitions() {
393 return typeDefinitions;
397 public final Set<DataSchemaNode> getChildNodes() {
398 return publicChildNodes;
402 public final Set<GroupingDefinition> getGroupings() {
407 public final DataSchemaNode getDataChildByName(final QName name) {
408 // Child nodes are keyed by their container name, so we can do a direct
410 return childNodes.get(name);
414 public final DataSchemaNode getDataChildByName(final String name) {
415 for (DataSchemaNode node : childNodes.values()) {
416 if (node.getQName().getLocalName().equals(name)) {
424 public Set<UsesNode> getUses() {
429 public String getSource() {
434 public int hashCode() {
435 final int prime = 31;
437 result = prime * result + ((name == null) ? 0 : name.hashCode());
438 result = prime * result
439 + ((yangVersion == null) ? 0 : yangVersion.hashCode());
440 result = prime * result + qNameModule.hashCode();
445 public boolean equals(final Object obj) {
452 if (getClass() != obj.getClass()) {
455 ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
457 if (other.name != null) {
460 } else if (!name.equals(other.name)) {
463 if (!qNameModule.equals(other.qNameModule)) {
466 if (yangVersion == null) {
467 if (other.yangVersion != null) {
470 } else if (!yangVersion.equals(other.yangVersion)) {
477 public String toString() {
478 StringBuilder sb = new StringBuilder(
479 ModuleEffectiveStatementImpl.class.getSimpleName());
481 sb.append("name=").append(name);
482 sb.append(", namespace=").append(getNamespace());
483 sb.append(", revision=").append(getRevision());
484 sb.append(", prefix=").append(prefix);
485 sb.append(", yangVersion=").append(yangVersion);
487 return sb.toString();
491 public QNameModule getQNameModule() {