Fixed bug in handling enumeration in BaseYangTypes.
[yangtools.git] / code-generator / binding-type-provider / src / main / java / org / opendaylight / yangtools / sal / binding / yang / types / BaseYangTypes.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.yangtools.sal.binding.yang.types;\r
9 \r
10 import java.math.BigDecimal;\r
11 import java.math.BigInteger;\r
12 import java.util.HashMap;\r
13 import java.util.Map;\r
14 \r
15 import org.opendaylight.yangtools.binding.generator.util.Types;\r
16 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;\r
17 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;\r
18 import org.opendaylight.yangtools.sal.binding.model.api.Type;\r
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;\r
21 import org.opendaylight.yangtools.yang.model.api.SchemaNode;\r
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;\r
23 \r
24 public final class BaseYangTypes {\r
25     /**\r
26      * mapping of basic built-in YANG types (keys) to JAVA\r
27      * {@link org.opendaylight.yangtools.sal.binding.model.api.Type Type}. This\r
28      * map is filled with mapping data in static initialization block\r
29      */\r
30     private static Map<String, Type> typeMap = new HashMap<String, Type>();\r
31 \r
32     /**\r
33      * <code>Type</code> representation of <code>boolean</code> YANG type\r
34      */\r
35     public static final Type BOOLEAN_TYPE = Types.typeForClass(Boolean.class);\r
36 \r
37     /**\r
38      * <code>Type</code> representation of <code>empty</code> YANG type\r
39      */\r
40     public static final Type EMPTY_TYPE = Types.typeForClass(Boolean.class);\r
41 \r
42     /**\r
43      * <code>Type</code> representation of <code>int8</code> YANG type\r
44      */\r
45     public static final Type INT8_TYPE = Types.typeForClass(Byte.class);\r
46 \r
47     /**\r
48      * <code>Type</code> representation of <code>int16</code> YANG type\r
49      */\r
50     public static final Type INT16_TYPE = Types.typeForClass(Short.class);\r
51 \r
52     /**\r
53      * <code>Type</code> representation of <code>int32</code> YANG type\r
54      */\r
55     public static final Type INT32_TYPE = Types.typeForClass(Integer.class);\r
56 \r
57     /**\r
58      * <code>Type</code> representation of <code>int64</code> YANG type\r
59      */\r
60     public static final Type INT64_TYPE = Types.typeForClass(Long.class);\r
61 \r
62     /**\r
63      * <code>Type</code> representation of <code>string</code> YANG type\r
64      */\r
65     public static final Type STRING_TYPE = Types.typeForClass(String.class);\r
66 \r
67     /**\r
68      * <code>Type</code> representation of <code>decimal64</code> YANG type\r
69      */\r
70     public static final Type DECIMAL64_TYPE = Types.typeForClass(BigDecimal.class);\r
71 \r
72     /**\r
73      * <code>Type</code> representation of <code>uint8</code> YANG type\r
74      */\r
75     public static final Type UINT8_TYPE = Types.typeForClass(Short.class);\r
76 \r
77     /**\r
78      * <code>Type</code> representation of <code>uint16</code> YANG type\r
79      */\r
80     public static final Type UINT16_TYPE = Types.typeForClass(Integer.class);\r
81 \r
82     /**\r
83      * <code>Type</code> representation of <code>uint32</code> YANG type\r
84      */\r
85     public static final Type UINT32_TYPE = Types.typeForClass(Long.class);\r
86 \r
87     /**\r
88      * <code>Type</code> representation of <code>uint64</code> YANG type\r
89      */\r
90     public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class);\r
91 \r
92     /**\r
93      * <code>Type</code> representation of <code>binary</code> YANG type\r
94      */\r
95     public static final Type BINARY_TYPE = Types.primitiveType("byte[]", null);\r
96 \r
97     public static final Type INSTANCE_IDENTIFIER = Types.parameterizedTypeFor(Types\r
98             .typeForClass(InstanceIdentifier.class));\r
99 \r
100     /**\r
101      * It is undesirable to create instance of this class.\r
102      */\r
103     private BaseYangTypes() {\r
104 \r
105     }\r
106 \r
107     static {\r
108         typeMap.put("boolean", BOOLEAN_TYPE);\r
109         typeMap.put("empty", EMPTY_TYPE);\r
110         typeMap.put("int8", INT8_TYPE);\r
111         typeMap.put("int16", INT16_TYPE);\r
112         typeMap.put("int32", INT32_TYPE);\r
113         typeMap.put("int64", INT64_TYPE);\r
114         typeMap.put("string", STRING_TYPE);\r
115         typeMap.put("decimal64", DECIMAL64_TYPE);\r
116         typeMap.put("uint8", UINT8_TYPE);\r
117         typeMap.put("uint16", UINT16_TYPE);\r
118         typeMap.put("uint32", UINT32_TYPE);\r
119         typeMap.put("uint64", UINT64_TYPE);\r
120         typeMap.put("binary", BINARY_TYPE);\r
121         typeMap.put("instance-identifier", INSTANCE_IDENTIFIER );\r
122     }\r
123 \r
124     public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {\r
125         /**\r
126          * Searches <code>Type</code> value to which is YANG <code>type</code>\r
127          * mapped.\r
128          *\r
129          * @param type\r
130          *            string with YANG type name\r
131          * @return java <code>Type</code> representation of <code>type</code>\r
132          */\r
133         @Override\r
134         public Type javaTypeForYangType(String type) {\r
135             return typeMap.get(type);\r
136         }\r
137 \r
138         /**\r
139          * Searches <code>Type</code> value to which is YANG <code>type</code>\r
140          * mapped.\r
141          *\r
142          * @param type\r
143          *            type definition representation of YANG type\r
144          * @return java <code>Type</code> representation of <code>type</code>.\r
145          *         If <code>type</code> isn't found then <code>null</code> is\r
146          *         returned.\r
147          */\r
148         @Override\r
149         public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode) {\r
150             if (type != null) {\r
151                 return typeMap.get(type.getQName().getLocalName());\r
152             }\r
153 \r
154             return null;\r
155         }\r
156 \r
157         @Override\r
158         public Type javaTypeForSchemaDefinitionType(TypeDefinition<?> type, SchemaNode parentNode, Restrictions restrictions) {\r
159             String typeName = type.getQName().getLocalName();\r
160             switch (typeName) {\r
161             case "binary" : return Types.primitiveType("byte[]", restrictions);\r
162             case "decimal64": return Types.typeForClass(BigDecimal.class, restrictions);\r
163             case "enumeration": return Types.typeForClass(Enum.class, restrictions);\r
164             case "int8": return Types.typeForClass(Byte.class, restrictions);\r
165             case "int16": return Types.typeForClass(Short.class, restrictions);\r
166             case "int32": return Types.typeForClass(Integer.class, restrictions);\r
167             case "int64": return Types.typeForClass(Long.class, restrictions);\r
168             case "string": return Types.typeForClass(String.class, restrictions);\r
169             case "uint8": return Types.typeForClass(Short.class, restrictions);\r
170             case "uint16": Types.typeForClass(Integer.class, restrictions);\r
171             case "uint32": Types.typeForClass(Long.class, restrictions);\r
172             case "uint64": Types.typeForClass(BigInteger.class, restrictions);\r
173             default: return javaTypeForSchemaDefinitionType(type, parentNode);\r
174             }\r
175         }\r
176 \r
177         @Override\r
178         public String getTypeDefaultConstruction(LeafSchemaNode node) {\r
179             return null;\r
180         }\r
181 \r
182         @Override\r
183         public String getConstructorPropertyName(SchemaNode node) {\r
184             return null;\r
185         }\r
186     };\r
187 }\r