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