21d2bc890ab5161631b1259793f0821e7a000001
[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.yang.data.api.YangInstanceIdentifier;
17
18 /**
19  * DataTree configuration class.
20  *
21  * <p>
22  * TreeConfig supports currently the following options:
23  * <ul>
24  * <li>treeType</li>
25  * <li>enable/disable unique indexes and unique constraint validation</li>
26  * <li>enable/disable mandatory nodes validation</li>
27  * </ul>
28  *
29  * <p>
30  * TreeConfig can be easily extended in order to support further data tree configuration options, like following:
31  * <ul>
32  * <li>enable/disable case exclusion validation</li>
33  * <li>enable/disable other indexes</li>
34  * <li>other schema aware validation options</li>
35  * </ul>
36  *
37  * <p>
38  * This can be useful when strict validation is not required or useful for some reasons.
39  */
40 @Beta
41 public class DataTreeConfiguration implements Immutable {
42     public static final DataTreeConfiguration DEFAULT_CONFIGURATION = new Builder(TreeType.CONFIGURATION)
43             .setMandatoryNodesValidation(true).build();
44     public static final DataTreeConfiguration DEFAULT_OPERATIONAL = new Builder(TreeType.OPERATIONAL)
45             .setMandatoryNodesValidation(true).build();
46
47     private final @NonNull TreeType treeType;
48     private final @NonNull YangInstanceIdentifier rootPath;
49     private final boolean uniqueIndexes;
50     private final boolean mandatoryNodesValidation;
51
52     DataTreeConfiguration(final TreeType treeType, final YangInstanceIdentifier rootPath, final boolean uniqueIndexes,
53             final boolean mandatoryNodesValidation) {
54         this.treeType = requireNonNull(treeType);
55         this.rootPath = requireNonNull(rootPath);
56         this.uniqueIndexes = uniqueIndexes;
57         this.mandatoryNodesValidation = mandatoryNodesValidation;
58     }
59
60     public @NonNull YangInstanceIdentifier getRootPath() {
61         return rootPath;
62     }
63
64     public @NonNull TreeType getTreeType() {
65         return treeType;
66     }
67
68     public boolean isUniqueIndexEnabled() {
69         return uniqueIndexes;
70     }
71
72     public boolean isMandatoryNodesValidationEnabled() {
73         return mandatoryNodesValidation;
74     }
75
76     @Override
77     public String toString() {
78         return MoreObjects.toStringHelper(this).add("type", treeType).add("root", rootPath)
79                 .add("mandatory", mandatoryNodesValidation)
80                 .add("unique", uniqueIndexes).toString();
81     }
82
83     public static DataTreeConfiguration getDefault(final TreeType treeType) {
84         switch (requireNonNull(treeType)) {
85             case CONFIGURATION:
86                 return DEFAULT_CONFIGURATION;
87             case OPERATIONAL:
88                 return DEFAULT_OPERATIONAL;
89             default:
90                 return new DataTreeConfiguration(treeType, YangInstanceIdentifier.empty(), false, true);
91         }
92     }
93
94     public static Builder builder(final TreeType treeType) {
95         return new Builder(treeType);
96     }
97
98     public Builder copyBuilder() {
99         return new Builder(treeType)
100                 .setMandatoryNodesValidation(isMandatoryNodesValidationEnabled())
101                 .setUniqueIndexes(isUniqueIndexEnabled())
102                 .setRootPath(getRootPath());
103     }
104
105     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<DataTreeConfiguration> {
106         private final TreeType treeType;
107         private YangInstanceIdentifier rootPath;
108         private boolean uniqueIndexes;
109         private boolean mandatoryNodesValidation;
110
111         public Builder(final TreeType treeType) {
112             this.treeType = requireNonNull(treeType);
113             rootPath = YangInstanceIdentifier.empty();
114         }
115
116         public Builder setUniqueIndexes(final boolean uniqueIndexes) {
117             this.uniqueIndexes = uniqueIndexes;
118             return this;
119         }
120
121         public Builder setMandatoryNodesValidation(final boolean mandatoryNodesValidation) {
122             this.mandatoryNodesValidation = mandatoryNodesValidation;
123             return this;
124         }
125
126         public Builder setRootPath(final YangInstanceIdentifier rootPath) {
127             this.rootPath = rootPath.toOptimized();
128             return this;
129         }
130
131         @Override
132         public DataTreeConfiguration build() {
133             return new DataTreeConfiguration(treeType, rootPath, uniqueIndexes, mandatoryNodesValidation);
134         }
135     }
136 }