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