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