f0b1db4fbe3d2cf2e5a14ec618f61a49021e853d
[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.Strings;
11 import com.google.common.collect.ImmutableSet;
12 import java.net.URI;
13 import java.util.Set;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.common.YangConstants;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
18
19 /**
20  * Utility methods and constants to work with built-in YANG types
21  *
22  *
23  */
24 public final class BaseTypes {
25
26     private BaseTypes() {
27     }
28
29     /**
30      * @deprecated Use {@link YangConstants#RFC6020_YANG_NAMESPACE} instead. To be removed in Boron release.
31      */
32     @Deprecated
33     public static final URI BASE_TYPES_NAMESPACE = YangConstants.RFC6020_YANG_NAMESPACE;
34     /**
35      * @deprecated Use {@link YangConstants#RFC6020_YANG_MODULE} instead. To be removed in Boron release.
36      */
37     @Deprecated
38     public static final QNameModule BASE_TYPES_MODULE = YangConstants.RFC6020_YANG_MODULE;
39
40     public static final QName BINARY_QNAME = constructQName("binary");
41     public static final QName BITS_QNAME = constructQName("bits");
42     public static final QName BOOLEAN_QNAME = constructQName("boolean");
43     public static final QName DECIMAL64_QNAME = constructQName("decimal64");
44     public static final QName EMPTY_QNAME = constructQName("empty");
45     public static final QName ENUMERATION_QNAME = constructQName("enumeration");
46     public static final QName IDENTITYREF_QNAME = constructQName("identityref");
47     public static final QName INSTANCE_IDENTIFIER_QNAME = constructQName("instance-identifier");
48     public static final QName INT8_QNAME = constructQName("int8");
49     public static final QName INT16_QNAME = constructQName("int16");
50     public static final QName INT32_QNAME = constructQName("int32");
51     public static final QName INT64_QNAME = constructQName("int64");
52     public static final QName LEAFREF_QNAME = constructQName("leafref");
53     public static final QName STRING_QNAME = constructQName("string");
54     public static final QName UINT8_QNAME = constructQName("uint8");
55     public static final QName UINT16_QNAME = constructQName("uint16");
56     public static final QName UINT32_QNAME = constructQName("uint32");
57     public static final QName UINT64_QNAME = constructQName("uint64");
58     public static final QName UNION_QNAME = constructQName("union");
59
60     private static final Set<QName> BUILT_IN_TYPES = ImmutableSet.<QName>builder()
61             .add(BINARY_QNAME)
62             .add(BITS_QNAME)
63             .add(BOOLEAN_QNAME)
64             .add(DECIMAL64_QNAME)
65             .add(EMPTY_QNAME)
66             .add(ENUMERATION_QNAME)
67             .add(IDENTITYREF_QNAME)
68             .add(INSTANCE_IDENTIFIER_QNAME)
69             .add(INT8_QNAME)
70             .add(INT16_QNAME)
71             .add(INT32_QNAME)
72             .add(INT64_QNAME)
73             .add(LEAFREF_QNAME)
74             .add(STRING_QNAME)
75             .add(UINT8_QNAME)
76             .add(UINT16_QNAME)
77             .add(UINT32_QNAME)
78             .add(UINT64_QNAME)
79             .add(UNION_QNAME)
80             .build();
81
82     /**
83      * Construct QName for Built-in base Yang type. The namespace for built-in
84      * base yang types is defined as: urn:ietf:params:xml:ns:yang:1
85      *
86      * @param typeName
87      *            yang type name
88      * @return built-in base yang type QName.
89      */
90     public static QName constructQName(final String typeName) {
91         return QName.create(YangConstants.RFC6020_YANG_MODULE, typeName).intern();
92     }
93
94     /**
95      * Returns true if supplied type is representation of built-in YANG type as
96      * per RFC 6020.
97      *
98      * See package documentation for description of base types.
99      *
100      * @param type
101      * @return true if type is built-in YANG Types.
102      */
103     public static boolean isYangBuildInType(final String type) {
104         return !Strings.isNullOrEmpty(type) && BUILT_IN_TYPES.contains(
105                 QName.create(YangConstants.RFC6020_YANG_MODULE, type));
106     }
107
108     /**
109      * Returns true if supplied type is representation of built-in YANG type as
110      * per RFC 6020.
111      *
112      * See package documentation for description of base types.
113      *
114      * @param type
115      * @return true if type is built-in YANG Types.
116      */
117     public static boolean isYangBuildInType(final TypeDefinition<?> type) {
118         return type != null && BUILT_IN_TYPES.contains(type.getQName());
119     }
120 }