2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.reactor;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.io.ByteSource;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.EnumMap;
20 import java.util.HashMap;
21 import java.util.List;
24 import java.util.function.Predicate;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
31 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
32 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
35 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
37 public class CrossSourceStatementReactor {
39 private final Map<ModelProcessingPhase,StatementSupportBundle> supportedTerminology;
40 private final Map<ValidationBundleType,Collection<?>> supportedValidation;
42 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
43 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
44 this.supportedValidation = ImmutableMap.of();
47 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology, final Map<ValidationBundleType,Collection<?>> supportedValidation) {
48 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
49 this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
52 public static Builder builder() {
56 public final BuildAction newBuild() {
57 return newBuild(t -> true);
60 public final BuildAction newBuild(final Predicate<QName> isFeatureSupported) {
61 return new BuildAction(isFeatureSupported);
64 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor>{
66 final Map<ModelProcessingPhase,StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
67 final Map<ValidationBundleType,Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
69 public Builder setBundle(final ModelProcessingPhase phase,final StatementSupportBundle bundle) {
70 bundles.put(phase, bundle);
75 public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
76 validationBundles.put(type,validationBundle);
81 public CrossSourceStatementReactor build() {
82 return new CrossSourceStatementReactor(bundles, validationBundles);
86 public class BuildAction {
87 private final BuildGlobalContext context;
89 public BuildAction() {
93 public BuildAction(Predicate<QName> isFeatureSupported) {
94 Preconditions.checkNotNull(isFeatureSupported);
95 this.context = new BuildGlobalContext(supportedTerminology, supportedValidation, isFeatureSupported);
98 public void addSource(final StatementStreamSource source) {
99 context.addSource(source);
102 public void addSources(final StatementStreamSource... sources) {
103 for (StatementStreamSource source : sources) {
104 context.addSource(source);
109 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
110 * @throws ReactorException
112 public EffectiveModelContext build() throws ReactorException {
113 return context.build();
116 public EffectiveSchemaContext buildEffective() throws ReactorException {
117 return context.buildEffective();
120 public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws
121 ReactorException, IOException {
122 for (ByteSource yangByteSource : yangByteSources) {
123 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
126 return buildEffective();
129 public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws
131 for (InputStream yangInputStream : yangInputStreams) {
132 addSource(new YangStatementSourceImpl(yangInputStream));
135 return buildEffective();
139 * @deprecated This method was never used and relies on deprecated module methods.
142 public Map<File, Module> buildEffectiveMappedToSource(final List<File> yangFiles) throws
143 ReactorException, FileNotFoundException {
144 if (yangFiles == null || yangFiles.isEmpty()) {
145 return Collections.emptyMap();
148 Map<String, File> pathToFile = new HashMap<>();
149 Map<File, Module> sourceFileToModule = new HashMap<>();
151 for (File yangFile : yangFiles) {
152 addSource(new YangStatementSourceImpl(new NamedFileInputStream(yangFile, yangFile.getPath())));
153 pathToFile.put(yangFile.getPath(), yangFile);
156 EffectiveSchemaContext schema = buildEffective();
157 Set<Module> modules = schema.getModules();
158 for (Module module : modules) {
159 sourceFileToModule.put(pathToFile.get(module.getModuleSourcePath()), module);
162 return sourceFileToModule;