API Clarity: Documentented o.o.y.yang.parser.impl.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / util / YangModelDependencyInfo.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/eplv10.html
6  */
7 package org.opendaylight.yangtools.yang.parser.impl.util;
8
9 import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.getArgumentString;
10 import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.getFirstContext;
11
12 import java.io.InputStream;
13 import java.util.Date;
14 import java.util.List;
15
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Belongs_to_stmtContext;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Import_stmtContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Include_stmtContext;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_stmtContext;
20 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
21 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtContext;
22 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtsContext;
23 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Submodule_stmtContext;
24 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.YangContext;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28
29 import com.google.common.base.Optional;
30 import com.google.common.collect.ImmutableSet;
31
32 /**
33  * Helper transfer object which holds basic and dependency information for YANG
34  * model.
35  *
36  *
37  *
38  * There are two concrete implementations of this interface:
39  * <ul>
40  * <li>{@link ModuleDependencyInfo} - Dependency information for module</li>
41  * <li>{@link SubmoduleDependencyInfo} - Dependency information for submodule</li>
42  * </ul>
43  *
44  * @see ModuleDependencyInfo
45  * @see SubmoduleDependencyInfo
46  *
47  */
48 public abstract class YangModelDependencyInfo {
49
50     private final String name;
51     private final String formattedRevision;
52     private final Date revision;
53     private final ImmutableSet<ModuleImport> submoduleIncludes;
54     private final ImmutableSet<ModuleImport> moduleImports;
55     private final ImmutableSet<ModuleImport> dependencies;
56
57     protected YangModelDependencyInfo(final String name, final String formattedRevision,
58             final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
59         this.name = name;
60         this.formattedRevision = formattedRevision;
61         this.revision = QName.parseRevision(formattedRevision);
62         this.moduleImports = imports;
63         this.submoduleIncludes = includes;
64         this.dependencies = ImmutableSet.<ModuleImport> builder() //
65                 .addAll(moduleImports) //
66                 .addAll(submoduleIncludes) //
67                 .build();
68     }
69
70     /**
71      * Returns immutable collection of all module imports.
72      *
73      * This collection contains both <code>import</code> statements
74      * and <code>include</code> statements for submodules.
75      *
76      * @return Immutable collection of imports.
77      */
78     public ImmutableSet<ModuleImport> getDependencies() {
79         return dependencies;
80     }
81
82     /**
83      * Returns model name
84      *
85      * @return model name
86      */
87     public String getName() {
88         return name;
89     }
90
91     /**
92      * Returns formatted revision string
93      *
94      * @return formatted revision string
95      */
96     public String getFormattedRevision() {
97         return formattedRevision;
98     }
99
100     /**
101      * Returns revision
102      *
103      * @return revision
104      */
105     public Date getRevision() {
106         return revision;
107     }
108
109     @Override
110     public int hashCode() {
111         final int prime = 31;
112         int result = 1;
113         result = prime * result + ((formattedRevision == null) ? 0 : formattedRevision.hashCode());
114         result = prime * result + ((name == null) ? 0 : name.hashCode());
115         return result;
116     }
117
118     @Override
119     public boolean equals(final Object obj) {
120         if (this == obj) {
121             return true;
122         }
123         if (obj == null) {
124             return false;
125         }
126         if (!(obj instanceof YangModelDependencyInfo)) {
127             return false;
128         }
129         YangModelDependencyInfo other = (YangModelDependencyInfo) obj;
130         if (formattedRevision == null) {
131             if (other.formattedRevision != null) {
132                 return false;
133             }
134         } else if (!formattedRevision.equals(other.formattedRevision)) {
135             return false;
136         }
137         if (name == null) {
138             if (other.name != null) {
139                 return false;
140             }
141         } else if (!name.equals(other.name)) {
142             return false;
143         }
144         return true;
145     }
146
147     /**
148      * Extracts {@link YangModelDependencyInfo} from input stream
149      * containing YANG model.
150      *
151      * This parsing does not validate full YANG module, only
152      * parses header up to the revisions and imports.
153      *
154      * @param yangStream
155      *            Opened Input stream containing text source of YANG model
156      * @return {@link YangModelDependencyInfo}
157      * @throws IllegalArgumentException
158      *             If input stream is not valid YANG stream
159      */
160     public static YangModelDependencyInfo fromInputStream(final InputStream yangStream) {
161         YangContext yangContext = YangParserImpl.parseStreamWithoutErrorListeners(yangStream);
162
163         Optional<Module_stmtContext> moduleCtx = getFirstContext(yangContext, Module_stmtContext.class);
164         if (moduleCtx.isPresent()) {
165             return parseModuleContext(moduleCtx.get());
166         }
167         Optional<Submodule_stmtContext> submoduleCtx = getFirstContext(yangContext, Submodule_stmtContext.class);
168         if (submoduleCtx.isPresent()) {
169             return parseSubmoduleContext(submoduleCtx.get());
170         }
171         throw new IllegalArgumentException("Supplied stream is not valid yang file.");
172     }
173
174     private static YangModelDependencyInfo parseModuleContext(final Module_stmtContext module) {
175         String name = getArgumentString(module);
176         // String prefix =
177         // getArgumentString(module.module_header_stmts().prefix_stmt(0));
178         String namespace = getArgumentString(module.module_header_stmts().namespace_stmt(0));
179         String latestRevision = getLatestRevision(module.revision_stmts());
180         ImmutableSet<ModuleImport> imports = parseImports(module.linkage_stmts().import_stmt());
181         ImmutableSet<ModuleImport> includes = parseIncludes(module.linkage_stmts().include_stmt());
182
183         return new ModuleDependencyInfo(name, latestRevision, namespace, imports, includes);
184     }
185
186     private static ImmutableSet<ModuleImport> parseImports(final List<Import_stmtContext> importStatements) {
187         ImmutableSet.Builder<ModuleImport> builder = ImmutableSet.builder();
188         for (Import_stmtContext importStmt : importStatements) {
189             String moduleName = getArgumentString(importStmt);
190             Date revision = getRevision(importStmt.revision_date_stmt());
191             builder.add(new ModuleImportImpl(moduleName, revision));
192         }
193         return builder.build();
194     }
195
196     private static String getLatestRevision(final Revision_stmtsContext revision_stmts) {
197         List<Revision_stmtContext> revisions = revision_stmts.getRuleContexts(Revision_stmtContext.class);
198         String latestRevision = null;
199         for (Revision_stmtContext revisionStmt : revisions) {
200             String currentRevision = getArgumentString(revisionStmt);
201             if (latestRevision == null || latestRevision.compareTo(currentRevision) == -1) {
202                 latestRevision = currentRevision;
203             }
204         }
205         return latestRevision;
206     }
207
208     private static YangModelDependencyInfo parseSubmoduleContext(final Submodule_stmtContext submodule) {
209         String name = getArgumentString(submodule);
210         Belongs_to_stmtContext belongsToStmt = submodule.submodule_header_stmts().belongs_to_stmt(0);
211         String belongsTo = getArgumentString(belongsToStmt);
212
213         String latestRevision = getLatestRevision(submodule.revision_stmts());
214         ImmutableSet<ModuleImport> imports = parseImports(submodule.linkage_stmts().import_stmt());
215         ImmutableSet<ModuleImport> includes = parseIncludes(submodule.linkage_stmts().include_stmt());
216
217         return new SubmoduleDependencyInfo(name, latestRevision, belongsTo, imports, includes);
218     }
219
220     private static ImmutableSet<ModuleImport> parseIncludes(final List<Include_stmtContext> importStatements) {
221         ImmutableSet.Builder<ModuleImport> builder = ImmutableSet.builder();
222         for (Include_stmtContext importStmt : importStatements) {
223             String moduleName = getArgumentString(importStmt);
224             Date revision = getRevision(importStmt.revision_date_stmt());
225             builder.add(new ModuleImportImpl(moduleName, revision));
226         }
227         return builder.build();
228     }
229
230     private static Date getRevision(final Revision_date_stmtContext revision_date_stmt) {
231         if (revision_date_stmt == null) {
232             return null;
233         }
234         String formatedDate = getArgumentString(revision_date_stmt);
235         return QName.parseRevision(formatedDate);
236     }
237
238     /**
239      *
240      * Dependency information for YANG module.
241      *
242      */
243     public static final class ModuleDependencyInfo extends YangModelDependencyInfo {
244
245         private ModuleDependencyInfo(final String name, final String latestRevision, final String namespace,
246                 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
247             super(name, latestRevision, imports, includes);
248         }
249
250         @Override
251         public String toString() {
252             return "Module [name=" + getName() + ", revision=" + getRevision() + ", dependencies=" + getDependencies()
253                     + "]";
254         }
255     }
256
257     /**
258      *
259      * Dependency information for submodule, also provides name
260      * for parent module.
261      *
262      */
263     public static final class SubmoduleDependencyInfo extends YangModelDependencyInfo {
264
265         private final String belongsTo;
266
267         /**
268          * Returns name of parent module.
269          *
270          */
271         public String getParentModule() {
272             return belongsTo;
273         }
274
275         private SubmoduleDependencyInfo(final String name, final String latestRevision, final String belongsTo,
276                 final ImmutableSet<ModuleImport> imports, final ImmutableSet<ModuleImport> includes) {
277             super(name, latestRevision, imports, includes);
278             this.belongsTo = belongsTo;
279         }
280
281         @Override
282         public String toString() {
283             return "Submodule [name=" + getName() + ", revision=" + getRevision() + ", dependencies="
284                     + getDependencies() + "]";
285         }
286     }
287
288     /**
289      * Utility implementation of {@link ModuleImport} to be used by
290      * {@link YangModelDependencyInfo}.
291      *
292      */
293     private static final class ModuleImportImpl implements ModuleImport {
294
295         private final Date revision;
296         private final String name;
297
298         public ModuleImportImpl(final String moduleName, final Date revision) {
299             this.name = moduleName;
300             this.revision = revision;
301         }
302
303         @Override
304         public String getModuleName() {
305             return this.name;
306         }
307
308         @Override
309         public Date getRevision() {
310             return this.revision;
311         }
312
313         @Override
314         public String getPrefix() {
315             return null;
316         }
317
318         @Override
319         public int hashCode() {
320             final int prime = 31;
321             int result = 1;
322             result = prime * result + ((name == null) ? 0 : name.hashCode());
323             result = prime * result + ((revision == null) ? 0 : revision.hashCode());
324             return result;
325         }
326
327         @Override
328         public boolean equals(final Object obj) {
329             if (this == obj) {
330                 return true;
331             }
332             if (obj == null) {
333                 return false;
334             }
335             if (getClass() != obj.getClass()) {
336                 return false;
337             }
338             ModuleImportImpl other = (ModuleImportImpl) obj;
339             if (name == null) {
340                 if (other.name != null) {
341                     return false;
342                 }
343             } else if (!name.equals(other.name)) {
344                 return false;
345             }
346             if (revision == null) {
347                 if (other.revision != null) {
348                     return false;
349                 }
350             } else if (!revision.equals(other.revision)) {
351                 return false;
352             }
353             return true;
354         }
355
356         @Override
357         public String toString() {
358             return "ModuleImportImpl [name=" + name + ", revision=" + QName.formattedRevision(revision) + "]";
359         }
360     }
361 }