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