Bug 6868: If-feature argument may be boolean expression
[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.IOException;
13 import java.io.InputStream;
14 import java.util.Collection;
15 import java.util.EnumMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
26 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
29
30 public class CrossSourceStatementReactor {
31
32     private final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology;
33     private final Map<ValidationBundleType, Collection<?>> supportedValidation;
34
35     CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
36         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
37         this.supportedValidation = ImmutableMap.of();
38     }
39
40     CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology,
41             final Map<ValidationBundleType, Collection<?>> supportedValidation) {
42         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
43         this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
44     }
45
46     public static Builder builder() {
47         return new Builder();
48     }
49
50     public final BuildAction newBuild() {
51         return newBuild(StatementParserMode.DEFAULT_MODE);
52     }
53
54     public final BuildAction newBuild(final Set<QName> supportedFeatures) {
55         return new BuildAction(StatementParserMode.DEFAULT_MODE, supportedFeatures);
56     }
57
58     public final BuildAction newBuild(final StatementParserMode statementParserMode) {
59         return new BuildAction(statementParserMode, null);
60     }
61
62     public final BuildAction newBuild(final StatementParserMode statementParserMode,
63             final Set<QName> supportedFeatures) {
64         return new BuildAction(statementParserMode, supportedFeatures);
65     }
66
67     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor> {
68
69         final Map<ModelProcessingPhase, StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
70         final Map<ValidationBundleType, Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
71
72         public Builder setBundle(final ModelProcessingPhase phase, final StatementSupportBundle bundle) {
73             bundles.put(phase, bundle);
74             return this;
75         }
76
77         public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
78             validationBundles.put(type, validationBundle);
79             return this;
80         }
81
82         @Override
83         public CrossSourceStatementReactor build() {
84             return new CrossSourceStatementReactor(bundles, validationBundles);
85         }
86     }
87
88     public class BuildAction {
89         private final BuildGlobalContext context;
90
91         public BuildAction() {
92             this(StatementParserMode.DEFAULT_MODE);
93         }
94
95         public BuildAction(final StatementParserMode statementParserMode) {
96             this(statementParserMode, null);
97         }
98
99         public BuildAction(final Set<QName> supportedFeatures) {
100             this(StatementParserMode.DEFAULT_MODE, supportedFeatures);
101         }
102
103         public BuildAction(final StatementParserMode statementParserMode, final Set<QName> supportedFeatures) {
104             this.context = new BuildGlobalContext(supportedTerminology, supportedValidation, statementParserMode,
105                     supportedFeatures);
106         }
107
108         public void addSource(final StatementStreamSource source) {
109             context.addSource(source);
110         }
111
112         public void addSources(final StatementStreamSource... sources) {
113             for (final StatementStreamSource source : sources) {
114                 context.addSource(source);
115             }
116         }
117
118         /**
119          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
120          * @throws ReactorException
121          */
122         public EffectiveModelContext build() throws ReactorException {
123             return context.build();
124         }
125
126         public EffectiveSchemaContext buildEffective() throws ReactorException {
127             return context.buildEffective();
128         }
129
130         public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws ReactorException,
131                 IOException {
132             for (final ByteSource yangByteSource : yangByteSources) {
133                 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
134             }
135
136             return buildEffective();
137         }
138
139         public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws ReactorException {
140             for (final InputStream yangInputStream : yangInputStreams) {
141                 addSource(new YangStatementSourceImpl(yangInputStream));
142             }
143
144             return buildEffective();
145         }
146     }
147 }