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.yangtools.yang.model.util;
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;
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;
22 * Utility methods and constants to work with built-in YANG types
26 public final class BaseTypes {
32 * @deprecated Use {@link YangConstants#RFC6020_YANG_NAMESPACE} instead. To be removed in Boron release.
35 public static final URI BASE_TYPES_NAMESPACE = YangConstants.RFC6020_YANG_NAMESPACE;
37 * @deprecated Use {@link YangConstants#RFC6020_YANG_MODULE} instead. To be removed in Boron release.
40 public static final QNameModule BASE_TYPES_MODULE = YangConstants.RFC6020_YANG_MODULE;
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");
62 private static final Set<QName> BUILT_IN_TYPES = ImmutableSet.<QName>builder()
68 .add(ENUMERATION_QNAME)
69 .add(IDENTITYREF_QNAME)
70 .add(INSTANCE_IDENTIFIER_QNAME)
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
90 * @return built-in base yang type QName.
92 public static QName constructQName(final String typeName) {
93 return QName.create(YangConstants.RFC6020_YANG_MODULE, typeName).intern();
97 * Returns true if supplied type is representation of built-in YANG type as
100 * See package documentation for description of base types.
103 * @return true if type is built-in YANG Types.
105 public static boolean isYangBuildInType(final String type) {
106 if (Strings.isNullOrEmpty(type)) {
109 return BUILT_IN_TYPES.contains(QName.create(YangConstants.RFC6020_YANG_MODULE, type));
113 * Returns true if supplied type is representation of built-in YANG type as
116 * See package documentation for description of base types.
119 * @return true if type is built-in YANG Types.
121 public static boolean isYangBuildInType(final TypeDefinition<?> type) {
125 return BUILT_IN_TYPES.contains(type.getQName());
129 * Returns default instance of built-in for supplied type
131 * See package documentation for description of base build-in types
132 * with default instance.
135 * @return Returns default instance or {@link Optional#absent()} if default
136 * instance does not exists
138 * @deprecated Use parser-specific lookup tables.
141 public static Optional<TypeDefinition<?>> defaultBaseTypeFor(final String typeName) {
142 return Optional.<TypeDefinition<?>> fromNullable(defaultBaseTypeForImpl(typeName));
146 private static TypeDefinition<?> defaultBaseTypeForImpl(final String typeName) {
147 Preconditions.checkNotNull(typeName, "typeName must not be null.");
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();
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();
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();