92cd4c1cc913dc2e81c9f1b2c5a8362bfbc279cc
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / BaseTypes.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 org.opendaylight.yangtools.yang.model.api.SchemaPath;
12 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
13 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
20
21 /**
22  * Utility access methods for creating and accessing YANG base type definitions. YANG types come in two basic variants,
23  * depending on whether they fully define their base instance or model input is required to fully-form the type.
24  *
25  * The following types have their base type fully specified and are exposed as appropriate TypeDefinition sub-interfaces:
26  * <ul>
27  *     <li>boolean</li>
28  *     <li>empty</li>
29  *     <li>binary</li>
30  *     <li>int{8,16,32,64}</li>
31  *     <li>string</li>
32  *     <li>uint{8,16,32,64}</li>
33  * </ul>
34  *
35  * The following types require additional specification in the model and are exposed by means of a specialized
36  * {@link TypeBuilder}s for each type:
37  * <ul>
38  *    <li>decimal64</li>
39  *    <li>instance-identifier</li>
40  *    <li>enumeration</li>
41  *    <li>identityref</li>
42  *    <li>leafref</li>
43  *    <li>union</li>
44  * </ul>
45  */
46 @Beta
47 public final class BaseTypes {
48     private BaseTypes() {
49         throw new UnsupportedOperationException();
50     }
51
52     public static BinaryTypeDefinition binaryType() {
53         return BaseBinaryType.INSTANCE;
54     }
55
56     public static BitsTypeBuilder bitsTypeBuilder(final SchemaPath path) {
57         return new BitsTypeBuilder(path);
58     }
59
60     public static BooleanTypeDefinition booleanType() {
61         return BaseBooleanType.INSTANCE;
62     }
63
64     public static DecimalTypeBuilder decimalTypeBuilder(final SchemaPath path) {
65         return new DecimalTypeBuilder(path);
66     }
67
68     public static EmptyTypeDefinition emptyType() {
69         return BaseEmptyType.INSTANCE;
70     }
71
72     public static EnumerationTypeBuilder enumerationTypeBuilder(final SchemaPath path) {
73         return new EnumerationTypeBuilder(path);
74     }
75
76     public static IdentityrefTypeBuilder identityrefTypeBuilder(final SchemaPath path) {
77         return new IdentityrefTypeBuilder(path);
78     }
79
80     public static InstanceIdentifierTypeDefinition instanceIdentifierType() {
81         return BaseInstanceIdentifierType.INSTANCE;
82     }
83
84     public static IntegerTypeDefinition int8Type() {
85         return BaseInt8Type.INSTANCE;
86     }
87
88     public static boolean isInt8(final TypeDefinition<?> type) {
89         return BaseInt8Type.INSTANCE.getPath().equals(findBaseTypePath(type));
90     }
91
92     public static IntegerTypeDefinition int16Type() {
93         return BaseInt16Type.INSTANCE;
94     }
95
96     public static boolean isInt16(final TypeDefinition<?> type) {
97         return BaseInt16Type.INSTANCE.getPath().equals(findBaseTypePath(type));
98     }
99
100     public static IntegerTypeDefinition int32Type() {
101         return BaseInt32Type.INSTANCE;
102     }
103
104     public static boolean isInt32(final TypeDefinition<?> type) {
105         return BaseInt32Type.INSTANCE.getPath().equals(findBaseTypePath(type));
106     }
107
108     public static IntegerTypeDefinition int64Type() {
109         return BaseInt64Type.INSTANCE;
110     }
111
112     public static boolean isInt64(final TypeDefinition<?> type) {
113         return BaseInt64Type.INSTANCE.getPath().equals(findBaseTypePath(type));
114     }
115
116     public static LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
117         return new LeafrefTypeBuilder(path);
118     }
119
120     public static StringTypeDefinition stringType() {
121         return BaseStringType.INSTANCE;
122     }
123
124     public static UnionTypeBuilder unionTypeBuilder(final SchemaPath path) {
125         return new UnionTypeBuilder(path);
126     }
127
128     public static UnsignedIntegerTypeDefinition uint8Type() {
129         return BaseUint8Type.INSTANCE;
130     }
131
132     public static boolean isUint8(final TypeDefinition<?> type) {
133         return BaseUint8Type.INSTANCE.getPath().equals(findBaseTypePath(type));
134     }
135
136     public static UnsignedIntegerTypeDefinition uint16Type() {
137         return BaseUint16Type.INSTANCE;
138     }
139
140     public static boolean isUint16(final TypeDefinition<?> type) {
141         return BaseUint16Type.INSTANCE.getPath().equals(findBaseTypePath(type));
142     }
143
144     public static UnsignedIntegerTypeDefinition uint32Type() {
145         return BaseUint32Type.INSTANCE;
146     }
147
148     public static boolean isUint32(final TypeDefinition<?> type) {
149         return BaseUint32Type.INSTANCE.getPath().equals(findBaseTypePath(type));
150     }
151
152     public static UnsignedIntegerTypeDefinition uint64Type() {
153         return BaseUint64Type.INSTANCE;
154     }
155
156     public static boolean isUint64(final TypeDefinition<?> type) {
157         return BaseUint64Type.INSTANCE.getPath().equals(findBaseTypePath(type));
158     }
159
160     private static SchemaPath findBaseTypePath(final TypeDefinition<?> type) {
161         TypeDefinition<?> ret = type;
162         while (ret.getBaseType() != null) {
163             ret = ret.getBaseType();
164         }
165         return ret.getPath();
166     }
167 }