bbfb37e77f1d62606a7aa2cae070df4aada1ce7a
[yangtools.git] / yang / yang-parser-api / src / main / java / org / opendaylight / yangtools / yang / model / parser / api / YangParser.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.model.parser.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.SetMultimap;
12 import java.io.IOException;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Set;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
23
24 /**
25  * Configurable single-use YANG parser. Each instance can be configured to use a different set of models after
26  * which it is built. Models once added cannot be removed. Implementations are expected to be NOT thread-safe.
27  *
28  * @author Robert Varga
29  */
30 @Beta
31 public interface YangParser {
32     /**
33      * Return enumeration of concrete types of {@link SchemaSourceRepresentation} parsers created from this factory
34      * support. Users can use this information prepare the source they have to a representation which will be accepted
35      * by this parser.
36      *
37      * @return Enumeration of supported schema source representations.
38      */
39     @NonNull Collection<Class<? extends SchemaSourceRepresentation>> supportedSourceRepresentations();
40
41     /**
42      * Return the set of all YANG statements semantically supported by this parser instance.
43      *
44      * @return Set of all YANG statements semantically supported by this parser instance.
45      */
46     @NonNull Set<QName> supportedStatements();
47
48     /**
49      * Add main source. All main sources are present in resulting SchemaContext.
50      *
51      * @param source which should be added into main sources
52      * @throws YangSyntaxErrorException when one of the sources fails syntactic analysis
53      * @throws IOException when an IO error occurs
54      * @throws IllegalArgumentException if the representation is not supported
55      */
56     @NonNull YangParser addSource(SchemaSourceRepresentation source) throws IOException, YangSyntaxErrorException;
57
58     /**
59      * Add main sources. All main sources are present in resulting SchemaContext.
60      *
61      * @param sources which should be added into main sources
62      * @throws YangSyntaxErrorException when one of the sources fails syntactic analysis
63      * @throws IOException when an IO error occurs
64      * @throws IllegalArgumentException if the representation is not supported
65      */
66     default @NonNull YangParser addSources(final SchemaSourceRepresentation... sources) throws IOException,
67         YangSyntaxErrorException {
68         for (SchemaSourceRepresentation source : sources) {
69             addSource(source);
70         }
71         return this;
72     }
73
74     default @NonNull YangParser addSources(final Collection<? extends SchemaSourceRepresentation> sources)
75             throws IOException, YangSyntaxErrorException {
76         for (SchemaSourceRepresentation source : sources) {
77             addSource(source);
78         }
79         return this;
80     }
81
82     YangParser addLibSource(SchemaSourceRepresentation source) throws IOException, YangSyntaxErrorException;
83
84     /**
85      * Add library sources. Only library sources required by main sources are present in resulting SchemaContext.
86      * Any other library sources are ignored and this also applies to error reporting.
87      *
88      * <p>
89      * Note: Library sources are not supported in semantic version mode currently.
90      *
91      * @param sources YANG sources which should be added into library sources
92      * @throws YangSyntaxErrorException when one of the sources fails syntactic analysis
93      * @throws IOException when an IO error occurs
94      * @throws IllegalArgumentException if the representation is not supported
95      */
96     default @NonNull YangParser addLibSources(final SchemaSourceRepresentation... sources) throws IOException,
97             YangSyntaxErrorException {
98         for (SchemaSourceRepresentation source : sources) {
99             addLibSource(source);
100         }
101         return this;
102     }
103
104     default @NonNull YangParser addLibSources(final Collection<SchemaSourceRepresentation> sources) throws IOException,
105             YangSyntaxErrorException {
106         for (SchemaSourceRepresentation source : sources) {
107             addLibSource(source);
108         }
109         return this;
110     }
111
112     /**
113      * Set supported features based on which all if-feature statements in the parsed YANG modules will be resolved. If
114      * this method is not invoked, all features will be supported.
115      *
116      * @param supportedFeatures Set of supported features in the final SchemaContext. If the set is empty, no features
117      *                          encountered will be supported.
118      */
119     @NonNull YangParser setSupportedFeatures(@NonNull Set<QName> supportedFeatures);
120
121     /**
122      * Set YANG modules which can be deviated by specified modules during the parsing process. Map key (QNameModule)
123      * denotes a module which can be deviated by the modules in the Map value.
124      *
125      * @param modulesDeviatedByModules Map of YANG modules (Map key) which can be deviated by specified modules (Map
126      *                                 value) in the final SchemaContext. If the map is empty, no deviations encountered
127      *                                 will be supported.
128      */
129     @NonNull YangParser setModulesWithSupportedDeviations(
130             @NonNull SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules);
131
132     /**
133      * Build the declared view of a combined view of declared statements.
134      *
135      * @return Ordered collection of declared statements from requested sources.
136      * @throws YangSyntaxErrorException When a syntactic error is encountered.
137      */
138     @NonNull List<DeclaredStatement<?>> buildDeclaredModel() throws YangParserException;
139
140     /**
141      * Build the effective view of a combined view of effective statements. Note that this representation, unlike
142      * {@link #buildDeclaredModel()} does not expose submodules as top-level contracts. These are available from their
143      * respective parent modules.
144      *
145      * @return Effective module statements indexed by their QNameModule.
146      * @throws YangSyntaxErrorException When a syntactic error is encountered.
147      */
148     @NonNull EffectiveModelContext buildEffectiveModel() throws YangParserException;
149
150     /**
151      * Build effective {@link SchemaContext}.
152      *
153      * @return An effective schema context comprised of configured models.
154      * @throws YangSyntaxErrorException When a syntactic error is encountered.
155      */
156     default @NonNull SchemaContext buildSchemaContext() throws YangParserException {
157         return buildEffectiveModel();
158     }
159 }