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