2863b462f1f311b18120cd71011931fa74424741
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / yang / types / BaseYangTypes.java
1 /*
2  * Copyright (c) 2013 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.mdsal.binding.yang.types;
9
10 import com.google.common.collect.ImmutableMap;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.math.BigDecimal;
13 import java.math.BigInteger;
14 import java.util.Map;
15 import org.opendaylight.mdsal.binding.generator.spi.TypeProvider;
16 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
17 import org.opendaylight.mdsal.binding.model.api.Restrictions;
18 import org.opendaylight.mdsal.binding.model.api.Type;
19 import org.opendaylight.mdsal.binding.model.util.Types;
20 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25
26 public final class BaseYangTypes {
27     /**
28      * <code>Type</code> representation of <code>boolean</code> YANG type.
29      */
30     public static final Type BOOLEAN_TYPE = Types.BOOLEAN;
31
32     /**
33      * <code>Type</code> representation of <code>empty</code> YANG type.
34      */
35     public static final Type EMPTY_TYPE = BOOLEAN_TYPE;
36
37     public static final Type ENUM_TYPE = Types.typeForClass(Enum.class);
38
39     /**
40      * <code>Type</code> representation of <code>int8</code> YANG type.
41      */
42     public static final Type INT8_TYPE = Types.typeForClass(Byte.class);
43
44     /**
45      * <code>Type</code> representation of <code>int16</code> YANG type.
46      */
47     public static final Type INT16_TYPE = Types.typeForClass(Short.class);
48
49     /**
50      * <code>Type</code> representation of <code>int32</code> YANG type.
51      */
52     public static final Type INT32_TYPE = Types.typeForClass(Integer.class);
53
54     /**
55      * <code>Type</code> representation of <code>int64</code> YANG type.
56      */
57     public static final Type INT64_TYPE = Types.typeForClass(Long.class);
58
59     /**
60      * <code>Type</code> representation of <code>string</code> YANG type.
61      */
62     public static final Type STRING_TYPE = Types.STRING;
63
64     /**
65      * <code>Type</code> representation of <code>decimal64</code> YANG type.
66      */
67     public static final Type DECIMAL64_TYPE = Types.typeForClass(BigDecimal.class);
68
69     /**
70      * <code>Type</code> representation of <code>uint8</code> YANG type.
71      */
72     public static final Type UINT8_TYPE = Types.typeForClass(Short.class, singleRangeRestrictions((short)0,
73         (short)255));
74
75     /**
76      * <code>Type</code> representation of <code>uint16</code> YANG type.
77      */
78     public static final Type UINT16_TYPE = Types.typeForClass(Integer.class, singleRangeRestrictions(0, 65535));
79
80     /**
81      * <code>Type</code> representation of <code>uint32</code> YANG type.
82      */
83     public static final Type UINT32_TYPE = Types.typeForClass(Long.class, singleRangeRestrictions(0L, 4294967295L));
84
85     /**
86      * <code>Type</code> representation of <code>uint64</code> YANG type.
87      */
88     public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class,
89             singleRangeRestrictions(BigInteger.ZERO, new BigInteger("18446744073709551615")));
90
91     public static final Type UNION_TYPE = new UnionType();
92
93     /**
94      * <code>Type</code> representation of <code>binary</code> YANG type.
95      */
96     public static final Type BINARY_TYPE = Types.typeForClass(byte[].class);
97
98     public static final Type INSTANCE_IDENTIFIER = Types.parameterizedTypeFor(Types
99             .typeForClass(InstanceIdentifier.class));
100
101     /**
102      * mapping of basic built-in YANG types (keys) to JAVA {@link org.opendaylight.mdsal.binding.model.api.Type Type}.
103      * This map is filled with mapping data in static initialization block.
104      */
105     private static final Map<String, Type> TYPE_MAP = ImmutableMap.<String, Type>builder()
106             .put("boolean", BOOLEAN_TYPE)
107             .put("empty", EMPTY_TYPE)
108             .put("enumeration", ENUM_TYPE)
109             .put("int8", INT8_TYPE)
110             .put("int16", INT16_TYPE)
111             .put("int32", INT32_TYPE)
112             .put("int64", INT64_TYPE)
113             .put("string", STRING_TYPE)
114             .put("decimal64", DECIMAL64_TYPE)
115             .put("uint8", UINT8_TYPE)
116             .put("uint16", UINT16_TYPE)
117             .put("uint32", UINT32_TYPE)
118             .put("uint64", UINT64_TYPE)
119             .put("union", UNION_TYPE)
120             .put("binary", BINARY_TYPE)
121             .put("instance-identifier", INSTANCE_IDENTIFIER)
122             .build();
123
124     /**
125      * It is undesirable to create instance of this class.
126      */
127     private BaseYangTypes() {
128         throw new UnsupportedOperationException();
129     }
130
131     /**
132      * Searches <code>Type</code> value to which is YANG <code>type</code>
133      * mapped.
134      *
135      * @param type
136      *            string with YANG type name
137      * @return java <code>Type</code> representation of <code>type</code>
138      */
139     public static Type javaTypeForYangType(final String type) {
140         return TYPE_MAP.get(type);
141     }
142
143     public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {
144         /**
145          * Searches <code>Type</code> value to which is YANG <code>type</code>
146          * mapped.
147          *
148          * @param type
149          *            type definition representation of YANG type
150          * @return java <code>Type</code> representation of <code>type</code>.
151          *         If <code>type</code> isn't found then <code>null</code> is
152          *         returned.
153          */
154         @Override
155         public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode) {
156             if (type != null) {
157                 return TYPE_MAP.get(type.getQName().getLocalName());
158             }
159
160             return null;
161         }
162
163         @Override
164         public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
165                 final Restrictions restrictions) {
166             String typeName = type.getQName().getLocalName();
167             switch (typeName) {
168                 case "binary":
169                     return restrictions == null ? Types.BYTE_ARRAY : Types.typeForClass(byte[].class, restrictions);
170                 case "decimal64":
171                     return Types.typeForClass(BigDecimal.class, restrictions);
172                 case "enumeration":
173                     return Types.typeForClass(Enum.class, restrictions);
174                 case "int8":
175                     return Types.typeForClass(Byte.class, restrictions);
176                 case "int16":
177                     return Types.typeForClass(Short.class, restrictions);
178                 case "int32":
179                     return Types.typeForClass(Integer.class, restrictions);
180                 case "int64":
181                     return Types.typeForClass(Long.class, restrictions);
182                 case "string":
183                     return Types.typeForClass(String.class, restrictions);
184                 case "uint8":
185                     return Types.typeForClass(Short.class, restrictions);
186                 case "uint16":
187                     return Types.typeForClass(Integer.class, restrictions);
188                 case "uint32":
189                     return Types.typeForClass(Long.class, restrictions);
190                 case "uint64":
191                     return Types.typeForClass(BigInteger.class, restrictions);
192                 case "union" :
193                     return UNION_TYPE;
194                 default:
195                     return javaTypeForSchemaDefinitionType(type, parentNode);
196             }
197         }
198
199         @Override
200         public String getTypeDefaultConstruction(final LeafSchemaNode node) {
201             return null;
202         }
203
204         @Override
205         public String getConstructorPropertyName(final SchemaNode node) {
206             return null;
207         }
208
209         @Override
210         public String getParamNameFromType(final TypeDefinition<?> type) {
211             return "_" + BindingMapping.getPropertyName(type.getQName().getLocalName());
212         }
213     };
214
215     private static <T extends Number & Comparable<T>> Restrictions singleRangeRestrictions(final T min, final T max) {
216         return Types.getDefaultRestrictions(min, max);
217     }
218
219     // FIXME: 4.0.0: remove this class
220     @Deprecated
221     public static final class UnionType implements Type {
222         @Override
223         public String getPackageName() {
224             return null;
225         }
226
227         @Override
228         public String getName() {
229             return "Union";
230         }
231
232         @Override
233         public String getFullyQualifiedName() {
234             return "Union";
235         }
236
237         @Override
238         @SuppressFBWarnings("NP_NONNULL_RETURN_VIOLATION")
239         public JavaTypeName getIdentifier() {
240             return null;
241         }
242     }
243 }