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