2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.impl;
10 import static java.util.Objects.requireNonNull;
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;
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.YangIRSchemaSource;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import org.opendaylight.yangtools.yang.model.repo.api.YinDomSchemaSource;
28 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
29 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.api.YangParser;
31 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
32 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
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;
41 final class DefaultYangParser implements YangParser {
42 static final @NonNull Collection<Class<? extends SchemaSourceRepresentation>> REPRESENTATIONS =
43 ImmutableList.of(YangIRSchemaSource.class, YangTextSchemaSource.class, YinDomSchemaSource.class,
44 YinXmlSchemaSource.class, YinTextSchemaSource.class);
46 private final BuildAction buildAction;
48 DefaultYangParser(final BuildAction buildAction) {
49 this.buildAction = requireNonNull(buildAction);
53 public @NonNull Collection<Class<? extends SchemaSourceRepresentation>> supportedSourceRepresentations() {
54 return REPRESENTATIONS;
58 public @NonNull YangParser addSource(final SchemaSourceRepresentation source) throws IOException,
59 YangSyntaxErrorException {
60 buildAction.addSource(sourceToStatementStream(source));
65 public @NonNull YangParser addLibSource(final SchemaSourceRepresentation source) throws IOException,
66 YangSyntaxErrorException {
67 buildAction.addLibSource(sourceToStatementStream(source));
72 public @NonNull YangParser setSupportedFeatures(final Set<QName> supportedFeatures) {
73 buildAction.setSupportedFeatures(supportedFeatures);
78 public @NonNull YangParser setModulesWithSupportedDeviations(
79 final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
80 buildAction.setModulesWithSupportedDeviations(modulesDeviatedByModules);
85 public @NonNull List<DeclaredStatement<?>> buildDeclaredModel() throws YangParserException {
87 return buildAction.build().getRootStatements();
88 } catch (ReactorException e) {
89 throw decodeReactorException(e);
94 public @NonNull EffectiveModelContext buildEffectiveModel() throws YangParserException {
96 return buildAction.buildEffective();
97 } catch (ReactorException e) {
98 throw decodeReactorException(e);
102 static YangParserException decodeReactorException(final ReactorException reported) {
103 // FIXME: map exception in some reasonable manner
104 return new YangParserException("Failed to assemble sources", reported);
107 static StatementStreamSource sourceToStatementStream(final SchemaSourceRepresentation source)
108 throws IOException, YangSyntaxErrorException {
109 requireNonNull(source);
110 if (source instanceof YangIRSchemaSource) {
111 return YangStatementStreamSource.create((YangIRSchemaSource) 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) {
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);
123 } else if (source instanceof YinXmlSchemaSource) {
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);
131 throw new IllegalArgumentException("Unsupported source " + source);