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