c206e0e6375c80b65f6f67062eff1ca23eed1f03
[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 java.io.IOException;
11
12 import com.google.common.io.ByteSource;
13 import java.util.Set;
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;
19 import java.io.File;
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;
29 import java.util.Map;
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;
35
36 public class CrossSourceStatementReactor {
37
38     private final Map<ModelProcessingPhase,StatementSupportBundle> supportedTerminology;
39     private final Map<ValidationBundleType,Collection<?>> supportedValidation;
40
41     CrossSourceStatementReactor(Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
42         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
43         this.supportedValidation = ImmutableMap.of();
44     }
45
46     CrossSourceStatementReactor(Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology, Map<ValidationBundleType,Collection<?>> supportedValidation) {
47         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
48         this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
49     }
50
51     public static final Builder builder() {
52         return new Builder();
53     }
54
55     public final BuildAction newBuild() {
56         return new BuildAction();
57     }
58
59     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor>{
60
61         final Map<ModelProcessingPhase,StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
62         final Map<ValidationBundleType,Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
63
64         public Builder setBundle(ModelProcessingPhase phase,StatementSupportBundle bundle) {
65             bundles.put(phase, bundle);
66             return this;
67         }
68
69
70         public Builder setValidationBundle(
71                 ValidationBundleType type,
72                 Collection<?> validationBundle) {
73             validationBundles.put(type,validationBundle);
74             return this;
75         }
76
77         @Override
78         public CrossSourceStatementReactor build() {
79             return new CrossSourceStatementReactor(bundles, validationBundles);
80         }
81
82     }
83
84     public class BuildAction {
85
86         private final BuildGlobalContext context;
87
88         public BuildAction() {
89             this.context = new BuildGlobalContext(supportedTerminology, supportedValidation);
90         }
91
92         public void addSource(StatementStreamSource source) {
93             context.addSource(source);
94         }
95
96         public void addSources(StatementStreamSource... sources) {
97             for (StatementStreamSource source : sources) {
98                 context.addSource(source);
99             }
100         }
101
102         public EffectiveModelContext build() throws SourceException, ReactorException {
103             return context.build();
104         }
105
106         public EffectiveSchemaContext buildEffective() throws SourceException, ReactorException {
107             return context.buildEffective();
108         }
109
110         public SchemaContext buildEffective(Collection<ByteSource> yangByteSources) throws SourceException, ReactorException, IOException {
111
112             for(ByteSource yangByteSource : yangByteSources) {
113                 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
114             }
115
116             return buildEffective();
117         }
118
119         public SchemaContext buildEffective(List<InputStream> yangInputStreams) throws SourceException, ReactorException {
120
121             for(InputStream yangInputStream : yangInputStreams) {
122                 addSource(new YangStatementSourceImpl(yangInputStream));
123             }
124
125             return buildEffective();
126         }
127
128         public Map<File, Module> buildEffectiveMappedToSource(
129                 List<File> yangFiles) throws SourceException, ReactorException,
130                 FileNotFoundException {
131
132             if (yangFiles == null || yangFiles.isEmpty()) {
133                 return Collections.emptyMap();
134             }
135
136             Map<String, File> pathToFile = new HashMap<>();
137             Map<File, Module> sourceFileToModule = new HashMap<>();
138
139             for (File yangFile : yangFiles) {
140                 addSource(new YangStatementSourceImpl(new NamedFileInputStream(
141                         yangFile, yangFile.getPath())));
142                 pathToFile.put(yangFile.getPath(), yangFile);
143             }
144
145             EffectiveSchemaContext schema = buildEffective();
146             Set<Module> modules = schema.getModules();
147             for (Module module : modules) {
148                 sourceFileToModule.put(
149                         pathToFile.get(module.getModuleSourcePath()), module);
150             }
151
152             return sourceFileToModule;
153         }
154     }
155 }