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;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.List;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.ImmutableSet;
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");
37 public static final QName BINARY_QNAME = constructQName("binary");
38 public static final QName BITS_QNAME = constructQName("bits");
39 public static final QName BOOLEAN_QNAME = constructQName("boolean");
40 public static final QName DECIMAL64_QNAME = constructQName("decimal64");
41 public static final QName EMPTY_QNAME = constructQName("empty");
42 public static final QName ENUMERATION_QNAME = constructQName("enumeration");
43 public static final QName IDENTITYREF_QNAME = constructQName("identityref");
44 public static final QName INSTANCE_IDENTIFIER_QNAME = constructQName("instance-identifier");
45 public static final QName INT8_QNAME = constructQName("int8");
46 public static final QName INT16_QNAME = constructQName("int16");
47 public static final QName INT32_QNAME = constructQName("int32");
48 public static final QName INT64_QNAME = constructQName("int64");
49 public static final QName LEAFREF_QNAME = constructQName("leafref");
50 public static final QName STRING_QNAME = constructQName("string");
51 public static final QName UINT8_QNAME = constructQName("uint8");
52 public static final QName UINT16_QNAME = constructQName("uint16");
53 public static final QName UINT32_QNAME = constructQName("uint32");
54 public static final QName UINT64_QNAME = constructQName("uint64");
55 public static final QName UNION_QNAME = constructQName("union");
57 private static final Set<String> BUILD_IN_TYPES = ImmutableSet.<String> builder().add(BINARY_QNAME.getLocalName()) //
58 .add(BITS_QNAME.getLocalName()) //
59 .add(BOOLEAN_QNAME.getLocalName()) //
60 .add(DECIMAL64_QNAME.getLocalName()) //
61 .add(EMPTY_QNAME.getLocalName()) //
62 .add(ENUMERATION_QNAME.getLocalName()) //
63 .add(IDENTITYREF_QNAME.getLocalName()) //
64 .add(INSTANCE_IDENTIFIER_QNAME.getLocalName()) //
65 .add(INT8_QNAME.getLocalName()) //
66 .add(INT16_QNAME.getLocalName()) //
67 .add(INT32_QNAME.getLocalName()) //
68 .add(INT64_QNAME.getLocalName()) //
69 .add(LEAFREF_QNAME.getLocalName()) //
70 .add(STRING_QNAME.getLocalName()) //
71 .add(UINT8_QNAME.getLocalName()) //
72 .add(UINT16_QNAME.getLocalName()) //
73 .add(UINT32_QNAME.getLocalName()) //
74 .add(UINT64_QNAME.getLocalName()) //
75 .add(UNION_QNAME.getLocalName()) //
79 * Construct QName for Built-in base Yang type. The namespace for built-in
80 * base yang types is defined as: urn:ietf:params:xml:ns:yang:1
84 * @return built-in base yang type QName.
86 public static QName constructQName(final String typeName) {
87 return new QName(BASE_TYPES_NAMESPACE, typeName);
91 * Creates Schema Path from {@link QName}.
95 * @return Schema Path from Qname.
96 * @deprecated Use {@link SchemaPath#create(boolean, QName...)} instead.
99 public static SchemaPath schemaPath(final QName typeName) {
100 return SchemaPath.create(Collections.singletonList(typeName), true);
104 * Creates Schema Path from List of partial paths defined as Strings, module
105 * Namespace and module latest Revision Date.
108 * List of partial paths
113 * @return Schema Path
115 * @deprecated Use {@link SchemaPath#create(boolean, Iterable)} with QNames
116 * manually constructed.
119 public static SchemaPath schemaPath(final List<String> actualPath, final URI namespace, final Date revision) {
120 if (actualPath == null) {
121 throw new IllegalArgumentException("The actual path List MUST be specified.");
123 final List<QName> pathList = new ArrayList<QName>();
124 for (final String path : actualPath) {
125 final QName qname = new QName(namespace, revision, path);
130 return SchemaPath.create(pathList, true);
134 * Returns true if supplied type is representation of built-in YANG type as
137 * See package documentation for description of base types.
140 * @return true if type is built-in YANG Types.
142 public static boolean isYangBuildInType(final String type) {
143 return BUILD_IN_TYPES.contains(type);
147 * Returns default instance of built-in for supplied type
149 * See package documentation for description of base build-in types
150 * with default instance.
153 * @return Returns default instance or {@link Optional#absent()} if default
154 * instance does not exists
157 public static Optional<TypeDefinition<?>> defaultBaseTypeFor(final String typeName) {
158 return Optional.<TypeDefinition<?>> fromNullable(defaultBaseTypeForImpl(typeName));
161 private static TypeDefinition<?> defaultBaseTypeForImpl(final String typeName) {
162 Preconditions.checkNotNull(typeName, "typeName must not be null.");
164 if (typeName.startsWith("int")) {
165 if ("int8".equals(typeName)) {
166 return Int8.getInstance();
167 } else if ("int16".equals(typeName)) {
168 return Int16.getInstance();
169 } else if ("int32".equals(typeName)) {
170 return Int32.getInstance();
171 } else if ("int64".equals(typeName)) {
172 return Int64.getInstance();
174 } else if (typeName.startsWith("uint")) {
175 if ("uint8".equals(typeName)) {
176 return Uint8.getInstance();
177 } else if ("uint16".equals(typeName)) {
178 return Uint16.getInstance();
179 } else if ("uint32".equals(typeName)) {
180 return Uint32.getInstance();
181 } else if ("uint64".equals(typeName)) {
182 return Uint64.getInstance();
184 } else if ("string".equals(typeName)) {
185 return StringType.getInstance();
186 } else if ("binary".equals(typeName)) {
187 return BinaryType.getInstance();
188 } else if ("boolean".equals(typeName)) {
189 return BooleanType.getInstance();
190 } else if ("empty".equals(typeName)) {
191 return EmptyType.getInstance();
192 } else if ("instance-identifier".equals(typeName)) {
193 return InstanceIdentifier.getInstance();