BUG-1276: fixed generated union constructor
[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 java.net.URI;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.List;
15 import java.util.Set;
16
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;
20
21 import com.google.common.base.Optional;
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.ImmutableSet;
24
25 /**
26  * Utility methods and constants to work with built-in YANG types
27  *
28  *
29  */
30 public final class BaseTypes {
31
32     private BaseTypes() {
33     }
34
35     public static final URI BASE_TYPES_NAMESPACE = URI.create("urn:ietf:params:xml:ns:yang:1");
36
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");
56
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()) //
76             .build();
77
78     /**
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
81      *
82      * @param typeName
83      *            yang type name
84      * @return built-in base yang type QName.
85      */
86     public static QName constructQName(final String typeName) {
87         return new QName(BASE_TYPES_NAMESPACE, typeName);
88     }
89
90     /**
91      * Creates Schema Path from {@link QName}.
92      *
93      * @param typeName
94      *            yang type QName
95      * @return Schema Path from Qname.
96      * @deprecated Use {@link SchemaPath#create(boolean, QName...)} instead.
97      */
98     @Deprecated
99     public static SchemaPath schemaPath(final QName typeName) {
100         return SchemaPath.create(Collections.singletonList(typeName), true);
101     }
102
103     /**
104      * Creates Schema Path from List of partial paths defined as Strings, module
105      * Namespace and module latest Revision Date.
106      *
107      * @param actualPath
108      *            List of partial paths
109      * @param namespace
110      *            Module Namespace
111      * @param revision
112      *            Revision Date
113      * @return Schema Path
114      *
115      * @deprecated Use {@link SchemaPath#create(boolean, Iterable)} with QNames
116      *             manually constructed.
117      */
118     @Deprecated
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.");
122         }
123         final List<QName> pathList = new ArrayList<QName>();
124         for (final String path : actualPath) {
125             final QName qname = new QName(namespace, revision, path);
126             if (qname != null) {
127                 pathList.add(qname);
128             }
129         }
130         return SchemaPath.create(pathList, true);
131     }
132
133     /**
134      * Returns true if supplied type is representation of built-in YANG type as
135      * per RFC 6020.
136      *
137      * See package documentation for description of base types.
138      *
139      * @param type
140      * @return true if type is built-in YANG Types.
141      */
142     public static boolean isYangBuildInType(final String type) {
143         return BUILD_IN_TYPES.contains(type);
144     }
145
146     /**
147      * Returns default instance of built-in for supplied type
148      *
149      * See package documentation for description of base build-in types
150      * with default instance.
151      *
152      * @param typeName
153      * @return Returns default instance or {@link Optional#absent()} if default
154      *         instance does not exists
155      *
156      */
157     public static Optional<TypeDefinition<?>> defaultBaseTypeFor(final String typeName) {
158         return Optional.<TypeDefinition<?>> fromNullable(defaultBaseTypeForImpl(typeName));
159     }
160
161     private static TypeDefinition<?> defaultBaseTypeForImpl(final String typeName) {
162         Preconditions.checkNotNull(typeName, "typeName must not be null.");
163
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();
173             }
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();
183             }
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();
194         }
195         return null;
196     }
197
198 }