2 * Copyright (c) 2013 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
8 package org.opendaylight.mdsal.binding.yang.types;
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.ImmutableMap.Builder;
12 import java.math.BigDecimal;
13 import java.math.BigInteger;
15 import org.opendaylight.mdsal.binding.generator.spi.TypeProvider;
16 import org.opendaylight.mdsal.binding.model.api.Restrictions;
17 import org.opendaylight.mdsal.binding.model.api.Type;
18 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
19 import org.opendaylight.mdsal.binding.model.util.Types;
20 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 public final class BaseYangTypes {
28 * mapping of basic built-in YANG types (keys) to JAVA
29 * {@link org.opendaylight.mdsal.binding.model.api.Type Type}. This
30 * map is filled with mapping data in static initialization block
32 private static final Map<String, Type> TYPE_MAP;
35 * <code>Type</code> representation of <code>boolean</code> YANG type
37 public static final Type BOOLEAN_TYPE = Types.typeForClass(Boolean.class);
40 * <code>Type</code> representation of <code>empty</code> YANG type
42 public static final Type EMPTY_TYPE = Types.typeForClass(Boolean.class);
44 public static final Type ENUM_TYPE = Types.typeForClass(Enum.class);
47 * <code>Type</code> representation of <code>int8</code> YANG type
49 public static final Type INT8_TYPE = Types.typeForClass(Byte.class);
52 * <code>Type</code> representation of <code>int16</code> YANG type
54 public static final Type INT16_TYPE = Types.typeForClass(Short.class);
57 * <code>Type</code> representation of <code>int32</code> YANG type
59 public static final Type INT32_TYPE = Types.typeForClass(Integer.class);
62 * <code>Type</code> representation of <code>int64</code> YANG type
64 public static final Type INT64_TYPE = Types.typeForClass(Long.class);
67 * <code>Type</code> representation of <code>string</code> YANG type
69 public static final Type STRING_TYPE = Types.typeForClass(String.class);
72 * <code>Type</code> representation of <code>decimal64</code> YANG type
74 public static final Type DECIMAL64_TYPE = Types.typeForClass(BigDecimal.class);
77 * <code>Type</code> representation of <code>uint8</code> YANG type
79 public static final Type UINT8_TYPE = Types.typeForClass(Short.class, singleRangeRestrictions((short)0, (short)255));
82 * <code>Type</code> representation of <code>uint16</code> YANG type
84 public static final Type UINT16_TYPE = Types.typeForClass(Integer.class, singleRangeRestrictions(0, 65535));
87 * <code>Type</code> representation of <code>uint32</code> YANG type
89 public static final Type UINT32_TYPE = Types.typeForClass(Long.class, singleRangeRestrictions(0L, 4294967295L));
92 * <code>Type</code> representation of <code>uint64</code> YANG type
94 public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class,
95 singleRangeRestrictions(BigInteger.ZERO, new BigInteger("18446744073709551615")));
97 public static final Type UNION_TYPE = new UnionType();
100 * <code>Type</code> representation of <code>binary</code> YANG type
102 public static final Type BINARY_TYPE = Types.typeForClass(byte[].class);
104 public static final Type INSTANCE_IDENTIFIER = Types.parameterizedTypeFor(Types
105 .typeForClass(InstanceIdentifier.class));
108 * It is undesirable to create instance of this class.
110 private BaseYangTypes() {
111 throw new UnsupportedOperationException();
115 final Builder<String, Type> b = ImmutableMap.<String, Type>builder();
117 b.put("boolean", BOOLEAN_TYPE);
118 b.put("empty", EMPTY_TYPE);
119 b.put("enumeration", ENUM_TYPE);
120 b.put("int8", INT8_TYPE);
121 b.put("int16", INT16_TYPE);
122 b.put("int32", INT32_TYPE);
123 b.put("int64", INT64_TYPE);
124 b.put("string", STRING_TYPE);
125 b.put("decimal64", DECIMAL64_TYPE);
126 b.put("uint8", UINT8_TYPE);
127 b.put("uint16", UINT16_TYPE);
128 b.put("uint32", UINT32_TYPE);
129 b.put("uint64", UINT64_TYPE);
130 b.put("union", UNION_TYPE);
131 b.put("binary", BINARY_TYPE);
132 b.put("instance-identifier", INSTANCE_IDENTIFIER);
134 TYPE_MAP = b.build();
137 public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {
139 * Searches <code>Type</code> value to which is YANG <code>type</code>
143 * string with YANG type name
144 * @return java <code>Type</code> representation of <code>type</code>
147 public Type javaTypeForYangType(final String type) {
148 return TYPE_MAP.get(type);
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 boolean lenientRelativeLeafrefs) {
165 return TYPE_MAP.get(type.getQName().getLocalName());
172 public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
173 final Restrictions restrictions, final boolean lenientRelativeLeafrefs) {
174 String typeName = type.getQName().getLocalName();
177 return restrictions == null ? Types.BYTE_ARRAY : Types.typeForClass(byte[].class, restrictions);
179 return Types.typeForClass(BigDecimal.class, restrictions);
181 return Types.typeForClass(Enum.class, restrictions);
183 return Types.typeForClass(Byte.class, restrictions);
185 return Types.typeForClass(Short.class, restrictions);
187 return Types.typeForClass(Integer.class, restrictions);
189 return Types.typeForClass(Long.class, restrictions);
191 return Types.typeForClass(String.class, restrictions);
193 return Types.typeForClass(Short.class, restrictions);
195 return Types.typeForClass(Integer.class, restrictions);
197 return Types.typeForClass(Long.class, restrictions);
199 return Types.typeForClass(BigInteger.class, restrictions);
203 return javaTypeForSchemaDefinitionType(type, parentNode, lenientRelativeLeafrefs);
208 public String getTypeDefaultConstruction(final LeafSchemaNode node) {
213 public String getConstructorPropertyName(final SchemaNode node) {
218 public String getParamNameFromType(final TypeDefinition<?> type) {
219 return "_" + BindingMapping.getPropertyName(type.getQName().getLocalName());
223 private static <T extends Number & Comparable<T>> Restrictions singleRangeRestrictions(final T min, final T max) {
224 return Types.getDefaultRestrictions(min, max);
227 // FIXME: why do we even have this class?
228 public static final class UnionType implements Type {
230 public String getPackageName() {
235 public String getName() {
240 public String getFullyQualifiedName() {
245 public JavaTypeName getIdentifier() {