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