/*
* Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.model.repo.api;
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.SetMultimap;
import java.util.Objects;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.concepts.Mutable;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
/**
* SchemaContextFactory configuration class. It currently supports the following options to be set:
*
* - schema source filter
* - statement parser mode
* - supported features
* - supported deviations
*
*/
@Beta
public final class SchemaContextFactoryConfiguration implements Immutable {
private static final @NonNull SchemaContextFactoryConfiguration DEFAULT_CONFIGURATION = new Builder().build();
private final @NonNull SchemaSourceFilter filter;
private final @NonNull StatementParserMode statementParserMode;
private final @Nullable FeatureSet supportedFeatures;
private final @Nullable ImmutableSetMultimap modulesDeviatedByModules;
private SchemaContextFactoryConfiguration(final @NonNull SchemaSourceFilter filter,
final @NonNull StatementParserMode statementParserMode,
final @Nullable FeatureSet supportedFeatures,
final @Nullable ImmutableSetMultimap modulesDeviatedByModules) {
this.filter = requireNonNull(filter);
this.statementParserMode = requireNonNull(statementParserMode);
this.supportedFeatures = supportedFeatures;
this.modulesDeviatedByModules = modulesDeviatedByModules;
}
public @NonNull SchemaSourceFilter getSchemaSourceFilter() {
return filter;
}
public @NonNull StatementParserMode getStatementParserMode() {
return statementParserMode;
}
public Optional getSupportedFeatures() {
return Optional.ofNullable(supportedFeatures);
}
public Optional> getModulesDeviatedByModules() {
return Optional.ofNullable(modulesDeviatedByModules);
}
public static @NonNull SchemaContextFactoryConfiguration getDefault() {
return DEFAULT_CONFIGURATION;
}
public static @NonNull Builder builder() {
return new Builder();
}
@Override
public int hashCode() {
return Objects.hash(filter, statementParserMode, supportedFeatures, modulesDeviatedByModules);
}
@Override
public boolean equals(final Object obj) {
return this == obj || obj instanceof SchemaContextFactoryConfiguration other && filter.equals(other.filter)
&& statementParserMode.equals(other.statementParserMode)
&& Objects.equals(supportedFeatures, other.supportedFeatures)
&& Objects.equals(modulesDeviatedByModules, other.modulesDeviatedByModules);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).omitNullValues().add("schemaSourceFilter", filter)
.add("statementParserMode", statementParserMode).add("supportedFeatures", supportedFeatures)
.add("modulesDeviatedByModules", modulesDeviatedByModules).toString();
}
public static class Builder implements Mutable {
private @NonNull SchemaSourceFilter filter = SchemaSourceFilter.ALWAYS_ACCEPT;
private @NonNull StatementParserMode statementParserMode = StatementParserMode.DEFAULT_MODE;
private ImmutableSetMultimap modulesDeviatedByModules;
private FeatureSet supportedFeatures;
/**
* Set schema source filter which will filter available schema sources using the provided filter.
*
* @param filter schema source filter which acts as the gating function before a schema source is considered
* by the factory for inclusion in the SchemaContext it produces.
* @return this builder
*/
public @NonNull Builder setFilter(final @NonNull SchemaSourceFilter filter) {
this.filter = requireNonNull(filter);
return this;
}
/**
* Set YANG statement parser mode.
*
* @param statementParserMode mode of yang statement parser
* @return this builder
*/
public @NonNull Builder setStatementParserMode(final @NonNull StatementParserMode statementParserMode) {
this.statementParserMode = requireNonNull(statementParserMode);
return this;
}
/**
* Set supported features based on which all if-feature statements in the parsed YANG modules will be resolved.
*
* @param supportedFeatures Set of supported features in the final SchemaContext. If the set is empty, no
* features encountered will be supported.
* @return this builder
*/
public @NonNull Builder setSupportedFeatures(final FeatureSet supportedFeatures) {
this.supportedFeatures = supportedFeatures;
return this;
}
/**
* Set YANG modules which can be deviated by specified modules during the parsing process. Map key (QNameModule)
* denotes a module which can be deviated by the modules in the Map value.
*
* @param modulesDeviatedByModules Map of YANG modules (Map key) which can be deviated by specified modules
* (Map values) in the final SchemaContext. If the map is empty, no deviations
* encountered will be supported. If the map is null, all deviations will be
* applied.
* @return this builder
*/
public @NonNull Builder setModulesDeviatedByModules(
final @Nullable SetMultimap modulesDeviatedByModules) {
this.modulesDeviatedByModules = modulesDeviatedByModules != null
? ImmutableSetMultimap.copyOf(modulesDeviatedByModules) : null;
return this;
}
/**
* Return a new {@link SchemaContextFactoryConfiguration} based on the contents of this builder.
*
* @return A new {@link SchemaContextFactoryConfiguration}
*/
public @NonNull SchemaContextFactoryConfiguration build() {
return new SchemaContextFactoryConfiguration(filter, statementParserMode, supportedFeatures,
modulesDeviatedByModules);
}
}
}