Fix Module.getVersion()
[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.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.Iterables;
15 import java.net.URI;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Date;
19 import java.util.HashSet;
20 import java.util.LinkedHashMap;
21 import java.util.LinkedHashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yangtools.concepts.SemVer;
28 import org.opendaylight.yangtools.yang.common.QName;
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.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Deviation;
34 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
35 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
36 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
37 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
40 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
41 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
42 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
43 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
45 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.UsesNode;
47 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
48 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
49 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
50 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
51 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
54 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
55 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
56 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
57
58 abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
59         AbstractEffectiveDocumentedNode<String, D> implements Module, MutableStatement {
60     private final String name;
61     private final String prefix;
62     private final YangVersion yangVersion;
63     private final String organization;
64     private final String contact;
65     private final Set<ModuleImport> imports;
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     private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
82         submoduleContextsToBuild;
83     private Set<Module> submodules;
84     private boolean sealed;
85
86     AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
87         super(ctx);
88
89         this.name = argument();
90
91         EffectiveStatementBase<?, ?> parentOfPrefix = this;
92         if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
93             parentOfPrefix = firstEffective(BelongsToEffectiveStatementImpl.class);
94             SourceException.throwIfNull(parentOfPrefix, ctx.getStatementSourceReference(),
95                     "Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
96         }
97
98         final PrefixEffectiveStatementImpl prefixStmt = parentOfPrefix
99                 .firstEffective(PrefixEffectiveStatementImpl.class);
100         SourceException.throwIfNull(prefixStmt, ctx.getStatementSourceReference(),
101                 "Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
102         this.prefix = prefixStmt.argument();
103
104         final YangVersionEffectiveStatementImpl yangVersionStmt =
105                 firstEffective(YangVersionEffectiveStatementImpl.class);
106         this.yangVersion = yangVersionStmt == null ? YangVersion.VERSION_1 : yangVersionStmt.argument();
107
108         final OpenconfigVersionEffectiveStatementImpl semanticVersionStmt =
109                 firstEffective(OpenconfigVersionEffectiveStatementImpl.class);
110         this.semanticVersion = semanticVersionStmt == null ? DEFAULT_SEMANTIC_VERSION : semanticVersionStmt.argument();
111
112         final OrganizationEffectiveStatementImpl organizationStmt =
113                 firstEffective(OrganizationEffectiveStatementImpl.class);
114         this.organization = organizationStmt == null ? null : organizationStmt.argument();
115
116         final ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
117         this.contact = contactStmt == null ? null : contactStmt.argument();
118
119         // init submodules and substatements of submodules
120         final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
121         final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
122                 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
123
124         if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
125             this.submodules = ImmutableSet.of();
126             this.submoduleContextsToBuild = ImmutableSet.of();
127             substatementsOfSubmodules = ImmutableList.of();
128         } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
129             /*
130              * Aggregation of substatements from submodules should be done only
131              * for modules. In case of submodules it does not make sense because
132              * of possible circular chains of includes between submodules.
133              */
134             final Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
135             final Set<Module> submodulesInit = new HashSet<>();
136             final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
137             for (final ModuleIdentifier submoduleIdentifier : includedSubmodules) {
138                 @SuppressWarnings("unchecked")
139                 final Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
140                         (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
141                         .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
142                 final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
143                         .buildEffective();
144                 submodulesInit.add(submodule);
145                 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
146                         .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
147                         .collect(Collectors.toList()));
148             }
149
150             this.submodules = ImmutableSet.copyOf(submodulesInit);
151             this.submoduleContextsToBuild = ImmutableSet.of();
152             substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
153         } else {
154             /*
155              * Because of possible circular chains of includes between submodules we can
156              * collect only submodule contexts here and then build them during
157              * sealing of this statement.
158              */
159             final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
160                 submoduleContextsInit = new HashSet<>();
161             for (final ModuleIdentifier submoduleIdentifier : includedSubmodulesMap.values()) {
162                 final StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
163                         ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
164                 submoduleContextsInit.add(submoduleCtx);
165             }
166
167             this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
168             substatementsOfSubmodules = ImmutableList.of();
169         }
170
171         if (!submoduleContextsToBuild.isEmpty()) {
172             ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
173             sealed = false;
174         } else {
175             sealed = true;
176         }
177
178         // init substatements collections
179         final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
180         effectiveSubstatements.addAll(effectiveSubstatements());
181         effectiveSubstatements.addAll(substatementsOfSubmodules);
182
183         final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
184         final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
185         final Set<ModuleImport> importsInit = new HashSet<>();
186         final Set<NotificationDefinition> notificationsInit = new HashSet<>();
187         final Set<RpcDefinition> rpcsInit = new HashSet<>();
188         final Set<Deviation> deviationsInit = new HashSet<>();
189         final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
190         final Set<FeatureDefinition> featuresInit = new HashSet<>();
191         final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
192
193         final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
194         final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
195         final Set<UsesNode> mutableUses = new HashSet<>();
196         final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
197         final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
198
199         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
200             if (effectiveStatement instanceof UnknownSchemaNode) {
201                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
202             }
203             if (effectiveStatement instanceof AugmentationSchema) {
204                 augmentationsInit.add((AugmentationSchema) effectiveStatement);
205             }
206             if (effectiveStatement instanceof ModuleImport) {
207                 importsInit.add((ModuleImport) effectiveStatement);
208             }
209             if (effectiveStatement instanceof NotificationDefinition) {
210                 notificationsInit.add((NotificationDefinition) effectiveStatement);
211             }
212             if (effectiveStatement instanceof RpcDefinition) {
213                 rpcsInit.add((RpcDefinition) effectiveStatement);
214             }
215             if (effectiveStatement instanceof Deviation) {
216                 deviationsInit.add((Deviation) effectiveStatement);
217             }
218             if (effectiveStatement instanceof IdentitySchemaNode) {
219                 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
220             }
221             if (effectiveStatement instanceof FeatureDefinition) {
222                 featuresInit.add((FeatureDefinition) effectiveStatement);
223             }
224             if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
225                 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
226             }
227             if (effectiveStatement instanceof DataSchemaNode) {
228                 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
229                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
230                     mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
231                     mutablePublicChildNodes.add(dataSchemaNode);
232                 } else {
233                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
234                 }
235             }
236             if (effectiveStatement instanceof UsesNode) {
237                 final UsesNode usesNode = (UsesNode) effectiveStatement;
238                 if (!mutableUses.contains(usesNode)) {
239                     mutableUses.add(usesNode);
240                 } else {
241                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
242                 }
243             }
244             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
245                 final TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
246                 final TypeDefinition<?> type = typeDef.getTypeDefinition();
247                 if (!mutableTypeDefinitions.contains(type)) {
248                     mutableTypeDefinitions.add(type);
249                 } else {
250                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
251                 }
252             }
253             if (effectiveStatement instanceof GroupingDefinition) {
254                 final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
255                 if (!mutableGroupings.contains(grp)) {
256                     mutableGroupings.add(grp);
257                 } else {
258                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
259                 }
260             }
261         }
262
263         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
264         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
265         this.imports = ImmutableSet.copyOf(importsInit);
266         this.notifications = ImmutableSet.copyOf(notificationsInit);
267         this.rpcs = ImmutableSet.copyOf(rpcsInit);
268         this.deviations = ImmutableSet.copyOf(deviationsInit);
269         this.identities = ImmutableSet.copyOf(identitiesInit);
270         this.features = ImmutableSet.copyOf(featuresInit);
271         this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
272
273         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
274         this.groupings = ImmutableSet.copyOf(mutableGroupings);
275         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
276         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
277         this.uses = ImmutableSet.copyOf(mutableUses);
278
279     }
280
281     @Override
282     public URI getNamespace() {
283         return getQNameModule().getNamespace();
284     }
285
286     @Override
287     public String getName() {
288         return name;
289     }
290
291     @Override
292     public Date getRevision() {
293         return getQNameModule().getRevision();
294     }
295
296     @Override
297     public String getPrefix() {
298         return prefix;
299     }
300
301     @Override
302     public YangVersion getYangVersion() {
303         return yangVersion;
304     }
305
306     @Override
307     public String getOrganization() {
308         return organization;
309     }
310
311     @Override
312     public String getContact() {
313         return contact;
314     }
315
316     @Override
317     public Set<ModuleImport> getImports() {
318         return imports;
319     }
320
321     @Override
322     public Set<Module> getSubmodules() {
323         Preconditions.checkState(sealed,
324             "Attempt to get base submodules from unsealed submodule effective statement %s", getQNameModule());
325         return submodules;
326     }
327
328     @Override
329     public Set<FeatureDefinition> getFeatures() {
330         return features;
331     }
332
333     @Override
334     public Set<NotificationDefinition> getNotifications() {
335         return notifications;
336     }
337
338     @Override
339     public Set<AugmentationSchema> getAugmentations() {
340         return augmentations;
341     }
342
343     @Override
344     public Set<RpcDefinition> getRpcs() {
345         return rpcs;
346     }
347
348     @Override
349     public Set<Deviation> getDeviations() {
350         return deviations;
351     }
352
353     @Override
354     public List<ExtensionDefinition> getExtensionSchemaNodes() {
355         return extensionNodes;
356     }
357
358     @Override
359     public Set<IdentitySchemaNode> getIdentities() {
360         return identities;
361     }
362
363     @Nonnull
364     @Override
365     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
366         return unknownNodes;
367     }
368
369     @Override
370     public final Set<TypeDefinition<?>> getTypeDefinitions() {
371         return typeDefinitions;
372     }
373
374     @Override
375     public final Set<DataSchemaNode> getChildNodes() {
376         return publicChildNodes;
377     }
378
379     @Override
380     public final Set<GroupingDefinition> getGroupings() {
381         return groupings;
382     }
383
384     @Override
385     public final DataSchemaNode getDataChildByName(final QName name) {
386         // Child nodes are keyed by their container name, so we can do a direct
387         // lookup
388         return childNodes.get(name);
389     }
390
391     @Override
392     public Set<UsesNode> getUses() {
393         return uses;
394     }
395
396     @Override
397     public SemVer getSemanticVersion() {
398         return semanticVersion;
399     }
400
401     @Override
402     public String toString() {
403         return this.getClass().getSimpleName() + "["
404                 + "name=" + name
405                 + ", namespace=" + getNamespace()
406                 + ", revision=" + getQNameModule().getFormattedRevision()
407                 + ", prefix=" + prefix
408                 + ", yangVersion=" + yangVersion
409                 + "]";
410     }
411
412     @Override
413     public void seal() {
414         if (!sealed) {
415             submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
416                 ctx -> (Module) ctx.buildEffective()));
417             submoduleContextsToBuild = ImmutableSet.of();
418             sealed = true;
419         }
420     }
421 }