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