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 | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
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)) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 250 | throw new TranslatorException("Expected java identity instance node"); |
| 251 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 252 | YangIdentity identity = (YangIdentity) curNode; |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 253 | if (identity.getBaseNode() != null) { |
| 254 | YangIdentity baseIdentity = identity.getBaseNode().getReferredIdentity(); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 255 | if (baseIdentity == null) { |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 256 | throw new TranslatorException("Expected java identity instance node"); |
| 257 | } |
| 258 | |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 259 | JavaFileInfoTranslator fileInfo = ((JavaFileInfoContainer) baseIdentity).getJavaFileInfo(); |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 260 | return PUBLIC + SPACE + ABSTRACT + SPACE + CLASS + SPACE + yangName + SPACE + EXTEND + SPACE |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 261 | + getCapitalCase(fileInfo.getJavaName()) + SPACE + |
Shankara-Huawei | 234cd09 | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 262 | OPEN_CURLY_BRACKET + NEW_LINE; |
| 263 | } |
| 264 | |
| 265 | return PUBLIC + SPACE + ABSTRACT + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 266 | } |
| 267 | |
| 268 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 269 | * Returns type file class definition. |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 270 | * |
| 271 | * @param yangName file name |
| 272 | * @return definition |
| 273 | */ |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 274 | private static String getTypeClassDefinition(String yangName) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 275 | 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] | 276 | } |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 277 | |
| 278 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 279 | * Returns RPC file interface definition. |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 280 | * |
| 281 | * @param yangName file name |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 282 | * @param curNode current YANG node |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 283 | * @return definition |
| 284 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 285 | private static String getRpcInterfaceDefinition(String yangName, YangNode curNode) { |
| 286 | JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) curNode) |
| 287 | .getTempJavaCodeFragmentFiles().getServiceTempFiles().getJavaExtendsListHolder(); |
| 288 | if (holder.getExtendsList() != null && !holder.getExtendsList().isEmpty()) { |
| 289 | curNode = curNode.getChild(); |
| 290 | while (curNode != null) { |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 291 | if (curNode instanceof YangNotification) { |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 292 | return getRpcInterfaceDefinitionWhenItExtends(yangName); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 293 | } |
| 294 | curNode = curNode.getNextSibling(); |
| 295 | } |
| 296 | } |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 297 | if (yangName.matches(REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE)) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 298 | return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 299 | } |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 300 | return PUBLIC + SPACE + CLASS + SPACE + yangName + SPACE + IMPLEMENTS + SPACE |
| 301 | + 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] | 302 | } |
| 303 | |
| 304 | /* Provides class definition when RPC interface needs to extends any event.*/ |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 305 | private static String getRpcInterfaceDefinitionWhenItExtends(String yangName) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 306 | |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 307 | if (yangName.matches(REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE)) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 308 | String[] strArray = yangName.split(SERVICE); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 309 | return PUBLIC + SPACE + INTERFACE + SPACE + yangName + NEW_LINE + EIGHT_SPACE_INDENTATION |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 310 | + EXTEND + SPACE + LISTENER_SERVICE + DIAMOND_OPEN_BRACKET + strArray[0] + EVENT_STRING + COMMA |
| 311 | + SPACE + strArray[0] + EVENT_LISTENER_STRING + DIAMOND_CLOSE_BRACKET + SPACE |
| 312 | + OPEN_CURLY_BRACKET + NEW_LINE; |
| 313 | } |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 314 | yangName = yangName.substring(0, yangName.length() - 7); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 315 | return PUBLIC + SPACE + CLASS + SPACE + yangName + MANAGER + NEW_LINE + EIGHT_SPACE_INDENTATION |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 316 | + EXTEND + SPACE + LISTENER_REG + DIAMOND_OPEN_BRACKET + yangName + EVENT_STRING + COMMA + SPACE |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 317 | + yangName + EVENT_LISTENER_STRING + DIAMOND_CLOSE_BRACKET + NEW_LINE |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 318 | + EIGHT_SPACE_INDENTATION + IMPLEMENTS + SPACE + yangName + SERVICE + SPACE + OPEN_CURLY_BRACKET |
| 319 | + NEW_LINE; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 320 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 321 | |
| 322 | /** |
| 323 | * Returns event class definition. |
| 324 | * |
| 325 | * @param javaName file name |
| 326 | * @return definition |
| 327 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 328 | private static String getEventDefinition(String javaName, String eventName) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 329 | return PUBLIC + SPACE + CLASS + SPACE + javaName + SPACE + "extends AbstractEvent<" |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 330 | + javaName + ".Type, " + eventName + ">" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Returns event listener interface definition. |
| 335 | * |
| 336 | * @param javaName file name |
| 337 | * @return definition |
| 338 | */ |
| 339 | private static String getEventListenerDefinition(String javaName) { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 340 | String interfaceDef = PUBLIC + SPACE + INTERFACE + SPACE + javaName + SPACE + "extends EventListener<" |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 341 | + javaName; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 342 | if (interfaceDef.length() < 8) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 343 | throw new RuntimeException("Event listener interface name is error"); |
| 344 | } |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 345 | interfaceDef = interfaceDef.substring(0, interfaceDef.length() - 8); |
| 346 | interfaceDef = interfaceDef + ">" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 347 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 348 | return interfaceDef; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 349 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 350 | |
| 351 | /** |
| 352 | * Returns class definition when class is extending another class. |
| 353 | * |
| 354 | * @param curNode current node |
| 355 | * @param yangName name |
| 356 | * @param genFileTypes gen file type |
| 357 | * @return class definition |
| 358 | */ |
| 359 | private static String getClassDefinitionForWhenExtended(YangNode curNode, String yangName, int genFileTypes) { |
| 360 | JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) curNode) |
| 361 | .getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder(); |
| 362 | |
| 363 | if (holder.getExtendsList() != null && !holder.getExtendsList().isEmpty()) { |
| 364 | String def = PUBLIC + SPACE; |
| 365 | switch (genFileTypes) { |
| 366 | case INTERFACE_MASK: |
| 367 | def = def + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 368 | def = getDefinitionString(def, holder); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 369 | return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 370 | case BUILDER_INTERFACE_MASK: |
| 371 | String builderDef = INTERFACE + SPACE + yangName + BUILDER + SPACE + EXTEND + SPACE; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 372 | builderDef = getDefinitionString(builderDef, holder); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 373 | return builderDef + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 374 | case BUILDER_CLASS_MASK: |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 375 | def = def + STATIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + EXTEND + SPACE; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 376 | def = getDefinitionString(def, holder); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 377 | if (curNode instanceof YangSubModule || curNode instanceof YangModule) { |
| 378 | return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 379 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 380 | return def + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD |
| 381 | + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 382 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 383 | case DEFAULT_CLASS_MASK: |
| 384 | if (curNode instanceof YangSubModule || curNode instanceof YangModule) { |
| 385 | def = def + CLASS + SPACE + yangName + OP_PARAM + SPACE + EXTEND + SPACE; |
| 386 | } else { |
| 387 | def = def + CLASS + SPACE + getCapitalCase(DEFAULT) + yangName + SPACE + EXTEND + SPACE; |
| 388 | } |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 389 | def = getDefinitionString(def, holder); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 390 | return def + SPACE + IMPLEMENTS + SPACE |
| 391 | + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 392 | default: |
| 393 | return null; |
| 394 | } |
| 395 | } |
| 396 | return null; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Returns updated class definition. |
| 401 | * |
| 402 | * @param def current definition |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 403 | * @param holder extend list holder |
| 404 | * @return updated class definition |
| 405 | */ |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 406 | private static String getDefinitionString(String def, JavaExtendsListHolder holder) { |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 407 | for (JavaQualifiedTypeInfoTranslator info : holder.getExtendsList()) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 408 | if (!holder.getExtendedClassStore().get(info)) { |
| 409 | def = def + info.getClassInfo() + COMMA + SPACE; |
| 410 | } else { |
| 411 | def = def + info.getPkgInfo() + PERIOD + info.getClassInfo() + COMMA + SPACE; |
| 412 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 413 | } |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 414 | return trimAtLast(def, COMMA); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 415 | } |
| 416 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 417 | } |