Merge "Add hooking into NormalizedNodeParsers."
[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 java.net.URI;
11 import java.util.Collection;
12 import java.util.Date;
13 import java.util.HashSet;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.Deviation;
22 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
23 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
24 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
27 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
28 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
29 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
33 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleImpl;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35
36 import com.google.common.collect.ImmutableList;
37 import com.google.common.collect.ImmutableSet;
38 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName;
39
40 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
41
42 public class SubmoduleEffectiveStatementImpl extends
43         AbstractEffectiveDocumentedDataNodeContainer<String, SubmoduleStatement> implements Module, Immutable {
44
45     private final QNameModule qNameModule;
46     private final String name;
47     private String sourcePath;
48     private String prefix;
49     private String yangVersion;
50     private String organization;
51     private String contact;
52     private ImmutableSet<ModuleImport> imports;
53     private ImmutableSet<Module> submodules;
54     private ImmutableSet<FeatureDefinition> features;
55     private ImmutableSet<NotificationDefinition> notifications;
56     private ImmutableSet<AugmentationSchema> augmentations;
57     private ImmutableSet<RpcDefinition> rpcs;
58     private ImmutableSet<Deviation> deviations;
59     private ImmutableList<ExtensionDefinition> extensionNodes;
60     private ImmutableSet<IdentitySchemaNode> identities;
61     private ImmutableList<UnknownSchemaNode> unknownNodes;
62     private String source;
63
64     public SubmoduleEffectiveStatementImpl(StmtContext<String, SubmoduleStatement, ?> ctx) {
65         super(ctx);
66
67         name = argument();
68
69         String belongsToModuleName = firstAttributeOf(ctx.declaredSubstatements(), BelongsToStatement.class);
70         final QNameModule belongsToModule = ctx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName);
71         qNameModule = QNameModule.create(belongsToModule.getNamespace(), belongsToModule.getRevision());
72
73         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
74             if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
75                 prefix = ((PrefixEffectiveStatementImpl) effectiveStatement).argument();
76             }
77             if (effectiveStatement instanceof YangVersionEffectiveStatementImpl) {
78                 yangVersion = ((YangVersionEffectiveStatementImpl) effectiveStatement).argument();
79             }
80             if (effectiveStatement instanceof OrganizationEffectiveStatementImpl) {
81                 organization = ((OrganizationEffectiveStatementImpl) effectiveStatement).argument();
82             }
83             if (effectiveStatement instanceof ContactEffectiveStatementImpl) {
84                 contact = ((ContactEffectiveStatementImpl) effectiveStatement).argument();
85             }
86         }
87
88         source = ctx.getStatementSource().name();
89
90         initSubstatementCollections();
91     }
92
93     private void initSubstatementCollections() {
94         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
95
96         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
97         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
98         Set<ModuleImport> importsInit = new HashSet<>();
99         Set<Module> submodulesInit = new HashSet<>();
100         Set<NotificationDefinition> notificationsInit = new HashSet<>();
101         Set<RpcDefinition> rpcsInit = new HashSet<>();
102         Set<Deviation> deviationsInit = new HashSet<>();
103         Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
104         Set<FeatureDefinition> featuresInit = new HashSet<>();
105         List<ExtensionDefinition> extensionNodesInit = new LinkedList<>();
106
107         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
108             if (effectiveStatement instanceof UnknownSchemaNode) {
109                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
110             }
111             if (effectiveStatement instanceof AugmentationSchema) {
112                 augmentationsInit.add((AugmentationSchema) effectiveStatement);
113             }
114             if (effectiveStatement instanceof ModuleImport) {
115                 importsInit.add((ModuleImport) effectiveStatement);
116             }
117             if (effectiveStatement instanceof Module) {
118                 submodulesInit.add((Module) effectiveStatement);
119             }
120             if (effectiveStatement instanceof NotificationDefinition) {
121                 notificationsInit.add((NotificationDefinition) effectiveStatement);
122             }
123             if (effectiveStatement instanceof RpcDefinition) {
124                 rpcsInit.add((RpcDefinition) effectiveStatement);
125             }
126             if (effectiveStatement instanceof Deviation) {
127                 deviationsInit.add((Deviation) effectiveStatement);
128             }
129             if (effectiveStatement instanceof IdentitySchemaNode) {
130                 identitiesInit.add((IdentitySchemaNode) effectiveStatement);
131             }
132             if (effectiveStatement instanceof FeatureDefinition) {
133                 featuresInit.add((FeatureDefinition) effectiveStatement);
134             }
135             if (effectiveStatement instanceof ExtensionDefinition) {
136                 extensionNodesInit.add((ExtensionDefinition) effectiveStatement);
137             }
138         }
139
140         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
141         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
142         this.imports = ImmutableSet.copyOf(importsInit);
143         this.submodules = ImmutableSet.copyOf(submodulesInit);
144         this.notifications = ImmutableSet.copyOf(notificationsInit);
145         this.rpcs = ImmutableSet.copyOf(rpcsInit);
146         this.deviations = ImmutableSet.copyOf(deviationsInit);
147         this.identities = ImmutableSet.copyOf(identitiesInit);
148         this.features = ImmutableSet.copyOf(featuresInit);
149         this.extensionNodes = ImmutableList.copyOf(extensionNodesInit);
150     }
151
152     @Override
153     public String getModuleSourcePath() {
154         return sourcePath;
155     }
156
157     @Override
158     public URI getNamespace() {
159         return qNameModule.getNamespace();
160     }
161
162     @Override
163     public String getName() {
164         return name;
165     }
166
167     @Override
168     public Date getRevision() {
169         return qNameModule.getRevision();
170     }
171
172     @Override
173     public String getPrefix() {
174         return prefix;
175     }
176
177     @Override
178     public String getYangVersion() {
179         return yangVersion;
180     }
181
182     @Override
183     public String getOrganization() {
184         return organization;
185     }
186
187     @Override
188     public String getContact() {
189         return contact;
190     }
191
192     @Override
193     public Set<ModuleImport> getImports() {
194         return imports;
195     }
196
197     @Override
198     public Set<Module> getSubmodules() {
199         return submodules;
200     }
201
202     @Override
203     public Set<FeatureDefinition> getFeatures() {
204         return features;
205     }
206
207     @Override
208     public Set<NotificationDefinition> getNotifications() {
209         return notifications;
210     }
211
212     @Override
213     public Set<AugmentationSchema> getAugmentations() {
214         return augmentations;
215     }
216
217     @Override
218     public Set<RpcDefinition> getRpcs() {
219         return rpcs;
220     }
221
222     @Override
223     public Set<Deviation> getDeviations() {
224         return deviations;
225     }
226
227     @Override
228     public List<ExtensionDefinition> getExtensionSchemaNodes() {
229         return extensionNodes;
230     }
231
232     @Override
233     public Set<IdentitySchemaNode> getIdentities() {
234         return identities;
235     }
236
237     @Override
238     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
239         return unknownNodes;
240     }
241
242     @Override
243     public String getSource() {
244         return source;
245     }
246
247     @Override
248     public int hashCode() {
249         final int prime = 31;
250         int result = 1;
251         result = prime * result + ((name == null) ? 0 : name.hashCode());
252         result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
253         result = prime * result + qNameModule.hashCode();
254         return result;
255     }
256
257     @Override
258     public boolean equals(final Object obj) {
259         if (this == obj) {
260             return true;
261         }
262         if (obj == null) {
263             return false;
264         }
265         if (getClass() != obj.getClass()) {
266             return false;
267         }
268         SubmoduleEffectiveStatementImpl other = (SubmoduleEffectiveStatementImpl) obj;
269         if (name == null) {
270             if (other.name != null) {
271                 return false;
272             }
273         } else if (!name.equals(other.name)) {
274             return false;
275         }
276         if (!qNameModule.equals(other.qNameModule)) {
277             return false;
278         }
279         if (yangVersion == null) {
280             if (other.yangVersion != null) {
281                 return false;
282             }
283         } else if (!yangVersion.equals(other.yangVersion)) {
284             return false;
285         }
286         return true;
287     }
288
289     @Override
290     public String toString() {
291         StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
292         sb.append("[");
293         sb.append("name=").append(name);
294         sb.append(", namespace=").append(getNamespace());
295         sb.append(", revision=").append(getRevision());
296         sb.append(", prefix=").append(prefix);
297         sb.append(", yangVersion=").append(yangVersion);
298         sb.append("]");
299         return sb.toString();
300     }
301
302     @Override
303     public QNameModule getQNameModule() {
304         return qNameModule;
305     }
306 }