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.ArrayList;
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.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 ArrayList<>();
113 for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
114 @SuppressWarnings("unchecked")
115 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
116 (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)
117 ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
118 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
120 submodulesInit.add(submodule);
121 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
124 this.submodules = ImmutableSet.copyOf(submodulesInit);
125 substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
128 // init substatements collections
129 List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
130 effectiveSubstatements.addAll(effectiveSubstatements());
131 effectiveSubstatements.addAll(substatementsOfSubmodules);
133 List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
134 Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
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 ArrayList<>();
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 ExtensionEffectiveStatementImpl) {
175 ExtensionEffectiveStatementImpl extensionDefinition = (ExtensionEffectiveStatementImpl) effectiveStatement;
176 extensionDefinition.initUnknownSchemaNodes();
178 .add(extensionDefinition);
180 if (effectiveStatement instanceof DataSchemaNode) {
181 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
182 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
183 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
184 mutablePublicChildNodes.add(dataSchemaNode);
186 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
189 if (effectiveStatement instanceof UsesNode) {
190 UsesNode usesNode = (UsesNode) effectiveStatement;
191 if (!mutableUses.contains(usesNode)) {
192 mutableUses.add(usesNode);
194 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
197 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
198 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
199 TypeDefinition<?> type = typeDef.getTypeDefinition();
200 if (!mutableTypeDefinitions.contains(type)) {
201 mutableTypeDefinitions.add(type);
203 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
206 if (effectiveStatement instanceof GroupingDefinition) {
207 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
208 if (!mutableGroupings.contains(grp)) {
209 mutableGroupings.add(grp);
211 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
216 this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
217 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
218 this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
219 this.notifications = ImmutableSet.copyOf(notificationsInit);
220 this.rpcs = ImmutableSet.copyOf(rpcsInit);
221 this.deviations = ImmutableSet.copyOf(deviationsInit);
222 this.identities = ImmutableSet.copyOf(identitiesInit);
223 this.features = ImmutableSet.copyOf(featuresInit);
224 this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
226 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
227 this.groupings = ImmutableSet.copyOf(mutableGroupings);
228 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
229 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
230 this.uses = ImmutableSet.copyOf(mutableUses);
234 private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
235 final StmtContext<String, ? extends DeclaredStatement<String>, ? extends EffectiveStatement<String, ?>> ctx) {
236 Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
237 for (ModuleImport moduleImport : importsInit) {
238 if (moduleImport.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
239 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, moduleImport.getPrefix());
240 if (!impModuleQName.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
241 ModuleImport resolvedModuleImport = new ModuleImportImpl(moduleImport.getModuleName(),
242 impModuleQName.getRevision(), moduleImport.getPrefix());
243 resolvedModuleImports.add(resolvedModuleImport);
246 resolvedModuleImports.add(moduleImport);
249 return resolvedModuleImports;
253 public String getModuleSourcePath() {
258 public String getSource() {
263 public URI getNamespace() {
264 return getQNameModule().getNamespace();
268 public String getName() {
273 public Date getRevision() {
274 return getQNameModule().getRevision();
278 public String getPrefix() {
283 public String getYangVersion() {
288 public String getOrganization() {
293 public String getContact() {
298 public Set<ModuleImport> getImports() {
303 public Set<Module> getSubmodules() {
308 public Set<FeatureDefinition> getFeatures() {
313 public Set<NotificationDefinition> getNotifications() {
314 return notifications;
318 public Set<AugmentationSchema> getAugmentations() {
319 return augmentations;
323 public Set<RpcDefinition> getRpcs() {
328 public Set<Deviation> getDeviations() {
333 public List<ExtensionDefinition> getExtensionSchemaNodes() {
334 return extensionNodes;
338 public Set<IdentitySchemaNode> getIdentities() {
343 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
348 public final Set<TypeDefinition<?>> getTypeDefinitions() {
349 return typeDefinitions;
353 public final Set<DataSchemaNode> getChildNodes() {
354 return publicChildNodes;
358 public final Set<GroupingDefinition> getGroupings() {
363 public final DataSchemaNode getDataChildByName(final QName name) {
364 // Child nodes are keyed by their container name, so we can do a direct
366 return childNodes.get(name);
370 public final DataSchemaNode getDataChildByName(final String name) {
371 for (DataSchemaNode node : childNodes.values()) {
372 if (node.getQName().getLocalName().equals(name)) {
380 public Set<UsesNode> getUses() {
385 public String toString() {
386 return this.getClass().getSimpleName() + "[" +
388 ", namespace=" + getNamespace() +
389 ", revision=" + getRevision() +
390 ", prefix=" + prefix +
391 ", yangVersion=" + yangVersion +