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 final String source;
77 private ImmutableList<EffectiveStatement<?, ?>> substatementsOfSubmodules;
79 private ImmutableMap<QName, DataSchemaNode> childNodes;
80 private ImmutableSet<GroupingDefinition> groupings;
81 private ImmutableSet<UsesNode> uses;
82 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
83 private ImmutableSet<DataSchemaNode> publicChildNodes;
85 public ModuleEffectiveStatementImpl(
86 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
90 QNameModule qNameModuleInit = ctx.getFromNamespace( ModuleCtxToModuleQName.class, ctx);
91 qNameModule = qNameModuleInit.getRevision() == null
92 ? QNameModule.create(qNameModuleInit.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV)
95 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
96 if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
97 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement).argument();
99 if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
100 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement).argument();
102 if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
103 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement).argument();
105 if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
106 contact = ((ContactEffectiveStatementImpl) effectiveStatement).argument();
110 if (yangVersion == null) {
114 DeclarationInTextSource sourceReference = (DeclarationInTextSource) ctx.getStatementSourceReference();
115 sourcePath = sourceReference.getSourceName();
116 source = sourceReference.getSourceText();
119 initSubstatementCollections(ctx);
122 private void initSubmodules(
123 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
124 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
125 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
127 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
128 this.submodules = ImmutableSet.of();
129 this.substatementsOfSubmodules = ImmutableList.of();
133 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
135 Set<Module> submodulesInit = new HashSet<>();
136 List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
137 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
138 @SuppressWarnings("unchecked")
139 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
140 (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)
141 ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
142 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
144 submodulesInit.add(submodule);
145 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
148 this.submodules = ImmutableSet.copyOf(submodulesInit);
149 this.substatementsOfSubmodules = ImmutableList
150 .copyOf(substatementsOfSubmodulesInit);
153 private void initSubstatementCollections(
154 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
155 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
157 effectiveSubstatements.addAll(effectiveSubstatements());
158 effectiveSubstatements.addAll(substatementsOfSubmodules);
160 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
161 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
162 Set<ModuleImport> importsInit = new HashSet<>();
163 Set<NotificationDefinition> notificationsInit = new HashSet<>();
164 Set<RpcDefinition> rpcsInit = new HashSet<>();
165 Set<Deviation> deviationsInit = new HashSet<>();
166 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
167 Set<FeatureDefinition> featuresInit = new HashSet<>();
168 List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
170 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
171 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
172 Set<UsesNode> mutableUses = new HashSet<>();
173 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
174 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
176 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
177 if (effectiveStatement instanceof UnknownSchemaNode) {
178 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
180 if (effectiveStatement instanceof AugmentationSchema) {
181 augmentationsInit.add((AugmentationSchema) effectiveStatement);
183 if (effectiveStatement instanceof ModuleImport) {
184 importsInit.add((ModuleImport) effectiveStatement);
186 if (effectiveStatement instanceof NotificationDefinition) {
187 notificationsInit.add((NotificationDefinition) effectiveStatement);
189 if (effectiveStatement instanceof RpcDefinition) {
190 rpcsInit.add((RpcDefinition) effectiveStatement);
192 if (effectiveStatement instanceof Deviation) {
193 deviationsInit.add((Deviation) effectiveStatement);
195 if (effectiveStatement instanceof IdentitySchemaNode) {
196 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
198 if (effectiveStatement instanceof FeatureDefinition) {
199 featuresInit.add((FeatureDefinition) effectiveStatement);
201 if (effectiveStatement instanceof ExtensionDefinition) {
202 extensionNodesInit.add((ExtensionDefinition) effectiveStatement);
204 if (effectiveStatement instanceof DataSchemaNode) {
205 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
206 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
207 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
208 mutablePublicChildNodes.add(dataSchemaNode);
210 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
213 if (effectiveStatement instanceof UsesNode) {
214 UsesNode usesNode = (UsesNode) effectiveStatement;
215 if (!mutableUses.contains(usesNode)) {
216 mutableUses.add(usesNode);
218 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
221 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
222 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
223 ExtendedType extendedType = typeDef.buildType();
224 if (!mutableTypeDefinitions.contains(extendedType)) {
225 mutableTypeDefinitions.add(extendedType);
227 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
230 if (effectiveStatement instanceof GroupingDefinition) {
231 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
232 if (!mutableGroupings.contains(grp)) {
233 mutableGroupings.add(grp);
235 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
240 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
241 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
242 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
243 this.notifications = ImmutableSet.copyOf(notificationsInit);
244 this.rpcs = ImmutableSet.copyOf(rpcsInit);
245 this.deviations = ImmutableSet.copyOf(deviationsInit);
246 this.identities = ImmutableSet.copyOf(identitiesInit);
247 this.features = ImmutableSet.copyOf(featuresInit);
248 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
250 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
251 this.groupings = ImmutableSet.copyOf(mutableGroupings);
252 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
253 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
254 this.uses = ImmutableSet.copyOf(mutableUses);
257 private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
258 final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
259 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
260 for (ModuleImport moduleImport : importsInit) {
261 if (moduleImport.getRevision().equals(
262 SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
263 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
264 moduleImport.getPrefix());
265 if (!impModuleQName.getRevision().equals(
266 SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
267 ModuleImport resolvedModuleImport = new ModuleImportImpl(
268 moduleImport.getModuleName(),
269 impModuleQName.getRevision(),
270 moduleImport.getPrefix());
271 resolvedModuleImports.add(resolvedModuleImport);
274 resolvedModuleImports.add(moduleImport);
277 return resolvedModuleImports;
281 public String getModuleSourcePath() {
286 public URI getNamespace() {
287 return qNameModule.getNamespace();
291 public String getName() {
296 public Date getRevision() {
297 return qNameModule.getRevision();
301 public String getPrefix() {
306 public String getYangVersion() {
311 public String getOrganization() {
316 public String getContact() {
321 public Set<ModuleImport> getImports() {
326 public Set<Module> getSubmodules() {
331 public Set<FeatureDefinition> getFeatures() {
336 public Set<NotificationDefinition> getNotifications() {
337 return notifications;
341 public Set<AugmentationSchema> getAugmentations() {
342 return augmentations;
346 public Set<RpcDefinition> getRpcs() {
351 public Set<Deviation> getDeviations() {
356 public List<ExtensionDefinition> getExtensionSchemaNodes() {
357 return extensionNodes;
361 public Set<IdentitySchemaNode> getIdentities() {
366 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
371 public final Set<TypeDefinition<?>> getTypeDefinitions() {
372 return typeDefinitions;
376 public final Set<DataSchemaNode> getChildNodes() {
377 return publicChildNodes;
381 public final Set<GroupingDefinition> getGroupings() {
386 public final DataSchemaNode getDataChildByName(final QName name) {
387 // Child nodes are keyed by their container name, so we can do a direct
389 return childNodes.get(name);
393 public final DataSchemaNode getDataChildByName(final String name) {
394 for (DataSchemaNode node : childNodes.values()) {
395 if (node.getQName().getLocalName().equals(name)) {
403 public Set<UsesNode> getUses() {
408 public String getSource() {
413 public int hashCode() {
414 final int prime = 31;
416 result = prime * result + Objects.hashCode(name);
417 result = prime * result + Objects.hashCode(yangVersion);
418 result = prime * result + qNameModule.hashCode();
423 public boolean equals(final Object obj) {
430 if (getClass() != obj.getClass()) {
433 ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
434 if (!Objects.equals(name, other.name)) {
437 if (!qNameModule.equals(other.qNameModule)) {
440 if (!Objects.equals(yangVersion, other.yangVersion)) {
447 public String toString() {
448 StringBuilder sb = new StringBuilder(ModuleEffectiveStatementImpl.class.getSimpleName());
450 sb.append("name=").append(name);
451 sb.append(", namespace=").append(getNamespace());
452 sb.append(", revision=").append(getRevision());
453 sb.append(", prefix=").append(prefix);
454 sb.append(", yangVersion=").append(yangVersion);
456 return sb.toString();
460 public QNameModule getQNameModule() {