2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.parser.api;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import java.util.Objects;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.Immutable;
18 import org.opendaylight.yangtools.concepts.Mutable;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
20 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 * A configuration of {@link YangParser} wiring for use with {@link YangParserFactory}.
26 public final class YangParserConfiguration implements Immutable {
28 * System-wide default configuration.
30 public static final YangParserConfiguration DEFAULT = builder().build();
32 private final ImportResolutionMode importResolutionMode;
33 private final boolean retainDeclarationReferences;
34 private final boolean warnForUnkeyedLists;
36 private YangParserConfiguration(final ImportResolutionMode importResolutionMode,
37 final boolean retainDeclarationReferences, final boolean warnForUnkeyedLists) {
38 this.importResolutionMode = requireNonNull(importResolutionMode);
39 this.retainDeclarationReferences = retainDeclarationReferences;
40 this.warnForUnkeyedLists = warnForUnkeyedLists;
44 public ImportResolutionMode importResolutionMode() {
45 return importResolutionMode;
49 * Return {@code true} if {@link DeclarationReference} to source location in the final parser product, notably
50 * making {@link DeclaredStatement#declarationReference()} available.
52 * @return {@code true} if declaration references should be retained
54 public boolean retainDeclarationReferences() {
55 return retainDeclarationReferences;
59 * Issue a warning when a {@code list} statement without a {@code key} statement is found in the
60 * {@code config true} part of the schema tree. Such statements run contrary to
61 * <a href="https://www.rfc-editor.org/rfc/rfc7950.html#section-7.8.2">RFC7950</a>, but are readily supported
62 * by OpenDaylight infrastructure.
64 * @return {@code true} if non-compliant {@code list} statements should be reported
66 public boolean warnForUnkeyedLists() {
67 return warnForUnkeyedLists;
71 public int hashCode() {
72 return Objects.hash(importResolutionMode, retainDeclarationReferences);
76 public boolean equals(final @Nullable Object obj) {
80 if (!(obj instanceof YangParserConfiguration)) {
83 final YangParserConfiguration other = (YangParserConfiguration) obj;
84 return importResolutionMode == other.importResolutionMode
85 && retainDeclarationReferences == other.retainDeclarationReferences;
89 public String toString() {
90 return MoreObjects.toStringHelper(this)
91 .add("importResolution", importResolutionMode)
92 .add("declarationReferences", retainDeclarationReferences)
97 * Return a new {@link Builder} initialized to default configuration.
99 * @return A new builder
101 public static Builder builder() {
102 return new Builder();
105 public static final class Builder implements Mutable {
106 private ImportResolutionMode importResolutionMode = ImportResolutionMode.DEFAULT;
107 private boolean retainDeclarationReferences;
108 // FIXME: YANGTOOLS-1423: default to false
109 private boolean warnForUnkeyedLists = true;
116 * Return a {@link YangParserConfiguration} initialized with contents of this builder.
118 * @return A YangParserConfiguration
120 public YangParserConfiguration build() {
121 return new YangParserConfiguration(importResolutionMode, retainDeclarationReferences, warnForUnkeyedLists);
125 public Builder importResolutionMode(final ImportResolutionMode newImportResolutionMode) {
126 importResolutionMode = requireNonNull(newImportResolutionMode);
131 * Retain {@link DeclarationReference} to source location in the final parser product. This option results in
132 * quite significant memory overhead for storage of {@link DeclaredStatement}, but makes
133 * {@link DeclaredStatement#declarationReference()} available, which is useful in certain scenarios, for example
137 * This option is disabled by default.
139 * @param newRetainDeclarationReferences {@code true} if declaration references should be retained
140 * @return This builder
142 public Builder retainDeclarationReferences(final boolean newRetainDeclarationReferences) {
143 retainDeclarationReferences = newRetainDeclarationReferences;
148 * Issue a warning when a {@code list} statement without a {@code key} statement is found in the
149 * {@code config true} part of the schema tree. Such statements run contrary to
150 * <a href="https://www.rfc-editor.org/rfc/rfc7950.html#section-7.8.2">RFC7950</a>, but are readily supported
151 * by OpenDaylight infrastructure.
154 * This option is enabled by default.
156 * @param newWarnForUnkeyedLists {@code true} if non-compliant {@code list} statements should be reported
157 * @return This builder
159 public Builder warnForUnkeyedLists(final boolean newWarnForUnkeyedLists) {
160 warnForUnkeyedLists = newWarnForUnkeyedLists;