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 static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
15 import java.util.Collection;
16 import java.util.Date;
17 import java.util.HashSet;
18 import java.util.LinkedHashMap;
19 import java.util.LinkedHashSet;
20 import java.util.LinkedList;
21 import java.util.List;
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.BelongsToStatement;
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.ModuleNameToModuleQName;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
56 public class SubmoduleEffectiveStatementImpl
58 AbstractEffectiveDocumentedNode<String, SubmoduleStatement>
59 implements Module, Immutable {
61 private final QNameModule qNameModule;
62 private final String name;
63 private final String sourcePath;
64 private String prefix;
65 private String yangVersion;
66 private String organization;
67 private String contact;
68 private ImmutableSet<ModuleImport> imports;
69 private ImmutableSet<Module> submodules;
70 private ImmutableSet<FeatureDefinition> features;
71 private ImmutableSet<NotificationDefinition> notifications;
72 private ImmutableSet<AugmentationSchema> augmentations;
73 private ImmutableSet<RpcDefinition> rpcs;
74 private ImmutableSet<Deviation> deviations;
75 private ImmutableList<ExtensionDefinition> extensionNodes;
76 private ImmutableSet<IdentitySchemaNode> identities;
77 private ImmutableList<UnknownSchemaNode> unknownNodes;
78 private String source;
79 private ImmutableList<EffectiveStatement<?,?>> substatementsOfSubmodules;
81 private ImmutableMap<QName, DataSchemaNode> childNodes;
82 private ImmutableSet<GroupingDefinition> groupings;
83 private ImmutableSet<UsesNode> uses;
84 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
85 private ImmutableSet<DataSchemaNode> publicChildNodes;
87 public SubmoduleEffectiveStatementImpl(
88 final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
93 String belongsToModuleName = firstAttributeOf(
94 ctx.declaredSubstatements(), BelongsToStatement.class);
95 final QNameModule belongsToModuleQName = ctx.getFromNamespace(
96 ModuleNameToModuleQName.class, belongsToModuleName);
97 RevisionEffectiveStatementImpl submoduleRevision = firstEffective(RevisionEffectiveStatementImpl.class);
99 qNameModule = submoduleRevision == null ? QNameModule.create(
100 belongsToModuleQName.getNamespace(),
101 SimpleDateFormatUtil.DEFAULT_DATE_REV) : QNameModule.create(
102 belongsToModuleQName.getNamespace(),
103 submoduleRevision.argument());
105 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
106 if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
107 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement)
110 if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
111 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement)
114 if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
115 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement)
118 if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
119 contact = ((ContactEffectiveStatementImpl) effectiveStatement)
125 // source = ctx.getStatementSource().name();
126 sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
129 initSubstatementCollections(ctx);
132 private void initSubmodules(
133 final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
134 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
135 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
137 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
138 this.submodules = ImmutableSet.of();
139 this.substatementsOfSubmodules = ImmutableList.of();
143 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap
146 Set<Module> submodulesInit = new HashSet<>();
147 List<EffectiveStatement<?,?>> substatementsOfSubmodulesInit = new LinkedList<>();
148 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
149 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
150 .getFromNamespace(SubmoduleNamespace.class,
151 submoduleIdentifier);
152 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx.buildEffective();
153 submodulesInit.add(submodule);
154 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
157 this.submodules = ImmutableSet.copyOf(submodulesInit);
158 this.substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
161 private void initSubstatementCollections(final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
162 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
164 effectiveSubstatements.addAll(effectiveSubstatements());
165 effectiveSubstatements.addAll(this.substatementsOfSubmodules);
167 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
168 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
169 Set<ModuleImport> importsInit = new HashSet<>();
170 Set<NotificationDefinition> notificationsInit = new HashSet<>();
171 Set<RpcDefinition> rpcsInit = new HashSet<>();
172 Set<Deviation> deviationsInit = new HashSet<>();
173 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
174 Set<FeatureDefinition> featuresInit = new HashSet<>();
175 List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
177 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
178 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
179 Set<UsesNode> mutableUses = new HashSet<>();
180 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
181 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
183 //:TODO add validation also for other node types
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.createNameCollisionSourceException(ctx, effectiveStatement);
224 if (effectiveStatement instanceof UsesNode) {
225 UsesNode usesNode = (UsesNode) effectiveStatement;
226 if (!mutableUses.contains(usesNode)) {
227 mutableUses.add(usesNode);
229 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
232 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
233 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
234 ExtendedType extendedType = typeDef.buildType();
235 if (!mutableTypeDefinitions.contains(extendedType)) {
236 mutableTypeDefinitions.add(extendedType);
238 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
241 if (effectiveStatement instanceof GroupingDefinition) {
242 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
243 if (!mutableGroupings.contains(grp)) {
244 mutableGroupings.add(grp);
246 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
251 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
252 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
253 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit,ctx));
254 this.notifications = ImmutableSet.copyOf(notificationsInit);
255 this.rpcs = ImmutableSet.copyOf(rpcsInit);
256 this.deviations = ImmutableSet.copyOf(deviationsInit);
257 this.identities = ImmutableSet.copyOf(identitiesInit);
258 this.features = ImmutableSet.copyOf(featuresInit);
259 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
261 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
262 this.groupings = ImmutableSet.copyOf(mutableGroupings);
263 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
264 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
265 this.uses = ImmutableSet.copyOf(mutableUses);
268 private static Set<ModuleImport> resolveModuleImports(
269 final Set<ModuleImport> importsInit,
270 final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
271 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
272 for (ModuleImport moduleImport : importsInit) {
273 if (moduleImport.getRevision().equals(
274 SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
275 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
276 moduleImport.getPrefix());
277 if (!impModuleQName.getRevision().equals(
278 SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
279 ModuleImport resolvedModuleImport = new ModuleImportImpl(
280 moduleImport.getModuleName(),
281 impModuleQName.getRevision(),
282 moduleImport.getPrefix());
283 resolvedModuleImports.add(resolvedModuleImport);
286 resolvedModuleImports.add(moduleImport);
289 return resolvedModuleImports;
293 public String getModuleSourcePath() {
298 public URI getNamespace() {
299 return qNameModule.getNamespace();
303 public String getName() {
308 public Date getRevision() {
309 return qNameModule.getRevision();
313 public String getPrefix() {
318 public String getYangVersion() {
323 public String getOrganization() {
328 public String getContact() {
333 public Set<ModuleImport> getImports() {
338 public Set<Module> getSubmodules() {
343 public Set<FeatureDefinition> getFeatures() {
348 public Set<NotificationDefinition> getNotifications() {
349 return notifications;
353 public Set<AugmentationSchema> getAugmentations() {
354 return augmentations;
358 public Set<RpcDefinition> getRpcs() {
363 public Set<Deviation> getDeviations() {
368 public List<ExtensionDefinition> getExtensionSchemaNodes() {
369 return extensionNodes;
373 public Set<IdentitySchemaNode> getIdentities() {
378 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
383 public final Set<TypeDefinition<?>> getTypeDefinitions() {
384 return typeDefinitions;
388 public final Set<DataSchemaNode> getChildNodes() {
389 return publicChildNodes;
393 public final Set<GroupingDefinition> getGroupings() {
398 public final DataSchemaNode getDataChildByName(final QName name) {
399 // Child nodes are keyed by their container name, so we can do a direct
401 return childNodes.get(name);
405 public final DataSchemaNode getDataChildByName(final String name) {
406 for (DataSchemaNode node : childNodes.values()) {
407 if (node.getQName().getLocalName().equals(name)) {
415 public Set<UsesNode> getUses() {
420 public String getSource() {
425 public int hashCode() {
426 final int prime = 31;
428 result = prime * result + ((name == null) ? 0 : name.hashCode());
429 result = prime * result
430 + ((yangVersion == null) ? 0 : yangVersion.hashCode());
431 result = prime * result + qNameModule.hashCode();
436 public boolean equals(final Object obj) {
443 if (getClass() != obj.getClass()) {
446 SubmoduleEffectiveStatementImpl other = (SubmoduleEffectiveStatementImpl) obj;
448 if (other.name != null) {
451 } else if (!name.equals(other.name)) {
454 if (!qNameModule.equals(other.qNameModule)) {
457 if (yangVersion == null) {
458 if (other.yangVersion != null) {
461 } else if (!yangVersion.equals(other.yangVersion)) {
468 public String toString() {
470 StringBuilder sb = new StringBuilder(SubmoduleEffectiveStatementImpl.class.getSimpleName());
472 sb.append("name=").append(name);
473 sb.append(", namespace=").append(getNamespace());
474 sb.append(", revision=").append(getRevision());
475 sb.append(", prefix=").append(prefix);
476 sb.append(", yangVersion=").append(yangVersion);
478 return sb.toString();
482 public QNameModule getQNameModule() {