2 * Copyright (c) 2016 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.data.tree.api;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.concepts.Mutable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 * DataTree configuration class.
23 * TreeConfig supports currently the following options:
26 * <li>enable/disable unique indexes and unique constraint validation</li>
27 * <li>enable/disable mandatory nodes validation</li>
31 * TreeConfig can be easily extended in order to support further data tree configuration options, like following:
33 * <li>enable/disable case exclusion validation</li>
34 * <li>enable/disable other indexes</li>
35 * <li>other schema aware validation options</li>
39 * This can be useful when strict validation is not required or useful for some reasons.
42 public class DataTreeConfiguration implements Immutable {
43 public static final DataTreeConfiguration DEFAULT_CONFIGURATION = new Builder(TreeType.CONFIGURATION)
44 .setMandatoryNodesValidation(true).build();
45 public static final DataTreeConfiguration DEFAULT_OPERATIONAL = new Builder(TreeType.OPERATIONAL)
46 .setMandatoryNodesValidation(true).build();
48 private final @NonNull TreeType treeType;
49 private final @NonNull YangInstanceIdentifier rootPath;
50 private final boolean uniqueIndexes;
51 private final boolean mandatoryNodesValidation;
53 DataTreeConfiguration(final TreeType treeType, final YangInstanceIdentifier rootPath, final boolean uniqueIndexes,
54 final boolean mandatoryNodesValidation) {
55 this.treeType = requireNonNull(treeType);
56 this.rootPath = requireNonNull(rootPath);
57 this.uniqueIndexes = uniqueIndexes;
58 this.mandatoryNodesValidation = mandatoryNodesValidation;
61 public @NonNull YangInstanceIdentifier getRootPath() {
65 public @NonNull TreeType getTreeType() {
69 public boolean isUniqueIndexEnabled() {
73 public boolean isMandatoryNodesValidationEnabled() {
74 return mandatoryNodesValidation;
78 public String toString() {
79 return MoreObjects.toStringHelper(this).add("type", treeType).add("root", rootPath)
80 .add("mandatory", mandatoryNodesValidation)
81 .add("unique", uniqueIndexes).toString();
84 public static DataTreeConfiguration getDefault(final TreeType treeType) {
85 return switch (requireNonNull(treeType)) {
86 case CONFIGURATION -> DEFAULT_CONFIGURATION;
87 case OPERATIONAL -> DEFAULT_OPERATIONAL;
91 public static @NonNull Builder builder(final TreeType treeType) {
92 return new Builder(treeType);
95 public @NonNull Builder copyBuilder() {
96 return new Builder(treeType)
97 .setMandatoryNodesValidation(isMandatoryNodesValidationEnabled())
98 .setUniqueIndexes(isUniqueIndexEnabled())
99 .setRootPath(getRootPath());
102 public static class Builder implements Mutable {
103 private final TreeType treeType;
105 private YangInstanceIdentifier rootPath;
106 private boolean uniqueIndexes;
107 private boolean mandatoryNodesValidation;
109 public Builder(final TreeType treeType) {
110 this.treeType = requireNonNull(treeType);
111 rootPath = YangInstanceIdentifier.of();
114 public @NonNull Builder setUniqueIndexes(final boolean uniqueIndexes) {
115 this.uniqueIndexes = uniqueIndexes;
119 public @NonNull Builder setMandatoryNodesValidation(final boolean mandatoryNodesValidation) {
120 this.mandatoryNodesValidation = mandatoryNodesValidation;
124 public @NonNull Builder setRootPath(final YangInstanceIdentifier rootPath) {
125 this.rootPath = rootPath.toOptimized();
130 * Return {@link DataTreeConfiguration} as defined by this builder's current state.
132 * @return A DataTreeConfiguration
134 public @NonNull DataTreeConfiguration build() {
135 return new DataTreeConfiguration(treeType, rootPath, uniqueIndexes, mandatoryNodesValidation);