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