2 * Copyright (c) 2017 Pantheon Technologies, 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.parser.api.YangParser;
25 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
26 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.model.repo.api.YinDomSchemaSource;
30 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
31 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
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 YangParserImpl implements YangParser {
42 private static final @NonNull Collection<Class<? extends SchemaSourceRepresentation>> REPRESENTATIONS =
43 ImmutableList.of(ASTSchemaSource.class, YangTextSchemaSource.class,
44 YinDomSchemaSource.class, YinXmlSchemaSource.class, YinTextSchemaSource.class);
46 private final BuildAction buildAction;
48 YangParserImpl(final BuildAction buildAction) {
49 this.buildAction = requireNonNull(buildAction);
53 public @NonNull Collection<Class<? extends SchemaSourceRepresentation>> supportedSourceRepresentations() {
54 return REPRESENTATIONS;
58 public @NonNull Set<QName> supportedStatements() {
59 // TODO Auto-generated method stub
64 public @NonNull YangParser addSource(final SchemaSourceRepresentation source) throws IOException,
65 YangSyntaxErrorException {
66 buildAction.addSources(sourceToStatementStream(source));
71 public @NonNull YangParser addLibSource(final SchemaSourceRepresentation source) throws IOException,
72 YangSyntaxErrorException {
73 buildAction.addLibSources(sourceToStatementStream(source));
78 public @NonNull YangParser setSupportedFeatures(final Set<QName> supportedFeatures) {
79 buildAction.setSupportedFeatures(supportedFeatures);
84 public @NonNull YangParser setModulesWithSupportedDeviations(
85 final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
86 buildAction.setModulesWithSupportedDeviations(modulesDeviatedByModules);
91 public @NonNull List<DeclaredStatement<?>> buildDeclaredModel() throws YangParserException {
93 return buildAction.build().getRootStatements();
94 } catch (ReactorException e) {
95 throw decodeReactorException(e);
100 public @NonNull EffectiveModelContext buildEffectiveModel() throws YangParserException {
102 return buildAction.buildEffective();
103 } catch (ReactorException e) {
104 throw decodeReactorException(e);
108 private static YangParserException decodeReactorException(final ReactorException reported) {
109 // FIXME: map exception in some reasonable manner
110 return new YangParserException("Failed to assemble sources", reported);
113 private static StatementStreamSource sourceToStatementStream(final SchemaSourceRepresentation source)
114 throws IOException, YangSyntaxErrorException {
115 requireNonNull(source);
116 if (source instanceof ASTSchemaSource) {
117 return YangStatementStreamSource.create((ASTSchemaSource) source);
118 } else if (source instanceof YangTextSchemaSource) {
119 return YangStatementStreamSource.create((YangTextSchemaSource) source);
120 } else if (source instanceof YinDomSchemaSource) {
121 return YinStatementStreamSource.create((YinDomSchemaSource) source);
122 } else if (source instanceof YinTextSchemaSource) {
124 return YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
125 (YinTextSchemaSource) source));
126 } catch (SAXException e) {
127 throw new YangSyntaxErrorException(source.getIdentifier(), 0, 0, "Failed to parse XML text", e);
129 } else if (source instanceof YinXmlSchemaSource) {
131 return YinStatementStreamSource.create((YinXmlSchemaSource) source);
132 } catch (TransformerException e) {
133 throw new YangSyntaxErrorException(source.getIdentifier(), 0, 0,
134 "Failed to assemble in-memory representation", e);
137 throw new IllegalArgumentException("Unsupported source " + source);