e415ecbfda300771057084bbba7eb76e487b84bb
[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
57         AbstractEffectiveDocumentedNode<String, ModuleStatement> implements
58         Module, Immutable {
59
60     private final QNameModule qNameModule;
61     private final String name;
62     private final 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 final 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             final 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).argument();
101             }
102             if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
103                 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement).argument();
104             }
105             if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
106                 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement).argument();
107             }
108             if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
109                 contact = ((ContactEffectiveStatementImpl) effectiveStatement).argument();
110             }
111         }
112
113         if (yangVersion == null) {
114             yangVersion = "1";
115         }
116
117         DeclarationInTextSource sourceReference = (DeclarationInTextSource) ctx.getStatementSourceReference();
118         sourcePath = sourceReference.getSourceName();
119         source = sourceReference.getSourceText();
120
121         initSubmodules(ctx);
122         initSubstatementCollections(ctx);
123     }
124
125     private void initSubmodules(
126             final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
127         Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
128                 .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
129
130         if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
131             this.submodules = ImmutableSet.of();
132             this.substatementsOfSubmodules = ImmutableList.of();
133             return;
134         }
135
136         Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
137
138         Set<Module> submodulesInit = new HashSet<>();
139         List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new LinkedList<>();
140         for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
141             Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx = (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>) ctx
142                     .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
212                             .createNameCollisionSourceException(ctx,
213                                     effectiveStatement);
214                 }
215             }
216             if (effectiveStatement instanceof UsesNode) {
217                 UsesNode usesNode = (UsesNode) effectiveStatement;
218                 if (!mutableUses.contains(usesNode)) {
219                     mutableUses.add(usesNode);
220                 } else {
221                     throw EffectiveStmtUtils
222                             .createNameCollisionSourceException(ctx,
223                                     effectiveStatement);
224                 }
225             }
226             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
227                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
228                 ExtendedType extendedType = typeDef.buildType();
229                 if (!mutableTypeDefinitions.contains(extendedType)) {
230                     mutableTypeDefinitions.add(extendedType);
231                 } else {
232                     throw EffectiveStmtUtils
233                             .createNameCollisionSourceException(ctx,
234                                     effectiveStatement);
235                 }
236             }
237             if (effectiveStatement instanceof GroupingDefinition) {
238                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
239                 if (!mutableGroupings.contains(grp)) {
240                     mutableGroupings.add(grp);
241                 } else {
242                     throw EffectiveStmtUtils
243                             .createNameCollisionSourceException(ctx,
244                                     effectiveStatement);
245                 }
246             }
247         }
248
249         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
250         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
251         this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit,
252                 ctx));
253         this.notifications = ImmutableSet.copyOf(notificationsInit);
254         this.rpcs = ImmutableSet.copyOf(rpcsInit);
255         this.deviations = ImmutableSet.copyOf(deviationsInit);
256         this.identities = ImmutableSet.copyOf(identitiesInit);
257         this.features = ImmutableSet.copyOf(featuresInit);
258         this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
259
260         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
261         this.groupings = ImmutableSet.copyOf(mutableGroupings);
262         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
263         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
264         this.uses = ImmutableSet.copyOf(mutableUses);
265     }
266
267     private static Set<ModuleImport> resolveModuleImports(
268             final Set<ModuleImport> importsInit,
269             final StmtContext<String, ModuleStatement, EffectiveStatement<String, ModuleStatement>> ctx) {
270         Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
271         for (ModuleImport moduleImport : importsInit) {
272             if (moduleImport.getRevision().equals(
273                     SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
274                 QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx,
275                         moduleImport.getPrefix());
276                 if (!impModuleQName.getRevision().equals(
277                         SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
278                     ModuleImport resolvedModuleImport = new ModuleImportImpl(
279                             moduleImport.getModuleName(),
280                             impModuleQName.getRevision(),
281                             moduleImport.getPrefix());
282                     resolvedModuleImports.add(resolvedModuleImport);
283                 }
284             } else {
285                 resolvedModuleImports.add(moduleImport);
286             }
287         }
288         return resolvedModuleImports;
289     }
290
291     @Override
292     public String getModuleSourcePath() {
293         return sourcePath;
294     }
295
296     @Override
297     public URI getNamespace() {
298         return qNameModule.getNamespace();
299     }
300
301     @Override
302     public String getName() {
303         return name;
304     }
305
306     @Override
307     public Date getRevision() {
308         return qNameModule.getRevision();
309     }
310
311     @Override
312     public String getPrefix() {
313         return prefix;
314     }
315
316     @Override
317     public String getYangVersion() {
318         return yangVersion;
319     }
320
321     @Override
322     public String getOrganization() {
323         return organization;
324     }
325
326     @Override
327     public String getContact() {
328         return contact;
329     }
330
331     @Override
332     public Set<ModuleImport> getImports() {
333         return imports;
334     }
335
336     @Override
337     public Set<Module> getSubmodules() {
338         return submodules;
339     }
340
341     @Override
342     public Set<FeatureDefinition> getFeatures() {
343         return features;
344     }
345
346     @Override
347     public Set<NotificationDefinition> getNotifications() {
348         return notifications;
349     }
350
351     @Override
352     public Set<AugmentationSchema> getAugmentations() {
353         return augmentations;
354     }
355
356     @Override
357     public Set<RpcDefinition> getRpcs() {
358         return rpcs;
359     }
360
361     @Override
362     public Set<Deviation> getDeviations() {
363         return deviations;
364     }
365
366     @Override
367     public List<ExtensionDefinition> getExtensionSchemaNodes() {
368         return extensionNodes;
369     }
370
371     @Override
372     public Set<IdentitySchemaNode> getIdentities() {
373         return identities;
374     }
375
376     @Override
377     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
378         return unknownNodes;
379     }
380
381     @Override
382     public final Set<TypeDefinition<?>> getTypeDefinitions() {
383         return typeDefinitions;
384     }
385
386     @Override
387     public final Set<DataSchemaNode> getChildNodes() {
388         return publicChildNodes;
389     }
390
391     @Override
392     public final Set<GroupingDefinition> getGroupings() {
393         return groupings;
394     }
395
396     @Override
397     public final DataSchemaNode getDataChildByName(final QName name) {
398         // Child nodes are keyed by their container name, so we can do a direct
399         // lookup
400         return childNodes.get(name);
401     }
402
403     @Override
404     public final DataSchemaNode getDataChildByName(final String name) {
405         for (DataSchemaNode node : childNodes.values()) {
406             if (node.getQName().getLocalName().equals(name)) {
407                 return node;
408             }
409         }
410         return null;
411     }
412
413     @Override
414     public Set<UsesNode> getUses() {
415         return uses;
416     }
417
418     @Override
419     public String getSource() {
420         return source;
421     }
422
423     @Override
424     public int hashCode() {
425         final int prime = 31;
426         int result = 1;
427         result = prime * result + Objects.hashCode(name);
428         result = prime * result + Objects.hashCode(yangVersion);
429         result = prime * result + qNameModule.hashCode();
430         return result;
431     }
432
433     @Override
434     public boolean equals(final Object obj) {
435         if (this == obj) {
436             return true;
437         }
438         if (obj == null) {
439             return false;
440         }
441         if (getClass() != obj.getClass()) {
442             return false;
443         }
444         ModuleEffectiveStatementImpl other = (ModuleEffectiveStatementImpl) obj;
445         if (name == null) {
446             if (other.name != null) {
447                 return false;
448             }
449         } else if (!name.equals(other.name)) {
450             return false;
451         }
452         if (!qNameModule.equals(other.qNameModule)) {
453             return false;
454         }
455         if (yangVersion == null) {
456             if (other.yangVersion != null) {
457                 return false;
458             }
459         } else if (!yangVersion.equals(other.yangVersion)) {
460             return false;
461         }
462         return true;
463     }
464
465     @Override
466     public String toString() {
467         StringBuilder sb = new StringBuilder(
468                 ModuleEffectiveStatementImpl.class.getSimpleName());
469         sb.append("[");
470         sb.append("name=").append(name);
471         sb.append(", namespace=").append(getNamespace());
472         sb.append(", revision=").append(getRevision());
473         sb.append(", prefix=").append(prefix);
474         sb.append(", yangVersion=").append(yangVersion);
475         sb.append("]");
476         return sb.toString();
477     }
478
479     @Override
480     public QNameModule getQNameModule() {
481         return qNameModule;
482     }
483
484 }