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