Bug 3670: Replaced YANG parser for new statement parser
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ModuleEffectiveStatementImpl.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.Objects;
23 import java.util.Set;
24 import org.opendaylight.yangtools.concepts.Immutable;
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.EffectiveStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
46 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
47 import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
48 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
51 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
52 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
53 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
55
56 public class ModuleEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<String, ModuleStatement> implements
57         Module, Immutable {
58
59     private final QNameModule qNameModule;
60     private final String name;
61     private final String sourcePath;
62     private String prefix;
63     private String yangVersion;
64     private String organization;
65     private String contact;
66     private ImmutableSet<ModuleImport> imports;
67     private ImmutableSet<Module> submodules;
68     private ImmutableSet<FeatureDefinition> features;
69     private ImmutableSet<NotificationDefinition> notifications;
70     private ImmutableSet<AugmentationSchema> augmentations;
71     private ImmutableSet<RpcDefinition> rpcs;
72     private ImmutableSet<Deviation> deviations;
73     private ImmutableList<ExtensionDefinition> extensionNodes;
74     private ImmutableSet<IdentitySchemaNode> identities;
75     private ImmutableList<UnknownSchemaNode> unknownNodes;
76     private ImmutableList<EffectiveStatement<?, ?>> substatementsOfSubmodules;
77
78     private ImmutableMap<QName, DataSchemaNode> childNodes;
79     private ImmutableSet<GroupingDefinition> groupings;
80     private ImmutableSet<UsesNode> uses;
81     private ImmutableSet<TypeDefinition<?>> typeDefinitions;
82     private ImmutableSet<DataSchemaNode> publicChildNodes;
83
84     public ModuleEffectiveStatementImpl(
85             final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
86         super(ctx);
87
88         name = argument();
89         QNameModule qNameModuleInit = ctx.getFromNamespace( ModuleCtxToModuleQName.class, ctx);
90         qNameModule = qNameModuleInit.getRevision() == null
91                 ? QNameModule.create(qNameModuleInit.getNamespace(), SimpleDateFormatUtil.DEFAULT_DATE_REV)
92                 : qNameModuleInit;
93
94         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
95             if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
96                 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement).argument();
97             }
98             if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
99                 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement).argument();
100             }
101             if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
102                 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement).argument();
103             }
104             if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
105                 contact = ((ContactEffectiveStatementImpl) effectiveStatement).argument();
106             }
107         }
108
109         if (yangVersion == null) {
110             yangVersion = "1";
111         }
112         if(ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
113             sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
114         } else {
115             sourcePath = null;
116         }
117
118
119         initSubmodules(ctx);
120         initSubstatementCollections(ctx);
121     }
122
123     private void initSubmodules(
124             final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
125         Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
126                 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
127
128         if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
129             this.submodules = ImmutableSet.of();
130             this.substatementsOfSubmodules = ImmutableList.of();
131             return;
132         }
133
134         Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
135
136         Set<Module> submodulesInit = new HashSet<>();
137         List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
138         for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
139             @SuppressWarnings("unchecked")
140             Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
141                     (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)
142                     ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
143             SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
144                     .buildEffective();
145             submodulesInit.add(submodule);
146             substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
147         }
148
149         this.submodules = ImmutableSet.copyOf(submodulesInit);
150         this.substatementsOfSubmodules = ImmutableList
151                 .copyOf(substatementsOfSubmodulesInit);
152     }
153
154     private void initSubstatementCollections(
155             final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
156         List<EffectiveStatement<?, ?>> effectiveSubstatements = new LinkedList<>();
157
158         effectiveSubstatements.addAll(effectiveSubstatements());
159         effectiveSubstatements.addAll(substatementsOfSubmodules);
160
161         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
162         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
163         Set<ModuleImport> importsInit = new HashSet<>();
164         Set<NotificationDefinition> notificationsInit = new HashSet<>();
165         Set<RpcDefinition> rpcsInit = new HashSet<>();
166         Set<Deviation> deviationsInit = new HashSet<>();
167         Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
168         Set<FeatureDefinition> featuresInit = new HashSet<>();
169         List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
170
171         Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
172         Set<GroupingDefinition> mutableGroupings = new HashSet<>();
173         Set<UsesNode> mutableUses = new HashSet<>();
174         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
175         Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
176
177         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
178             if (effectiveStatement instanceof UnknownSchemaNode) {
179                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
180             }
181             if (effectiveStatement instanceof AugmentationSchema) {
182                 augmentationsInit.add((AugmentationSchema) effectiveStatement);
183             }
184             if (effectiveStatement instanceof ModuleImport) {
185                 importsInit.add((ModuleImport) effectiveStatement);
186             }
187             if (effectiveStatement instanceof NotificationDefinition) {
188                 notificationsInit.add((NotificationDefinition) effectiveStatement);
189             }
190             if (effectiveStatement instanceof RpcDefinition) {
191                 rpcsInit.add((RpcDefinition) effectiveStatement);
192             }
193             if (effectiveStatement instanceof Deviation) {
194                 deviationsInit.add((Deviation) effectiveStatement);
195             }
196             if (effectiveStatement instanceof IdentitySchemaNode) {
197                 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
198             }
199             if (effectiveStatement instanceof FeatureDefinition) {
200                 featuresInit.add((FeatureDefinition) effectiveStatement);
201             }
202             if (effectiveStatement instanceof ExtensionDefinition) {
203                 extensionNodesInit.add((ExtensionDefinition) effectiveStatement);
204             }
205             if (effectiveStatement instanceof DataSchemaNode) {
206                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
207                 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
208                     mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
209                     mutablePublicChildNodes.add(dataSchemaNode);
210                 } else {
211                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
212                 }
213             }
214             if (effectiveStatement instanceof UsesNode) {
215                 UsesNode usesNode = (UsesNode) effectiveStatement;
216                 if (!mutableUses.contains(usesNode)) {
217                     mutableUses.add(usesNode);
218                 } else {
219                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
220                 }
221             }
222             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
223                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
224                 ExtendedType extendedType = typeDef.buildType();
225                 if (!mutableTypeDefinitions.contains(extendedType)) {
226                     mutableTypeDefinitions.add(extendedType);
227                 } else {
228                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
229                 }
230             }
231             if (effectiveStatement instanceof GroupingDefinition) {
232                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
233                 if (!mutableGroupings.contains(grp)) {
234                     mutableGroupings.add(grp);
235                 } else {
236                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
237                 }
238             }
239         }
240
241         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
242         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
243         this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
244         this.notifications = ImmutableSet.copyOf(notificationsInit);
245         this.rpcs = ImmutableSet.copyOf(rpcsInit);
246         this.deviations = ImmutableSet.copyOf(deviationsInit);
247         this.identities = ImmutableSet.copyOf(identitiesInit);
248         this.features = ImmutableSet.copyOf(featuresInit);
249         this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
250
251         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
252         this.groupings = ImmutableSet.copyOf(mutableGroupings);
253         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
254         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
255         this.uses = ImmutableSet.copyOf(mutableUses);
256     }
257
258     private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
259             final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
260         Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
261         for (ModuleImport moduleImport : importsInit) {
262             if (moduleImport.getRevision().equals(
263                     SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
264                 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
265                         moduleImport.getPrefix());
266                 if (!impModuleQName.getRevision().equals(
267                         SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
268                     ModuleImport resolvedModuleImport = new ModuleImportImpl(
269                             moduleImport.getModuleName(),
270                             impModuleQName.getRevision(),
271                             moduleImport.getPrefix());
272                     resolvedModuleImports.add(resolvedModuleImport);
273                 }
274             } else {
275                 resolvedModuleImports.add(moduleImport);
276             }
277         }
278         return resolvedModuleImports;
279     }
280
281     @Override
282     public String getModuleSourcePath() {
283         return sourcePath;
284     }
285
286     @Override
287     public URI getNamespace() {
288         return qNameModule.getNamespace();
289     }
290
291     @Override
292     public String getName() {
293         return name;
294     }
295
296     @Override
297     public Date getRevision() {
298         return qNameModule.getRevision();
299     }
300
301     @Override
302     public String getPrefix() {
303         return prefix;
304     }
305
306     @Override
307     public String getYangVersion() {
308         return yangVersion;
309     }
310
311     @Override
312     public String getOrganization() {
313         return organization;
314     }
315
316     @Override
317     public String getContact() {
318         return contact;
319     }
320
321     @Override
322     public Set<ModuleImport> getImports() {
323         return imports;
324     }
325
326     @Override
327     public Set<Module> getSubmodules() {
328         return submodules;
329     }
330
331     @Override
332     public Set<FeatureDefinition> getFeatures() {
333         return features;
334     }
335
336     @Override
337     public Set<NotificationDefinition> getNotifications() {
338         return notifications;
339     }
340
341     @Override
342     public Set<AugmentationSchema> getAugmentations() {
343         return augmentations;
344     }
345
346     @Override
347     public Set<RpcDefinition> getRpcs() {
348         return rpcs;
349     }
350
351     @Override
352     public Set<Deviation> getDeviations() {
353         return deviations;
354     }
355
356     @Override
357     public List<ExtensionDefinition> getExtensionSchemaNodes() {
358         return extensionNodes;
359     }
360
361     @Override
362     public Set<IdentitySchemaNode> getIdentities() {
363         return identities;
364     }
365
366     @Override
367     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
368         return unknownNodes;
369     }
370
371     @Override
372     public final Set<TypeDefinition<?>> getTypeDefinitions() {
373         return typeDefinitions;
374     }
375
376     @Override
377     public final Set<DataSchemaNode> getChildNodes() {
378         return publicChildNodes;
379     }
380
381     @Override
382     public final Set<GroupingDefinition> getGroupings() {
383         return groupings;
384     }
385
386     @Override
387     public final DataSchemaNode getDataChildByName(final QName name) {
388         // Child nodes are keyed by their container name, so we can do a direct
389         // lookup
390         return childNodes.get(name);
391     }
392
393     @Override
394     public final DataSchemaNode getDataChildByName(final String name) {
395         for (DataSchemaNode node : childNodes.values()) {
396             if (node.getQName().getLocalName().equals(name)) {
397                 return node;
398             }
399         }
400         return null;
401     }
402
403     @Override
404     public Set<UsesNode> getUses() {
405         return uses;
406     }
407
408     @Override
409     public String getSource() {
410         return null;
411     }
412
413     @Override
414     public int hashCode() {
415         final int prime = 31;
416         int result = 1;
417         result = prime * result + Objects.hashCode(name);
418         result = prime * result + Objects.hashCode(yangVersion);
419         result = prime * result + qNameModule.hashCode();
420         return result;
421     }
422
423     @Override
424     public boolean equals(final Object obj) {
425         if (this == obj) {
426             return true;
427         }
428         if (obj == null) {
429             return false;
430         }
431         if (getClass() != obj.getClass()) {
432             return false;
433         }
434         ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
435         if (!Objects.equals(name, other.name)) {
436             return false;
437         }
438         if (!qNameModule.equals(other.qNameModule)) {
439             return false;
440         }
441         if (!Objects.equals(yangVersion, other.yangVersion)) {
442             return false;
443         }
444         return true;
445     }
446
447     @Override
448     public String toString() {
449         StringBuilder sb = new StringBuilder(ModuleEffectiveStatementImpl.class.getSimpleName());
450         sb.append("[");
451         sb.append("name=").append(name);
452         sb.append(", namespace=").append(getNamespace());
453         sb.append(", revision=").append(getRevision());
454         sb.append(", prefix=").append(prefix);
455         sb.append(", yangVersion=").append(yangVersion);
456         sb.append("]");
457         return sb.toString();
458     }
459
460     @Override
461     public QNameModule getQNameModule() {
462         return qNameModule;
463     }
464
465 }