Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin
[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 org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
11
12 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
13 import java.io.InputStream;
14 import java.util.List;
15 import java.util.Collection;
16 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
17 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
18 import com.google.common.collect.ImmutableMap;
19 import java.util.EnumMap;
20 import java.util.Map;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
26
27 public class CrossSourceStatementReactor {
28
29     private final Map<ModelProcessingPhase,StatementSupportBundle> supportedTerminology;
30     private final Map<ValidationBundleType,Collection<?>> supportedValidation;
31
32     CrossSourceStatementReactor(Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
33         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
34         this.supportedValidation = ImmutableMap.of();
35     }
36
37     CrossSourceStatementReactor(Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology, Map<ValidationBundleType,Collection<?>> supportedValidation) {
38         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
39         this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
40     }
41
42     public static final Builder builder() {
43         return new Builder();
44     }
45
46     public final BuildAction newBuild() {
47         return new BuildAction();
48     }
49
50     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor>{
51
52         final Map<ModelProcessingPhase,StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
53         final Map<ValidationBundleType,Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
54
55         public Builder setBundle(ModelProcessingPhase phase,StatementSupportBundle bundle) {
56             bundles.put(phase, bundle);
57             return this;
58         }
59
60
61         public Builder setValidationBundle(
62                 ValidationBundleType type,
63                 Collection<?> validationBundle) {
64             validationBundles.put(type,validationBundle);
65             return this;
66         }
67
68         @Override
69         public CrossSourceStatementReactor build() {
70             return new CrossSourceStatementReactor(bundles, validationBundles);
71         }
72
73     }
74
75     public class BuildAction {
76
77         private final BuildGlobalContext context;
78
79         public BuildAction() {
80             this.context = new BuildGlobalContext(supportedTerminology, supportedValidation);
81         }
82
83         public void addSource(StatementStreamSource source) {
84             context.addSource(source);
85         }
86
87         public void addSources(StatementStreamSource... sources) {
88             for (StatementStreamSource source : sources) {
89                 context.addSource(source);
90             }
91         }
92
93         public EffectiveModelContext build() throws SourceException, ReactorException {
94             return context.build();
95         }
96
97         public EffectiveSchemaContext buildEffective() throws SourceException, ReactorException {
98             return context.buildEffective();
99         }
100
101         public SchemaContext buildEffective(List<InputStream> yangInputStreams) throws SourceException, ReactorException {
102
103             for(InputStream yangInputStream : yangInputStreams) {
104                 addSource(new YangStatementSourceImpl(yangInputStream));
105             }
106
107             return buildEffective();
108         }
109
110     }
111
112
113 }