Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +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 | |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangCompilerAnnotation; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 21 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.TempJavaServiceFragmentFiles; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 25 | import org.onosproject.yangutils.utils.UtilConstants.Operation; |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 27 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 28 | import java.util.List; |
| 29 | |
| 30 | import static java.util.Collections.sort; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.UtilConstants.CLASS_STRING; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET; |
| 38 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.ENUM; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.EQUAL; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.utils.UtilConstants.HASH_MAP; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.utils.UtilConstants.INT; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.utils.UtilConstants.INT_MAX_RANGE_ATTR; |
| 47 | import static org.onosproject.yangutils.utils.UtilConstants.INT_MIN_RANGE_ATTR; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.UtilConstants.LIST; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.utils.UtilConstants.LONG_MAX_RANGE_ATTR; |
| 51 | import static org.onosproject.yangutils.utils.UtilConstants.LONG_MIN_RANGE_ATTR; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.utils.UtilConstants.MAP; |
| 53 | import static org.onosproject.yangutils.utils.UtilConstants.NEW; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.utils.UtilConstants.OBJECT_STRING; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 58 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 59 | import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE; |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 60 | import static org.onosproject.yangutils.utils.UtilConstants.PROTECTED; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 61 | import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.utils.UtilConstants.QUESTION_MARK; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 63 | import static org.onosproject.yangutils.utils.UtilConstants.QUEUE; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.utils.UtilConstants.QUOTES; |
| 65 | import static org.onosproject.yangutils.utils.UtilConstants.SCHEMA_NAME; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 66 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 67 | import static org.onosproject.yangutils.utils.UtilConstants.SET; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.utils.UtilConstants.SHORT_MAX_RANGE_ATTR; |
| 69 | import static org.onosproject.yangutils.utils.UtilConstants.SHORT_MIN_RANGE_ATTR; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 70 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 71 | import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 72 | import static org.onosproject.yangutils.utils.UtilConstants.TYPE; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 73 | import static org.onosproject.yangutils.utils.UtilConstants.UINT8_MAX_RANGE_ATTR; |
| 74 | import static org.onosproject.yangutils.utils.UtilConstants.UINT8_MIN_RANGE_ATTR; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 75 | import static org.onosproject.yangutils.utils.UtilConstants.UINT_MAX_RANGE_ATTR; |
| 76 | import static org.onosproject.yangutils.utils.UtilConstants.UINT_MIN_RANGE_ATTR; |
| 77 | import static org.onosproject.yangutils.utils.UtilConstants.ULONG_MAX_RANGE_ATTR; |
| 78 | import static org.onosproject.yangutils.utils.UtilConstants.ULONG_MIN_RANGE_ATTR; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 79 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 80 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_ATTRIBUTE; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 81 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.enumJavaDocForInnerClass; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 82 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc; |
| 83 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getSmallCase; |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 84 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 85 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 86 | * Represents utility class to generate the java snippet. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 87 | */ |
| 88 | public final class JavaCodeSnippetGen { |
| 89 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 90 | // No instantiation. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 91 | private JavaCodeSnippetGen() { |
| 92 | } |
| 93 | |
| 94 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 95 | * Returns the java file header comment. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 96 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 97 | * @return the java file header comment |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 98 | */ |
| 99 | public static String getFileHeaderComment() { |
| 100 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 101 | /* |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 102 | * TODO return the file header. |
| 103 | */ |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 108 | * Returns the textual java code information corresponding to the import |
| 109 | * list. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 110 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 111 | * @param importInfo import info |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 112 | * @return the textual java code information corresponding to the import |
| 113 | * list |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 114 | */ |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 115 | static String getImportText(JavaQualifiedTypeInfoTranslator importInfo) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 116 | return IMPORT + importInfo.getPkgInfo() + PERIOD + |
| 117 | importInfo.getClassInfo() + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 121 | * Returns the textual java code for attribute definition in class. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 122 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 123 | * @param typePkg Package of the attribute type |
| 124 | * @param attrType java attribute type |
| 125 | * @param attrName name of the attribute |
| 126 | * @param isList is list attribute |
| 127 | * @param accessType attribute access type |
| 128 | * @param annotation compiler annotation |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 129 | * @return the textual java code for attribute definition in class |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 130 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 131 | public static String getJavaAttributeDefinition(String typePkg, |
| 132 | String attrType, |
| 133 | String attrName, |
| 134 | boolean isList, |
| 135 | String accessType, |
| 136 | YangCompilerAnnotation annotation) { |
| 137 | StringBuilder attrDef = new StringBuilder(); |
| 138 | attrDef.append(accessType).append(SPACE); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 139 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 140 | if (!isList) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 141 | if (typePkg != null) { |
| 142 | attrDef.append(typePkg).append(PERIOD); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 143 | } |
| 144 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 145 | attrDef.append(attrType).append(SPACE) |
| 146 | .append(attrName).append(SEMI_COLAN) |
| 147 | .append(NEW_LINE); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 148 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 149 | // Add starting definition. |
| 150 | addAttrStartDef(annotation, attrDef); |
| 151 | |
| 152 | if (typePkg != null) { |
| 153 | attrDef.append(typePkg).append(PERIOD); |
Vidyashree Rama | b367047 | 2016-08-06 15:49:56 +0530 | [diff] [blame] | 154 | } |
| 155 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 156 | attrDef.append(attrType); |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 157 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 158 | // Add ending definition. |
| 159 | addAttrEndDef(annotation, attrDef, attrName); |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 160 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 161 | return attrDef.toString(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 165 | * Adds starting attribute definition. |
| 166 | * |
| 167 | * @param annotation compiler annotation |
| 168 | * @param attrDef JAVA attribute definition |
| 169 | */ |
| 170 | private static void addAttrStartDef(YangCompilerAnnotation annotation, |
| 171 | StringBuilder attrDef) { |
| 172 | if (annotation != null && |
| 173 | annotation.getYangAppDataStructure() != null) { |
| 174 | switch (annotation.getYangAppDataStructure().getDataStructure()) { |
| 175 | case QUEUE: { |
| 176 | attrDef.append(QUEUE) |
| 177 | .append(DIAMOND_OPEN_BRACKET); |
| 178 | break; |
| 179 | } |
| 180 | case SET: { |
| 181 | attrDef.append(SET) |
| 182 | .append(DIAMOND_OPEN_BRACKET); |
| 183 | break; |
| 184 | } |
| 185 | default: { |
| 186 | attrDef.append(LIST) |
| 187 | .append(DIAMOND_OPEN_BRACKET); |
| 188 | } |
| 189 | } |
| 190 | } else { |
| 191 | attrDef.append(LIST).append(DIAMOND_OPEN_BRACKET); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Adds ending attribute definition. |
| 197 | * |
| 198 | * @param annotation compiler annotation |
| 199 | * @param attrDef JAVA attribute definition |
| 200 | * @param attrName name of attribute |
| 201 | */ |
| 202 | private static void addAttrEndDef(YangCompilerAnnotation annotation, |
| 203 | StringBuilder attrDef, String attrName) { |
| 204 | if (annotation != null && |
| 205 | annotation.getYangAppDataStructure() != null) { |
| 206 | attrDef.append(DIAMOND_CLOSE_BRACKET).append(SPACE) |
| 207 | .append(attrName).append(SEMI_COLAN) |
| 208 | .append(NEW_LINE); |
| 209 | // TODO refactor SEMI_COLAN, when refactoring in method generator. |
| 210 | } else { |
| 211 | attrDef.append(DIAMOND_CLOSE_BRACKET).append(SPACE).append(attrName) |
| 212 | .append(SPACE).append(EQUAL).append(SPACE).append(NEW) |
| 213 | .append(SPACE).append(ARRAY_LIST).append(SEMI_COLAN) |
| 214 | .append(NEW_LINE); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Returns based on the file type and the YANG name of the file, generate |
| 220 | * the class / interface definition close. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 221 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 222 | * @return corresponding textual java code information |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 223 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 224 | public static String getJavaClassDefClose() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 225 | return CLOSE_CURLY_BRACKET; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 226 | } |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * Returns string for enum's attribute. |
| 230 | * |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 231 | * @param name name of attribute |
| 232 | * @param value value of the enum |
| 233 | * @return string for enum's attribute |
| 234 | */ |
| 235 | public static String generateEnumAttributeString(String name, int value) { |
| 236 | String enumName = getEnumJavaAttribute(name); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 237 | return NEW_LINE + enumJavaDocForInnerClass(name) + |
| 238 | EIGHT_SPACE_INDENTATION + enumName.toUpperCase() + |
| 239 | OPEN_PARENTHESIS + value + CLOSE_PARENTHESIS + COMMA + NEW_LINE; |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Returns string for enum's attribute for enum class. |
| 244 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 245 | * @param name name of attribute |
| 246 | * @param value value of the enum |
| 247 | * @param config plugin configurations |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 248 | * @return string for enum's attribute |
| 249 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 250 | public static String generateEnumAttributeStringWithSchemaName(String name, |
| 251 | int value, |
| 252 | YangPluginConfig config) { |
Bharat saraswal | 748fc3c | 2016-09-06 16:38:20 +0530 | [diff] [blame] | 253 | String enumName = getEnumJavaAttribute(name); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 254 | return NEW_LINE + getJavaDoc(ENUM_ATTRIBUTE, name, false, config, null) + |
| 255 | FOUR_SPACE_INDENTATION + enumName.toUpperCase() + |
| 256 | OPEN_PARENTHESIS + value + COMMA + SPACE + QUOTES + name + |
| 257 | QUOTES + CLOSE_PARENTHESIS + COMMA + NEW_LINE; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 258 | } |
| 259 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 260 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 261 | * Returns sorted import list. |
| 262 | * |
| 263 | * @param imports import list |
| 264 | * @return sorted import list |
| 265 | */ |
| 266 | public static List<String> sortImports(List<String> imports) { |
| 267 | sort(imports); |
| 268 | return imports; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Returns event enum start. |
| 273 | * |
| 274 | * @return event enum start |
| 275 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 276 | static String getEventEnumTypeStart() { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 277 | return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + ENUM + SPACE + TYPE + |
| 278 | SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Adds listener's imports. |
| 283 | * |
| 284 | * @param curNode currentYangNode. |
| 285 | * @param imports import list |
| 286 | * @param operation add or remove |
| 287 | * @param classInfo class info to be added to import list |
| 288 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 289 | public static void addListenersImport(YangNode curNode, |
| 290 | List<String> imports, |
| 291 | Operation operation, |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 292 | String classInfo) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 293 | String thisImport; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 294 | TempJavaServiceFragmentFiles tempFiles = |
| 295 | ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles() |
| 296 | .getServiceTempFiles(); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 297 | if (classInfo.equals(LISTENER_SERVICE)) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 298 | thisImport = tempFiles.getJavaImportData() |
| 299 | .getListenerServiceImport(); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 300 | performOperationOnImports(imports, thisImport, operation); |
| 301 | } else { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 302 | thisImport = tempFiles.getJavaImportData() |
| 303 | .getListenerRegistryImport(); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 304 | performOperationOnImports(imports, thisImport, operation); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Performs given operations on import list. |
| 310 | * |
| 311 | * @param imports list of imports |
| 312 | * @param curImport current import |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 313 | * @param operation ADD or REMOVE |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 314 | * @return import list |
| 315 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 316 | private static List<String> performOperationOnImports(List<String> imports, |
| 317 | String curImport, |
| 318 | Operation operation) { |
| 319 | switch (operation) { |
| 320 | case ADD: |
| 321 | imports.add(curImport); |
| 322 | break; |
| 323 | case REMOVE: |
| 324 | imports.remove(curImport); |
| 325 | break; |
| 326 | default: |
| 327 | throw new TranslatorException("Invalid operation type"); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 328 | } |
| 329 | sortImports(imports); |
| 330 | return imports; |
| 331 | } |
| 332 | |
| 333 | /** |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 334 | * Returns integer attribute for enum's class to get the values. |
| 335 | * |
| 336 | * @param className enum's class name |
| 337 | * @return enum's attribute |
| 338 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 339 | static String getEnumsValueAttribute(String className) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 340 | return NEW_LINE + FOUR_SPACE_INDENTATION + PRIVATE + SPACE + INT + |
| 341 | SPACE + getSmallCase(className) + SEMI_COLAN + NEW_LINE + |
| 342 | FOUR_SPACE_INDENTATION + PRIVATE + SPACE + STRING_DATA_TYPE + |
| 343 | SPACE + SCHEMA_NAME + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 347 | * Returns attribute for augmentation. |
| 348 | * |
| 349 | * @return attribute for augmentation |
| 350 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 351 | static String addAugmentationAttribute() { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 352 | return NEW_LINE + FOUR_SPACE_INDENTATION + PROTECTED + SPACE + MAP + |
| 353 | DIAMOND_OPEN_BRACKET + CLASS_STRING + DIAMOND_OPEN_BRACKET + |
| 354 | QUESTION_MARK + DIAMOND_CLOSE_BRACKET + COMMA + SPACE + |
| 355 | OBJECT_STRING + DIAMOND_CLOSE_BRACKET + SPACE + |
| 356 | getSmallCase(YANG_AUGMENTED_INFO) + MAP + SPACE + EQUAL + |
| 357 | SPACE + NEW + SPACE + HASH_MAP + DIAMOND_OPEN_BRACKET + |
| 358 | DIAMOND_CLOSE_BRACKET + OPEN_PARENTHESIS + CLOSE_PARENTHESIS + |
| 359 | SEMI_COLAN; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 360 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 361 | |
| 362 | /** |
| 363 | * Adds attribute for int ranges. |
| 364 | * |
| 365 | * @param modifier modifier for attribute |
| 366 | * @param addFirst true if int need to be added fist. |
| 367 | * @return attribute for int ranges |
| 368 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 369 | static String addStaticAttributeIntRange(String modifier, |
| 370 | boolean addFirst) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 371 | if (addFirst) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 372 | return NEW_LINE + FOUR_SPACE_INDENTATION + modifier + SPACE + |
| 373 | INT_MIN_RANGE_ATTR + FOUR_SPACE_INDENTATION + modifier + |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 374 | SPACE + INT_MAX_RANGE_ATTR; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 375 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 376 | return NEW_LINE + FOUR_SPACE_INDENTATION + modifier + SPACE + |
| 377 | UINT_MIN_RANGE_ATTR + FOUR_SPACE_INDENTATION + modifier + |
| 378 | SPACE + UINT_MAX_RANGE_ATTR; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Adds attribute for long ranges. |
| 383 | * |
| 384 | * @param modifier modifier for attribute |
| 385 | * @param addFirst if need to be added first |
| 386 | * @return attribute for long ranges |
| 387 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 388 | static String addStaticAttributeLongRange(String modifier, |
| 389 | boolean addFirst) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 390 | if (addFirst) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 391 | return NEW_LINE + FOUR_SPACE_INDENTATION + modifier + SPACE + |
| 392 | LONG_MIN_RANGE_ATTR + FOUR_SPACE_INDENTATION + |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 393 | modifier + SPACE + LONG_MAX_RANGE_ATTR; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 394 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 395 | return NEW_LINE + FOUR_SPACE_INDENTATION + modifier + SPACE + |
| 396 | ULONG_MIN_RANGE_ATTR + FOUR_SPACE_INDENTATION + modifier + |
| 397 | SPACE + ULONG_MAX_RANGE_ATTR; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 398 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 399 | |
| 400 | /** |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 401 | * Adds attribute for long ranges. |
| 402 | * |
| 403 | * @param modifier modifier for attribute |
| 404 | * @param addFirst if need to be added first |
| 405 | * @return attribute for long ranges |
| 406 | */ |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 407 | static String addStaticAttributeShortRange(String modifier, |
| 408 | boolean addFirst) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 409 | if (addFirst) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 410 | return NEW_LINE + FOUR_SPACE_INDENTATION + modifier + SPACE + |
| 411 | SHORT_MIN_RANGE_ATTR + FOUR_SPACE_INDENTATION + modifier + |
| 412 | SPACE + SHORT_MAX_RANGE_ATTR; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 413 | } |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 414 | return NEW_LINE + FOUR_SPACE_INDENTATION + modifier + SPACE + |
| 415 | UINT8_MIN_RANGE_ATTR + FOUR_SPACE_INDENTATION + modifier + |
| 416 | SPACE + UINT8_MAX_RANGE_ATTR; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /** |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 420 | * Returns operation type enum. |
| 421 | * |
| 422 | * @return operation type enum |
| 423 | */ |
| 424 | static String getOperationTypeEnum() { |
| 425 | return "\n" + |
| 426 | " /**\n" + |
| 427 | " * Specify the node specific operation in protocols like NETCONF.\n" + |
| 428 | " * Applicable in protocol edit operation, not applicable in query operation\n" + |
| 429 | " */\n" + |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 430 | " public enum OnosYangNodeOperationType {\n" + |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 431 | " MERGE,\n" + |
| 432 | " REPLACE,\n" + |
| 433 | " CREATE,\n" + |
| 434 | " DELETE,\n" + |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame] | 435 | " REMOVE,\n" + |
| 436 | " NONE\n" + |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 437 | " }\n"; |
| 438 | } |
| 439 | |
| 440 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 441 | * Returns operation type enum, leaf value set attribute and select leaf |
| 442 | * attribute. |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 443 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 444 | * @return operation attributes for value and select leaf flags |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 445 | */ |
| 446 | static String getOperationAttributes() { |
| 447 | return " /**\n" + |
| 448 | " * Identify the leafs whose value are explicitly set\n" + |
| 449 | " * Applicable in protocol edit and query operation\n" + |
| 450 | " */\n" + |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 451 | " private BitSet valueLeafFlags = new BitSet();\n" + |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 452 | "\n" + |
| 453 | " /**\n" + |
| 454 | " * Identify the leafs to be selected, in a query operation\n" + |
| 455 | " */\n" + |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 456 | " private BitSet selectLeafFlags = new BitSet();\n"; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 460 | * Returns operation type enum, leaf value set attribute and select leaf |
| 461 | * attribute for constructor. |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 462 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 463 | * @return operation attributes for constructor |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 464 | */ |
| 465 | static String getOperationAttributeForConstructor() { |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 466 | return " this.valueLeafFlags = builderObject.getValueLeafFlags();\n" + |
| 467 | " this.selectLeafFlags = builderObject.getSelectLeafFlags();\n"; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 471 | * Returns attribute in constructor for yang augmented info map. |
| 472 | * |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame^] | 473 | * @return augment info map |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 474 | */ |
| 475 | static String getYangAugmentedMapObjectForConstruct() { |
| 476 | return " this.yangAugmentedInfoMap = builderObject.yangAugmentedInfoMap();\n"; |
| 477 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 478 | } |