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.model.util.ModuleImportImpl;
13 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
14 import java.util.LinkedHashSet;
15 import java.util.LinkedHashMap;
16 import com.google.common.collect.ImmutableMap;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.UsesNode;
22 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
24 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
25 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
27 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
28 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
30 import java.util.Collection;
31 import java.util.Date;
32 import java.util.HashSet;
33 import java.util.LinkedList;
34 import java.util.List;
36 import org.opendaylight.yangtools.concepts.Immutable;
37 import org.opendaylight.yangtools.yang.common.QNameModule;
38 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
39 import org.opendaylight.yangtools.yang.model.api.Deviation;
40 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
41 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
42 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
45 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
46 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
47 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
49 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
50 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
52 import com.google.common.collect.ImmutableList;
53 import com.google.common.collect.ImmutableSet;
54 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
55 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
57 public class SubmoduleEffectiveStatementImpl
59 AbstractEffectiveDocumentedNode<String, SubmoduleStatement>
60 implements Module, Immutable {
62 private final QNameModule qNameModule;
63 private final String name;
64 private String sourcePath;
65 private String prefix;
66 private String yangVersion;
67 private String organization;
68 private String contact;
69 private ImmutableSet<ModuleImport> imports;
70 private ImmutableSet<Module> submodules;
71 private ImmutableSet<FeatureDefinition> features;
72 private ImmutableSet<NotificationDefinition> notifications;
73 private ImmutableSet<AugmentationSchema> augmentations;
74 private ImmutableSet<RpcDefinition> rpcs;
75 private ImmutableSet<Deviation> deviations;
76 private ImmutableList<ExtensionDefinition> extensionNodes;
77 private ImmutableSet<IdentitySchemaNode> identities;
78 private ImmutableList<UnknownSchemaNode> unknownNodes;
79 private String source;
80 private ImmutableList<EffectiveStatement<?,?>> substatementsOfSubmodules;
82 private ImmutableMap<QName, DataSchemaNode> childNodes;
83 private ImmutableSet<GroupingDefinition> groupings;
84 private ImmutableSet<UsesNode> uses;
85 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
86 private ImmutableSet<DataSchemaNode> publicChildNodes;
88 public SubmoduleEffectiveStatementImpl(
89 StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
94 String belongsToModuleName = firstAttributeOf(
95 ctx.declaredSubstatements(), BelongsToStatement.class);
96 final QNameModule belongsToModuleQName = ctx.getFromNamespace(
97 ModuleNameToModuleQName.class, belongsToModuleName);
98 RevisionEffectiveStatementImpl submoduleRevision = firstEffective(RevisionEffectiveStatementImpl.class);
100 qNameModule = submoduleRevision == null ? QNameModule.create(
101 belongsToModuleQName.getNamespace(),
102 SimpleDateFormatUtil.DEFAULT_DATE_REV) : QNameModule.create(
103 belongsToModuleQName.getNamespace(),
104 submoduleRevision.argument());
106 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
107 if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
108 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement)
111 if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
112 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement)
115 if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
116 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement)
119 if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
120 contact = ((ContactEffectiveStatementImpl) effectiveStatement)
126 // source = ctx.getStatementSource().name();
127 sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
130 initSubstatementCollections(ctx);
133 private void initSubmodules(
134 StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
135 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
136 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
138 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
139 this.submodules = ImmutableSet.of();
140 this.substatementsOfSubmodules = ImmutableList.of();
144 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap
147 Set<Module> submodulesInit = new HashSet<>();
148 List<EffectiveStatement<?,?>> substatementsOfSubmodulesInit = new LinkedList<>();
149 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
150 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
151 .getFromNamespace(SubmoduleNamespace.class,
152 submoduleIdentifier);
153 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx.buildEffective();
154 submodulesInit.add(submodule);
155 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
158 this.submodules = ImmutableSet.copyOf(submodulesInit);
159 this.substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
162 private void initSubstatementCollections(StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
163 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
165 effectiveSubstatements.addAll(effectiveSubstatements());
166 effectiveSubstatements.addAll(this.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 //:TODO add validation also for other node types
185 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
186 if (effectiveStatement instanceof UnknownSchemaNode) {
187 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
189 if (effectiveStatement instanceof AugmentationSchema) {
190 augmentationsInit.add((AugmentationSchema) effectiveStatement);
192 if (effectiveStatement instanceof ModuleImport) {
193 importsInit.add((ModuleImport) effectiveStatement);
195 if (effectiveStatement instanceof NotificationDefinition) {
197 .add((NotificationDefinition) effectiveStatement);
199 if (effectiveStatement instanceof RpcDefinition) {
200 rpcsInit.add((RpcDefinition) effectiveStatement);
202 if (effectiveStatement instanceof Deviation) {
203 deviationsInit.add((Deviation) effectiveStatement);
205 if (effectiveStatement instanceof IdentitySchemaNode) {
206 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
208 if (effectiveStatement instanceof FeatureDefinition) {
209 featuresInit.add((FeatureDefinition) effectiveStatement);
211 if (effectiveStatement instanceof ExtensionDefinition) {
213 .add((ExtensionDefinition) effectiveStatement);
215 if (effectiveStatement instanceof DataSchemaNode) {
216 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
217 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
218 mutableChildNodes.put(dataSchemaNode.getQName(),
220 mutablePublicChildNodes.add(dataSchemaNode);
222 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
225 if (effectiveStatement instanceof UsesNode) {
226 UsesNode usesNode = (UsesNode) effectiveStatement;
227 if (!mutableUses.contains(usesNode)) {
228 mutableUses.add(usesNode);
230 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
233 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
234 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
235 ExtendedType extendedType = typeDef.buildType();
236 if (!mutableTypeDefinitions.contains(extendedType)) {
237 mutableTypeDefinitions.add(extendedType);
239 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
242 if (effectiveStatement instanceof GroupingDefinition) {
243 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
244 if (!mutableGroupings.contains(grp)) {
245 mutableGroupings.add(grp);
247 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
252 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
253 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
254 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit,ctx));
255 this.notifications = ImmutableSet.copyOf(notificationsInit);
256 this.rpcs = ImmutableSet.copyOf(rpcsInit);
257 this.deviations = ImmutableSet.copyOf(deviationsInit);
258 this.identities = ImmutableSet.copyOf(identitiesInit);
259 this.features = ImmutableSet.copyOf(featuresInit);
260 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
262 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
263 this.groupings = ImmutableSet.copyOf(mutableGroupings);
264 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
265 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
266 this.uses = ImmutableSet.copyOf(mutableUses);
269 private Set<ModuleImport> resolveModuleImports(
270 Set<ModuleImport> importsInit,
271 StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
272 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
273 for (ModuleImport moduleImport : importsInit) {
274 if (moduleImport.getRevision().equals(
275 SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
276 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
277 moduleImport.getPrefix());
278 if (!impModuleQName.getRevision().equals(
279 SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
280 ModuleImport resolvedModuleImport = new ModuleImportImpl(
281 moduleImport.getModuleName(),
282 impModuleQName.getRevision(),
283 moduleImport.getPrefix());
284 resolvedModuleImports.add(resolvedModuleImport);
287 resolvedModuleImports.add(moduleImport);
290 return resolvedModuleImports;
294 public String getModuleSourcePath() {
299 public URI getNamespace() {
300 return qNameModule.getNamespace();
304 public String getName() {
309 public Date getRevision() {
310 return qNameModule.getRevision();
314 public String getPrefix() {
319 public String getYangVersion() {
324 public String getOrganization() {
329 public String getContact() {
334 public Set<ModuleImport> getImports() {
339 public Set<Module> getSubmodules() {
344 public Set<FeatureDefinition> getFeatures() {
349 public Set<NotificationDefinition> getNotifications() {
350 return notifications;
354 public Set<AugmentationSchema> getAugmentations() {
355 return augmentations;
359 public Set<RpcDefinition> getRpcs() {
364 public Set<Deviation> getDeviations() {
369 public List<ExtensionDefinition> getExtensionSchemaNodes() {
370 return extensionNodes;
374 public Set<IdentitySchemaNode> getIdentities() {
379 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
384 public final Set<TypeDefinition<?>> getTypeDefinitions() {
385 return typeDefinitions;
389 public final Set<DataSchemaNode> getChildNodes() {
390 return publicChildNodes;
394 public final Set<GroupingDefinition> getGroupings() {
399 public final DataSchemaNode getDataChildByName(final QName name) {
400 // Child nodes are keyed by their container name, so we can do a direct
402 return childNodes.get(name);
406 public final DataSchemaNode getDataChildByName(final String name) {
407 for (DataSchemaNode node : childNodes.values()) {
408 if (node.getQName().getLocalName().equals(name)) {
416 public Set<UsesNode> getUses() {
421 public String getSource() {
426 public int hashCode() {
427 final int prime = 31;
429 result = prime * result + ((name == null) ? 0 : name.hashCode());
430 result = prime * result
431 + ((yangVersion == null) ? 0 : yangVersion.hashCode());
432 result = prime * result + qNameModule.hashCode();
437 public boolean equals(final Object obj) {
444 if (getClass() != obj.getClass()) {
447 SubmoduleEffectiveStatementImpl other = (SubmoduleEffectiveStatementImpl) obj;
449 if (other.name != null) {
452 } else if (!name.equals(other.name)) {
455 if (!qNameModule.equals(other.qNameModule)) {
458 if (yangVersion == null) {
459 if (other.yangVersion != null) {
462 } else if (!yangVersion.equals(other.yangVersion)) {
469 public String toString() {
471 StringBuilder sb = new StringBuilder(SubmoduleEffectiveStatementImpl.class.getSimpleName());
473 sb.append("name=").append(name);
474 sb.append(", namespace=").append(getNamespace());
475 sb.append(", revision=").append(getRevision());
476 sb.append(", prefix=").append(prefix);
477 sb.append(", yangVersion=").append(yangVersion);
479 return sb.toString();
483 public QNameModule getQNameModule() {