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