2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.repo.api;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.ImmutableSetMultimap;
16 import com.google.common.collect.SetMultimap;
17 import java.util.Objects;
18 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.concepts.Immutable;
23 import org.opendaylight.yangtools.concepts.Mutable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.QNameModule;
28 * SchemaContextFactory configuration class. It currently supports the following options to be set:
30 * <li>schema source filter</li>
31 * <li>statement parser mode</li>
32 * <li>supported features</li>
33 * <li>supported deviations</li>
37 public final class SchemaContextFactoryConfiguration implements Immutable {
38 private static final @NonNull SchemaContextFactoryConfiguration DEFAULT_CONFIGURATION = new Builder().build();
40 private final @NonNull SchemaSourceFilter filter;
41 private final @NonNull StatementParserMode statementParserMode;
42 private final @Nullable ImmutableSet<QName> supportedFeatures;
43 private final @Nullable ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules;
45 private SchemaContextFactoryConfiguration(final @NonNull SchemaSourceFilter filter,
46 final @NonNull StatementParserMode statementParserMode,
47 final @Nullable ImmutableSet<QName> supportedFeatures,
48 final @Nullable ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
49 this.filter = requireNonNull(filter);
50 this.statementParserMode = requireNonNull(statementParserMode);
51 this.supportedFeatures = supportedFeatures;
52 this.modulesDeviatedByModules = modulesDeviatedByModules;
55 public @NonNull SchemaSourceFilter getSchemaSourceFilter() {
59 public @NonNull StatementParserMode getStatementParserMode() {
60 return statementParserMode;
63 public Optional<Set<QName>> getSupportedFeatures() {
64 return Optional.ofNullable(supportedFeatures);
67 public Optional<SetMultimap<QNameModule, QNameModule>> getModulesDeviatedByModules() {
68 return Optional.ofNullable(modulesDeviatedByModules);
71 public static @NonNull SchemaContextFactoryConfiguration getDefault() {
72 return DEFAULT_CONFIGURATION;
75 public static @NonNull Builder builder() {
80 public int hashCode() {
81 return Objects.hash(filter, statementParserMode, supportedFeatures, modulesDeviatedByModules);
85 public boolean equals(final Object obj) {
89 if (!(obj instanceof SchemaContextFactoryConfiguration)) {
92 final SchemaContextFactoryConfiguration other = (SchemaContextFactoryConfiguration) obj;
93 return filter.equals(other.filter) && statementParserMode.equals(other.statementParserMode)
94 && Objects.equals(supportedFeatures, other.supportedFeatures)
95 && Objects.equals(modulesDeviatedByModules, other.modulesDeviatedByModules);
99 public String toString() {
100 return MoreObjects.toStringHelper(this).omitNullValues().add("schemaSourceFilter", filter)
101 .add("statementParserMode", statementParserMode).add("supportedFeatures", supportedFeatures)
102 .add("modulesDeviatedByModules", modulesDeviatedByModules).toString();
105 public static class Builder implements Mutable {
106 private @NonNull SchemaSourceFilter filter = SchemaSourceFilter.ALWAYS_ACCEPT;
107 private @NonNull StatementParserMode statementParserMode = StatementParserMode.DEFAULT_MODE;
108 private ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules;
109 private ImmutableSet<QName> supportedFeatures;
112 * Set schema source filter which will filter available schema sources using the provided filter.
114 * @param filter schema source filter which acts as the gating function before a schema source is considered
115 * by the factory for inclusion in the SchemaContext it produces.
116 * @return this builder
118 public @NonNull Builder setFilter(final @NonNull SchemaSourceFilter filter) {
119 this.filter = requireNonNull(filter);
124 * Set YANG statement parser mode.
126 * @param statementParserMode mode of yang statement parser
127 * @return this builder
129 public @NonNull Builder setStatementParserMode(final @NonNull StatementParserMode statementParserMode) {
130 this.statementParserMode = requireNonNull(statementParserMode);
135 * Set supported features based on which all if-feature statements in the parsed YANG modules will be resolved.
137 * @param supportedFeatures Set of supported features in the final SchemaContext. If the set is empty, no
138 * features encountered will be supported.
139 * @return this builder
141 public @NonNull Builder setSupportedFeatures(final Set<QName> supportedFeatures) {
142 this.supportedFeatures = supportedFeatures != null ? ImmutableSet.copyOf(supportedFeatures) : null;
147 * Set YANG modules which can be deviated by specified modules during the parsing process. Map key (QNameModule)
148 * denotes a module which can be deviated by the modules in the Map value.
150 * @param modulesDeviatedByModules Map of YANG modules (Map key) which can be deviated by specified modules
151 * (Map values) in the final SchemaContext. If the map is empty, no deviations
152 * encountered will be supported. If the map is null, all deviations will be
154 * @return this builder
156 public @NonNull Builder setModulesDeviatedByModules(
157 final @Nullable SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
158 this.modulesDeviatedByModules = modulesDeviatedByModules != null
159 ? ImmutableSetMultimap.copyOf(modulesDeviatedByModules) : null;
164 * Return a new {@link SchemaContextFactoryConfiguration} based on the contents of this builder.
166 * @return A new {@link SchemaContextFactoryConfiguration}
168 public @NonNull SchemaContextFactoryConfiguration build() {
169 return new SchemaContextFactoryConfiguration(filter, statementParserMode, supportedFeatures,
170 modulesDeviatedByModules);