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