YangTextSchemaSource is a CharSource
[yangtools.git] / yang / yang-repo-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / SchemaContextFactoryConfiguration.java
1 /*
2  * Copyright (c) 2017 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.model.repo.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.collect.ImmutableSetMultimap;
15 import com.google.common.collect.SetMultimap;
16 import java.util.Objects;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.yangtools.concepts.Immutable;
21 import org.opendaylight.yangtools.concepts.Mutable;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
24
25 /**
26  * SchemaContextFactory configuration class. It currently supports the following options to be set:
27  * <ul>
28  *   <li>schema source filter</li>
29  *   <li>statement parser mode</li>
30  *   <li>supported features</li>
31  *   <li>supported deviations</li>
32  * </ul>
33  */
34 @Beta
35 public final class SchemaContextFactoryConfiguration implements Immutable {
36     private static final @NonNull SchemaContextFactoryConfiguration DEFAULT_CONFIGURATION = new Builder().build();
37
38     private final @NonNull SchemaSourceFilter filter;
39     private final @NonNull StatementParserMode statementParserMode;
40     private final @Nullable FeatureSet supportedFeatures;
41     private final @Nullable ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules;
42
43     private SchemaContextFactoryConfiguration(final @NonNull SchemaSourceFilter filter,
44             final @NonNull StatementParserMode statementParserMode,
45             final @Nullable FeatureSet supportedFeatures,
46             final @Nullable ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
47         this.filter = requireNonNull(filter);
48         this.statementParserMode = requireNonNull(statementParserMode);
49         this.supportedFeatures = supportedFeatures;
50         this.modulesDeviatedByModules = modulesDeviatedByModules;
51     }
52
53     public @NonNull SchemaSourceFilter getSchemaSourceFilter() {
54         return filter;
55     }
56
57     public @NonNull StatementParserMode getStatementParserMode() {
58         return statementParserMode;
59     }
60
61     public Optional<FeatureSet> getSupportedFeatures() {
62         return Optional.ofNullable(supportedFeatures);
63     }
64
65     public Optional<SetMultimap<QNameModule, QNameModule>> getModulesDeviatedByModules() {
66         return Optional.ofNullable(modulesDeviatedByModules);
67     }
68
69     public static @NonNull SchemaContextFactoryConfiguration getDefault() {
70         return DEFAULT_CONFIGURATION;
71     }
72
73     public static @NonNull Builder builder() {
74         return new Builder();
75     }
76
77     @Override
78     public int hashCode() {
79         return Objects.hash(filter, statementParserMode, supportedFeatures, modulesDeviatedByModules);
80     }
81
82     @Override
83     public boolean equals(final Object obj) {
84         return this == obj || obj instanceof SchemaContextFactoryConfiguration other && filter.equals(other.filter)
85             && statementParserMode.equals(other.statementParserMode)
86             && Objects.equals(supportedFeatures, other.supportedFeatures)
87             && Objects.equals(modulesDeviatedByModules, other.modulesDeviatedByModules);
88     }
89
90     @Override
91     public String toString() {
92         return MoreObjects.toStringHelper(this).omitNullValues().add("schemaSourceFilter", filter)
93                 .add("statementParserMode", statementParserMode).add("supportedFeatures", supportedFeatures)
94                 .add("modulesDeviatedByModules", modulesDeviatedByModules).toString();
95     }
96
97     public static class Builder implements Mutable {
98         private @NonNull SchemaSourceFilter filter = SchemaSourceFilter.ALWAYS_ACCEPT;
99         private @NonNull StatementParserMode statementParserMode = StatementParserMode.DEFAULT_MODE;
100         private ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules;
101         private FeatureSet supportedFeatures;
102
103         /**
104          * Set schema source filter which will filter available schema sources using the provided filter.
105          *
106          * @param filter schema source filter which acts as the gating function before a schema source is considered
107          *               by the factory for inclusion in the SchemaContext it produces.
108          * @return this builder
109          */
110         public @NonNull Builder setFilter(final @NonNull SchemaSourceFilter filter) {
111             this.filter = requireNonNull(filter);
112             return this;
113         }
114
115         /**
116          * Set YANG statement parser mode.
117          *
118          * @param statementParserMode mode of yang statement parser
119          * @return this builder
120          */
121         public @NonNull Builder setStatementParserMode(final @NonNull StatementParserMode statementParserMode) {
122             this.statementParserMode = requireNonNull(statementParserMode);
123             return this;
124         }
125
126         /**
127          * Set supported features based on which all if-feature statements in the parsed YANG modules will be resolved.
128          *
129          * @param supportedFeatures Set of supported features in the final SchemaContext. If the set is empty, no
130          *                          features encountered will be supported.
131          * @return this builder
132          */
133         public @NonNull Builder setSupportedFeatures(final FeatureSet supportedFeatures) {
134             this.supportedFeatures = supportedFeatures;
135             return this;
136         }
137
138         /**
139          * Set YANG modules which can be deviated by specified modules during the parsing process. Map key (QNameModule)
140          * denotes a module which can be deviated by the modules in the Map value.
141          *
142          * @param modulesDeviatedByModules Map of YANG modules (Map key) which can be deviated by specified modules
143          *                                 (Map values) in the final SchemaContext. If the map is empty, no deviations
144          *                                 encountered will be supported. If the map is null, all deviations will be
145          *                                 applied.
146          * @return this builder
147          */
148         public @NonNull Builder setModulesDeviatedByModules(
149                 final @Nullable SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
150             this.modulesDeviatedByModules = modulesDeviatedByModules != null
151                     ? ImmutableSetMultimap.copyOf(modulesDeviatedByModules) : null;
152             return this;
153         }
154
155         /**
156          * Return a new {@link SchemaContextFactoryConfiguration} based on the contents of this builder.
157          *
158          * @return A new {@link SchemaContextFactoryConfiguration}
159          */
160         public @NonNull SchemaContextFactoryConfiguration build() {
161             return new SchemaContextFactoryConfiguration(filter, statementParserMode, supportedFeatures,
162                     modulesDeviatedByModules);
163         }
164     }
165 }