Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 17 | package org.onosproject.yangutils.translator.tojava.javamodel; |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 18 | |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 19 | import java.util.Stack; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 20 | |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangDerivedInfo; |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame^] | 23 | import org.onosproject.yangutils.datamodel.YangIdentity; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame^] | 25 | import org.onosproject.yangutils.datamodel.YangIdentityRef; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangType; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangUnion; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.utils.io.impl.YangToJavaNamingConflictUtil; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 36 | |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.getCurNodePackage; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE; |
| 41 | import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.BYTE; |
| 43 | import static org.onosproject.yangutils.utils.UtilConstants.BYTE_WRAPPER; |
| 44 | import static org.onosproject.yangutils.utils.UtilConstants.INT; |
| 45 | import static org.onosproject.yangutils.utils.UtilConstants.INTEGER_WRAPPER; |
| 46 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_MATH; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.UtilConstants.LONG; |
| 49 | import static org.onosproject.yangutils.utils.UtilConstants.LONG_WRAPPER; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.utils.UtilConstants.SHORT; |
| 52 | import static org.onosproject.yangutils.utils.UtilConstants.SHORT_WRAPPER; |
| 53 | import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_BINARY_CLASS; |
| 55 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_BITS_CLASS; |
| 56 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_DECIMAL64_CLASS; |
| 57 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_TYPES_PKG; |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 58 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase; |
| 59 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 60 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage; |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 61 | |
| 62 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 63 | * Represents java data types info corresponding to YANG type. |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 64 | */ |
| 65 | public final class AttributesJavaDataType { |
| 66 | |
| 67 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 68 | * Creates an instance of attribute java data type. |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 69 | */ |
| 70 | private AttributesJavaDataType() { |
| 71 | } |
| 72 | |
| 73 | /** |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 74 | * Returns java type. |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 75 | * |
| 76 | * @param yangType YANG type |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 77 | * @return java type |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 78 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 79 | public static String getJavaDataType(YangType<?> yangType) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 80 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 81 | YangDataTypes type = yangType.getDataType(); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 82 | |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 83 | switch (type) { |
| 84 | case INT8: |
| 85 | return BYTE; |
| 86 | case INT16: |
| 87 | return SHORT; |
| 88 | case INT32: |
| 89 | return INT; |
| 90 | case INT64: |
| 91 | return LONG; |
| 92 | case UINT8: |
| 93 | return SHORT; |
| 94 | case UINT16: |
| 95 | return INT; |
| 96 | case UINT32: |
| 97 | return LONG; |
| 98 | case UINT64: |
| 99 | return BIG_INTEGER; |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 100 | case BINARY: |
| 101 | return YANG_BINARY_CLASS; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 102 | case DECIMAL64: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 103 | return YANG_DECIMAL64_CLASS; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 104 | case STRING: |
| 105 | return STRING_DATA_TYPE; |
| 106 | case BOOLEAN: |
| 107 | return BOOLEAN_DATA_TYPE; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 108 | case INSTANCE_IDENTIFIER: |
| 109 | return STRING_DATA_TYPE; |
| 110 | case LEAFREF: |
| 111 | return getJavaDataType(getReferredTypeFromLeafref(yangType)); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 112 | default: |
| 113 | throw new TranslatorException("given data type is not supported."); |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 114 | } |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 115 | } |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Returns java import class. |
| 119 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 120 | * @param yangType YANG type |
| 121 | * @param isListAttr if the attribute need to be a list |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 122 | * @param pluginConfig plugin configurations |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 123 | * @return java import class |
| 124 | */ |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 125 | public static String getJavaImportClass(YangType<?> yangType, boolean isListAttr, |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 126 | YangToJavaNamingConflictUtil pluginConfig) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 127 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 128 | YangDataTypes type = yangType.getDataType(); |
| 129 | |
| 130 | if (isListAttr) { |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 131 | switch (type) { |
| 132 | case INT8: |
| 133 | return BYTE_WRAPPER; |
| 134 | case INT16: |
| 135 | return SHORT_WRAPPER; |
| 136 | case INT32: |
| 137 | return INTEGER_WRAPPER; |
| 138 | case INT64: |
| 139 | return LONG_WRAPPER; |
| 140 | case UINT8: |
| 141 | return SHORT_WRAPPER; |
| 142 | case UINT16: |
| 143 | return INTEGER_WRAPPER; |
| 144 | case UINT32: |
| 145 | return LONG_WRAPPER; |
| 146 | case UINT64: |
| 147 | return BIG_INTEGER; |
| 148 | case DECIMAL64: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 149 | return YANG_DECIMAL64_CLASS; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 150 | case STRING: |
| 151 | return STRING_DATA_TYPE; |
| 152 | case BOOLEAN: |
| 153 | return BOOLEAN_WRAPPER; |
| 154 | case ENUMERATION: |
janani b | 4a6711a | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 155 | return getCapitalCase( |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 156 | getCamelCase(((YangJavaEnumeration) yangType.getDataTypeExtendedInfo()).getName(), |
| 157 | pluginConfig)); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 158 | case BITS: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 159 | return YANG_BITS_CLASS; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 160 | case BINARY: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 161 | return YANG_BINARY_CLASS; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 162 | case LEAFREF: |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 163 | YangType<?> referredType = getReferredTypeFromLeafref(yangType); |
| 164 | return getJavaImportClass(referredType, isListAttr, pluginConfig); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 165 | case IDENTITYREF: |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame^] | 166 | YangIdentityRef identityRef = (YangIdentityRef) yangType.getDataTypeExtendedInfo(); |
| 167 | YangIdentity identity = identityRef.getReferredIdentity(); |
| 168 | return getCapitalCase(getCamelCase(((YangJavaIdentity) identity). |
| 169 | getName(), pluginConfig)); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 170 | case EMPTY: |
| 171 | return BOOLEAN_WRAPPER; |
| 172 | case UNION: |
janani b | 4a6711a | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 173 | return getCapitalCase(getCamelCase(((YangJavaUnion) yangType.getDataTypeExtendedInfo()).getName(), |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 174 | pluginConfig)); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 175 | case INSTANCE_IDENTIFIER: |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 176 | return STRING_DATA_TYPE; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 177 | case DERIVED: |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 178 | return getCapitalCase( |
| 179 | getCamelCase(yangType.getDataTypeName(), pluginConfig)); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 180 | default: |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 181 | throw new TranslatorException("given data type is not supported."); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 182 | } |
| 183 | } else { |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 184 | switch (type) { |
| 185 | case UINT64: |
| 186 | return BIG_INTEGER; |
| 187 | case DECIMAL64: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 188 | return YANG_DECIMAL64_CLASS; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 189 | case STRING: |
| 190 | return STRING_DATA_TYPE; |
| 191 | case ENUMERATION: |
janani b | 4a6711a | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 192 | return getCapitalCase( |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 193 | getCamelCase(((YangJavaEnumeration) yangType.getDataTypeExtendedInfo()).getName(), |
| 194 | pluginConfig)); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 195 | case BITS: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 196 | return YANG_BITS_CLASS; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 197 | case BINARY: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 198 | return YANG_BINARY_CLASS; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 199 | case LEAFREF: |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 200 | YangType<?> referredType = getReferredTypeFromLeafref(yangType); |
| 201 | return getJavaImportClass(referredType, isListAttr, pluginConfig); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 202 | case IDENTITYREF: |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame^] | 203 | YangIdentityRef identityRef = (YangIdentityRef) yangType.getDataTypeExtendedInfo(); |
| 204 | YangIdentity identity = identityRef.getReferredIdentity(); |
| 205 | return getCapitalCase(getCamelCase(((YangJavaIdentity) identity).getName(), pluginConfig)); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 206 | case EMPTY: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 207 | return BOOLEAN_DATA_TYPE; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 208 | case UNION: |
janani b | 4a6711a | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 209 | return getCapitalCase(getCamelCase(((YangJavaUnion) yangType.getDataTypeExtendedInfo()).getName(), |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 210 | pluginConfig)); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 211 | case INSTANCE_IDENTIFIER: |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 212 | return STRING_DATA_TYPE; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 213 | case DERIVED: |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 214 | return getCapitalCase( |
| 215 | getCamelCase(yangType.getDataTypeName(), pluginConfig)); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 216 | default: |
| 217 | return null; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 218 | } |
| 219 | } |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Returns java import package. |
| 224 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 225 | * @param yangType YANG type |
| 226 | * @param isListAttr if the attribute is of list type |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 227 | * @param conflictResolver object of YANG to java naming conflict util |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 228 | * @return java import package |
| 229 | */ |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 230 | public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr, |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 231 | YangToJavaNamingConflictUtil conflictResolver) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 232 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 233 | YangDataTypes type = yangType.getDataType(); |
| 234 | |
| 235 | if (isListAttr) { |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 236 | switch (type) { |
| 237 | case INT8: |
| 238 | case INT16: |
| 239 | case INT32: |
| 240 | case INT64: |
| 241 | case UINT8: |
| 242 | case UINT16: |
| 243 | case UINT32: |
| 244 | case STRING: |
| 245 | case BOOLEAN: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 246 | case EMPTY: |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 247 | return JAVA_LANG; |
| 248 | case UINT64: |
| 249 | return JAVA_MATH; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 250 | case ENUMERATION: |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 251 | return getEnumsPackage(yangType, conflictResolver); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 252 | case DECIMAL64: |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 253 | case BITS: |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 254 | case BINARY: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 255 | return YANG_TYPES_PKG; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 256 | case LEAFREF: |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 257 | YangType<?> referredType = getReferredTypeFromLeafref(yangType); |
| 258 | return getJavaImportPackage(referredType, isListAttr, conflictResolver); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 259 | case IDENTITYREF: |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame^] | 260 | return getIdentityRefPackage(yangType, conflictResolver); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 261 | case UNION: |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 262 | return getUnionPackage(yangType, conflictResolver); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 263 | case INSTANCE_IDENTIFIER: |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 264 | return JAVA_LANG; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 265 | case DERIVED: |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 266 | return getTypDefsPackage(yangType, conflictResolver); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 267 | default: |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 268 | throw new TranslatorException("given data type is not supported."); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 269 | } |
| 270 | } else { |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 271 | switch (type) { |
| 272 | case UINT64: |
Bharat saraswal | c0e0484 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 273 | return JAVA_MATH; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 274 | case STRING: |
| 275 | return JAVA_LANG; |
| 276 | case ENUMERATION: |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 277 | return getEnumsPackage(yangType, conflictResolver); |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 278 | case DECIMAL64: |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 279 | case BITS: |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 280 | case BINARY: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 281 | return YANG_TYPES_PKG; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 282 | case LEAFREF: |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 283 | YangType<?> referredType = getReferredTypeFromLeafref(yangType); |
| 284 | return getJavaImportPackage(referredType, isListAttr, conflictResolver); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 285 | case IDENTITYREF: |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame^] | 286 | return getIdentityRefPackage(yangType, conflictResolver); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 287 | case EMPTY: |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 288 | return JAVA_LANG; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 289 | case UNION: |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 290 | return getUnionPackage(yangType, conflictResolver); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 291 | case INSTANCE_IDENTIFIER: |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 292 | return JAVA_LANG; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 293 | case DERIVED: |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 294 | return getTypDefsPackage(yangType, conflictResolver); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 295 | default: |
| 296 | return null; |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 297 | } |
| 298 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Returns java package for typedef node. |
| 303 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 304 | * @param type YANG type |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 305 | * @param conflictResolver object of YANG to java naming conflict util |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 306 | * @return java package for typedef node |
| 307 | */ |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 308 | private static String getTypDefsPackage(YangType<?> type, YangToJavaNamingConflictUtil conflictResolver) { |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 309 | Object var = type.getDataTypeExtendedInfo(); |
| 310 | if (!(var instanceof YangDerivedInfo)) { |
| 311 | throw new TranslatorException("type should have been derived."); |
| 312 | } |
| 313 | |
| 314 | if (!(((YangDerivedInfo<?>) var).getReferredTypeDef() instanceof YangTypeDef)) { |
| 315 | throw new TranslatorException("derived info is not an instance of typedef."); |
| 316 | } |
| 317 | |
| 318 | YangJavaTypeDef typedef = (YangJavaTypeDef) ((YangDerivedInfo<?>) var).getReferredTypeDef(); |
| 319 | if (typedef.getJavaFileInfo().getPackage() == null) { |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 320 | return getPackageFromParent(typedef.getParent(), conflictResolver); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 321 | } |
| 322 | return typedef.getJavaFileInfo().getPackage(); |
| 323 | } |
| 324 | |
| 325 | /** |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 326 | * Returns java package for union node. |
| 327 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 328 | * @param type YANG type |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 329 | * @param conflictResolver object of YANG to java naming conflict util |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 330 | * @return java package for union node |
| 331 | */ |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 332 | private static String getUnionPackage(YangType<?> type, YangToJavaNamingConflictUtil conflictResolver) { |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 333 | |
| 334 | if (!(type.getDataTypeExtendedInfo() instanceof YangUnion)) { |
| 335 | throw new TranslatorException("type should have been union."); |
| 336 | } |
| 337 | |
| 338 | YangJavaUnion union = (YangJavaUnion) type.getDataTypeExtendedInfo(); |
| 339 | if (union.getJavaFileInfo().getPackage() == null) { |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 340 | return getPackageFromParent(union.getParent(), conflictResolver); |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 341 | } |
| 342 | return union.getJavaFileInfo().getPackage(); |
| 343 | } |
| 344 | |
| 345 | /** |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 346 | * Returns YANG enumeration's java package. |
| 347 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 348 | * @param type YANG type |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 349 | * @param conflictResolver object of YANG to java naming conflict util |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 350 | * @return YANG enumeration's java package |
| 351 | */ |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 352 | private static String getEnumsPackage(YangType<?> type, YangToJavaNamingConflictUtil conflictResolver) { |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 353 | |
| 354 | if (!(type.getDataTypeExtendedInfo() instanceof YangEnumeration)) { |
| 355 | throw new TranslatorException("type should have been enumeration."); |
| 356 | } |
| 357 | YangJavaEnumeration enumeration = (YangJavaEnumeration) type.getDataTypeExtendedInfo(); |
| 358 | if (enumeration.getJavaFileInfo().getPackage() == null) { |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 359 | return getPackageFromParent(enumeration.getParent(), conflictResolver); |
Bharat saraswal | d72411a | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 360 | } |
| 361 | return enumeration.getJavaFileInfo().getPackage(); |
| 362 | } |
| 363 | |
| 364 | /** |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame^] | 365 | * Returns YANG identity's java package. |
| 366 | * |
| 367 | * @param type YANG type |
| 368 | * @param conflictResolver object of YANG to java naming conflict util |
| 369 | * @return YANG identity's java package |
| 370 | */ |
| 371 | private static String getIdentityRefPackage(YangType<?> type, YangToJavaNamingConflictUtil conflictResolver) { |
| 372 | |
| 373 | if (!(type.getDataTypeExtendedInfo() instanceof YangIdentityRef)) { |
| 374 | throw new TranslatorException("type should have been identityref."); |
| 375 | } |
| 376 | YangIdentityRef identityRef = (YangIdentityRef) type.getDataTypeExtendedInfo(); |
| 377 | YangJavaIdentity identity = (YangJavaIdentity) (identityRef.getReferredIdentity()); |
| 378 | if (identity.getJavaFileInfo().getPackage() == null) { |
| 379 | return getPackageFromParent(identity.getParent(), conflictResolver); |
| 380 | } |
| 381 | return identity.getJavaFileInfo().getPackage(); |
| 382 | } |
| 383 | /** |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 384 | * Returns package from parent node. |
| 385 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 386 | * @param parent parent YANG node |
janani b | dd1314f | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 387 | * @param conflictResolver object of YANG to java naming conflict util |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 388 | * @return java package from parent node |
| 389 | */ |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 390 | private static String getPackageFromParent(YangNode parent, |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 391 | YangToJavaNamingConflictUtil conflictResolver) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 392 | if (!(parent instanceof JavaFileInfoContainer)) { |
Gaurav Agrawal | 338735b | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 393 | throw new TranslatorException("invalid child node is being processed."); |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 394 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 395 | JavaFileInfo parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo(); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 396 | if (parentInfo.getPackage() == null) { |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 397 | updateJavaFileInfo(parent, conflictResolver); |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 398 | } |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 399 | return parentInfo.getPackage() + PERIOD + parentInfo.getJavaName().toLowerCase(); |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 400 | } |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 401 | |
| 402 | /** |
| 403 | * Update the referred data model nodes java file info, this will be called, |
| 404 | * when the linked node is yet to translate. Then resolve until the parent hierarchy. |
| 405 | * |
| 406 | * @param yangNode node whose java info needs to be updated |
| 407 | * @param conflictResolver yang plugin config |
| 408 | */ |
| 409 | public static void updateJavaFileInfo(YangNode yangNode, |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 410 | YangToJavaNamingConflictUtil conflictResolver) { |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 411 | Stack<YangNode> nodesToUpdatePackage = new Stack<YangNode>(); |
| 412 | |
| 413 | /* |
| 414 | * Add the nodes to be updated for package info in a stack. |
| 415 | */ |
| 416 | while (yangNode != null |
| 417 | && ((JavaFileInfoContainer) yangNode) |
| 418 | .getJavaFileInfo().getPackage() == null) { |
| 419 | nodesToUpdatePackage.push(yangNode); |
| 420 | yangNode = yangNode.getParent(); |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * If the package is not updated till root node, then root package needs to |
| 425 | * be updated. |
| 426 | */ |
| 427 | if (yangNode == null) { |
| 428 | yangNode = nodesToUpdatePackage.pop(); |
| 429 | String pkg; |
| 430 | if (yangNode instanceof YangJavaModule) { |
| 431 | YangJavaModule module = (YangJavaModule) yangNode; |
| 432 | pkg = getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module |
| 433 | .getRevision().getRevDate(), conflictResolver); |
| 434 | } else if (yangNode instanceof YangJavaSubModule) { |
| 435 | YangJavaSubModule submodule = (YangJavaSubModule) yangNode; |
| 436 | pkg = getRootPackage(submodule.getVersion(), |
| 437 | submodule.getNameSpaceFromModule(submodule.getBelongsTo()), |
| 438 | submodule.getRevision().getRevDate(), conflictResolver); |
| 439 | } else { |
| 440 | throw new TranslatorException("Invalid root node of data model tree"); |
| 441 | } |
| 442 | |
| 443 | ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo() |
| 444 | .setJavaName(getCamelCase(yangNode.getName(), conflictResolver)); |
| 445 | ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo() |
| 446 | .setPackage(pkg); |
| 447 | ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo() |
| 448 | .setPackageFilePath(getPackageDirPathFromJavaJPackage( |
| 449 | ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo() |
| 450 | .getPackage())); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Parent of the node in stack is updated with java info, |
| 455 | * all the nodes can be popped and updated |
| 456 | */ |
| 457 | while (nodesToUpdatePackage.size() != 0) { |
| 458 | yangNode = nodesToUpdatePackage.pop(); |
| 459 | ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo() |
| 460 | .setJavaName(getCamelCase(yangNode.getName(), conflictResolver)); |
| 461 | ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo() |
| 462 | .setPackage(getCurNodePackage(yangNode)); |
| 463 | ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo() |
| 464 | .setPackageFilePath(getPackageDirPathFromJavaJPackage( |
| 465 | ((JavaCodeGeneratorInfo) yangNode).getJavaFileInfo() |
| 466 | .getPackage())); |
| 467 | } |
| 468 | } |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * Returns the referred type from leaf/leaf-list. |
| 472 | * |
| 473 | * @param type current type in leaf |
| 474 | * @return type from the leafref |
| 475 | */ |
| 476 | private static YangType<?> getReferredTypeFromLeafref(YangType type) { |
| 477 | YangLeafRef<?> leafRefInfo = (YangLeafRef<?>) type.getDataTypeExtendedInfo(); |
| 478 | return leafRefInfo.getEffectiveDataType(); |
| 479 | } |
Bharat saraswal | 4bf8b15 | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 480 | } |