3c48f95df38c02c5a28481906e4201bb94827f3a
[mdsal.git] / binding / mdsal-binding-generator-util / src / main / java / org / opendaylight / mdsal / binding / generator / util / BaseYangTypes.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.mdsal.binding.generator.util;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.math.BigDecimal;
12 import org.opendaylight.mdsal.binding.generator.spi.TypeProvider;
13 import org.opendaylight.mdsal.binding.model.api.Restrictions;
14 import org.opendaylight.mdsal.binding.model.api.Type;
15 import org.opendaylight.mdsal.binding.model.util.Types;
16 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.common.Empty;
19 import org.opendaylight.yangtools.yang.common.Uint16;
20 import org.opendaylight.yangtools.yang.common.Uint32;
21 import org.opendaylight.yangtools.yang.common.Uint64;
22 import org.opendaylight.yangtools.yang.common.Uint8;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26
27 public final class BaseYangTypes {
28     /**
29      * <code>Type</code> representation of <code>boolean</code> YANG type.
30      */
31     public static final Type BOOLEAN_TYPE = Types.BOOLEAN;
32
33     /**
34      * <code>Type</code> representation of <code>empty</code> YANG type.
35      */
36     public static final Type EMPTY_TYPE = Types.typeForClass(Empty.class);
37
38     public static final Type ENUM_TYPE = Types.typeForClass(Enum.class);
39
40     /**
41      * <code>Type</code> representation of <code>int8</code> YANG type.
42      */
43     public static final Type INT8_TYPE = Types.typeForClass(Byte.class);
44
45     /**
46      * <code>Type</code> representation of <code>int16</code> YANG type.
47      */
48     public static final Type INT16_TYPE = Types.typeForClass(Short.class);
49
50     /**
51      * <code>Type</code> representation of <code>int32</code> YANG type.
52      */
53     public static final Type INT32_TYPE = Types.typeForClass(Integer.class);
54
55     /**
56      * <code>Type</code> representation of <code>int64</code> YANG type.
57      */
58     public static final Type INT64_TYPE = Types.typeForClass(Long.class);
59
60     /**
61      * <code>Type</code> representation of <code>string</code> YANG type.
62      */
63     public static final Type STRING_TYPE = Types.STRING;
64
65     /**
66      * <code>Type</code> representation of <code>decimal64</code> YANG type.
67      */
68     public static final Type DECIMAL64_TYPE = Types.typeForClass(BigDecimal.class);
69
70     /**
71      * <code>Type</code> representation of <code>uint8</code> YANG type.
72      */
73     public static final Type UINT8_TYPE = Types.typeForClass(Uint8.class);
74
75     /**
76      * <code>Type</code> representation of <code>uint16</code> YANG type.
77      */
78     public static final Type UINT16_TYPE = Types.typeForClass(Uint16.class);
79
80     /**
81      * <code>Type</code> representation of <code>uint32</code> YANG type.
82      */
83     public static final Type UINT32_TYPE = Types.typeForClass(Uint32.class);
84
85     /**
86      * <code>Type</code> representation of <code>uint64</code> YANG type.
87      */
88     public static final Type UINT64_TYPE = Types.typeForClass(Uint64.class);
89
90     /**
91      * <code>Type</code> representation of <code>binary</code> YANG type.
92      */
93     public static final Type BINARY_TYPE = Types.typeForClass(byte[].class);
94
95     public static final Type INSTANCE_IDENTIFIER = Types.parameterizedTypeFor(Types
96             .typeForClass(InstanceIdentifier.class));
97
98     /**
99      * mapping of basic built-in YANG types (keys) to JAVA {@link org.opendaylight.mdsal.binding.model.api.Type Type}.
100      * This map is filled with mapping data in static initialization block.
101      */
102     private static final ImmutableMap<String, Type> TYPE_MAP = ImmutableMap.<String, Type>builder()
103             .put("boolean", BOOLEAN_TYPE)
104             .put("empty", EMPTY_TYPE)
105             .put("enumeration", ENUM_TYPE)
106             .put("int8", INT8_TYPE)
107             .put("int16", INT16_TYPE)
108             .put("int32", INT32_TYPE)
109             .put("int64", INT64_TYPE)
110             .put("string", STRING_TYPE)
111             .put("decimal64", DECIMAL64_TYPE)
112             .put("uint8", UINT8_TYPE)
113             .put("uint16", UINT16_TYPE)
114             .put("uint32", UINT32_TYPE)
115             .put("uint64", UINT64_TYPE)
116             .put("binary", BINARY_TYPE)
117             .put("instance-identifier", INSTANCE_IDENTIFIER)
118             .build();
119
120     /**
121      * It is undesirable to create instance of this class.
122      */
123     private BaseYangTypes() {
124         throw new UnsupportedOperationException();
125     }
126
127     /**
128      * Searches <code>Type</code> value to which is YANG <code>type</code>
129      * mapped.
130      *
131      * @param type
132      *            string with YANG type name
133      * @return java <code>Type</code> representation of <code>type</code>
134      */
135     public static Type javaTypeForYangType(final String type) {
136         return TYPE_MAP.get(type);
137     }
138
139     public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() {
140         /**
141          * Searches <code>Type</code> value to which is YANG <code>type</code>
142          * mapped.
143          *
144          * @param type
145          *            type definition representation of YANG type
146          * @return java <code>Type</code> representation of <code>type</code>.
147          *         If <code>type</code> isn't found then <code>null</code> is
148          *         returned.
149          */
150         @Override
151         public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
152                 final boolean lenientRelativeLeafrefs) {
153             if (type != null) {
154                 return TYPE_MAP.get(type.getQName().getLocalName());
155             }
156
157             return null;
158         }
159
160         @Override
161         public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> type, final SchemaNode parentNode,
162                 final Restrictions restrictions, final boolean lenientRelativeLeafrefs) {
163             String typeName = type.getQName().getLocalName();
164             switch (typeName) {
165                 case "binary":
166                     return restrictions == null ? Types.BYTE_ARRAY : Types.typeForClass(byte[].class, restrictions);
167                 case "decimal64":
168                     return Types.typeForClass(BigDecimal.class, restrictions);
169                 case "enumeration":
170                     return Types.typeForClass(Enum.class, restrictions);
171                 case "int8":
172                     return Types.typeForClass(Byte.class, restrictions);
173                 case "int16":
174                     return Types.typeForClass(Short.class, restrictions);
175                 case "int32":
176                     return Types.typeForClass(Integer.class, restrictions);
177                 case "int64":
178                     return Types.typeForClass(Long.class, restrictions);
179                 case "string":
180                     return Types.typeForClass(String.class, restrictions);
181                 case "uint8":
182                     return Types.typeForClass(Uint8.class, restrictions);
183                 case "uint16":
184                     return Types.typeForClass(Uint16.class, restrictions);
185                 case "uint32":
186                     return Types.typeForClass(Uint32.class, restrictions);
187                 case "uint64":
188                     return Types.typeForClass(Uint64.class, restrictions);
189                 default:
190                     return javaTypeForSchemaDefinitionType(type, parentNode, lenientRelativeLeafrefs);
191             }
192         }
193
194         @Override
195         public String getTypeDefaultConstruction(final LeafSchemaNode node) {
196             return null;
197         }
198
199         @Override
200         public String getConstructorPropertyName(final SchemaNode node) {
201             return null;
202         }
203
204         @Override
205         public String getParamNameFromType(final TypeDefinition<?> type) {
206             return "_" + BindingMapping.getPropertyName(type.getQName().getLocalName());
207         }
208     };
209 }