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