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.model.util.ExtendedType;
12 import java.util.LinkedHashSet;
13 import java.util.LinkedHashMap;
14 import com.google.common.collect.ImmutableMap;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UsesNode;
20 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
22 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
23 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
25 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
27 import java.util.Collection;
28 import java.util.Date;
29 import java.util.HashSet;
30 import java.util.LinkedList;
31 import java.util.List;
33 import org.opendaylight.yangtools.concepts.Immutable;
34 import org.opendaylight.yangtools.yang.common.QNameModule;
35 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
36 import org.opendaylight.yangtools.yang.model.api.Deviation;
37 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
38 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
39 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
42 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
43 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
44 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
49 import com.google.common.collect.ImmutableList;
50 import com.google.common.collect.ImmutableSet;
51 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
52 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
54 public class SubmoduleEffectiveStatementImpl
56 AbstractEffectiveDocumentedNode<String, SubmoduleStatement>
57 implements Module, Immutable {
59 private final QNameModule qNameModule;
60 private final String name;
61 private 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 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 SubmoduleEffectiveStatementImpl(
86 StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
91 String belongsToModuleName = firstAttributeOf(
92 ctx.declaredSubstatements(), BelongsToStatement.class);
93 final QNameModule belongsToModuleQName = ctx.getFromNamespace(
94 ModuleNameToModuleQName.class, belongsToModuleName);
95 RevisionEffectiveStatementImpl submoduleRevision = firstEffective(RevisionEffectiveStatementImpl.class);
97 qNameModule = submoduleRevision == null ? QNameModule.create(
98 belongsToModuleQName.getNamespace(),
99 SimpleDateFormatUtil.DEFAULT_DATE_REV) : QNameModule.create(
100 belongsToModuleQName.getNamespace(),
101 submoduleRevision.argument());
103 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
104 if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
105 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement)
108 if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
109 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement)
112 if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
113 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement)
116 if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
117 contact = ((ContactEffectiveStatementImpl) effectiveStatement)
122 //:TODO init source, sourcePath
123 source = ctx.getStatementSource().name();
126 initSubstatementCollections(ctx);
129 private void initSubmodules(
130 StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
131 Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
132 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
134 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
135 this.submodules = ImmutableSet.of();
136 this.substatementsOfSubmodules = ImmutableList.of();
140 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap
143 Set<Module> submodulesInit = new HashSet<>();
144 List<EffectiveStatement<?,?>> substatementsOfSubmodulesInit = new LinkedList<>();
145 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
146 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
147 .getFromNamespace(SubmoduleNamespace.class,
148 submoduleIdentifier);
149 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx.buildEffective();
150 submodulesInit.add(submodule);
151 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
154 this.submodules = ImmutableSet.copyOf(submodulesInit);
155 this.substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
158 private void initSubstatementCollections(StmtContext<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> ctx) {
159 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
161 effectiveSubstatements.addAll(effectiveSubstatements());
162 effectiveSubstatements.addAll(this.substatementsOfSubmodules);
164 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
165 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
166 Set<ModuleImport> importsInit = new HashSet<>();
167 Set<NotificationDefinition> notificationsInit = new HashSet<>();
168 Set<RpcDefinition> rpcsInit = new HashSet<>();
169 Set<Deviation> deviationsInit = new HashSet<>();
170 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
171 Set<FeatureDefinition> featuresInit = new HashSet<>();
172 List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
174 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
175 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
176 Set<UsesNode> mutableUses = new HashSet<>();
177 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
178 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
180 //:TODO add validation also for other node types
181 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
182 if (effectiveStatement instanceof UnknownSchemaNode) {
183 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
185 if (effectiveStatement instanceof AugmentationSchema) {
186 augmentationsInit.add((AugmentationSchema) effectiveStatement);
188 if (effectiveStatement instanceof ModuleImport) {
189 importsInit.add((ModuleImport) effectiveStatement);
191 if (effectiveStatement instanceof NotificationDefinition) {
193 .add((NotificationDefinition) effectiveStatement);
195 if (effectiveStatement instanceof RpcDefinition) {
196 rpcsInit.add((RpcDefinition) effectiveStatement);
198 if (effectiveStatement instanceof Deviation) {
199 deviationsInit.add((Deviation) effectiveStatement);
201 if (effectiveStatement instanceof IdentitySchemaNode) {
202 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
204 if (effectiveStatement instanceof FeatureDefinition) {
205 featuresInit.add((FeatureDefinition) effectiveStatement);
207 if (effectiveStatement instanceof ExtensionDefinition) {
209 .add((ExtensionDefinition) effectiveStatement);
211 if (effectiveStatement instanceof DataSchemaNode) {
212 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
213 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
214 mutableChildNodes.put(dataSchemaNode.getQName(),
216 mutablePublicChildNodes.add(dataSchemaNode);
218 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
221 if (effectiveStatement instanceof UsesNode) {
222 UsesNode usesNode = (UsesNode) effectiveStatement;
223 if (!mutableUses.contains(usesNode)) {
224 mutableUses.add(usesNode);
226 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
229 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
230 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
231 ExtendedType extendedType = typeDef.buildType();
232 if (!mutableTypeDefinitions.contains(extendedType)) {
233 mutableTypeDefinitions.add(extendedType);
235 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
238 if (effectiveStatement instanceof GroupingDefinition) {
239 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
240 if (!mutableGroupings.contains(grp)) {
241 mutableGroupings.add(grp);
243 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
248 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
249 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
250 this.imports = ImmutableSet.copyOf(importsInit);
251 this.notifications = ImmutableSet.copyOf(notificationsInit);
252 this.rpcs = ImmutableSet.copyOf(rpcsInit);
253 this.deviations = ImmutableSet.copyOf(deviationsInit);
254 this.identities = ImmutableSet.copyOf(identitiesInit);
255 this.features = ImmutableSet.copyOf(featuresInit);
256 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
258 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
259 this.groupings = ImmutableSet.copyOf(mutableGroupings);
260 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
261 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
262 this.uses = ImmutableSet.copyOf(mutableUses);
266 public String getModuleSourcePath() {
271 public URI getNamespace() {
272 return qNameModule.getNamespace();
276 public String getName() {
281 public Date getRevision() {
282 return qNameModule.getRevision();
286 public String getPrefix() {
291 public String getYangVersion() {
296 public String getOrganization() {
301 public String getContact() {
306 public Set<ModuleImport> getImports() {
311 public Set<Module> getSubmodules() {
316 public Set<FeatureDefinition> getFeatures() {
321 public Set<NotificationDefinition> getNotifications() {
322 return notifications;
326 public Set<AugmentationSchema> getAugmentations() {
327 return augmentations;
331 public Set<RpcDefinition> getRpcs() {
336 public Set<Deviation> getDeviations() {
341 public List<ExtensionDefinition> getExtensionSchemaNodes() {
342 return extensionNodes;
346 public Set<IdentitySchemaNode> getIdentities() {
351 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
356 public final Set<TypeDefinition<?>> getTypeDefinitions() {
357 return typeDefinitions;
361 public final Set<DataSchemaNode> getChildNodes() {
362 return publicChildNodes;
366 public final Set<GroupingDefinition> getGroupings() {
371 public final DataSchemaNode getDataChildByName(final QName name) {
372 // Child nodes are keyed by their container name, so we can do a direct
374 return childNodes.get(name);
378 public final DataSchemaNode getDataChildByName(final String name) {
379 for (DataSchemaNode node : childNodes.values()) {
380 if (node.getQName().getLocalName().equals(name)) {
388 public Set<UsesNode> getUses() {
393 public String getSource() {
398 public int hashCode() {
399 final int prime = 31;
401 result = prime * result + ((name == null) ? 0 : name.hashCode());
402 result = prime * result
403 + ((yangVersion == null) ? 0 : yangVersion.hashCode());
404 result = prime * result + qNameModule.hashCode();
409 public boolean equals(final Object obj) {
416 if (getClass() != obj.getClass()) {
419 SubmoduleEffectiveStatementImpl other = (SubmoduleEffectiveStatementImpl) obj;
421 if (other.name != null) {
424 } else if (!name.equals(other.name)) {
427 if (!qNameModule.equals(other.qNameModule)) {
430 if (yangVersion == null) {
431 if (other.yangVersion != null) {
434 } else if (!yangVersion.equals(other.yangVersion)) {
441 public String toString() {
443 StringBuilder sb = new StringBuilder(SubmoduleEffectiveStatementImpl.class.getSimpleName());
445 sb.append("name=").append(name);
446 sb.append(", namespace=").append(getNamespace());
447 sb.append(", revision=").append(getRevision());
448 sb.append(", prefix=").append(prefix);
449 sb.append(", yangVersion=").append(yangVersion);
451 return sb.toString();
455 public QNameModule getQNameModule() {