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