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