BUG-865: deprecate SourceStreamAware
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / CrossSourceStatementReactor.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.io.ByteSource;
12 import java.io.File;
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;
21 import java.util.Map;
22 import java.util.Set;
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;
34
35 public class CrossSourceStatementReactor {
36
37     private final Map<ModelProcessingPhase,StatementSupportBundle> supportedTerminology;
38     private final Map<ValidationBundleType,Collection<?>> supportedValidation;
39
40     CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
41         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
42         this.supportedValidation = ImmutableMap.of();
43     }
44
45     CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology, final Map<ValidationBundleType,Collection<?>> supportedValidation) {
46         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
47         this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
48     }
49
50     public static final Builder builder() {
51         return new Builder();
52     }
53
54     public final BuildAction newBuild() {
55         return new BuildAction();
56     }
57
58     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor>{
59
60         final Map<ModelProcessingPhase,StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
61         final Map<ValidationBundleType,Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
62
63         public Builder setBundle(final ModelProcessingPhase phase,final StatementSupportBundle bundle) {
64             bundles.put(phase, bundle);
65             return this;
66         }
67
68
69         public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
70             validationBundles.put(type,validationBundle);
71             return this;
72         }
73
74         @Override
75         public CrossSourceStatementReactor build() {
76             return new CrossSourceStatementReactor(bundles, validationBundles);
77         }
78     }
79
80     public class BuildAction {
81         private final BuildGlobalContext context;
82
83         public BuildAction() {
84             this.context = new BuildGlobalContext(supportedTerminology, supportedValidation);
85         }
86
87         public void addSource(final StatementStreamSource source) {
88             context.addSource(source);
89         }
90
91         public void addSources(final StatementStreamSource... sources) {
92             for (StatementStreamSource source : sources) {
93                 context.addSource(source);
94             }
95         }
96
97         public EffectiveModelContext build() throws SourceException, ReactorException {
98             return context.build();
99         }
100
101         public EffectiveSchemaContext buildEffective() throws SourceException, ReactorException {
102             return context.buildEffective();
103         }
104
105         public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws SourceException,
106                 ReactorException, IOException {
107             for (ByteSource yangByteSource : yangByteSources) {
108                 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
109             }
110
111             return buildEffective();
112         }
113
114         public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws SourceException,
115                 ReactorException {
116             for (InputStream yangInputStream : yangInputStreams) {
117                 addSource(new YangStatementSourceImpl(yangInputStream));
118             }
119
120             return buildEffective();
121         }
122
123         /**
124          * @deprecated This method was never used and relies on deprecated module methods.
125          */
126         @Deprecated
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();
131             }
132
133             Map<String, File> pathToFile = new HashMap<>();
134             Map<File, Module> sourceFileToModule = new HashMap<>();
135
136             for (File yangFile : yangFiles) {
137                 addSource(new YangStatementSourceImpl(new NamedFileInputStream(yangFile, yangFile.getPath())));
138                 pathToFile.put(yangFile.getPath(), yangFile);
139             }
140
141             EffectiveSchemaContext schema = buildEffective();
142             Set<Module> modules = schema.getModules();
143             for (Module module : modules) {
144                 sourceFileToModule.put(pathToFile.get(module.getModuleSourcePath()), module);
145             }
146
147             return sourceFileToModule;
148         }
149     }
150 }