Bug 6867: Extend yang statement parser to support different yang versions
[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 javax.annotation.Nonnull;
24 import org.opendaylight.yangtools.concepts.Immutable;
25 import org.opendaylight.yangtools.concepts.SemVer;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
29 import org.opendaylight.yangtools.yang.common.YangVersion;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.Deviation;
33 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
34 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
35 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
36 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
39 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
40 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
41 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
42 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
43 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.UsesNode;
45 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
46 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
48 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
51 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
52 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
53 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
54
55 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
56         AbstractEffectiveDocumentedNode<String, D> implements Module, Immutable {
57
58     private final String name;
59     private final String sourcePath;
60     private final String prefix;
61     private final YangVersion yangVersion;
62     private final String organization;
63     private final String contact;
64     private final Set<ModuleImport> imports;
65     private final Set<Module> submodules;
66     private final Set<FeatureDefinition> features;
67     private final Set<NotificationDefinition> notifications;
68     private final Set<AugmentationSchema> augmentations;
69     private final Set<RpcDefinition> rpcs;
70     private final Set<Deviation> deviations;
71     private final List<ExtensionDefinition> extensionNodes;
72     private final Set<IdentitySchemaNode> identities;
73     private final List<UnknownSchemaNode> unknownNodes;
74     private final Map<QName, DataSchemaNode> childNodes;
75     private final Set<GroupingDefinition> groupings;
76     private final Set<UsesNode> uses;
77     private final Set<TypeDefinition<?>> typeDefinitions;
78     private final Set<DataSchemaNode> publicChildNodes;
79     private final SemVer semanticVersion;
80
81     AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
82         super(ctx);
83
84         this.name = argument();
85
86         PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
87         this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
88
89         YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
90         this.yangVersion = (yangVersionStmt == null) ? YangVersion.VERSION_1 : yangVersionStmt.argument();
91
92         SemanticVersionEffectiveStatementImpl semanticVersionStmt = firstEffective(SemanticVersionEffectiveStatementImpl.class);
93         this.semanticVersion = (semanticVersionStmt == null) ? DEFAULT_SEMANTIC_VERSION : semanticVersionStmt.argument();
94
95         OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
96         this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
97
98         ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
99         this.contact = (contactStmt == null) ? null : contactStmt.argument();
100
101         if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
102             this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
103         } else {
104             this.sourcePath = null;
105         }
106
107         // init submodules and substatements of submodules
108         final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
109         final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
110                 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
111
112         if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
113             this.submodules = ImmutableSet.of();
114             substatementsOfSubmodules = ImmutableList.of();
115         } else {
116             Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
117             Set<Module> submodulesInit = new HashSet<>();
118             List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
119             for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
120                 @SuppressWarnings("unchecked")
121                 Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
122                     (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)
123                         ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
124                 SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
125                         .buildEffective();
126                 submodulesInit.add(submodule);
127                 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
128             }
129
130             this.submodules = ImmutableSet.copyOf(submodulesInit);
131             substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
132         }
133
134         // init substatements collections
135         List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
136         effectiveSubstatements.addAll(effectiveSubstatements());
137         effectiveSubstatements.addAll(substatementsOfSubmodules);
138
139         List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
140         Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
141         Set<ModuleImport> importsInit = new HashSet<>();
142         Set<NotificationDefinition> notificationsInit = new HashSet<>();
143         Set<RpcDefinition> rpcsInit = new HashSet<>();
144         Set<Deviation> deviationsInit = new HashSet<>();
145         Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
146         Set<FeatureDefinition> featuresInit = new HashSet<>();
147         List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
148
149         Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
150         Set<GroupingDefinition> mutableGroupings = new HashSet<>();
151         Set<UsesNode> mutableUses = new HashSet<>();
152         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
153         Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
154
155         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
156             if (effectiveStatement instanceof UnknownSchemaNode) {
157                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
158             }
159             if (effectiveStatement instanceof AugmentationSchema) {
160                 augmentationsInit.add((AugmentationSchema) effectiveStatement);
161             }
162             if (effectiveStatement instanceof ModuleImport) {
163                 importsInit.add((ModuleImport) effectiveStatement);
164             }
165             if (effectiveStatement instanceof NotificationDefinition) {
166                 notificationsInit.add((NotificationDefinition) effectiveStatement);
167             }
168             if (effectiveStatement instanceof RpcDefinition) {
169                 rpcsInit.add((RpcDefinition) effectiveStatement);
170             }
171             if (effectiveStatement instanceof Deviation) {
172                 deviationsInit.add((Deviation) effectiveStatement);
173             }
174             if (effectiveStatement instanceof IdentitySchemaNode) {
175                 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
176             }
177             if (effectiveStatement instanceof FeatureDefinition) {
178                 featuresInit.add((FeatureDefinition) effectiveStatement);
179             }
180             if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
181                 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
182             }
183             if (effectiveStatement instanceof DataSchemaNode) {
184                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
185                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
186                     mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
187                     mutablePublicChildNodes.add(dataSchemaNode);
188                 } else {
189                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
190                 }
191             }
192             if (effectiveStatement instanceof UsesNode) {
193                 UsesNode usesNode = (UsesNode) effectiveStatement;
194                 if (!mutableUses.contains(usesNode)) {
195                     mutableUses.add(usesNode);
196                 } else {
197                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
198                 }
199             }
200             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
201                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
202                 TypeDefinition<?> type = typeDef.getTypeDefinition();
203                 if (!mutableTypeDefinitions.contains(type)) {
204                     mutableTypeDefinitions.add(type);
205                 } else {
206                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
207                 }
208             }
209             if (effectiveStatement instanceof GroupingDefinition) {
210                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
211                 if (!mutableGroupings.contains(grp)) {
212                     mutableGroupings.add(grp);
213                 } else {
214                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
215                 }
216             }
217         }
218
219         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
220         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
221         if (ctx.isEnabledSemanticVersioning()) {
222             this.imports = ImmutableSet.copyOf(importsInit);
223         } else {
224             this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
225         }
226         this.notifications = ImmutableSet.copyOf(notificationsInit);
227         this.rpcs = ImmutableSet.copyOf(rpcsInit);
228         this.deviations = ImmutableSet.copyOf(deviationsInit);
229         this.identities = ImmutableSet.copyOf(identitiesInit);
230         this.features = ImmutableSet.copyOf(featuresInit);
231         this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
232
233         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
234         this.groupings = ImmutableSet.copyOf(mutableGroupings);
235         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
236         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
237         this.uses = ImmutableSet.copyOf(mutableUses);
238
239     }
240
241     private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
242             final StmtContext<String, ? extends DeclaredStatement<String>, ? extends EffectiveStatement<String, ?>> ctx) {
243         final Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
244         for (ModuleImport moduleImport : importsInit) {
245             if (moduleImport.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
246                 final QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, moduleImport.getPrefix());
247                 final ModuleImport resolvedModuleImport = new ModuleImportImpl(moduleImport.getModuleName(),
248                         impModuleQName.getRevision(), moduleImport.getPrefix());
249                 resolvedModuleImports.add(resolvedModuleImport);
250             } else {
251                 resolvedModuleImports.add(moduleImport);
252             }
253         }
254         return resolvedModuleImports;
255     }
256
257     @Override
258     public String getModuleSourcePath() {
259         return sourcePath;
260     }
261
262     @Override
263     public String getSource() {
264         return null;
265     }
266
267     @Override
268     public URI getNamespace() {
269         return getQNameModule().getNamespace();
270     }
271
272     @Override
273     public String getName() {
274         return name;
275     }
276
277     @Override
278     public Date getRevision() {
279         return getQNameModule().getRevision();
280     }
281
282     @Override
283     public String getPrefix() {
284         return prefix;
285     }
286
287     @Override
288     public String getYangVersion() {
289         return yangVersion.toCanonicalString();
290     }
291
292     @Override
293     public String getOrganization() {
294         return organization;
295     }
296
297     @Override
298     public String getContact() {
299         return contact;
300     }
301
302     @Override
303     public Set<ModuleImport> getImports() {
304         return imports;
305     }
306
307     @Override
308     public Set<Module> getSubmodules() {
309         return submodules;
310     }
311
312     @Override
313     public Set<FeatureDefinition> getFeatures() {
314         return features;
315     }
316
317     @Override
318     public Set<NotificationDefinition> getNotifications() {
319         return notifications;
320     }
321
322     @Override
323     public Set<AugmentationSchema> getAugmentations() {
324         return augmentations;
325     }
326
327     @Override
328     public Set<RpcDefinition> getRpcs() {
329         return rpcs;
330     }
331
332     @Override
333     public Set<Deviation> getDeviations() {
334         return deviations;
335     }
336
337     @Override
338     public List<ExtensionDefinition> getExtensionSchemaNodes() {
339         return extensionNodes;
340     }
341
342     @Override
343     public Set<IdentitySchemaNode> getIdentities() {
344         return identities;
345     }
346
347     @Nonnull
348     @Override
349     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
350         return unknownNodes;
351     }
352
353     @Override
354     public final Set<TypeDefinition<?>> getTypeDefinitions() {
355         return typeDefinitions;
356     }
357
358     @Override
359     public final Set<DataSchemaNode> getChildNodes() {
360         return publicChildNodes;
361     }
362
363     @Override
364     public final Set<GroupingDefinition> getGroupings() {
365         return groupings;
366     }
367
368     @Override
369     public final DataSchemaNode getDataChildByName(final QName name) {
370         // Child nodes are keyed by their container name, so we can do a direct
371         // lookup
372         return childNodes.get(name);
373     }
374
375     @Override
376     public Set<UsesNode> getUses() {
377         return uses;
378     }
379
380     @Override
381     public SemVer getSemanticVersion() {
382         return semanticVersion;
383     }
384
385     @Override
386     public String toString() {
387         return this.getClass().getSimpleName() + "[" +
388                 "name=" + name +
389                 ", namespace=" + getNamespace() +
390                 ", revision=" + getRevision() +
391                 ", prefix=" + prefix +
392                 ", yangVersion=" + yangVersion +
393                 "]";
394     }
395
396 }