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