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