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