Bug 6771: Problem with typedefs nested in augment
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AbstractEffectiveModule.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
9
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import java.net.URI;
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;
21 import java.util.Map;
22 import java.util.Set;
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;
52
53 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
54         AbstractEffectiveDocumentedNode<String, D> implements Module, Immutable {
55
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;
77
78     AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
79         super(ctx);
80
81         this.name = argument();
82
83         PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
84         this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
85
86         YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
87         this.yangVersion = (yangVersionStmt == null) ? "1" : yangVersionStmt.argument();
88
89         OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
90         this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
91
92         ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
93         this.contact = (contactStmt == null) ? null : contactStmt.argument();
94
95         if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
96             this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
97         } else {
98             this.sourcePath = null;
99         }
100
101         // init submodules and substatements of submodules
102         final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
103         final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
104                 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
105
106         if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
107             this.submodules = ImmutableSet.of();
108             substatementsOfSubmodules = ImmutableList.of();
109         } else {
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
119                         .buildEffective();
120                 submodulesInit.add(submodule);
121                 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
122             }
123
124             this.submodules = ImmutableSet.copyOf(submodulesInit);
125             substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
126         }
127
128         // init substatements collections
129         List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
130         effectiveSubstatements.addAll(effectiveSubstatements());
131         effectiveSubstatements.addAll(substatementsOfSubmodules);
132
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<>();
142
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<>();
148
149         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
150             if (effectiveStatement instanceof UnknownSchemaNode) {
151                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
152             }
153             if (effectiveStatement instanceof AugmentationSchema) {
154                 augmentationsInit.add((AugmentationSchema) effectiveStatement);
155             }
156             if (effectiveStatement instanceof ModuleImport) {
157                 importsInit.add((ModuleImport) effectiveStatement);
158             }
159             if (effectiveStatement instanceof NotificationDefinition) {
160                 notificationsInit.add((NotificationDefinition) effectiveStatement);
161             }
162             if (effectiveStatement instanceof RpcDefinition) {
163                 rpcsInit.add((RpcDefinition) effectiveStatement);
164             }
165             if (effectiveStatement instanceof Deviation) {
166                 deviationsInit.add((Deviation) effectiveStatement);
167             }
168             if (effectiveStatement instanceof IdentitySchemaNode) {
169                 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
170             }
171             if (effectiveStatement instanceof FeatureDefinition) {
172                 featuresInit.add((FeatureDefinition) effectiveStatement);
173             }
174             if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
175                 ExtensionEffectiveStatementImpl extensionDefinition = (ExtensionEffectiveStatementImpl) effectiveStatement;
176                 extensionDefinition.initUnknownSchemaNodes();
177                 extensionNodesInit
178                         .add(extensionDefinition);
179             }
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);
185                 } else {
186                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
187                 }
188             }
189             if (effectiveStatement instanceof UsesNode) {
190                 UsesNode usesNode = (UsesNode) effectiveStatement;
191                 if (!mutableUses.contains(usesNode)) {
192                     mutableUses.add(usesNode);
193                 } else {
194                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
195                 }
196             }
197             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
198                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
199                 TypeDefinition<?> type = typeDef.getTypeDefinition();
200                 if (!mutableTypeDefinitions.contains(type)) {
201                     mutableTypeDefinitions.add(type);
202                 } else {
203                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
204                 }
205             }
206             if (effectiveStatement instanceof GroupingDefinition) {
207                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
208                 if (!mutableGroupings.contains(grp)) {
209                     mutableGroupings.add(grp);
210                 } else {
211                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
212                 }
213             }
214         }
215
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);
225
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);
231
232     }
233
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);
244                 }
245             } else {
246                 resolvedModuleImports.add(moduleImport);
247             }
248         }
249         return resolvedModuleImports;
250     }
251
252     @Override
253     public String getModuleSourcePath() {
254         return sourcePath;
255     }
256
257     @Override
258     public String getSource() {
259         return null;
260     }
261
262     @Override
263     public URI getNamespace() {
264         return getQNameModule().getNamespace();
265     }
266
267     @Override
268     public String getName() {
269         return name;
270     }
271
272     @Override
273     public Date getRevision() {
274         return getQNameModule().getRevision();
275     }
276
277     @Override
278     public String getPrefix() {
279         return prefix;
280     }
281
282     @Override
283     public String getYangVersion() {
284         return yangVersion;
285     }
286
287     @Override
288     public String getOrganization() {
289         return organization;
290     }
291
292     @Override
293     public String getContact() {
294         return contact;
295     }
296
297     @Override
298     public Set<ModuleImport> getImports() {
299         return imports;
300     }
301
302     @Override
303     public Set<Module> getSubmodules() {
304         return submodules;
305     }
306
307     @Override
308     public Set<FeatureDefinition> getFeatures() {
309         return features;
310     }
311
312     @Override
313     public Set<NotificationDefinition> getNotifications() {
314         return notifications;
315     }
316
317     @Override
318     public Set<AugmentationSchema> getAugmentations() {
319         return augmentations;
320     }
321
322     @Override
323     public Set<RpcDefinition> getRpcs() {
324         return rpcs;
325     }
326
327     @Override
328     public Set<Deviation> getDeviations() {
329         return deviations;
330     }
331
332     @Override
333     public List<ExtensionDefinition> getExtensionSchemaNodes() {
334         return extensionNodes;
335     }
336
337     @Override
338     public Set<IdentitySchemaNode> getIdentities() {
339         return identities;
340     }
341
342     @Override
343     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
344         return unknownNodes;
345     }
346
347     @Override
348     public final Set<TypeDefinition<?>> getTypeDefinitions() {
349         return typeDefinitions;
350     }
351
352     @Override
353     public final Set<DataSchemaNode> getChildNodes() {
354         return publicChildNodes;
355     }
356
357     @Override
358     public final Set<GroupingDefinition> getGroupings() {
359         return groupings;
360     }
361
362     @Override
363     public final DataSchemaNode getDataChildByName(final QName name) {
364         // Child nodes are keyed by their container name, so we can do a direct
365         // lookup
366         return childNodes.get(name);
367     }
368
369     @Override
370     public final DataSchemaNode getDataChildByName(final String name) {
371         for (DataSchemaNode node : childNodes.values()) {
372             if (node.getQName().getLocalName().equals(name)) {
373                 return node;
374             }
375         }
376         return null;
377     }
378
379     @Override
380     public Set<UsesNode> getUses() {
381         return uses;
382     }
383
384     @Override
385     public String toString() {
386         StringBuilder sb = new StringBuilder(this.getClass().getSimpleName());
387         sb.append("[");
388         sb.append("name=").append(name);
389         sb.append(", namespace=").append(getNamespace());
390         sb.append(", revision=").append(getRevision());
391         sb.append(", prefix=").append(prefix);
392         sb.append(", yangVersion=").append(yangVersion);
393         sb.append("]");
394         return sb.toString();
395     }
396
397 }