2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.mdsal.binding.javav2.generator.yang.types;
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;
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;
34 public final class BaseYangTypes {
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
40 private static final Map<String, Type> TYPE_MAP;
43 * <code>Type</code> representation of <code>binary</code> YANG type
45 public static final Type BINARY_TYPE = Types.primitiveType("byte[]", null);
48 * <code>Type</code> representation of <code>boolean</code> YANG type
50 public static final Type BOOLEAN_TYPE = Types.typeForClass(Boolean.class);
53 * <code>Type</code> representation of <code>decimal64</code> YANG type
55 public static final Type DECIMAL64_TYPE = Types.typeForClass(BigDecimal.class);
58 * <code>Type</code> representation of <code>empty</code> YANG type
60 public static final Type EMPTY_TYPE = Types.typeForClass(Empty.class);
63 * <code>Type</code> representation of <code>enumeration</code> YANG type
65 public static final Type ENUM_TYPE = Types.typeForClass(Enum.class);
68 * <code>Type</code> representation of <code>instance-identifier</code> YANG type
70 public static final Type INSTANCE_IDENTIFIER = Types.parameterizedTypeFor(Types
71 .typeForClass(InstanceIdentifier.class));
74 * <code>Type</code> representation of <code>int8</code> YANG type
76 public static final Type INT8_TYPE = Types.typeForClass(Byte.class);
79 * <code>Type</code> representation of <code>int16</code> YANG type
81 public static final Type INT16_TYPE = Types.typeForClass(Short.class);
84 * <code>Type</code> representation of <code>int32</code> YANG type
86 public static final Type INT32_TYPE = Types.typeForClass(Integer.class);
89 * <code>Type</code> representation of <code>int64</code> YANG type
91 public static final Type INT64_TYPE = Types.typeForClass(Long.class);
94 * <code>Type</code> representation of <code>string</code> YANG type
96 public static final Type STRING_TYPE = Types.typeForClass(String.class);
99 * <code>Type</code> representation of <code>uint8</code> YANG type
101 public static final Type UINT8_TYPE = Types.typeForClass(Uint8.class);
104 * <code>Type</code> representation of <code>uint16</code> YANG type
106 public static final Type UINT16_TYPE = Types.typeForClass(Uint16.class);
109 * <code>Type</code> representation of <code>uint32</code> YANG type
111 public static final Type UINT32_TYPE = Types.typeForClass(Uint32.class);
114 * <code>Type</code> representation of <code>uint64</code> YANG type
116 public static final Type UINT64_TYPE = Types.typeForClass(Uint64.class);
119 * <code>Type</code> representation of <code>union</code> YANG type
121 public static final Type UNION_TYPE = new UnionType();
123 private BaseYangTypes() {
124 throw new UnsupportedOperationException();
128 final Builder<String, Type> b = ImmutableMap.builder();
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);
147 TYPE_MAP = b.build();
150 public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {
152 * Searches <code>Type</code> value to which is YANG <code>type</code>
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
162 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
163 final ModuleContext context) {
165 return TYPE_MAP.get(type.getQName().getLocalName());
172 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
173 final Restrictions restrictions, final ModuleContext context) {
175 final String typeName = type.getQName().getLocalName();
178 return restrictions == null ? Types.BYTE_ARRAY : Types.primitiveType("byte[]", restrictions);
180 return Types.typeForClass(BigDecimal.class, restrictions, context);
182 return Types.typeForClass(Enum.class, restrictions, context);
184 return Types.typeForClass(Byte.class, restrictions, context);
186 return Types.typeForClass(Short.class, restrictions, context);
188 return Types.typeForClass(Integer.class, restrictions, context);
190 return Types.typeForClass(Long.class, restrictions, context);
192 return Types.typeForClass(String.class, restrictions, context);
194 return Types.typeForClass(Uint8.class, restrictions, context);
196 return Types.typeForClass(Uint16.class, restrictions, context);
198 return Types.typeForClass(Uint32.class, restrictions, context);
200 return Types.typeForClass(Uint64.class, restrictions, context);
204 return javaTypeForSchemaDefinitionType(type, parentNode, context);
209 public String getTypeDefaultConstruction(final LeafSchemaNode node) {
214 public String getConstructorPropertyName(final SchemaNode node) {
219 public String getParamNameFromType(final TypeDefinition<?> type) {
220 return JavaIdentifierNormalizer.normalizeSpecificIdentifier(type.getQName().getLocalName(),
221 JavaIdentifier.METHOD);
226 private static <T extends Number & Comparable<T>> Restrictions singleRangeRestrictions(final T min, final T max) {
227 return Types.getDefaultRestrictions(min, max);
230 public static final class UnionType implements Type {
232 public String getPackageName() {
233 return BindingMapping.PACKAGE_PREFIX;
236 public String getName() {
240 public String getFullyQualifiedName() {