Bug 4969: NPE in JSONCodecFactory by attempt to find codec for a leafref
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / BaseTypes.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.yangtools.yang.model.util;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
13 import com.google.common.collect.ImmutableSet;
14 import java.net.URI;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.YangConstants;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20
21 /**
22  * Utility methods and constants to work with built-in YANG types
23  *
24  *
25  */
26 public final class BaseTypes {
27
28     private BaseTypes() {
29     }
30
31     /**
32      * @deprecated Use {@link YangConstants#RFC6020_YANG_NAMESPACE} instead. To be removed in Boron release.
33      */
34     @Deprecated
35     public static final URI BASE_TYPES_NAMESPACE = YangConstants.RFC6020_YANG_NAMESPACE;
36     /**
37      * @deprecated Use {@link YangConstants#RFC6020_YANG_MODULE} instead. To be removed in Boron release.
38      */
39     @Deprecated
40     public static final QNameModule BASE_TYPES_MODULE = YangConstants.RFC6020_YANG_MODULE;
41
42     public static final QName BINARY_QNAME = constructQName("binary");
43     public static final QName BITS_QNAME = constructQName("bits");
44     public static final QName BOOLEAN_QNAME = constructQName("boolean");
45     public static final QName DECIMAL64_QNAME = constructQName("decimal64");
46     public static final QName EMPTY_QNAME = constructQName("empty");
47     public static final QName ENUMERATION_QNAME = constructQName("enumeration");
48     public static final QName IDENTITYREF_QNAME = constructQName("identityref");
49     public static final QName INSTANCE_IDENTIFIER_QNAME = constructQName("instance-identifier");
50     public static final QName INT8_QNAME = constructQName("int8");
51     public static final QName INT16_QNAME = constructQName("int16");
52     public static final QName INT32_QNAME = constructQName("int32");
53     public static final QName INT64_QNAME = constructQName("int64");
54     public static final QName LEAFREF_QNAME = constructQName("leafref");
55     public static final QName STRING_QNAME = constructQName("string");
56     public static final QName UINT8_QNAME = constructQName("uint8");
57     public static final QName UINT16_QNAME = constructQName("uint16");
58     public static final QName UINT32_QNAME = constructQName("uint32");
59     public static final QName UINT64_QNAME = constructQName("uint64");
60     public static final QName UNION_QNAME = constructQName("union");
61
62     private static final Set<QName> BUILT_IN_TYPES = ImmutableSet.<QName>builder()
63             .add(BINARY_QNAME)
64             .add(BITS_QNAME)
65             .add(BOOLEAN_QNAME)
66             .add(DECIMAL64_QNAME)
67             .add(EMPTY_QNAME)
68             .add(ENUMERATION_QNAME)
69             .add(IDENTITYREF_QNAME)
70             .add(INSTANCE_IDENTIFIER_QNAME)
71             .add(INT8_QNAME)
72             .add(INT16_QNAME)
73             .add(INT32_QNAME)
74             .add(INT64_QNAME)
75             .add(LEAFREF_QNAME)
76             .add(STRING_QNAME)
77             .add(UINT8_QNAME)
78             .add(UINT16_QNAME)
79             .add(UINT32_QNAME)
80             .add(UINT64_QNAME)
81             .add(UNION_QNAME)
82             .build();
83
84     /**
85      * Construct QName for Built-in base Yang type. The namespace for built-in
86      * base yang types is defined as: urn:ietf:params:xml:ns:yang:1
87      *
88      * @param typeName
89      *            yang type name
90      * @return built-in base yang type QName.
91      */
92     public static QName constructQName(final String typeName) {
93         return QName.create(YangConstants.RFC6020_YANG_MODULE, typeName).intern();
94     }
95
96     /**
97      * Returns true if supplied type is representation of built-in YANG type as
98      * per RFC 6020.
99      *
100      * See package documentation for description of base types.
101      *
102      * @param type
103      * @return true if type is built-in YANG Types.
104      */
105     public static boolean isYangBuildInType(final String type) {
106         if (Strings.isNullOrEmpty(type)) {
107             return false;
108         }
109         return BUILT_IN_TYPES.contains(QName.create(YangConstants.RFC6020_YANG_MODULE, type));
110     }
111
112     /**
113      * Returns true if supplied type is representation of built-in YANG type as
114      * per RFC 6020.
115      *
116      * See package documentation for description of base types.
117      *
118      * @param type
119      * @return true if type is built-in YANG Types.
120      */
121     public static boolean isYangBuildInType(final TypeDefinition<?> type) {
122         if(type == null) {
123             return false;
124         }
125         return BUILT_IN_TYPES.contains(type.getQName());
126     }
127
128     /**
129      * Returns default instance of built-in for supplied type
130      *
131      * See package documentation for description of base build-in types
132      * with default instance.
133      *
134      * @param typeName
135      * @return Returns default instance or {@link Optional#absent()} if default
136      *         instance does not exists
137      *
138      * @deprecated Use parser-specific lookup tables.
139      */
140     @Deprecated
141     public static Optional<TypeDefinition<?>> defaultBaseTypeFor(final String typeName) {
142         return Optional.<TypeDefinition<?>> fromNullable(defaultBaseTypeForImpl(typeName));
143     }
144
145     @Deprecated
146     private static TypeDefinition<?> defaultBaseTypeForImpl(final String typeName) {
147         Preconditions.checkNotNull(typeName, "typeName must not be null.");
148
149         if (typeName.startsWith("int")) {
150             if ("int8".equals(typeName)) {
151                 return Int8.getInstance();
152             } else if ("int16".equals(typeName)) {
153                 return Int16.getInstance();
154             } else if ("int32".equals(typeName)) {
155                 return Int32.getInstance();
156             } else if ("int64".equals(typeName)) {
157                 return Int64.getInstance();
158             }
159         } else if (typeName.startsWith("uint")) {
160             if ("uint8".equals(typeName)) {
161                 return Uint8.getInstance();
162             } else if ("uint16".equals(typeName)) {
163                 return Uint16.getInstance();
164             } else if ("uint32".equals(typeName)) {
165                 return Uint32.getInstance();
166             } else if ("uint64".equals(typeName)) {
167                 return Uint64.getInstance();
168             }
169         } else if ("string".equals(typeName)) {
170             return StringType.getInstance();
171         } else if ("binary".equals(typeName)) {
172             return BinaryType.getInstance();
173         } else if ("boolean".equals(typeName)) {
174             return BooleanType.getInstance();
175         } else if ("empty".equals(typeName)) {
176             return EmptyType.getInstance();
177         } else if ("instance-identifier".equals(typeName)) {
178             return InstanceIdentifierType.getInstance();
179         }
180         return null;
181     }
182
183 }