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 java.io.IOException;
12 import com.google.common.io.ByteSource;
14 import java.io.FileNotFoundException;
15 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
16 import java.util.HashMap;
17 import java.util.Collections;
18 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import java.io.InputStream;
23 import java.util.List;
24 import java.util.Collection;
25 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
27 import com.google.common.collect.ImmutableMap;
28 import java.util.EnumMap;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
36 public class CrossSourceStatementReactor {
38 private final Map<ModelProcessingPhase,StatementSupportBundle> supportedTerminology;
39 private final Map<ValidationBundleType,Collection<?>> supportedValidation;
41 CrossSourceStatementReactor(Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
42 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
43 this.supportedValidation = ImmutableMap.of();
46 CrossSourceStatementReactor(Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology, Map<ValidationBundleType,Collection<?>> supportedValidation) {
47 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
48 this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
51 public static final Builder builder() {
55 public final BuildAction newBuild() {
56 return new BuildAction();
59 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor>{
61 final Map<ModelProcessingPhase,StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
62 final Map<ValidationBundleType,Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
64 public Builder setBundle(ModelProcessingPhase phase,StatementSupportBundle bundle) {
65 bundles.put(phase, bundle);
70 public Builder setValidationBundle(
71 ValidationBundleType type,
72 Collection<?> validationBundle) {
73 validationBundles.put(type,validationBundle);
78 public CrossSourceStatementReactor build() {
79 return new CrossSourceStatementReactor(bundles, validationBundles);
84 public class BuildAction {
86 private final BuildGlobalContext context;
88 public BuildAction() {
89 this.context = new BuildGlobalContext(supportedTerminology, supportedValidation);
92 public void addSource(StatementStreamSource source) {
93 context.addSource(source);
96 public void addSources(StatementStreamSource... sources) {
97 for (StatementStreamSource source : sources) {
98 context.addSource(source);
102 public EffectiveModelContext build() throws SourceException, ReactorException {
103 return context.build();
106 public EffectiveSchemaContext buildEffective() throws SourceException, ReactorException {
107 return context.buildEffective();
110 public SchemaContext buildEffective(Collection<ByteSource> yangByteSources) throws SourceException, ReactorException, IOException {
112 for(ByteSource yangByteSource : yangByteSources) {
113 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
116 return buildEffective();
119 public SchemaContext buildEffective(List<InputStream> yangInputStreams) throws SourceException, ReactorException {
121 for(InputStream yangInputStream : yangInputStreams) {
122 addSource(new YangStatementSourceImpl(yangInputStream));
125 return buildEffective();
128 public Map<File, Module> buildEffectiveMappedToSource(
129 List<File> yangFiles) throws SourceException, ReactorException,
130 FileNotFoundException {
132 if (yangFiles == null || yangFiles.isEmpty()) {
133 return Collections.emptyMap();
136 Map<String, File> pathToFile = new HashMap<>();
137 Map<File, Module> sourceFileToModule = new HashMap<>();
139 for (File yangFile : yangFiles) {
140 addSource(new YangStatementSourceImpl(new NamedFileInputStream(
141 yangFile, yangFile.getPath())));
142 pathToFile.put(yangFile.getPath(), yangFile);
145 EffectiveSchemaContext schema = buildEffective();
146 Set<Module> modules = schema.getModules();
147 for (Module module : modules) {
148 sourceFileToModule.put(
149 pathToFile.get(module.getModuleSourcePath()), module);
152 return sourceFileToModule;