Populate data/ hierarchy
[yangtools.git] / data / 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 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
31  * 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
40  * reasons.
41  */
42 @Beta
43 public class DataTreeConfiguration implements Immutable {
44     public static final DataTreeConfiguration DEFAULT_CONFIGURATION = new Builder(TreeType.CONFIGURATION)
45             .setMandatoryNodesValidation(true).build();
46     public static final DataTreeConfiguration DEFAULT_OPERATIONAL = new Builder(TreeType.OPERATIONAL)
47             .setMandatoryNodesValidation(true).build();
48
49     private final @NonNull TreeType treeType;
50     private final @NonNull YangInstanceIdentifier rootPath;
51     private final boolean uniqueIndexes;
52     private final boolean mandatoryNodesValidation;
53
54     DataTreeConfiguration(final TreeType treeType, final YangInstanceIdentifier rootPath, final boolean uniqueIndexes,
55             final boolean mandatoryNodesValidation) {
56         this.treeType = requireNonNull(treeType);
57         this.rootPath = requireNonNull(rootPath);
58         this.uniqueIndexes = uniqueIndexes;
59         this.mandatoryNodesValidation = mandatoryNodesValidation;
60     }
61
62     public @NonNull YangInstanceIdentifier getRootPath() {
63         return rootPath;
64     }
65
66     public @NonNull TreeType getTreeType() {
67         return treeType;
68     }
69
70     public boolean isUniqueIndexEnabled() {
71         return uniqueIndexes;
72     }
73
74     public boolean isMandatoryNodesValidationEnabled() {
75         return mandatoryNodesValidation;
76     }
77
78     @Override
79     public String toString() {
80         return MoreObjects.toStringHelper(this).add("type", treeType).add("root", rootPath)
81                 .add("mandatory", mandatoryNodesValidation)
82                 .add("unique", uniqueIndexes).toString();
83     }
84
85     public static DataTreeConfiguration getDefault(final TreeType treeType) {
86         switch (requireNonNull(treeType)) {
87             case CONFIGURATION:
88                 return DEFAULT_CONFIGURATION;
89             case OPERATIONAL:
90                 return DEFAULT_OPERATIONAL;
91             default:
92                 return new DataTreeConfiguration(treeType, YangInstanceIdentifier.empty(), false, true);
93         }
94     }
95
96     public static Builder builder(final TreeType treeType) {
97         return new Builder(treeType);
98     }
99
100     public Builder copyBuilder() {
101         return new Builder(treeType)
102                 .setMandatoryNodesValidation(isMandatoryNodesValidationEnabled())
103                 .setUniqueIndexes(isUniqueIndexEnabled())
104                 .setRootPath(getRootPath());
105     }
106
107     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<DataTreeConfiguration> {
108         private final TreeType treeType;
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             this.rootPath = YangInstanceIdentifier.empty();
116         }
117
118         public Builder setUniqueIndexes(final boolean uniqueIndexes) {
119             this.uniqueIndexes = uniqueIndexes;
120             return this;
121         }
122
123         public Builder setMandatoryNodesValidation(final boolean mandatoryNodesValidation) {
124             this.mandatoryNodesValidation = mandatoryNodesValidation;
125             return this;
126         }
127
128         public Builder setRootPath(final YangInstanceIdentifier rootPath) {
129             this.rootPath = rootPath.toOptimized();
130             return this;
131         }
132
133         @Override
134         public DataTreeConfiguration build() {
135             return new DataTreeConfiguration(treeType, rootPath, uniqueIndexes, mandatoryNodesValidation);
136         }
137     }
138 }