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.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 public final class BaseYangTypes {
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
35 private static final Map<String, Type> TYPE_MAP;
38 * <code>Type</code> representation of <code>binary</code> YANG type
40 public static final Type BINARY_TYPE = Types.primitiveType("byte[]", null);
43 * <code>Type</code> representation of <code>boolean</code> YANG type
45 public static final Type BOOLEAN_TYPE = Types.typeForClass(Boolean.class);
48 * <code>Type</code> representation of <code>decimal64</code> YANG type
50 public static final Type DECIMAL64_TYPE = Types.typeForClass(BigDecimal.class);
53 * <code>Type</code> representation of <code>empty</code> YANG type
55 public static final Type EMPTY_TYPE = Types.typeForClass(Boolean.class);
58 * <code>Type</code> representation of <code>enumeration</code> YANG type
60 public static final Type ENUM_TYPE = Types.typeForClass(Enum.class);
63 * <code>Type</code> representation of <code>instance-identifier</code> YANG type
65 public static final Type INSTANCE_IDENTIFIER = Types.parameterizedTypeFor(Types
66 .typeForClass(InstanceIdentifier.class));
69 * <code>Type</code> representation of <code>int8</code> YANG type
71 public static final Type INT8_TYPE = Types.typeForClass(Byte.class);
74 * <code>Type</code> representation of <code>int16</code> YANG type
76 public static final Type INT16_TYPE = Types.typeForClass(Short.class);
79 * <code>Type</code> representation of <code>int32</code> YANG type
81 public static final Type INT32_TYPE = Types.typeForClass(Integer.class);
84 * <code>Type</code> representation of <code>int64</code> YANG type
86 public static final Type INT64_TYPE = Types.typeForClass(Long.class);
89 * <code>Type</code> representation of <code>string</code> YANG type
91 public static final Type STRING_TYPE = Types.typeForClass(String.class);
94 * <code>Type</code> representation of <code>uint8</code> YANG type
96 public static final Type UINT8_TYPE = Types.typeForClass(Short.class, singleRangeRestrictions((short)0, (short)255));
99 * <code>Type</code> representation of <code>uint16</code> YANG type
101 public static final Type UINT16_TYPE = Types.typeForClass(Integer.class, singleRangeRestrictions(0, 65535));
104 * <code>Type</code> representation of <code>uint32</code> YANG type
106 public static final Type UINT32_TYPE = Types.typeForClass(Long.class, singleRangeRestrictions(0L, 4294967295L));
109 * <code>Type</code> representation of <code>uint64</code> YANG type
111 public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class,
112 singleRangeRestrictions(BigInteger.ZERO, new BigInteger("18446744073709551615")));
115 * <code>Type</code> representation of <code>union</code> YANG type
117 public static final Type UNION_TYPE = new UnionType();
119 private BaseYangTypes() {
120 throw new UnsupportedOperationException();
124 final Builder<String, Type> b = ImmutableMap.builder();
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);
143 TYPE_MAP = b.build();
146 public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {
148 * Searches <code>Type</code> value to which is YANG <code>type</code>
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
158 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
159 ModuleContext context) {
161 return TYPE_MAP.get(type.getQName().getLocalName());
168 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
169 final Restrictions restrictions, ModuleContext context) {
171 final String typeName = type.getQName().getLocalName();
174 return restrictions == null ? Types.BYTE_ARRAY : Types.primitiveType("byte[]", restrictions);
176 return Types.typeForClass(BigDecimal.class, restrictions, context);
178 return Types.typeForClass(Enum.class, restrictions, context);
180 return Types.typeForClass(Byte.class, restrictions, context);
182 return Types.typeForClass(Short.class, restrictions, context);
184 return Types.typeForClass(Integer.class, restrictions, context);
186 return Types.typeForClass(Long.class, restrictions, context);
188 return Types.typeForClass(String.class, restrictions, context);
190 return Types.typeForClass(Short.class, restrictions, context);
192 return Types.typeForClass(Integer.class, restrictions, context);
194 return Types.typeForClass(Long.class, restrictions, context);
196 return Types.typeForClass(BigInteger.class, restrictions, context);
200 return javaTypeForSchemaDefinitionType(type, parentNode, context);
205 public String getTypeDefaultConstruction(final LeafSchemaNode node) {
210 public String getConstructorPropertyName(final SchemaNode node) {
215 public String getParamNameFromType(final TypeDefinition<?> type) {
216 return JavaIdentifierNormalizer.normalizeSpecificIdentifier(type.getQName().getLocalName(),
217 JavaIdentifier.METHOD);
221 private static <T extends Number> Restrictions singleRangeRestrictions(final T min, final T max) {
222 return Types.getDefaultRestrictions(min, max);
225 public static final class UnionType implements Type {
227 public String getPackageName() {
228 return BindingMapping.PACKAGE_PREFIX;
231 public String getName() {
235 public String getFullyQualifiedName() {