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