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.collect.ImmutableSet;
15 import java.util.ArrayList;
16 import java.util.Date;
17 import java.util.List;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 * Utility methods and constants to work with built-in YANG types
30 public final class BaseTypes {
35 public static final URI BASE_TYPES_NAMESPACE = URI.create("urn:ietf:params:xml:ns:yang:1");
36 public static final QNameModule BASE_TYPES_MODULE = QNameModule.create(BASE_TYPES_NAMESPACE, null);
38 public static final QName BINARY_QNAME = constructQName("binary");
39 public static final QName BITS_QNAME = constructQName("bits");
40 public static final QName BOOLEAN_QNAME = constructQName("boolean");
41 public static final QName DECIMAL64_QNAME = constructQName("decimal64");
42 public static final QName EMPTY_QNAME = constructQName("empty");
43 public static final QName ENUMERATION_QNAME = constructQName("enumeration");
44 public static final QName IDENTITYREF_QNAME = constructQName("identityref");
45 public static final QName INSTANCE_IDENTIFIER_QNAME = constructQName("instance-identifier");
46 public static final QName INT8_QNAME = constructQName("int8");
47 public static final QName INT16_QNAME = constructQName("int16");
48 public static final QName INT32_QNAME = constructQName("int32");
49 public static final QName INT64_QNAME = constructQName("int64");
50 public static final QName LEAFREF_QNAME = constructQName("leafref");
51 public static final QName STRING_QNAME = constructQName("string");
52 public static final QName UINT8_QNAME = constructQName("uint8");
53 public static final QName UINT16_QNAME = constructQName("uint16");
54 public static final QName UINT32_QNAME = constructQName("uint32");
55 public static final QName UINT64_QNAME = constructQName("uint64");
56 public static final QName UNION_QNAME = constructQName("union");
58 private static final Set<String> BUILD_IN_TYPES = ImmutableSet.<String> builder().add(BINARY_QNAME.getLocalName()) //
59 .add(BITS_QNAME.getLocalName()) //
60 .add(BOOLEAN_QNAME.getLocalName()) //
61 .add(DECIMAL64_QNAME.getLocalName()) //
62 .add(EMPTY_QNAME.getLocalName()) //
63 .add(ENUMERATION_QNAME.getLocalName()) //
64 .add(IDENTITYREF_QNAME.getLocalName()) //
65 .add(INSTANCE_IDENTIFIER_QNAME.getLocalName()) //
66 .add(INT8_QNAME.getLocalName()) //
67 .add(INT16_QNAME.getLocalName()) //
68 .add(INT32_QNAME.getLocalName()) //
69 .add(INT64_QNAME.getLocalName()) //
70 .add(LEAFREF_QNAME.getLocalName()) //
71 .add(STRING_QNAME.getLocalName()) //
72 .add(UINT8_QNAME.getLocalName()) //
73 .add(UINT16_QNAME.getLocalName()) //
74 .add(UINT32_QNAME.getLocalName()) //
75 .add(UINT64_QNAME.getLocalName()) //
76 .add(UNION_QNAME.getLocalName()) //
80 * Construct QName for Built-in base Yang type. The namespace for built-in
81 * base yang types is defined as: urn:ietf:params:xml:ns:yang:1
85 * @return built-in base yang type QName.
87 public static QName constructQName(final String typeName) {
88 return QName.create(BASE_TYPES_MODULE, "", typeName);
92 * Creates Schema Path from {@link QName}.
96 * @return Schema Path from Qname.
97 * @deprecated Use {@link SchemaPath#create(boolean, QName...)} instead.
100 public static SchemaPath schemaPath(final QName typeName) {
101 return SchemaPath.create(true, typeName);
105 * Creates Schema Path from List of partial paths defined as Strings, module
106 * Namespace and module latest Revision Date.
109 * List of partial paths
114 * @return Schema Path
116 * @deprecated Use {@link SchemaPath#create(boolean, Iterable)} with QNames
117 * manually constructed.
120 public static SchemaPath schemaPath(final List<String> actualPath, final URI namespace, final Date revision) {
121 if (actualPath == null) {
122 throw new IllegalArgumentException("The actual path List MUST be specified.");
124 final List<QName> pathList = new ArrayList<QName>();
125 for (final String path : actualPath) {
126 final QName qname = QName.create(namespace, revision, path);
131 return SchemaPath.create(pathList, true);
135 * Returns true if supplied type is representation of built-in YANG type as
138 * See package documentation for description of base types.
141 * @return true if type is built-in YANG Types.
143 public static boolean isYangBuildInType(final String type) {
144 return BUILD_IN_TYPES.contains(type);
148 * Returns default instance of built-in for supplied type
150 * See package documentation for description of base build-in types
151 * with default instance.
154 * @return Returns default instance or {@link Optional#absent()} if default
155 * instance does not exists
158 public static Optional<TypeDefinition<?>> defaultBaseTypeFor(final String typeName) {
159 return Optional.<TypeDefinition<?>> fromNullable(defaultBaseTypeForImpl(typeName));
162 private static TypeDefinition<?> defaultBaseTypeForImpl(final String typeName) {
163 Preconditions.checkNotNull(typeName, "typeName must not be null.");
165 if (typeName.startsWith("int")) {
166 if ("int8".equals(typeName)) {
167 return Int8.getInstance();
168 } else if ("int16".equals(typeName)) {
169 return Int16.getInstance();
170 } else if ("int32".equals(typeName)) {
171 return Int32.getInstance();
172 } else if ("int64".equals(typeName)) {
173 return Int64.getInstance();
175 } else if (typeName.startsWith("uint")) {
176 if ("uint8".equals(typeName)) {
177 return Uint8.getInstance();
178 } else if ("uint16".equals(typeName)) {
179 return Uint16.getInstance();
180 } else if ("uint32".equals(typeName)) {
181 return Uint32.getInstance();
182 } else if ("uint64".equals(typeName)) {
183 return Uint64.getInstance();
185 } else if ("string".equals(typeName)) {
186 return StringType.getInstance();
187 } else if ("binary".equals(typeName)) {
188 return BinaryType.getInstance();
189 } else if ("boolean".equals(typeName)) {
190 return BooleanType.getInstance();
191 } else if ("empty".equals(typeName)) {
192 return EmptyType.getInstance();
193 } else if ("instance-identifier".equals(typeName)) {
194 return InstanceIdentifierType.getInstance();