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 com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.Date;
16 import java.util.HashSet;
17 import java.util.LinkedHashMap;
18 import java.util.LinkedHashSet;
19 import java.util.LinkedList;
20 import java.util.List;
23 import org.opendaylight.yangtools.concepts.Immutable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
26 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Deviation;
30 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
31 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
32 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
33 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
37 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
38 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.UsesNode;
42 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
43 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
45 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
46 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
49 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
50 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
53 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
54 AbstractEffectiveDocumentedNode<String, D> implements Module, Immutable {
56 private final String name;
57 private final String sourcePath;
58 private final String prefix;
59 private final String yangVersion;
60 private final String organization;
61 private final String contact;
62 private final Set<ModuleImport> imports;
63 private final Set<Module> submodules;
64 private final Set<FeatureDefinition> features;
65 private final Set<NotificationDefinition> notifications;
66 private final Set<AugmentationSchema> augmentations;
67 private final Set<RpcDefinition> rpcs;
68 private final Set<Deviation> deviations;
69 private final List<ExtensionDefinition> extensionNodes;
70 private final Set<IdentitySchemaNode> identities;
71 private final List<UnknownSchemaNode> unknownNodes;
72 private final Map<QName, DataSchemaNode> childNodes;
73 private final Set<GroupingDefinition> groupings;
74 private final Set<UsesNode> uses;
75 private final Set<TypeDefinition<?>> typeDefinitions;
76 private final Set<DataSchemaNode> publicChildNodes;
78 AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
81 this.name = argument();
83 PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
84 this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
86 YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
87 this.yangVersion = (yangVersionStmt == null) ? "1" : yangVersionStmt.argument();
89 OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
90 this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
92 ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
93 this.contact = (contactStmt == null) ? null : contactStmt.argument();
95 if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
96 this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
98 this.sourcePath = null;
101 // init submodules and substatements of submodules
102 final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
103 final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
104 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
106 if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
107 this.submodules = ImmutableSet.of();
108 substatementsOfSubmodules = ImmutableList.of();
110 Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
111 Set<Module> submodulesInit = new HashSet<>();
112 List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
113 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
114 @SuppressWarnings("unchecked")
115 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
116 .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
117 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
119 submodulesInit.add(submodule);
120 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
123 this.submodules = ImmutableSet.copyOf(submodulesInit);
124 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
127 // init substatements collections
128 List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
130 effectiveSubstatements.addAll(effectiveSubstatements());
131 effectiveSubstatements.addAll(substatementsOfSubmodules);
133 List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
134 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
135 Set<ModuleImport> importsInit = new HashSet<>();
136 Set<NotificationDefinition> notificationsInit = new HashSet<>();
137 Set<RpcDefinition> rpcsInit = new HashSet<>();
138 Set<Deviation> deviationsInit = new HashSet<>();
139 Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
140 Set<FeatureDefinition> featuresInit = new HashSet<>();
141 List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
143 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
144 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
145 Set<UsesNode> mutableUses = new HashSet<>();
146 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
147 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
149 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
150 if (effectiveStatement instanceof UnknownSchemaNode) {
151 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
153 if (effectiveStatement instanceof AugmentationSchema) {
154 augmentationsInit.add((AugmentationSchema) effectiveStatement);
156 if (effectiveStatement instanceof ModuleImport) {
157 importsInit.add((ModuleImport) effectiveStatement);
159 if (effectiveStatement instanceof NotificationDefinition) {
160 notificationsInit.add((NotificationDefinition) effectiveStatement);
162 if (effectiveStatement instanceof RpcDefinition) {
163 rpcsInit.add((RpcDefinition) effectiveStatement);
165 if (effectiveStatement instanceof Deviation) {
166 deviationsInit.add((Deviation) effectiveStatement);
168 if (effectiveStatement instanceof IdentitySchemaNode) {
169 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
171 if (effectiveStatement instanceof FeatureDefinition) {
172 featuresInit.add((FeatureDefinition) effectiveStatement);
174 if (effectiveStatement instanceof ExtensionDefinition) {
175 extensionNodesInit.add((ExtensionDefinition) effectiveStatement);
177 if (effectiveStatement instanceof DataSchemaNode) {
178 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
179 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
180 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
181 mutablePublicChildNodes.add(dataSchemaNode);
183 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
186 if (effectiveStatement instanceof UsesNode) {
187 UsesNode usesNode = (UsesNode) effectiveStatement;
188 if (!mutableUses.contains(usesNode)) {
189 mutableUses.add(usesNode);
191 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
194 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
195 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
196 TypeDefinition<?> type = typeDef.getTypeDefinition();
197 if (!mutableTypeDefinitions.contains(type)) {
198 mutableTypeDefinitions.add(type);
200 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
203 if (effectiveStatement instanceof GroupingDefinition) {
204 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
205 if (!mutableGroupings.contains(grp)) {
206 mutableGroupings.add(grp);
208 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
213 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
214 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
215 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
216 this.notifications = ImmutableSet.copyOf(notificationsInit);
217 this.rpcs = ImmutableSet.copyOf(rpcsInit);
218 this.deviations = ImmutableSet.copyOf(deviationsInit);
219 this.identities = ImmutableSet.copyOf(identitiesInit);
220 this.features = ImmutableSet.copyOf(featuresInit);
221 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
223 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
224 this.groupings = ImmutableSet.copyOf(mutableGroupings);
225 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
226 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
227 this.uses = ImmutableSet.copyOf(mutableUses);
231 private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
232 final StmtContext<String, ? extends DeclaredStatement<String>, ? extends EffectiveStatement<String, ?>> ctx) {
233 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
234 for (ModuleImport moduleImport : importsInit) {
235 if (moduleImport.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
236 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, moduleImport.getPrefix());
237 if (!impModuleQName.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
238 ModuleImport resolvedModuleImport = new ModuleImportImpl(moduleImport.getModuleName(),
239 impModuleQName.getRevision(), moduleImport.getPrefix());
240 resolvedModuleImports.add(resolvedModuleImport);
243 resolvedModuleImports.add(moduleImport);
246 return resolvedModuleImports;
250 public String getModuleSourcePath() {
255 public String getSource() {
260 public URI getNamespace() {
261 return getQNameModule().getNamespace();
265 public String getName() {
270 public Date getRevision() {
271 return getQNameModule().getRevision();
275 public String getPrefix() {
280 public String getYangVersion() {
285 public String getOrganization() {
290 public String getContact() {
295 public Set<ModuleImport> getImports() {
300 public Set<Module> getSubmodules() {
305 public Set<FeatureDefinition> getFeatures() {
310 public Set<NotificationDefinition> getNotifications() {
311 return notifications;
315 public Set<AugmentationSchema> getAugmentations() {
316 return augmentations;
320 public Set<RpcDefinition> getRpcs() {
325 public Set<Deviation> getDeviations() {
330 public List<ExtensionDefinition> getExtensionSchemaNodes() {
331 return extensionNodes;
335 public Set<IdentitySchemaNode> getIdentities() {
340 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
345 public final Set<TypeDefinition<?>> getTypeDefinitions() {
346 return typeDefinitions;
350 public final Set<DataSchemaNode> getChildNodes() {
351 return publicChildNodes;
355 public final Set<GroupingDefinition> getGroupings() {
360 public final DataSchemaNode getDataChildByName(final QName name) {
361 // Child nodes are keyed by their container name, so we can do a direct
363 return childNodes.get(name);
367 public final DataSchemaNode getDataChildByName(final String name) {
368 for (DataSchemaNode node : childNodes.values()) {
369 if (node.getQName().getLocalName().equals(name)) {
377 public Set<UsesNode> getUses() {
382 public String toString() {
383 StringBuilder sb = new StringBuilder(this.getClass().getSimpleName());
385 sb.append("name=").append(name);
386 sb.append(", namespace=").append(getNamespace());
387 sb.append(", revision=").append(getRevision());
388 sb.append(", prefix=").append(prefix);
389 sb.append(", yangVersion=").append(yangVersion);
391 return sb.toString();