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 static com.google.common.base.Preconditions.checkNotNull;
12 import com.google.common.annotations.Beta;
13 import java.util.List;
14 import javax.annotation.Nonnull;
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.IntegerTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
27 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
32 * Restricted types are a refinement of the restrictions applied to a particular type. YANG defines restrictions only
33 * on a subset of the base types, but conceptually any such definition can hold unknown nodes.
38 * int{8,16,32,64} (range)
39 * string (length, patterns)
40 * uint{8,16,32,64} (range)
42 * instance-identifier (require-instance)
48 * empty (ignores default on derivation)
55 * This class holds methods which allow creation of restricted types using {@link TypeBuilder} and its subclasses. Each
56 * restricted type is logically anchored at a {@link SchemaPath}, but can be substituted by its base type if it does
57 * not contribute any additional restrictions. TypeBuilder instances take this into account, and result in the base type
58 * being returned from the builder when the base type and restricted type are semantically equal.
61 * Restricted types inherit the default value, description, reference, status and units from the base type, if that type
65 public final class RestrictedTypes {
66 private RestrictedTypes() {
67 throw new UnsupportedOperationException();
70 public static LengthRestrictedTypeBuilder<BinaryTypeDefinition> newBinaryBuilder(
71 @Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) {
72 return new LengthRestrictedTypeBuilder<BinaryTypeDefinition>(baseType, path) {
74 BinaryTypeDefinition buildType(final List<LengthConstraint> lengthConstraints) {
75 return new RestrictedBinaryType(getBaseType(), getPath(), getUnknownSchemaNodes(), lengthConstraints);
79 List<LengthConstraint> typeLengthConstraints() {
81 * Length constraint imposed on YANG binary type by our implementation. byte[].length is an integer,
82 * capping our ability to support arbitrary binary data.
84 return JavaLengthConstraints.INTEGER_SIZE_CONSTRAINTS;
89 public static BitsTypeBuilder newBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) {
90 return new BitsTypeBuilder(baseType, path);
93 public static TypeBuilder<BooleanTypeDefinition> newBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType,
94 @Nonnull final SchemaPath path) {
95 return new AbstractRestrictedTypeBuilder<BooleanTypeDefinition>(baseType, path) {
97 BooleanTypeDefinition buildType() {
98 return new RestrictedBooleanType(getBaseType(), getPath(), getUnknownSchemaNodes());
103 public static RangeRestrictedTypeBuilder<DecimalTypeDefinition> newDecima64Builder(
104 final DecimalTypeDefinition baseType, final SchemaPath path) {
105 return new RangeRestrictedTypeBuilder<DecimalTypeDefinition>(checkNotNull(baseType), path) {
107 DecimalTypeDefinition buildType() {
108 return new RestrictedDecimalType(getBaseType(), getPath(), getUnknownSchemaNodes(),
109 calculateRangeConstraints(getBaseType().getRangeConstraints()));
114 public static TypeBuilder<EmptyTypeDefinition> newEmptyBuilder(final EmptyTypeDefinition baseType,
115 final SchemaPath path) {
116 return new AbstractRestrictedTypeBuilder<EmptyTypeDefinition>(baseType, path) {
118 EmptyTypeDefinition buildType() {
119 return new RestrictedEmptyType(getBaseType(), getPath(), getUnknownSchemaNodes());
124 public static EnumerationTypeBuilder newEnumerationBuilder(final EnumTypeDefinition baseType,
125 final SchemaPath path) {
126 return new EnumerationTypeBuilder(baseType, path);
129 public static TypeBuilder<IdentityrefTypeDefinition> newIdentityrefBuilder(final IdentityrefTypeDefinition baseType,
130 final SchemaPath path) {
131 return new AbstractRestrictedTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
133 IdentityrefTypeDefinition buildType() {
134 return new RestrictedIdentityrefType(getBaseType(), getPath(), getUnknownSchemaNodes());
139 public static InstanceIdentifierTypeBuilder newInstanceIdentifierBuilder(
140 final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
141 return new InstanceIdentifierTypeBuilder(baseType, path);
144 public static RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition> newLeafrefBuilder(
145 final LeafrefTypeDefinition baseType, final SchemaPath path) {
146 return new RequireInstanceRestrictedTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
148 LeafrefTypeDefinition buildType() {
149 if (getRequireInstance() == getBaseType().requireInstance()) {
150 return getBaseType();
152 return new RestrictedLeafrefType(getBaseType(), getPath(), getUnknownSchemaNodes(),
153 getRequireInstance());
158 public static RangeRestrictedTypeBuilder<IntegerTypeDefinition> newIntegerBuilder(
159 final IntegerTypeDefinition baseType, final SchemaPath path) {
160 return new RangeRestrictedTypeBuilder<IntegerTypeDefinition>(checkNotNull(baseType), path) {
162 IntegerTypeDefinition buildType() {
163 return new RestrictedIntegerType(getBaseType(), getPath(), getUnknownSchemaNodes(),
164 calculateRangeConstraints(getBaseType().getRangeConstraints()));
169 public static StringTypeBuilder newStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) {
170 return new StringTypeBuilder(baseType, path);
173 public static TypeBuilder<UnionTypeDefinition> newUnionBuilder(final UnionTypeDefinition baseType,
174 final SchemaPath path) {
175 return new AbstractRestrictedTypeBuilder<UnionTypeDefinition>(baseType, path) {
177 UnionTypeDefinition buildType() {
178 return new RestrictedUnionType(getBaseType(), getPath(), getUnknownSchemaNodes());
183 public static RangeRestrictedTypeBuilder<UnsignedIntegerTypeDefinition> newUnsignedBuilder(
184 final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
185 return new RangeRestrictedTypeBuilder<UnsignedIntegerTypeDefinition>(checkNotNull(baseType), path) {
187 UnsignedIntegerTypeDefinition buildType() {
188 return new RestrictedUnsignedType(getBaseType(), getPath(), getUnknownSchemaNodes(),
189 calculateRangeConstraints(getBaseType().getRangeConstraints()));