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 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangAugment; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangCase; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangIdentity; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangModule; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangNotification; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangSubModule; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame^] | 28 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 31 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 33 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.DEFAULT_CLASS_MASK; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS; |
| 37 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_IDENTITY_CLASS; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.utils.UtilConstants.ABSTRACT; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
| 46 | import static org.onosproject.yangutils.utils.UtilConstants.CLASS; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET; |
| 50 | import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET; |
| 51 | import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.utils.UtilConstants.ENUM; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING; |
| 54 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.utils.UtilConstants.EXTEND; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.utils.UtilConstants.FINAL; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS; |
| 58 | import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 59 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG; |
| 60 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 61 | import static org.onosproject.yangutils.utils.UtilConstants.MANAGER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 62 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 63 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 64 | import static org.onosproject.yangutils.utils.UtilConstants.OP_PARAM; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 65 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 66 | import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC; |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 67 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 68 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 69 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 70 | import static org.onosproject.yangutils.utils.UtilConstants.STATIC; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 71 | import static org.onosproject.yangutils.utils.UtilConstants.SUBJECT; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 72 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 73 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 74 | |
| 75 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 76 | * Represents generator for class definition of generated files. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 77 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 78 | final class ClassDefinitionGenerator { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 79 | |
| 80 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 81 | * Creates an instance of class definition generator. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 82 | */ |
| 83 | private ClassDefinitionGenerator() { |
| 84 | } |
| 85 | |
| 86 | /** |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 87 | * Based on the file type and the YANG name of the file, generate the class / interface definition start. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 88 | * |
| 89 | * @param genFileTypes generated file type |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 90 | * @param yangName class name |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 91 | * @return class definition |
| 92 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 93 | static String generateClassDefinition(int genFileTypes, String yangName) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 94 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 95 | /* |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 96 | * Based on the file type and the YANG name of the file, generate the |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 97 | * class / interface definition start. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 98 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 99 | switch (genFileTypes) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 100 | case GENERATE_TYPEDEF_CLASS: |
| 101 | case GENERATE_UNION_CLASS: |
| 102 | return getTypeClassDefinition(yangName); |
| 103 | case GENERATE_ENUM_CLASS: |
| 104 | return getEnumClassDefinition(yangName); |
| 105 | default: |
| 106 | return null; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 107 | } |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 111 | * Based on the file type and the YANG name of the file, generate the class / interface definition start. |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 112 | * |
| 113 | * @param genFileTypes generated file type |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 114 | * @param yangName class name |
| 115 | * @param curNode current YANG node |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 116 | * @return class definition |
| 117 | */ |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 118 | static String generateClassDefinition(int genFileTypes, String yangName, YangNode curNode) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 119 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 120 | /* |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 121 | * Based on the file type and the YANG name of the file, generate the |
| 122 | * class / interface definition start. |
| 123 | */ |
| 124 | switch (genFileTypes) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 125 | case INTERFACE_MASK: |
| 126 | return getInterfaceDefinition(yangName, curNode); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 127 | case BUILDER_CLASS_MASK: |
| 128 | return getBuilderClassDefinition(yangName, curNode); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 129 | case DEFAULT_CLASS_MASK: |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 130 | return getImplClassDefinition(yangName, curNode); |
| 131 | case BUILDER_INTERFACE_MASK: |
| 132 | return getBuilderInterfaceDefinition(yangName, curNode); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 133 | case GENERATE_SERVICE_AND_MANAGER: |
| 134 | return getRpcInterfaceDefinition(yangName, curNode); |
| 135 | case GENERATE_EVENT_CLASS: |
| 136 | String eventName = yangName + SUBJECT; |
| 137 | return getEventDefinition(yangName, eventName); |
| 138 | case GENERATE_EVENT_LISTENER_INTERFACE: |
| 139 | return getEventListenerDefinition(yangName); |
| 140 | case GENERATE_EVENT_SUBJECT_CLASS: |
| 141 | return getClassDefinition(yangName); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 142 | case GENERATE_IDENTITY_CLASS: |
| 143 | return getIdentityClassDefinition(yangName, curNode); |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 144 | default: |
| 145 | return null; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 146 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 150 | * Returns enum file class definition. |
| 151 | * |
| 152 | * @param yangName class name |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 153 | * @return enum file class definition |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 154 | */ |
| 155 | private static String getEnumClassDefinition(String yangName) { |
| 156 | return PUBLIC + SPACE + ENUM + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 157 | } |
| 158 | |
| 159 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 160 | * Returns interface file class definition. |
| 161 | * |
| 162 | * @param yangName file name |
| 163 | * @return definition |
| 164 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 165 | private static String getInterfaceDefinition(String yangName, YangNode curNode) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 166 | |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 167 | String clsDef = getClassDefinitionForWhenExtended(curNode, yangName, INTERFACE_MASK); |
| 168 | if (clsDef != null) { |
| 169 | return clsDef; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 170 | } |
| 171 | return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Returns builder interface file class definition. |
| 176 | * |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 177 | * @param yangName java class name, corresponding to which the builder class is being generated |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 178 | * @return definition |
| 179 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 180 | private static String getBuilderInterfaceDefinition(String yangName, YangNode curNode) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 181 | if (!(curNode instanceof YangCase) && !(curNode instanceof YangAugment)) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 182 | String clsDef = getClassDefinitionForWhenExtended(curNode, yangName, BUILDER_INTERFACE_MASK); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 183 | if (clsDef != null) { |
| 184 | return clsDef; |
| 185 | } |
| 186 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 187 | return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Returns builder file class definition. |
| 192 | * |
| 193 | * @param yangName file name |
| 194 | * @return definition |
| 195 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 196 | private static String getBuilderClassDefinition(String yangName, YangNode curNode) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 197 | if (!(curNode instanceof YangCase)) { |
| 198 | String clsDef = getClassDefinitionForWhenExtended(curNode, yangName, BUILDER_CLASS_MASK); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 199 | if (clsDef != null) { |
| 200 | return clsDef; |
| 201 | } |
| 202 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 203 | if (curNode instanceof YangModule || curNode instanceof YangSubModule) { |
| 204 | return PUBLIC + SPACE + STATIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE |
| 205 | + OPEN_CURLY_BRACKET + NEW_LINE; |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 206 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 207 | return PUBLIC + SPACE + STATIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + |
| 208 | yangName + PERIOD + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 212 | * Returns impl file class definition. |
| 213 | * |
| 214 | * @param yangName file name |
| 215 | * @return definition |
| 216 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 217 | private static String getImplClassDefinition(String yangName, YangNode curNode) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 218 | if (!(curNode instanceof YangCase)) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 219 | String clsDef = getClassDefinitionForWhenExtended(curNode, yangName, DEFAULT_CLASS_MASK); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 220 | if (clsDef != null) { |
| 221 | return clsDef; |
| 222 | } |
| 223 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 224 | if (curNode instanceof YangModule || curNode instanceof YangSubModule) { |
| 225 | return PUBLIC + SPACE + CLASS + SPACE + yangName + OP_PARAM + SPACE + IMPLEMENTS + SPACE |
| 226 | + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 227 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 228 | return PUBLIC + SPACE + CLASS + SPACE + getCapitalCase(DEFAULT) + yangName + SPACE + IMPLEMENTS + SPACE |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 229 | + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Returns impl file class definition. |
| 234 | * |
| 235 | * @param yangName file name |
| 236 | * @return definition |
| 237 | */ |
| 238 | private static String getClassDefinition(String yangName) { |
| 239 | return PUBLIC + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 240 | } |
| 241 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 242 | /** |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 243 | * Returns implementation file identity class definition. |
| 244 | * |
| 245 | * @param yangName file name |
| 246 | * @return identity class definition |
| 247 | */ |
| 248 | private static String getIdentityClassDefinition(String yangName, YangNode curNode) { |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 249 | if (!(curNode instanceof YangIdentity)) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame^] | 250 | throw new TranslatorException("Expected java identity instance node " + |
| 251 | curNode.getName() + " in " + |
| 252 | curNode.getLineNumber() + " at " + |
| 253 | curNode.getCharPosition() |
| 254 | + " in " + curNode.getFileName()); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 255 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 256 | YangIdentity identity = (YangIdentity) curNode; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 257 | if (identity.getBaseNode() != null) { |
| 258 | YangIdentity baseIdentity = identity.getBaseNode().getReferredIdentity(); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 259 | if (baseIdentity == null) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame^] | 260 | throw new TranslatorException("Expected java identity instance node " + |
| 261 | curNode.getName() + " in " + |
| 262 | curNode.getLineNumber() + " at " + |
| 263 | curNode.getCharPosition() |
| 264 | + " in " + curNode.getFileName()); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 265 | } |
| 266 | |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 267 | JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) baseIdentity).getJavaFileInfo(); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 268 | return PUBLIC + SPACE + ABSTRACT + SPACE + CLASS + SPACE + yangName + SPACE + EXTEND + SPACE |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 269 | + getCapitalCase(fileInfo.getJavaName()) + SPACE + |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 270 | OPEN_CURLY_BRACKET + NEW_LINE; |
| 271 | } |
| 272 | |
| 273 | return PUBLIC + SPACE + ABSTRACT + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 274 | } |
| 275 | |
| 276 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 277 | * Returns type file class definition. |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 278 | * |
| 279 | * @param yangName file name |
| 280 | * @return definition |
| 281 | */ |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 282 | private static String getTypeClassDefinition(String yangName) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 283 | return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 284 | } |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 285 | |
| 286 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 287 | * Returns RPC file interface definition. |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 288 | * |
| 289 | * @param yangName file name |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 290 | * @param curNode current YANG node |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 291 | * @return definition |
| 292 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 293 | private static String getRpcInterfaceDefinition(String yangName, YangNode curNode) { |
| 294 | JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) curNode) |
| 295 | .getTempJavaCodeFragmentFiles().getServiceTempFiles().getJavaExtendsListHolder(); |
| 296 | if (holder.getExtendsList() != null && !holder.getExtendsList().isEmpty()) { |
| 297 | curNode = curNode.getChild(); |
| 298 | while (curNode != null) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 299 | if (curNode instanceof YangNotification) { |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 300 | return getRpcInterfaceDefinitionWhenItExtends(yangName); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 301 | } |
| 302 | curNode = curNode.getNextSibling(); |
| 303 | } |
| 304 | } |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 305 | if (yangName.matches(REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE)) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 306 | return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 307 | } |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 308 | return PUBLIC + SPACE + CLASS + SPACE + yangName + SPACE + IMPLEMENTS + SPACE |
| 309 | + yangName.substring(0, yangName.length() - 7) + SERVICE + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* Provides class definition when RPC interface needs to extends any event.*/ |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 313 | private static String getRpcInterfaceDefinitionWhenItExtends(String yangName) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 314 | |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 315 | if (yangName.matches(REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE)) { |
Gaurav Agrawal | d39a921 | 2016-08-20 18:51:20 +0530 | [diff] [blame] | 316 | StringBuilder newString = new StringBuilder(yangName); |
| 317 | newString.replace(yangName.lastIndexOf("Service"), yangName.lastIndexOf("Service") + 7, ""); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 318 | return PUBLIC + SPACE + INTERFACE + SPACE + yangName + NEW_LINE + EIGHT_SPACE_INDENTATION |
Gaurav Agrawal | d39a921 | 2016-08-20 18:51:20 +0530 | [diff] [blame] | 319 | + EXTEND + SPACE + LISTENER_SERVICE + DIAMOND_OPEN_BRACKET + newString + EVENT_STRING + COMMA |
| 320 | + SPACE + newString + EVENT_LISTENER_STRING + DIAMOND_CLOSE_BRACKET + SPACE |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 321 | + OPEN_CURLY_BRACKET + NEW_LINE; |
| 322 | } |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 323 | yangName = yangName.substring(0, yangName.length() - 7); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 324 | return PUBLIC + SPACE + CLASS + SPACE + yangName + MANAGER + NEW_LINE + EIGHT_SPACE_INDENTATION |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 325 | + EXTEND + SPACE + LISTENER_REG + DIAMOND_OPEN_BRACKET + yangName + EVENT_STRING + COMMA + SPACE |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 326 | + yangName + EVENT_LISTENER_STRING + DIAMOND_CLOSE_BRACKET + NEW_LINE |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 327 | + EIGHT_SPACE_INDENTATION + IMPLEMENTS + SPACE + yangName + SERVICE + SPACE + OPEN_CURLY_BRACKET |
| 328 | + NEW_LINE; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 329 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 330 | |
| 331 | /** |
| 332 | * Returns event class definition. |
| 333 | * |
| 334 | * @param javaName file name |
| 335 | * @return definition |
| 336 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 337 | private static String getEventDefinition(String javaName, String eventName) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 338 | return PUBLIC + SPACE + CLASS + SPACE + javaName + SPACE + "extends AbstractEvent<" |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 339 | + javaName + ".Type, " + eventName + ">" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Returns event listener interface definition. |
| 344 | * |
| 345 | * @param javaName file name |
| 346 | * @return definition |
| 347 | */ |
| 348 | private static String getEventListenerDefinition(String javaName) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 349 | String interfaceDef = PUBLIC + SPACE + INTERFACE + SPACE + javaName + SPACE + "extends EventListener<" |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 350 | + javaName; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 351 | if (interfaceDef.length() < 8) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 352 | throw new RuntimeException("Event listener interface name is error"); |
| 353 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 354 | interfaceDef = interfaceDef.substring(0, interfaceDef.length() - 8); |
| 355 | interfaceDef = interfaceDef + ">" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 356 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 357 | return interfaceDef; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 358 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 359 | |
| 360 | /** |
| 361 | * Returns class definition when class is extending another class. |
| 362 | * |
| 363 | * @param curNode current node |
| 364 | * @param yangName name |
| 365 | * @param genFileTypes gen file type |
| 366 | * @return class definition |
| 367 | */ |
| 368 | private static String getClassDefinitionForWhenExtended(YangNode curNode, String yangName, int genFileTypes) { |
| 369 | JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) curNode) |
| 370 | .getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder(); |
| 371 | |
| 372 | if (holder.getExtendsList() != null && !holder.getExtendsList().isEmpty()) { |
| 373 | String def = PUBLIC + SPACE; |
| 374 | switch (genFileTypes) { |
| 375 | case INTERFACE_MASK: |
| 376 | def = def + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 377 | def = getDefinitionString(def, holder); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 378 | return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 379 | case BUILDER_INTERFACE_MASK: |
| 380 | String builderDef = INTERFACE + SPACE + yangName + BUILDER + SPACE + EXTEND + SPACE; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 381 | builderDef = getDefinitionString(builderDef, holder); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 382 | return builderDef + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 383 | case BUILDER_CLASS_MASK: |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 384 | def = def + STATIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + EXTEND + SPACE; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 385 | def = getDefinitionString(def, holder); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 386 | if (curNode instanceof YangSubModule || curNode instanceof YangModule) { |
| 387 | return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 388 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 389 | return def + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD |
| 390 | + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 391 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 392 | case DEFAULT_CLASS_MASK: |
| 393 | if (curNode instanceof YangSubModule || curNode instanceof YangModule) { |
| 394 | def = def + CLASS + SPACE + yangName + OP_PARAM + SPACE + EXTEND + SPACE; |
| 395 | } else { |
| 396 | def = def + CLASS + SPACE + getCapitalCase(DEFAULT) + yangName + SPACE + EXTEND + SPACE; |
| 397 | } |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 398 | def = getDefinitionString(def, holder); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 399 | return def + SPACE + IMPLEMENTS + SPACE |
| 400 | + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 401 | default: |
| 402 | return null; |
| 403 | } |
| 404 | } |
| 405 | return null; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Returns updated class definition. |
| 410 | * |
| 411 | * @param def current definition |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 412 | * @param holder extend list holder |
| 413 | * @return updated class definition |
| 414 | */ |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 415 | private static String getDefinitionString(String def, JavaExtendsListHolder holder) { |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 416 | for (JavaQualifiedTypeInfoTranslator info : holder.getExtendsList()) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 417 | if (!holder.getExtendedClassStore().get(info)) { |
| 418 | def = def + info.getClassInfo() + COMMA + SPACE; |
| 419 | } else { |
| 420 | def = def + info.getPkgInfo() + PERIOD + info.getClassInfo() + COMMA + SPACE; |
| 421 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 422 | } |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 423 | return trimAtLast(def, COMMA); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 424 | } |
| 425 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 426 | } |