Use on-demand component activation
[yangtools.git] / parser / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / DefaultYangParser.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, 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.parser.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.SetMultimap;
14 import java.io.IOException;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Set;
18 import javax.xml.transform.TransformerException;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26 import org.opendaylight.yangtools.yang.model.repo.api.YinDomSchemaSource;
27 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
28 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.api.YangParser;
30 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
31 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
39 import org.xml.sax.SAXException;
40
41 final class DefaultYangParser implements YangParser {
42     private static final @NonNull Collection<Class<? extends SchemaSourceRepresentation>> REPRESENTATIONS =
43             ImmutableList.of(IRSchemaSource.class, YangTextSchemaSource.class, YinDomSchemaSource.class,
44                 YinXmlSchemaSource.class, YinTextSchemaSource.class);
45
46     private final BuildAction buildAction;
47
48     DefaultYangParser(final BuildAction buildAction) {
49         this.buildAction = requireNonNull(buildAction);
50     }
51
52     @Override
53     public @NonNull Collection<Class<? extends SchemaSourceRepresentation>> supportedSourceRepresentations() {
54         return REPRESENTATIONS;
55     }
56
57     @Override
58     public @NonNull YangParser addSource(final SchemaSourceRepresentation source) throws IOException,
59             YangSyntaxErrorException {
60         buildAction.addSource(sourceToStatementStream(source));
61         return this;
62     }
63
64     @Override
65     public @NonNull YangParser addLibSource(final SchemaSourceRepresentation source) throws IOException,
66             YangSyntaxErrorException {
67         buildAction.addLibSource(sourceToStatementStream(source));
68         return this;
69     }
70
71     @Override
72     public @NonNull YangParser setSupportedFeatures(final Set<QName> supportedFeatures) {
73         buildAction.setSupportedFeatures(supportedFeatures);
74         return this;
75     }
76
77     @Override
78     public @NonNull YangParser setModulesWithSupportedDeviations(
79             final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
80         buildAction.setModulesWithSupportedDeviations(modulesDeviatedByModules);
81         return this;
82     }
83
84     @Override
85     public @NonNull List<DeclaredStatement<?>> buildDeclaredModel() throws YangParserException {
86         try {
87             return buildAction.build().getRootStatements();
88         } catch (ReactorException e) {
89             throw decodeReactorException(e);
90         }
91     }
92
93     @Override
94     public @NonNull EffectiveModelContext buildEffectiveModel() throws YangParserException {
95         try {
96             return buildAction.buildEffective();
97         } catch (ReactorException e) {
98             throw decodeReactorException(e);
99         }
100     }
101
102     private static YangParserException decodeReactorException(final ReactorException reported) {
103         // FIXME: map exception in some reasonable manner
104         return new YangParserException("Failed to assemble sources", reported);
105     }
106
107     private static StatementStreamSource sourceToStatementStream(final SchemaSourceRepresentation source)
108             throws IOException, YangSyntaxErrorException {
109         requireNonNull(source);
110         if (source instanceof IRSchemaSource) {
111             return YangStatementStreamSource.create((IRSchemaSource) source);
112         } else if (source instanceof YangTextSchemaSource) {
113             return YangStatementStreamSource.create((YangTextSchemaSource) source);
114         } else if (source instanceof YinDomSchemaSource) {
115             return YinStatementStreamSource.create((YinDomSchemaSource) source);
116         } else if (source instanceof YinTextSchemaSource) {
117             try {
118                 return YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
119                     (YinTextSchemaSource) source));
120             } catch (SAXException e) {
121                 throw new YangSyntaxErrorException(source.getIdentifier(), 0, 0, "Failed to parse XML text", e);
122             }
123         } else if (source instanceof YinXmlSchemaSource) {
124             try {
125                 return YinStatementStreamSource.create((YinXmlSchemaSource) source);
126             } catch (TransformerException e) {
127                 throw new YangSyntaxErrorException(source.getIdentifier(), 0, 0,
128                     "Failed to assemble in-memory representation", e);
129             }
130         } else {
131             throw new IllegalArgumentException("Unsupported source " + source);
132         }
133     }
134 }