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