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.StatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
32 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
34 public class CrossSourceStatementReactor {
36 private final Map<ModelProcessingPhase,StatementSupportBundle> supportedTerminology;
37 private final Map<ValidationBundleType,Collection<?>> supportedValidation;
39 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
40 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
41 this.supportedValidation = ImmutableMap.of();
44 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology, final Map<ValidationBundleType,Collection<?>> supportedValidation) {
45 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
46 this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
49 public static Builder builder() {
53 public final BuildAction newBuild() {
54 return new BuildAction();
57 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor>{
59 final Map<ModelProcessingPhase,StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
60 final Map<ValidationBundleType,Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
62 public Builder setBundle(final ModelProcessingPhase phase,final StatementSupportBundle bundle) {
63 bundles.put(phase, bundle);
68 public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
69 validationBundles.put(type,validationBundle);
74 public CrossSourceStatementReactor build() {
75 return new CrossSourceStatementReactor(bundles, validationBundles);
79 public class BuildAction {
80 private final BuildGlobalContext context;
82 public BuildAction() {
83 this.context = new BuildGlobalContext(supportedTerminology, supportedValidation);
86 public void addSource(final StatementStreamSource source) {
87 context.addSource(source);
90 public void addSources(final StatementStreamSource... sources) {
91 for (StatementStreamSource source : sources) {
92 context.addSource(source);
97 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
98 * @throws ReactorException
100 public EffectiveModelContext build() throws ReactorException {
101 return context.build();
104 public EffectiveSchemaContext buildEffective() throws ReactorException {
105 return context.buildEffective();
108 public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws
109 ReactorException, IOException {
110 for (ByteSource yangByteSource : yangByteSources) {
111 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
114 return buildEffective();
117 public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws
119 for (InputStream yangInputStream : yangInputStreams) {
120 addSource(new YangStatementSourceImpl(yangInputStream));
123 return buildEffective();
127 * @deprecated This method was never used and relies on deprecated module methods.
130 public Map<File, Module> buildEffectiveMappedToSource(final List<File> yangFiles) throws
131 ReactorException, 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(yangFile, yangFile.getPath())));
141 pathToFile.put(yangFile.getPath(), yangFile);
144 EffectiveSchemaContext schema = buildEffective();
145 Set<Module> modules = schema.getModules();
146 for (Module module : modules) {
147 sourceFileToModule.put(pathToFile.get(module.getModuleSourcePath()), module);
150 return sourceFileToModule;