BUG-4688: Make SourceIdentifier use Revision
[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.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Iterables;
16 import java.net.URI;
17 import java.util.ArrayList;
18 import java.util.Collection;
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.Optional;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yangtools.concepts.SemVer;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.Revision;
31 import org.opendaylight.yangtools.yang.common.YangVersion;
32 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Deviation;
36 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
37 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
38 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
39 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
42 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
43 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
46 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.UsesNode;
48 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
49 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
50 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
51 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
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.IncludedSubmoduleNameToModuleCtx;
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 ? null : 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, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx
122                 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.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<StmtContext<?, ?, ?>> includedSubmodules = includedSubmodulesMap.values();
135             final Set<Module> submodulesInit = new HashSet<>();
136             final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
137             for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodules) {
138                 final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
139                         .buildEffective();
140                 submodulesInit.add(submodule);
141                 substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
142                         .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
143                         .collect(Collectors.toList()));
144             }
145
146             this.submodules = ImmutableSet.copyOf(submodulesInit);
147             this.submoduleContextsToBuild = ImmutableSet.of();
148             substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
149         } else {
150             /*
151              * Because of possible circular chains of includes between submodules we can
152              * collect only submodule contexts here and then build them during
153              * sealing of this statement.
154              */
155             final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
156                 submoduleContextsInit = new HashSet<>();
157             for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
158                 submoduleContextsInit.add(
159                     (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
160             }
161
162             this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
163             substatementsOfSubmodules = ImmutableList.of();
164         }
165
166         if (!submoduleContextsToBuild.isEmpty()) {
167             ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
168             sealed = false;
169         } else {
170             sealed = true;
171         }
172
173         // init substatements collections
174         final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
175         effectiveSubstatements.addAll(effectiveSubstatements());
176         effectiveSubstatements.addAll(substatementsOfSubmodules);
177
178         final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
179         final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
180         final Set<ModuleImport> importsInit = new HashSet<>();
181         final Set<NotificationDefinition> notificationsInit = new HashSet<>();
182         final Set<RpcDefinition> rpcsInit = new HashSet<>();
183         final Set<Deviation> deviationsInit = new HashSet<>();
184         final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
185         final Set<FeatureDefinition> featuresInit = new HashSet<>();
186         final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
187
188         final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
189         final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
190         final Set<UsesNode> mutableUses = new HashSet<>();
191         final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
192         final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
193
194         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
195             if (effectiveStatement instanceof UnknownSchemaNode) {
196                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
197             }
198             if (effectiveStatement instanceof AugmentationSchema) {
199                 augmentationsInit.add((AugmentationSchema) effectiveStatement);
200             }
201             if (effectiveStatement instanceof ModuleImport) {
202                 importsInit.add((ModuleImport) effectiveStatement);
203             }
204             if (effectiveStatement instanceof NotificationDefinition) {
205                 notificationsInit.add((NotificationDefinition) effectiveStatement);
206             }
207             if (effectiveStatement instanceof RpcDefinition) {
208                 rpcsInit.add((RpcDefinition) effectiveStatement);
209             }
210             if (effectiveStatement instanceof Deviation) {
211                 deviationsInit.add((Deviation) effectiveStatement);
212             }
213             if (effectiveStatement instanceof IdentitySchemaNode) {
214                 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
215             }
216             if (effectiveStatement instanceof FeatureDefinition) {
217                 featuresInit.add((FeatureDefinition) effectiveStatement);
218             }
219             if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
220                 extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
221             }
222             if (effectiveStatement instanceof DataSchemaNode) {
223                 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
224                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
225                     mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
226                     mutablePublicChildNodes.add(dataSchemaNode);
227                 } else {
228                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
229                 }
230             }
231             if (effectiveStatement instanceof UsesNode) {
232                 final UsesNode usesNode = (UsesNode) effectiveStatement;
233                 if (!mutableUses.contains(usesNode)) {
234                     mutableUses.add(usesNode);
235                 } else {
236                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
237                 }
238             }
239             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
240                 final TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
241                 final TypeDefinition<?> type = typeDef.getTypeDefinition();
242                 if (!mutableTypeDefinitions.contains(type)) {
243                     mutableTypeDefinitions.add(type);
244                 } else {
245                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
246                 }
247             }
248             if (effectiveStatement instanceof GroupingDefinition) {
249                 final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
250                 if (!mutableGroupings.contains(grp)) {
251                     mutableGroupings.add(grp);
252                 } else {
253                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
254                 }
255             }
256         }
257
258         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
259         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
260         this.imports = ImmutableSet.copyOf(importsInit);
261         this.notifications = ImmutableSet.copyOf(notificationsInit);
262         this.rpcs = ImmutableSet.copyOf(rpcsInit);
263         this.deviations = ImmutableSet.copyOf(deviationsInit);
264         this.identities = ImmutableSet.copyOf(identitiesInit);
265         this.features = ImmutableSet.copyOf(featuresInit);
266         this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
267
268         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
269         this.groupings = ImmutableSet.copyOf(mutableGroupings);
270         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
271         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
272         this.uses = ImmutableSet.copyOf(mutableUses);
273
274     }
275
276     @Override
277     public URI getNamespace() {
278         return getQNameModule().getNamespace();
279     }
280
281     @Override
282     public String getName() {
283         return name;
284     }
285
286     @Override
287     public Optional<Revision> getRevision() {
288         return getQNameModule().getRevision();
289     }
290
291     @Override
292     public String getPrefix() {
293         return prefix;
294     }
295
296     @Override
297     public YangVersion getYangVersion() {
298         return yangVersion;
299     }
300
301     @Override
302     public String getOrganization() {
303         return organization;
304     }
305
306     @Override
307     public String getContact() {
308         return contact;
309     }
310
311     @Override
312     public Set<ModuleImport> getImports() {
313         return imports;
314     }
315
316     @Override
317     public Set<Module> getSubmodules() {
318         Preconditions.checkState(sealed,
319             "Attempt to get base submodules from unsealed submodule effective statement %s", getQNameModule());
320         return submodules;
321     }
322
323     @Override
324     public Set<FeatureDefinition> getFeatures() {
325         return features;
326     }
327
328     @Override
329     public Set<NotificationDefinition> getNotifications() {
330         return notifications;
331     }
332
333     @Override
334     public Set<AugmentationSchema> getAugmentations() {
335         return augmentations;
336     }
337
338     @Override
339     public Set<RpcDefinition> getRpcs() {
340         return rpcs;
341     }
342
343     @Override
344     public Set<Deviation> getDeviations() {
345         return deviations;
346     }
347
348     @Override
349     public List<ExtensionDefinition> getExtensionSchemaNodes() {
350         return extensionNodes;
351     }
352
353     @Override
354     public Set<IdentitySchemaNode> getIdentities() {
355         return identities;
356     }
357
358     @Nonnull
359     @Override
360     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
361         return unknownNodes;
362     }
363
364     @Override
365     public final Set<TypeDefinition<?>> getTypeDefinitions() {
366         return typeDefinitions;
367     }
368
369     @Override
370     public final Set<DataSchemaNode> getChildNodes() {
371         return publicChildNodes;
372     }
373
374     @Override
375     public final Set<GroupingDefinition> getGroupings() {
376         return groupings;
377     }
378
379     @Override
380     public final DataSchemaNode getDataChildByName(final QName name) {
381         // Child nodes are keyed by their container name, so we can do a direct
382         // lookup
383         return childNodes.get(name);
384     }
385
386     @Override
387     public Set<UsesNode> getUses() {
388         return uses;
389     }
390
391     @Override
392     public Optional<SemVer> getSemanticVersion() {
393         return Optional.ofNullable(semanticVersion);
394     }
395
396     @Override
397     public String toString() {
398         return MoreObjects.toStringHelper(this).omitNullValues()
399                 .add("name", name)
400                 .add("namespace", getNamespace())
401                 .add("revision", getRevision().orElse(null))
402                 .add("prefix", prefix)
403                 .add("yangVersion", yangVersion)
404                 .toString();
405     }
406
407     @Override
408     public void seal() {
409         if (!sealed) {
410             submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
411                 ctx -> (Module) ctx.buildEffective()));
412             submoduleContextsToBuild = ImmutableSet.of();
413             sealed = true;
414         }
415     }
416 }