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