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.spi.source.ModuleCtxToModuleQName;
12 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
16 import java.util.LinkedHashSet;
17 import java.util.LinkedHashMap;
18 import com.google.common.collect.ImmutableMap;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.UsesNode;
24 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
27 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
28 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
30 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
32 import java.util.Collection;
33 import java.util.Date;
34 import java.util.HashSet;
35 import java.util.LinkedList;
36 import java.util.List;
38 import org.opendaylight.yangtools.concepts.Immutable;
39 import org.opendaylight.yangtools.yang.common.QNameModule;
40 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
41 import org.opendaylight.yangtools.yang.model.api.Deviation;
42 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
43 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
44 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.Module;
46 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
47 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
48 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
49 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
53 public class ModuleEffectiveStatementImpl extends
54 AbstractEffectiveDocumentedNode<String, ModuleStatement>
55 implements Module, Immutable {
57 private final QNameModule qNameModule;
58 private final String name;
59 private String sourcePath; // TODO fill
60 private String prefix;
61 private String yangVersion;
62 private String organization;
63 private String contact;
64 private ImmutableSet<ModuleImport> imports;
65 private ImmutableSet<Module> submodules;
66 private ImmutableSet<FeatureDefinition> features;
67 private ImmutableSet<NotificationDefinition> notifications;
68 private ImmutableSet<AugmentationSchema> augmentations;
69 private ImmutableSet<RpcDefinition> rpcs;
70 private ImmutableSet<Deviation> deviations;
71 private ImmutableList<ExtensionDefinition> extensionNodes;
72 private ImmutableSet<IdentitySchemaNode> identities;
73 private ImmutableList<UnknownSchemaNode> unknownNodes;
74 private String source;
75 private ImmutableList<EffectiveStatement<?,?>> substatementsOfSubmodules;
77 private ImmutableMap<QName, DataSchemaNode> childNodes;
78 private ImmutableSet<GroupingDefinition> groupings;
79 private ImmutableSet<UsesNode> uses;
80 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
81 private ImmutableSet<DataSchemaNode> publicChildNodes;
83 public ModuleEffectiveStatementImpl(
84 StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
88 QNameModule qNameModuleInit = ctx.getFromNamespace(
89 ModuleCtxToModuleQName.class, ctx);
90 qNameModule = qNameModuleInit.getRevision() == null ? QNameModule
91 .create(qNameModuleInit.getNamespace(),
92 SimpleDateFormatUtil.DEFAULT_DATE_REV)
95 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
96 if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
97 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement)
100 if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
101 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement)
104 if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
105 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement)
108 if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
109 contact = ((ContactEffectiveStatementImpl) effectiveStatement)
114 // TODO init source, sourcePath
115 source = ctx.getStatementSource().name();
118 initSubstatementCollections(ctx);
121 private void initSubmodules(
122 StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
123 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
124 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
126 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
127 this.submodules = ImmutableSet.of();
128 this.substatementsOfSubmodules = ImmutableList.of();
132 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap
135 Set<Module> submodulesInit = new HashSet<>();
136 List<EffectiveStatement<?,?>> substatementsOfSubmodulesInit = new LinkedList<>();
137 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
138 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
139 .getFromNamespace(SubmoduleNamespace.class,
140 submoduleIdentifier);
141 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx.buildEffective();
142 submodulesInit.add(submodule);
143 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
146 this.submodules = ImmutableSet.copyOf(submodulesInit);
147 this.substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
150 private void initSubstatementCollections(StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
151 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
153 effectiveSubstatements.addAll(effectiveSubstatements());
154 effectiveSubstatements.addAll(substatementsOfSubmodules);
156 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
157 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
158 Set<ModuleImport> importsInit = new HashSet<>();
159 Set<NotificationDefinition> notificationsInit = new HashSet<>();
160 Set<RpcDefinition> rpcsInit = new HashSet<>();
161 Set<Deviation> deviationsInit = new HashSet<>();
162 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
163 Set<FeatureDefinition> featuresInit = new HashSet<>();
164 List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
166 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
167 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
168 Set<UsesNode> mutableUses = new HashSet<>();
169 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
170 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
172 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
173 if (effectiveStatement instanceof UnknownSchemaNode) {
174 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
176 if (effectiveStatement instanceof AugmentationSchema) {
177 augmentationsInit.add((AugmentationSchema) effectiveStatement);
179 if (effectiveStatement instanceof ModuleImport) {
180 importsInit.add((ModuleImport) effectiveStatement);
182 if (effectiveStatement instanceof NotificationDefinition) {
184 .add((NotificationDefinition) effectiveStatement);
186 if (effectiveStatement instanceof RpcDefinition) {
187 rpcsInit.add((RpcDefinition) effectiveStatement);
189 if (effectiveStatement instanceof Deviation) {
190 deviationsInit.add((Deviation) effectiveStatement);
192 if (effectiveStatement instanceof IdentitySchemaNode) {
193 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
195 if (effectiveStatement instanceof FeatureDefinition) {
196 featuresInit.add((FeatureDefinition) effectiveStatement);
198 if (effectiveStatement instanceof ExtensionDefinition) {
200 .add((ExtensionDefinition) effectiveStatement);
202 if (effectiveStatement instanceof DataSchemaNode) {
203 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
204 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
205 mutableChildNodes.put(dataSchemaNode.getQName(),
207 mutablePublicChildNodes.add(dataSchemaNode);
209 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
212 if (effectiveStatement instanceof UsesNode) {
213 UsesNode usesNode = (UsesNode) effectiveStatement;
214 if (!mutableUses.contains(usesNode)) {
215 mutableUses.add(usesNode);
217 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
220 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
221 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
222 ExtendedType extendedType = typeDef.buildType();
223 if (!mutableTypeDefinitions.contains(extendedType)) {
224 mutableTypeDefinitions.add(extendedType);
226 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
229 if (effectiveStatement instanceof GroupingDefinition) {
230 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
231 if (!mutableGroupings.contains(grp)) {
232 mutableGroupings.add(grp);
234 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
239 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
240 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
241 this.imports = ImmutableSet.copyOf(importsInit);
242 this.notifications = ImmutableSet.copyOf(notificationsInit);
243 this.rpcs = ImmutableSet.copyOf(rpcsInit);
244 this.deviations = ImmutableSet.copyOf(deviationsInit);
245 this.identities = ImmutableSet.copyOf(identitiesInit);
246 this.features = ImmutableSet.copyOf(featuresInit);
247 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
249 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
250 this.groupings = ImmutableSet.copyOf(mutableGroupings);
251 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
252 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
253 this.uses = ImmutableSet.copyOf(mutableUses);
257 public String getModuleSourcePath() {
262 public URI getNamespace() {
263 return qNameModule.getNamespace();
267 public String getName() {
272 public Date getRevision() {
273 return qNameModule.getRevision();
277 public String getPrefix() {
282 public String getYangVersion() {
287 public String getOrganization() {
292 public String getContact() {
297 public Set<ModuleImport> getImports() {
302 public Set<Module> getSubmodules() {
307 public Set<FeatureDefinition> getFeatures() {
312 public Set<NotificationDefinition> getNotifications() {
313 return notifications;
317 public Set<AugmentationSchema> getAugmentations() {
318 return augmentations;
322 public Set<RpcDefinition> getRpcs() {
327 public Set<Deviation> getDeviations() {
332 public List<ExtensionDefinition> getExtensionSchemaNodes() {
333 return extensionNodes;
337 public Set<IdentitySchemaNode> getIdentities() {
342 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
347 public final Set<TypeDefinition<?>> getTypeDefinitions() {
348 return typeDefinitions;
352 public final Set<DataSchemaNode> getChildNodes() {
353 return publicChildNodes;
357 public final Set<GroupingDefinition> getGroupings() {
362 public final DataSchemaNode getDataChildByName(final QName name) {
363 // Child nodes are keyed by their container name, so we can do a direct
365 return childNodes.get(name);
369 public final DataSchemaNode getDataChildByName(final String name) {
370 for (DataSchemaNode node : childNodes.values()) {
371 if (node.getQName().getLocalName().equals(name)) {
379 public Set<UsesNode> getUses() {
384 public String getSource() {
389 public int hashCode() {
390 final int prime = 31;
392 result = prime * result + ((name == null) ? 0 : name.hashCode());
393 result = prime * result
394 + ((yangVersion == null) ? 0 : yangVersion.hashCode());
395 result = prime * result + qNameModule.hashCode();
400 public boolean equals(final Object obj) {
407 if (getClass() != obj.getClass()) {
410 ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
412 if (other.name != null) {
415 } else if (!name.equals(other.name)) {
418 if (!qNameModule.equals(other.qNameModule)) {
421 if (yangVersion == null) {
422 if (other.yangVersion != null) {
425 } else if (!yangVersion.equals(other.yangVersion)) {
432 public String toString() {
433 StringBuilder sb = new StringBuilder(ModuleEffectiveStatementImpl.class.getSimpleName());
435 sb.append("name=").append(name);
436 sb.append(", namespace=").append(getNamespace());
437 sb.append(", revision=").append(getRevision());
438 sb.append(", prefix=").append(prefix);
439 sb.append(", yangVersion=").append(yangVersion);
441 return sb.toString();
445 public QNameModule getQNameModule() {