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