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;
23 import java.util.Objects;
25 import org.opendaylight.yangtools.concepts.Immutable;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
29 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Deviation;
32 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
33 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
34 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
35 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
38 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
39 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
40 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
41 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
42 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.UsesNode;
44 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
47 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
48 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
49 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
52 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
53 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
54 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
55 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
57 public class SubmoduleEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<String, SubmoduleStatement>
58 implements Module, Immutable {
60 private final QNameModule qNameModule;
61 private final String name;
62 private final 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 SubmoduleEffectiveStatementImpl(
87 final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
92 String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
93 final QNameModule belongsToModuleQName = ctx.getFromNamespace(
94 ModuleNameToModuleQName.class, belongsToModuleName);
95 RevisionEffectiveStatementImpl submoduleRevision = firstEffective(RevisionEffectiveStatementImpl.class);
97 qNameModule = submoduleRevision == null
98 ? QNameModule.create(belongsToModuleQName.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV)
99 : QNameModule.create(belongsToModuleQName.getNamespace(), submoduleRevision.argument());
101 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
102 if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
103 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement).argument();
105 if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
106 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement).argument();
108 if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
109 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement).argument();
111 if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
112 contact = ((ContactEffectiveStatementImpl) effectiveStatement).argument();
117 // source = ctx.getStatementSource().name();
118 sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
121 initSubstatementCollections(ctx);
124 private void initSubmodules(
125 final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
126 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
127 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
129 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
130 this.submodules = ImmutableSet.of();
131 this.substatementsOfSubmodules = ImmutableList.of();
135 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap
138 Set<Module> submodulesInit = new HashSet<>();
139 List<EffectiveStatement<?,?>> substatementsOfSubmodulesInit = new LinkedList<>();
140 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
141 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
142 .getFromNamespace(SubmoduleNamespace.class,
143 submoduleIdentifier);
144 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx.buildEffective();
145 submodulesInit.add(submodule);
146 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
149 this.submodules = ImmutableSet.copyOf(submodulesInit);
150 this.substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
153 private void initSubstatementCollections(final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
154 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
156 effectiveSubstatements.addAll(effectiveSubstatements());
157 effectiveSubstatements.addAll(this.substatementsOfSubmodules);
159 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
160 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
161 Set<ModuleImport> importsInit = new HashSet<>();
162 Set<NotificationDefinition> notificationsInit = new HashSet<>();
163 Set<RpcDefinition> rpcsInit = new HashSet<>();
164 Set<Deviation> deviationsInit = new HashSet<>();
165 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
166 Set<FeatureDefinition> featuresInit = new HashSet<>();
167 List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
169 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
170 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
171 Set<UsesNode> mutableUses = new HashSet<>();
172 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
173 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
175 //:TODO add validation also for other node types
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) {
188 .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) {
204 .add((ExtensionDefinition) effectiveStatement);
206 if (effectiveStatement instanceof DataSchemaNode) {
207 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
208 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
209 mutableChildNodes.put(dataSchemaNode.getQName(),
211 mutablePublicChildNodes.add(dataSchemaNode);
213 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
216 if (effectiveStatement instanceof UsesNode) {
217 UsesNode usesNode = (UsesNode) effectiveStatement;
218 if (!mutableUses.contains(usesNode)) {
219 mutableUses.add(usesNode);
221 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
224 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
225 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
226 ExtendedType extendedType = typeDef.buildType();
227 if (!mutableTypeDefinitions.contains(extendedType)) {
228 mutableTypeDefinitions.add(extendedType);
230 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
233 if (effectiveStatement instanceof GroupingDefinition) {
234 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
235 if (!mutableGroupings.contains(grp)) {
236 mutableGroupings.add(grp);
238 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
243 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
244 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
245 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit,ctx));
246 this.notifications = ImmutableSet.copyOf(notificationsInit);
247 this.rpcs = ImmutableSet.copyOf(rpcsInit);
248 this.deviations = ImmutableSet.copyOf(deviationsInit);
249 this.identities = ImmutableSet.copyOf(identitiesInit);
250 this.features = ImmutableSet.copyOf(featuresInit);
251 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
253 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
254 this.groupings = ImmutableSet.copyOf(mutableGroupings);
255 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
256 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
257 this.uses = ImmutableSet.copyOf(mutableUses);
260 private static Set<ModuleImport> resolveModuleImports(
261 final Set<ModuleImport> importsInit,
262 final StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
263 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
264 for (ModuleImport moduleImport : importsInit) {
265 if (moduleImport.getRevision().equals(
266 SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
267 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
268 moduleImport.getPrefix());
269 if (!impModuleQName.getRevision().equals(
270 SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
271 ModuleImport resolvedModuleImport = new ModuleImportImpl(
272 moduleImport.getModuleName(),
273 impModuleQName.getRevision(),
274 moduleImport.getPrefix());
275 resolvedModuleImports.add(resolvedModuleImport);
278 resolvedModuleImports.add(moduleImport);
281 return resolvedModuleImports;
285 public String getModuleSourcePath() {
290 public URI getNamespace() {
291 return qNameModule.getNamespace();
295 public String getName() {
300 public Date getRevision() {
301 return qNameModule.getRevision();
305 public String getPrefix() {
310 public String getYangVersion() {
315 public String getOrganization() {
320 public String getContact() {
325 public Set<ModuleImport> getImports() {
330 public Set<Module> getSubmodules() {
335 public Set<FeatureDefinition> getFeatures() {
340 public Set<NotificationDefinition> getNotifications() {
341 return notifications;
345 public Set<AugmentationSchema> getAugmentations() {
346 return augmentations;
350 public Set<RpcDefinition> getRpcs() {
355 public Set<Deviation> getDeviations() {
360 public List<ExtensionDefinition> getExtensionSchemaNodes() {
361 return extensionNodes;
365 public Set<IdentitySchemaNode> getIdentities() {
370 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
375 public final Set<TypeDefinition<?>> getTypeDefinitions() {
376 return typeDefinitions;
380 public final Set<DataSchemaNode> getChildNodes() {
381 return publicChildNodes;
385 public final Set<GroupingDefinition> getGroupings() {
390 public final DataSchemaNode getDataChildByName(final QName name) {
391 // Child nodes are keyed by their container name, so we can do a direct
393 return childNodes.get(name);
397 public final DataSchemaNode getDataChildByName(final String name) {
398 for (DataSchemaNode node : childNodes.values()) {
399 if (node.getQName().getLocalName().equals(name)) {
407 public Set<UsesNode> getUses() {
412 public String getSource() {
417 public int hashCode() {
418 final int prime = 31;
420 result = prime * result + Objects.hashCode(name);
421 result = prime * result + Objects.hashCode(yangVersion);
422 result = prime * result + qNameModule.hashCode();
427 public boolean equals(final Object obj) {
434 if (getClass() != obj.getClass()) {
437 SubmoduleEffectiveStatementImpl other = (SubmoduleEffectiveStatementImpl) obj;
438 if (!Objects.equals(name, other.name)) {
441 if (!qNameModule.equals(other.qNameModule)) {
444 if (!Objects.equals(yangVersion, other.yangVersion)) {
451 public String toString() {
453 StringBuilder sb = new StringBuilder(SubmoduleEffectiveStatementImpl.class.getSimpleName());
455 sb.append("name=").append(name);
456 sb.append(", namespace=").append(getNamespace());
457 sb.append(", revision=").append(getRevision());
458 sb.append(", prefix=").append(prefix);
459 sb.append(", yangVersion=").append(yangVersion);
461 return sb.toString();
465 public QNameModule getQNameModule() {