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 com.google.common.collect.ImmutableMap.toImmutableMap;
11 import static java.util.Objects.requireNonNull;
12 import static java.util.function.Function.identity;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.SetMultimap;
16 import java.io.IOException;
17 import java.util.Collection;
18 import java.util.List;
21 import javax.xml.transform.TransformerException;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
30 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.model.repo.api.YinDomSchemaSource;
34 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
35 import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
39 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
42 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
43 import org.xml.sax.SAXException;
45 final class YangParserImpl implements YangParser {
46 private static final Collection<Class<? extends SchemaSourceRepresentation>> REPRESENTATIONS = ImmutableList.of(
47 ASTSchemaSource.class, YangTextSchemaSource.class, YinDomSchemaSource.class, YinXmlSchemaSource.class,
48 YinTextSchemaSource.class);
50 private final BuildAction buildAction;
52 YangParserImpl(final BuildAction buildAction) {
53 this.buildAction = requireNonNull(buildAction);
57 public Collection<Class<? extends SchemaSourceRepresentation>> supportedSourceRepresentations() {
58 return REPRESENTATIONS;
62 public Set<QName> supportedStatements() {
63 // TODO Auto-generated method stub
68 public YangParser addSource(final SchemaSourceRepresentation source) throws IOException, YangSyntaxErrorException {
69 buildAction.addSources(sourceToStatementStream(source));
74 public YangParser addLibSource(final SchemaSourceRepresentation source) throws IOException,
75 YangSyntaxErrorException {
76 buildAction.addLibSources(sourceToStatementStream(source));
81 public YangParser setSupportedFeatures(final Set<QName> supportedFeatures) {
82 buildAction.setSupportedFeatures(supportedFeatures);
87 public YangParser setModulesWithSupportedDeviations(
88 final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
89 buildAction.setModulesWithSupportedDeviations(modulesDeviatedByModules);
94 public List<DeclaredStatement<?>> buildDeclaredModel() throws YangParserException {
96 return buildAction.build().getRootStatements();
97 } catch (ReactorException e) {
98 throw decodeReactorException(e);
103 public Map<QNameModule, ModuleEffectiveStatement> buildEffectiveModel() throws YangParserException {
104 final List<EffectiveStatement<?, ?>> effectiveStatements;
106 effectiveStatements = buildAction.buildEffective().getRootEffectiveStatements();
107 } catch (ReactorException e) {
108 throw decodeReactorException(e);
111 return effectiveStatements.stream()
112 .filter(ModuleEffectiveStatement.class::isInstance).map(ModuleEffectiveStatement.class::cast)
113 .collect(toImmutableMap(ModuleEffectiveStatement::localQNameModule, identity()));
117 public SchemaContext buildSchemaContext() throws YangParserException {
119 return buildAction.buildEffective();
120 } catch (ReactorException e) {
121 throw decodeReactorException(e);
125 private static YangParserException decodeReactorException(final ReactorException reported) {
126 // FIXME: map exception in some reasonable manner
127 return new YangParserException("Failed to assemble sources", reported);
130 private static StatementStreamSource sourceToStatementStream(final SchemaSourceRepresentation source)
131 throws IOException, YangSyntaxErrorException {
132 requireNonNull(source);
133 if (source instanceof ASTSchemaSource) {
134 return YangStatementStreamSource.create((ASTSchemaSource) source);
135 } else if (source instanceof YangTextSchemaSource) {
136 return YangStatementStreamSource.create((YangTextSchemaSource) source);
137 } else if (source instanceof YinDomSchemaSource) {
138 return YinStatementStreamSource.create((YinDomSchemaSource) source);
139 } else if (source instanceof YinTextSchemaSource) {
141 return YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
142 (YinTextSchemaSource) source));
143 } catch (SAXException e) {
144 throw new YangSyntaxErrorException(source.getIdentifier(), 0, 0, "Failed to parse XML text", e);
146 } else if (source instanceof YinXmlSchemaSource) {
148 return YinStatementStreamSource.create((YinXmlSchemaSource) source);
149 } catch (TransformerException e) {
150 throw new YangSyntaxErrorException(source.getIdentifier(), 0, 0,
151 "Failed to assemble in-memory representation", e);
154 throw new IllegalArgumentException("Unsupported source " + source);