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 AbstractEffectiveDocumentedNode<String, ModuleStatement> implements
59 private final QNameModule qNameModule;
60 private final String name;
61 private final String sourcePath;
62 private String prefix;
63 private String yangVersion;
64 private String organization;
65 private String contact;
66 private ImmutableSet<ModuleImport> imports;
67 private ImmutableSet<Module> submodules;
68 private ImmutableSet<FeatureDefinition> features;
69 private ImmutableSet<NotificationDefinition> notifications;
70 private ImmutableSet<AugmentationSchema> augmentations;
71 private ImmutableSet<RpcDefinition> rpcs;
72 private ImmutableSet<Deviation> deviations;
73 private ImmutableList<ExtensionDefinition> extensionNodes;
74 private ImmutableSet<IdentitySchemaNode> identities;
75 private ImmutableList<UnknownSchemaNode> unknownNodes;
76 private ImmutableList<EffectiveStatement<?, ?>> substatementsOfSubmodules;
78 private ImmutableMap<QName, DataSchemaNode> childNodes;
79 private ImmutableSet<GroupingDefinition> groupings;
80 private ImmutableSet<UsesNode> uses;
81 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
82 private ImmutableSet<DataSchemaNode> publicChildNodes;
84 public ModuleEffectiveStatementImpl(
85 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
89 QNameModule qNameModuleInit = ctx.getFromNamespace( ModuleCtxToModuleQName.class, ctx);
90 qNameModule = qNameModuleInit.getRevision() == null
91 ? QNameModule.create(qNameModuleInit.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV)
94 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
95 if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
96 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement).argument();
98 if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
99 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement).argument();
101 if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
102 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement).argument();
104 if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
105 contact = ((ContactEffectiveStatementImpl) effectiveStatement).argument();
109 if (yangVersion == null) {
112 if(ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
113 sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
120 initSubstatementCollections(ctx);
123 private void initSubmodules(
124 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
125 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
126 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
128 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
129 this.submodules = ImmutableSet.of();
130 this.substatementsOfSubmodules = ImmutableList.of();
134 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
136 Set<Module> submodulesInit = new HashSet<>();
137 List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
138 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
139 @SuppressWarnings("unchecked")
140 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
141 (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)
142 ctx.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.createNameCollisionSourceException(ctx, effectiveStatement);
214 if (effectiveStatement instanceof UsesNode) {
215 UsesNode usesNode = (UsesNode) effectiveStatement;
216 if (!mutableUses.contains(usesNode)) {
217 mutableUses.add(usesNode);
219 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
222 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
223 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
224 ExtendedType extendedType = typeDef.buildType();
225 if (!mutableTypeDefinitions.contains(extendedType)) {
226 mutableTypeDefinitions.add(extendedType);
228 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
231 if (effectiveStatement instanceof GroupingDefinition) {
232 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
233 if (!mutableGroupings.contains(grp)) {
234 mutableGroupings.add(grp);
236 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
241 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
242 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
243 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
244 this.notifications = ImmutableSet.copyOf(notificationsInit);
245 this.rpcs = ImmutableSet.copyOf(rpcsInit);
246 this.deviations = ImmutableSet.copyOf(deviationsInit);
247 this.identities = ImmutableSet.copyOf(identitiesInit);
248 this.features = ImmutableSet.copyOf(featuresInit);
249 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
251 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
252 this.groupings = ImmutableSet.copyOf(mutableGroupings);
253 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
254 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
255 this.uses = ImmutableSet.copyOf(mutableUses);
258 private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
259 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
260 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
261 for (ModuleImport moduleImport : importsInit) {
262 if (moduleImport.getRevision().equals(
263 SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
264 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
265 moduleImport.getPrefix());
266 if (!impModuleQName.getRevision().equals(
267 SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
268 ModuleImport resolvedModuleImport = new ModuleImportImpl(
269 moduleImport.getModuleName(),
270 impModuleQName.getRevision(),
271 moduleImport.getPrefix());
272 resolvedModuleImports.add(resolvedModuleImport);
275 resolvedModuleImports.add(moduleImport);
278 return resolvedModuleImports;
282 public String getModuleSourcePath() {
287 public URI getNamespace() {
288 return qNameModule.getNamespace();
292 public String getName() {
297 public Date getRevision() {
298 return qNameModule.getRevision();
302 public String getPrefix() {
307 public String getYangVersion() {
312 public String getOrganization() {
317 public String getContact() {
322 public Set<ModuleImport> getImports() {
327 public Set<Module> getSubmodules() {
332 public Set<FeatureDefinition> getFeatures() {
337 public Set<NotificationDefinition> getNotifications() {
338 return notifications;
342 public Set<AugmentationSchema> getAugmentations() {
343 return augmentations;
347 public Set<RpcDefinition> getRpcs() {
352 public Set<Deviation> getDeviations() {
357 public List<ExtensionDefinition> getExtensionSchemaNodes() {
358 return extensionNodes;
362 public Set<IdentitySchemaNode> getIdentities() {
367 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
372 public final Set<TypeDefinition<?>> getTypeDefinitions() {
373 return typeDefinitions;
377 public final Set<DataSchemaNode> getChildNodes() {
378 return publicChildNodes;
382 public final Set<GroupingDefinition> getGroupings() {
387 public final DataSchemaNode getDataChildByName(final QName name) {
388 // Child nodes are keyed by their container name, so we can do a direct
390 return childNodes.get(name);
394 public final DataSchemaNode getDataChildByName(final String name) {
395 for (DataSchemaNode node : childNodes.values()) {
396 if (node.getQName().getLocalName().equals(name)) {
404 public Set<UsesNode> getUses() {
409 public String getSource() {
414 public int hashCode() {
415 final int prime = 31;
417 result = prime * result + Objects.hashCode(name);
418 result = prime * result + Objects.hashCode(yangVersion);
419 result = prime * result + qNameModule.hashCode();
424 public boolean equals(final Object obj) {
431 if (getClass() != obj.getClass()) {
434 ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
435 if (!Objects.equals(name, other.name)) {
438 if (!qNameModule.equals(other.qNameModule)) {
441 if (!Objects.equals(yangVersion, other.yangVersion)) {
448 public String toString() {
449 StringBuilder sb = new StringBuilder(ModuleEffectiveStatementImpl.class.getSimpleName());
451 sb.append("name=").append(name);
452 sb.append(", namespace=").append(getNamespace());
453 sb.append(", revision=").append(getRevision());
454 sb.append(", prefix=").append(prefix);
455 sb.append(", yangVersion=").append(yangVersion);
457 return sb.toString();
461 public QNameModule getQNameModule() {