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.collect.ImmutableMap;
11 import com.google.common.io.ByteSource;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.EnumMap;
19 import java.util.HashMap;
20 import java.util.List;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
30 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
33 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
35 public class CrossSourceStatementReactor {
37 private final Map<ModelProcessingPhase,StatementSupportBundle> supportedTerminology;
38 private final Map<ValidationBundleType,Collection<?>> supportedValidation;
40 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
41 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
42 this.supportedValidation = ImmutableMap.of();
45 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology, final Map<ValidationBundleType,Collection<?>> supportedValidation) {
46 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
47 this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
50 public static Builder builder() {
54 public final BuildAction newBuild() {
55 return new BuildAction();
58 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor>{
60 final Map<ModelProcessingPhase,StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
61 final Map<ValidationBundleType,Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
63 public Builder setBundle(final ModelProcessingPhase phase,final StatementSupportBundle bundle) {
64 bundles.put(phase, bundle);
69 public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
70 validationBundles.put(type,validationBundle);
75 public CrossSourceStatementReactor build() {
76 return new CrossSourceStatementReactor(bundles, validationBundles);
80 public class BuildAction {
81 private final BuildGlobalContext context;
83 public BuildAction() {
84 this.context = new BuildGlobalContext(supportedTerminology, supportedValidation);
87 public void addSource(final StatementStreamSource source) {
88 context.addSource(source);
91 public void addSources(final StatementStreamSource... sources) {
92 for (StatementStreamSource source : sources) {
93 context.addSource(source);
97 public EffectiveModelContext build() throws SourceException, ReactorException {
98 return context.build();
101 public EffectiveSchemaContext buildEffective() throws SourceException, ReactorException {
102 return context.buildEffective();
105 public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws SourceException,
106 ReactorException, IOException {
107 for (ByteSource yangByteSource : yangByteSources) {
108 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
111 return buildEffective();
114 public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws SourceException,
116 for (InputStream yangInputStream : yangInputStreams) {
117 addSource(new YangStatementSourceImpl(yangInputStream));
120 return buildEffective();
124 * @deprecated This method was never used and relies on deprecated module methods.
127 public Map<File, Module> buildEffectiveMappedToSource(final List<File> yangFiles) throws SourceException,
128 ReactorException, FileNotFoundException {
129 if (yangFiles == null || yangFiles.isEmpty()) {
130 return Collections.emptyMap();
133 Map<String, File> pathToFile = new HashMap<>();
134 Map<File, Module> sourceFileToModule = new HashMap<>();
136 for (File yangFile : yangFiles) {
137 addSource(new YangStatementSourceImpl(new NamedFileInputStream(yangFile, yangFile.getPath())));
138 pathToFile.put(yangFile.getPath(), yangFile);
141 EffectiveSchemaContext schema = buildEffective();
142 Set<Module> modules = schema.getModules();
143 for (Module module : modules) {
144 sourceFileToModule.put(pathToFile.get(module.getModuleSourcePath()), module);
147 return sourceFileToModule;