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