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