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