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