2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util.type;
10 import com.google.common.annotations.Beta;
11 import java.math.BigDecimal;
12 import java.math.BigInteger;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
30 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
31 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
39 * Restricted types are a refinement of the restrictions applied to a particular type. YANG defines restrictions only
40 * on a subset of the base types, but conceptually any such definition can hold unknown nodes.
45 * int{8,16,32,64} (range)
46 * string (length, patterns)
47 * uint{8,16,32,64} (range)
49 * instance-identifier (require-instance)
55 * empty (ignores default on derivation)
62 * This class holds methods which allow creation of restricted types using {@link TypeBuilder} and its subclasses. Each
63 * restricted type is logically anchored at a {@link SchemaPath}, but can be substituted by its base type if it does
64 * not contribute any additional restrictions. TypeBuilder instances take this into account, and result in the base type
65 * being returned from the builder when the base type and restricted type are semantically equal.
68 * Restricted types inherit the default value, description, reference, status and units from the base type, if that type
72 public final class RestrictedTypes {
73 private RestrictedTypes() {
74 throw new UnsupportedOperationException();
77 public static @NonNull LengthRestrictedTypeBuilder<BinaryTypeDefinition> newBinaryBuilder(
78 final @NonNull BinaryTypeDefinition baseType, final @NonNull SchemaPath path) {
79 return new LengthRestrictedTypeBuilder<BinaryTypeDefinition>(baseType, path) {
81 BinaryTypeDefinition buildType(final @Nullable LengthConstraint constraint) {
82 return new RestrictedBinaryType(getBaseType(), getPath(), getUnknownSchemaNodes(), constraint);
86 LengthConstraint typeLengthConstraints() {
88 * Length constraint imposed on YANG binary type by our implementation. byte[].length is an integer,
89 * capping our ability to support arbitrary binary data.
91 return JavaLengthConstraints.INTEGER_SIZE_CONSTRAINTS;
96 public static @NonNull BitsTypeBuilder newBitsBuilder(final BitsTypeDefinition baseType,
97 final SchemaPath path) {
98 return new BitsTypeBuilder(baseType, path);
101 public static @NonNull TypeBuilder<BooleanTypeDefinition> newBooleanBuilder(
102 final @NonNull BooleanTypeDefinition baseType, final @NonNull SchemaPath path) {
103 return new AbstractRestrictedTypeBuilder<BooleanTypeDefinition>(baseType, path) {
105 BooleanTypeDefinition buildType() {
106 return new RestrictedBooleanType(getBaseType(), getPath(), getUnknownSchemaNodes());
111 public static @NonNull RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> newDecima64Builder(
112 final DecimalTypeDefinition baseType, final SchemaPath path) {
113 return new RangeRestrictedTypeBuilderWithBase<DecimalTypeDefinition, BigDecimal>(baseType, path) {
115 DecimalTypeDefinition buildType(final RangeConstraint<BigDecimal> rangeConstraint) {
116 return new RestrictedDecimalType(getBaseType(), getPath(), getUnknownSchemaNodes(), rangeConstraint);
121 public static @NonNull TypeBuilder<EmptyTypeDefinition> newEmptyBuilder(final EmptyTypeDefinition baseType,
122 final SchemaPath path) {
123 return new AbstractRestrictedTypeBuilder<EmptyTypeDefinition>(baseType, path) {
125 EmptyTypeDefinition buildType() {
126 return new RestrictedEmptyType(getBaseType(), getPath(), getUnknownSchemaNodes());
131 public static @NonNull EnumerationTypeBuilder newEnumerationBuilder(final EnumTypeDefinition baseType,
132 final SchemaPath path) {
133 return new EnumerationTypeBuilder(baseType, path);
136 public static @NonNull TypeBuilder<IdentityrefTypeDefinition> newIdentityrefBuilder(
137 final IdentityrefTypeDefinition baseType, final SchemaPath path) {
138 return new AbstractRestrictedTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
140 IdentityrefTypeDefinition buildType() {
141 return new RestrictedIdentityrefType(getBaseType(), getPath(), getUnknownSchemaNodes());
146 public static @NonNull InstanceIdentifierTypeBuilder newInstanceIdentifierBuilder(
147 final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
148 return new InstanceIdentifierTypeBuilder(baseType, path);
151 public static @NonNull RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> newLeafrefBuilder(
152 final LeafrefTypeDefinition baseType, final SchemaPath path) {
153 return new RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
155 LeafrefTypeDefinition buildType() {
156 final LeafrefTypeDefinition base = getBaseType();
157 if (getRequireInstance() == base.requireInstance()) {
160 return new RestrictedLeafrefType(getBaseType(), getPath(), getUnknownSchemaNodes(),
161 getRequireInstance());
166 public static @NonNull RangeRestrictedTypeBuilder<Int8TypeDefinition, Byte> newInt8Builder(
167 final Int8TypeDefinition baseType, final SchemaPath path) {
168 return new RangeRestrictedTypeBuilderWithBase<Int8TypeDefinition, Byte>(baseType, path) {
170 Int8TypeDefinition buildType(final RangeConstraint<Byte> rangeConstraint) {
171 return new RestrictedInt8Type(getBaseType(), getPath(), getUnknownSchemaNodes(), rangeConstraint);
176 public static @NonNull RangeRestrictedTypeBuilder<Int16TypeDefinition, Short> newInt16Builder(
177 final Int16TypeDefinition baseType, final SchemaPath path) {
178 return new RangeRestrictedTypeBuilderWithBase<Int16TypeDefinition, Short>(baseType, path) {
180 Int16TypeDefinition buildType(final RangeConstraint<Short> rangeConstraint) {
181 return new RestrictedInt16Type(getBaseType(), getPath(), getUnknownSchemaNodes(), rangeConstraint);
186 public static @NonNull RangeRestrictedTypeBuilder<Int32TypeDefinition, Integer> newInt32Builder(
187 final Int32TypeDefinition baseType, final SchemaPath path) {
188 return new RangeRestrictedTypeBuilderWithBase<Int32TypeDefinition, Integer>(baseType, path) {
190 Int32TypeDefinition buildType(final RangeConstraint<Integer> rangeConstraint) {
191 return new RestrictedInt32Type(getBaseType(), getPath(), getUnknownSchemaNodes(), rangeConstraint);
196 public static @NonNull RangeRestrictedTypeBuilder<Int64TypeDefinition, Long> newInt64Builder(
197 final Int64TypeDefinition baseType, final SchemaPath path) {
198 return new RangeRestrictedTypeBuilderWithBase<Int64TypeDefinition, Long>(baseType, path) {
200 Int64TypeDefinition buildType(final RangeConstraint<Long> rangeConstraint) {
201 return new RestrictedInt64Type(getBaseType(), getPath(), getUnknownSchemaNodes(), rangeConstraint);
206 public static @NonNull StringTypeBuilder newStringBuilder(final StringTypeDefinition baseType,
207 final SchemaPath path) {
208 return new StringTypeBuilder(baseType, path);
211 public static @NonNull TypeBuilder<UnionTypeDefinition> newUnionBuilder(final UnionTypeDefinition baseType,
212 final SchemaPath path) {
213 return new AbstractRestrictedTypeBuilder<UnionTypeDefinition>(baseType, path) {
215 UnionTypeDefinition buildType() {
216 return new RestrictedUnionType(getBaseType(), getPath(), getUnknownSchemaNodes());
221 public static @NonNull RangeRestrictedTypeBuilder<Uint8TypeDefinition, Short> newUint8Builder(
222 final Uint8TypeDefinition baseType, final SchemaPath path) {
223 return new RangeRestrictedTypeBuilderWithBase<Uint8TypeDefinition, Short>(baseType, path) {
225 Uint8TypeDefinition buildType(final RangeConstraint<Short> rangeConstraint) {
226 return new RestrictedUint8Type(getBaseType(), getPath(), getUnknownSchemaNodes(), rangeConstraint);
231 public static @NonNull RangeRestrictedTypeBuilder<Uint16TypeDefinition, Integer> newUint16Builder(
232 final Uint16TypeDefinition baseType, final SchemaPath path) {
233 return new RangeRestrictedTypeBuilderWithBase<Uint16TypeDefinition, Integer>(baseType, path) {
235 Uint16TypeDefinition buildType(final RangeConstraint<Integer> rangeConstraint) {
236 return new RestrictedUint16Type(getBaseType(), getPath(), getUnknownSchemaNodes(), rangeConstraint);
241 public static @NonNull RangeRestrictedTypeBuilder<Uint32TypeDefinition, Long> newUint32Builder(
242 final Uint32TypeDefinition baseType, final SchemaPath path) {
243 return new RangeRestrictedTypeBuilderWithBase<Uint32TypeDefinition, Long>(baseType, path) {
245 Uint32TypeDefinition buildType(final RangeConstraint<Long> rangeConstraint) {
246 return new RestrictedUint32Type(getBaseType(), getPath(), getUnknownSchemaNodes(), rangeConstraint);
251 public static @NonNull RangeRestrictedTypeBuilder<Uint64TypeDefinition, BigInteger> newUint64Builder(
252 final Uint64TypeDefinition baseType, final SchemaPath path) {
253 return new RangeRestrictedTypeBuilderWithBase<Uint64TypeDefinition, BigInteger>(baseType, path) {
255 Uint64TypeDefinition buildType(final RangeConstraint<BigInteger> rangeConstraint) {
256 return new RestrictedUint64Type(getBaseType(), getPath(), getUnknownSchemaNodes(), rangeConstraint);