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 Set<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 Set<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) {
86 return this == obj || obj instanceof SchemaContextFactoryConfiguration other && filter.equals(other.filter)
87 && statementParserMode.equals(other.statementParserMode)
88 && equals(supportedFeatures, other.supportedFeatures)
89 && Objects.equals(modulesDeviatedByModules, other.modulesDeviatedByModules);
92 // This a bit of a dance to deal with FeatureSet not conforming to Set.equals()
93 private static boolean equals(final @Nullable Set<QName> thisFeatures, final @Nullable Set<QName> otherFeatures) {
94 if (thisFeatures == otherFeatures) {
97 if (thisFeatures == null || otherFeatures == null) {
100 if (thisFeatures instanceof FeatureSet) {
101 return thisFeatures.equals(otherFeatures);
103 if (otherFeatures instanceof FeatureSet) {
104 return otherFeatures.equals(thisFeatures);
106 return thisFeatures.equals(otherFeatures);
110 public String toString() {
111 return MoreObjects.toStringHelper(this).omitNullValues().add("schemaSourceFilter", filter)
112 .add("statementParserMode", statementParserMode).add("supportedFeatures", supportedFeatures)
113 .add("modulesDeviatedByModules", modulesDeviatedByModules).toString();
116 public static class Builder implements Mutable {
117 private @NonNull SchemaSourceFilter filter = SchemaSourceFilter.ALWAYS_ACCEPT;
118 private @NonNull StatementParserMode statementParserMode = StatementParserMode.DEFAULT_MODE;
119 private ImmutableSetMultimap<QNameModule, QNameModule> modulesDeviatedByModules;
120 private Set<QName> supportedFeatures;
123 * Set schema source filter which will filter available schema sources using the provided filter.
125 * @param filter schema source filter which acts as the gating function before a schema source is considered
126 * by the factory for inclusion in the SchemaContext it produces.
127 * @return this builder
129 public @NonNull Builder setFilter(final @NonNull SchemaSourceFilter filter) {
130 this.filter = requireNonNull(filter);
135 * Set YANG statement parser mode.
137 * @param statementParserMode mode of yang statement parser
138 * @return this builder
140 public @NonNull Builder setStatementParserMode(final @NonNull StatementParserMode statementParserMode) {
141 this.statementParserMode = requireNonNull(statementParserMode);
146 * Set supported features based on which all if-feature statements in the parsed YANG modules will be resolved.
148 * @param supportedFeatures Set of supported features in the final SchemaContext. If the set is empty, no
149 * features encountered will be supported.
150 * @return this builder
152 public @NonNull Builder setSupportedFeatures(final Set<QName> supportedFeatures) {
153 if (supportedFeatures == null || supportedFeatures instanceof FeatureSet) {
154 this.supportedFeatures = supportedFeatures;
156 this.supportedFeatures = ImmutableSet.copyOf(supportedFeatures);
162 * Set YANG modules which can be deviated by specified modules during the parsing process. Map key (QNameModule)
163 * denotes a module which can be deviated by the modules in the Map value.
165 * @param modulesDeviatedByModules Map of YANG modules (Map key) which can be deviated by specified modules
166 * (Map values) in the final SchemaContext. If the map is empty, no deviations
167 * encountered will be supported. If the map is null, all deviations will be
169 * @return this builder
171 public @NonNull Builder setModulesDeviatedByModules(
172 final @Nullable SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
173 this.modulesDeviatedByModules = modulesDeviatedByModules != null
174 ? ImmutableSetMultimap.copyOf(modulesDeviatedByModules) : null;
179 * Return a new {@link SchemaContextFactoryConfiguration} based on the contents of this builder.
181 * @return A new {@link SchemaContextFactoryConfiguration}
183 public @NonNull SchemaContextFactoryConfiguration build() {
184 return new SchemaContextFactoryConfiguration(filter, statementParserMode, supportedFeatures,
185 modulesDeviatedByModules);