Disconnect DataTreeConfiguration.Builder
[yangtools.git] / data / yang-data-tree-api / src / main / java / org / opendaylight / yangtools / yang / data / tree / api / DataTreeConfiguration.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.tree.api;
9
10 import static java.util.Objects.requireNonNull;
11
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;
18
19 /**
20  * DataTree configuration class.
21  *
22  * <p>
23  * TreeConfig supports currently the following options:
24  * <ul>
25  * <li>treeType</li>
26  * <li>enable/disable unique indexes and unique constraint validation</li>
27  * <li>enable/disable mandatory nodes validation</li>
28  * </ul>
29  *
30  * <p>
31  * TreeConfig can be easily extended in order to support further data tree configuration options, like following:
32  * <ul>
33  * <li>enable/disable case exclusion validation</li>
34  * <li>enable/disable other indexes</li>
35  * <li>other schema aware validation options</li>
36  * </ul>
37  *
38  * <p>
39  * This can be useful when strict validation is not required or useful for some reasons.
40  */
41 @Beta
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();
47
48     private final @NonNull TreeType treeType;
49     private final @NonNull YangInstanceIdentifier rootPath;
50     private final boolean uniqueIndexes;
51     private final boolean mandatoryNodesValidation;
52
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;
59     }
60
61     public @NonNull YangInstanceIdentifier getRootPath() {
62         return rootPath;
63     }
64
65     public @NonNull TreeType getTreeType() {
66         return treeType;
67     }
68
69     public boolean isUniqueIndexEnabled() {
70         return uniqueIndexes;
71     }
72
73     public boolean isMandatoryNodesValidationEnabled() {
74         return mandatoryNodesValidation;
75     }
76
77     @Override
78     public String toString() {
79         return MoreObjects.toStringHelper(this).add("type", treeType).add("root", rootPath)
80                 .add("mandatory", mandatoryNodesValidation)
81                 .add("unique", uniqueIndexes).toString();
82     }
83
84     public static DataTreeConfiguration getDefault(final TreeType treeType) {
85         switch (requireNonNull(treeType)) {
86             case CONFIGURATION:
87                 return DEFAULT_CONFIGURATION;
88             case OPERATIONAL:
89                 return DEFAULT_OPERATIONAL;
90             default:
91                 return new DataTreeConfiguration(treeType, YangInstanceIdentifier.empty(), false, true);
92         }
93     }
94
95     public static @NonNull Builder builder(final TreeType treeType) {
96         return new Builder(treeType);
97     }
98
99     public @NonNull Builder copyBuilder() {
100         return new Builder(treeType)
101                 .setMandatoryNodesValidation(isMandatoryNodesValidationEnabled())
102                 .setUniqueIndexes(isUniqueIndexEnabled())
103                 .setRootPath(getRootPath());
104     }
105
106     public static class Builder implements Mutable {
107         private final TreeType treeType;
108
109         private YangInstanceIdentifier rootPath;
110         private boolean uniqueIndexes;
111         private boolean mandatoryNodesValidation;
112
113         public Builder(final TreeType treeType) {
114             this.treeType = requireNonNull(treeType);
115             rootPath = YangInstanceIdentifier.empty();
116         }
117
118         public @NonNull Builder setUniqueIndexes(final boolean uniqueIndexes) {
119             this.uniqueIndexes = uniqueIndexes;
120             return this;
121         }
122
123         public @NonNull Builder setMandatoryNodesValidation(final boolean mandatoryNodesValidation) {
124             this.mandatoryNodesValidation = mandatoryNodesValidation;
125             return this;
126         }
127
128         public @NonNull Builder setRootPath(final YangInstanceIdentifier rootPath) {
129             this.rootPath = rootPath.toOptimized();
130             return this;
131         }
132
133         /**
134          * Return {@link DataTreeConfiguration} as defined by this builder's current state.
135          *
136          * @return A DataTreeConfiguration
137          */
138         public @NonNull DataTreeConfiguration build() {
139             return new DataTreeConfiguration(treeType, rootPath, uniqueIndexes, mandatoryNodesValidation);
140         }
141     }
142 }