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