2c762c113a0da1fadff647fbbc95f7dd3d1fc207
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / DerivedTypes.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.model.util.type;
9
10 import com.google.common.annotations.Beta;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
13 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
27
28 /**
29  * YANG defines 'derived type' as a type created through a 'typedef' statement. These types are exposed in the
30  * hierarchical namespace and can be looked up.
31  *
32  * <p>
33  * A derived type can redefine the default value, description, status and reference of a particular type definition.
34  * It can only refine the units attribute, as that attribute is tied to the semantics of the value. The default value,
35  * and units attributes are inherited from the super (base or restricted) type, others are left undefined if not
36  * explicitly set. Status defaults to current.
37  */
38 @Beta
39 public final class DerivedTypes {
40     private DerivedTypes() {
41         throw new UnsupportedOperationException();
42     }
43
44     public static DerivedTypeBuilder<?> derivedTypeBuilder(@Nonnull final TypeDefinition<?> baseType,
45             @Nonnull final SchemaPath path) {
46         if (baseType instanceof BinaryTypeDefinition) {
47             return derivedBinaryBuilder((BinaryTypeDefinition) baseType, path);
48         } else if (baseType instanceof BitsTypeDefinition) {
49             return derivedBitsBuilder((BitsTypeDefinition) baseType, path);
50         } else if (baseType instanceof BooleanTypeDefinition) {
51             return derivedBooleanBuilder((BooleanTypeDefinition) baseType, path);
52         } else if (baseType instanceof DecimalTypeDefinition) {
53             return derivedDecimalBuilder((DecimalTypeDefinition) baseType, path);
54         } else if (baseType instanceof EmptyTypeDefinition) {
55             return derivedEmptyBuilder((EmptyTypeDefinition) baseType, path);
56         } else if (baseType instanceof EnumTypeDefinition) {
57             return derivedEnumerationBuilder((EnumTypeDefinition) baseType, path);
58         } else if (baseType instanceof IdentityrefTypeDefinition) {
59             return derivedIdentityrefBuilder((IdentityrefTypeDefinition) baseType, path);
60         } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
61             return derivedInstanceIdentifierBuilder((InstanceIdentifierTypeDefinition) baseType, path);
62         } else if (baseType instanceof IntegerTypeDefinition) {
63             return derivedIntegerBuilder((IntegerTypeDefinition) baseType, path);
64         } else if (baseType instanceof LeafrefTypeDefinition) {
65             return derivedLeafrefBuilder((LeafrefTypeDefinition) baseType, path);
66         } else if (baseType instanceof StringTypeDefinition) {
67             return derivedStringBuilder((StringTypeDefinition) baseType, path);
68         } else if (baseType instanceof UnionTypeDefinition) {
69             return derivedUnionBuilder((UnionTypeDefinition) baseType, path);
70         } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
71             return derivedUnsignedBuilder((UnsignedIntegerTypeDefinition) baseType, path);
72         } else {
73             throw new IllegalArgumentException("Unhandled type definition class " + baseType.getClass());
74         }
75     }
76
77     /**
78      * Check if a particular type is corresponds to int8. Unlike {@link BaseTypes#isInt8(TypeDefinition)}, this
79      * method performs recursive lookup to find the base type.
80      *
81      * @param type The type to check
82      * @return If the type belongs to the int8 type family.
83      * @throws NullPointerException if type is null
84      */
85     public static boolean isInt8(@Nonnull final TypeDefinition<?> type) {
86         return BaseTypes.isInt8(BaseTypes.baseTypeOf(type));
87     }
88
89     /**
90      * Check if a particular type is corresponds to int16. Unlike {@link BaseTypes#isInt16(TypeDefinition)}, this
91      * method performs recursive lookup to find the base type.
92      *
93      * @param type The type to check
94      * @return If the type belongs to the int16 type family.
95      * @throws NullPointerException if type is null
96      */
97     public static boolean isInt16(@Nonnull final TypeDefinition<?> type) {
98         return BaseTypes.isInt16(BaseTypes.baseTypeOf(type));
99     }
100
101     /**
102      * Check if a particular type is corresponds to int32. Unlike {@link BaseTypes#isInt32(TypeDefinition)}, this
103      * method performs recursive lookup to find the base type.
104      *
105      * @param type The type to check
106      * @return If the type belongs to the int32 type family.
107      * @throws NullPointerException if type is null
108      */
109     public static boolean isInt32(@Nonnull final TypeDefinition<?> type) {
110         return BaseTypes.isInt32(BaseTypes.baseTypeOf(type));
111     }
112
113     /**
114      * Check if a particular type is corresponds to int64. Unlike {@link BaseTypes#isInt64(TypeDefinition)}, this
115      * method performs recursive lookup to find the base type.
116      *
117      * @param type The type to check
118      * @return If the type belongs to the int64 type family.
119      * @throws NullPointerException if type is null
120      */
121     public static boolean isInt64(@Nonnull final TypeDefinition<?> type) {
122         return BaseTypes.isInt64(BaseTypes.baseTypeOf(type));
123     }
124
125     /**
126      * Check if a particular type is corresponds to uint8. Unlike {@link BaseTypes#isUint8(TypeDefinition)}, this
127      * method performs recursive lookup to find the base type.
128      *
129      * @param type The type to check
130      * @return If the type belongs to the uint8 type family.
131      * @throws NullPointerException if type is null
132      */
133     public static boolean isUint8(@Nonnull final TypeDefinition<?> type) {
134         return BaseTypes.isUint8(BaseTypes.baseTypeOf(type));
135     }
136
137     /**
138      * Check if a particular type is corresponds to uint16. Unlike {@link BaseTypes#isUint16(TypeDefinition)}, this
139      * method performs recursive lookup to find the base type.
140      *
141      * @param type The type to check
142      * @return If the type belongs to the uint16 type family.
143      * @throws NullPointerException if type is null
144      */
145     public static boolean isUint16(@Nonnull final TypeDefinition<?> type) {
146         return BaseTypes.isUint16(BaseTypes.baseTypeOf(type));
147     }
148
149     /**
150      * Check if a particular type is corresponds to uint32. Unlike {@link BaseTypes#isUint32(TypeDefinition)}, this
151      * method performs recursive lookup to find the base type.
152      *
153      * @param type The type to check
154      * @return If the type belongs to the uint32 type family.
155      * @throws NullPointerException if type is null
156      */
157     public static boolean isUint32(@Nonnull final TypeDefinition<?> type) {
158         return BaseTypes.isUint32(BaseTypes.baseTypeOf(type));
159     }
160
161     /**
162      * Check if a particular type is corresponds to uint64. Unlike {@link BaseTypes#isUint64(TypeDefinition)}, this
163      * method performs recursive lookup to find the base type.
164      *
165      * @param type The type to check
166      * @return If the type belongs to the uint64 type family.
167      * @throws NullPointerException if type is null
168      */
169     public static boolean isUint64(@Nonnull final TypeDefinition<?> type) {
170         return BaseTypes.isUint64(BaseTypes.baseTypeOf(type));
171     }
172
173     private static DerivedTypeBuilder<BinaryTypeDefinition> derivedBinaryBuilder(
174             @Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) {
175         return new DerivedTypeBuilder<BinaryTypeDefinition>(baseType, path) {
176             @Override
177             public BinaryTypeDefinition build() {
178                 return new DerivedBinaryType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
179                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
180             }
181         };
182     }
183
184     private static DerivedTypeBuilder<BitsTypeDefinition> derivedBitsBuilder(final BitsTypeDefinition baseType,
185             final SchemaPath path) {
186         return new DerivedTypeBuilder<BitsTypeDefinition>(baseType, path) {
187             @Override
188             public BitsTypeDefinition build() {
189                 return new DerivedBitsType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
190                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
191             }
192         };
193     }
194
195     private static DerivedTypeBuilder<BooleanTypeDefinition> derivedBooleanBuilder(
196             @Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) {
197         return new DerivedTypeBuilder<BooleanTypeDefinition>(baseType, path) {
198             @Override
199             public BooleanTypeDefinition build() {
200                 return new DerivedBooleanType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
201                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
202             }
203         };
204     }
205
206     private static DerivedTypeBuilder<DecimalTypeDefinition> derivedDecimalBuilder(final DecimalTypeDefinition baseType,
207             final SchemaPath path) {
208         return new DerivedTypeBuilder<DecimalTypeDefinition>(baseType, path) {
209             @Override
210             public DecimalTypeDefinition build() {
211                 return new DerivedDecimalType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
212                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
213             }
214         };
215     }
216
217     private static DerivedTypeBuilder<EmptyTypeDefinition> derivedEmptyBuilder(final EmptyTypeDefinition baseType,
218             final SchemaPath path) {
219         return new DerivedTypeBuilder<EmptyTypeDefinition>(baseType, path) {
220             @Override
221             public EmptyTypeDefinition build() {
222                 return new DerivedEmptyType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
223                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
224             }
225         };
226     }
227
228     private static DerivedTypeBuilder<EnumTypeDefinition> derivedEnumerationBuilder(final EnumTypeDefinition baseType,
229             final SchemaPath path) {
230         return new DerivedTypeBuilder<EnumTypeDefinition>(baseType, path) {
231             @Override
232             public EnumTypeDefinition build() {
233                 return new DerivedEnumerationType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
234                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
235             }
236         };
237     }
238
239     private static DerivedTypeBuilder<IdentityrefTypeDefinition> derivedIdentityrefBuilder(
240             final IdentityrefTypeDefinition baseType, final SchemaPath path) {
241         return new DerivedTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
242             @Override
243             public IdentityrefTypeDefinition build() {
244                 return new DerivedIdentityrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
245                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
246             }
247         };
248     }
249
250     private static DerivedTypeBuilder<InstanceIdentifierTypeDefinition> derivedInstanceIdentifierBuilder(
251             final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
252         return new DerivedTypeBuilder<InstanceIdentifierTypeDefinition>(baseType, path) {
253             @Override
254             public InstanceIdentifierTypeDefinition build() {
255                 return new DerivedInstanceIdentifierType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
256                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes(), baseType.requireInstance());
257             }
258         };
259     }
260
261     private static DerivedTypeBuilder<IntegerTypeDefinition> derivedIntegerBuilder(final IntegerTypeDefinition baseType,
262             final SchemaPath path) {
263         return new DerivedTypeBuilder<IntegerTypeDefinition>(baseType, path) {
264             @Override
265             public IntegerTypeDefinition build() {
266                 return new DerivedIntegerType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
267                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
268             }
269         };
270     }
271
272     private static DerivedTypeBuilder<LeafrefTypeDefinition> derivedLeafrefBuilder(final LeafrefTypeDefinition baseType,
273             final SchemaPath path) {
274         return new DerivedTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
275             @Override
276             public LeafrefTypeDefinition build() {
277                 return new DerivedLeafrefType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
278                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
279             }
280         };
281     }
282
283     private static DerivedTypeBuilder<StringTypeDefinition> derivedStringBuilder(final StringTypeDefinition baseType,
284             final SchemaPath path) {
285         return new DerivedTypeBuilder<StringTypeDefinition>(baseType, path) {
286             @Override
287             public StringTypeDefinition build() {
288                 return new DerivedStringType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
289                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
290             }
291         };
292     }
293
294     private static DerivedTypeBuilder<UnionTypeDefinition> derivedUnionBuilder(final UnionTypeDefinition baseType,
295             final SchemaPath path) {
296         return new DerivedTypeBuilder<UnionTypeDefinition>(baseType, path) {
297             @Override
298             public DerivedUnionType build() {
299                 return new DerivedUnionType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
300                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
301             }
302         };
303     }
304
305     private static DerivedTypeBuilder<UnsignedIntegerTypeDefinition> derivedUnsignedBuilder(
306             final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
307         return new DerivedTypeBuilder<UnsignedIntegerTypeDefinition>(baseType, path) {
308             @Override
309             public UnsignedIntegerTypeDefinition build() {
310                 return new DerivedUnsignedType(getBaseType(), getPath(), getDefaultValue(), getDescription(),
311                         getReference(), getStatus(), getUnits(), getUnknownSchemaNodes());
312             }
313         };
314     }
315 }