b8eae24c3fdde405ea189f4918f5cc5d12f76dcc
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / 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.api.schema.tree;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16
17 /**
18  * DataTree configuration class.
19  *
20  * <p>
21  * TreeConfig supports currently the following options:
22  * <ul>
23  * <li>treeType</li>
24  * <li>enable/disable unique indexes and unique constraint validation</li>
25  * <li>enable/disable mandatory nodes validation</li>
26  * </ul>
27  *
28  * <p>
29  * TreeConfig can be easily extended in order to support further data tree
30  * 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
39  * 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).build();
46
47     private final TreeType treeType;
48     private final 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 = Preconditions.checkNotNull(treeType);
55         this.rootPath = Preconditions.checkNotNull(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         Preconditions.checkNotNull(treeType);
85         switch (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 class Builder implements org.opendaylight.yangtools.concepts.Builder<DataTreeConfiguration> {
96         private final TreeType treeType;
97         private YangInstanceIdentifier rootPath;
98         private boolean uniqueIndexes;
99         private boolean mandatoryNodesValidation;
100
101         public Builder(final TreeType treeType) {
102             this.treeType = Preconditions.checkNotNull(treeType);
103             this.rootPath = YangInstanceIdentifier.EMPTY;
104         }
105
106         public Builder setUniqueIndexes(final boolean uniqueIndexes) {
107             this.uniqueIndexes = uniqueIndexes;
108             return this;
109         }
110
111         public Builder setMandatoryNodesValidation(final boolean mandatoryNodesValidation) {
112             this.mandatoryNodesValidation = mandatoryNodesValidation;
113             return this;
114         }
115
116         public Builder setRootPath(final YangInstanceIdentifier rootPath) {
117             this.rootPath = rootPath.toOptimized();
118             return this;
119         }
120
121         @Override
122         public DataTreeConfiguration build() {
123             return new DataTreeConfiguration(treeType, rootPath, uniqueIndexes, mandatoryNodesValidation);
124         }
125     }
126 }