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 | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 19 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 20 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 21 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 22 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS; |
| 23 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE; |
| 24 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 25 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK; |
| 28 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList; |
| 30 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.isExtendsList; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.CLASS; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.UtilConstants.ENUM; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.utils.UtilConstants.EXTEND; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.UtilConstants.FINAL; |
| 37 | import static org.onosproject.yangutils.utils.UtilConstants.IMPL; |
| 38 | import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.MANAGER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET; |
| 43 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 44 | import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 48 | |
| 49 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 50 | * Represents generator for class definition of generated files. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 51 | */ |
| 52 | public final class ClassDefinitionGenerator { |
| 53 | |
| 54 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 55 | * Creates an instance of class definition generator. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 56 | */ |
| 57 | private ClassDefinitionGenerator() { |
| 58 | } |
| 59 | |
| 60 | /** |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 61 | * Based on the file type and the YANG name of the file, generate the class |
| 62 | * / interface definition start. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 63 | * |
| 64 | * @param genFileTypes generated file type |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 65 | * @param yangName class name |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 66 | * @return class definition |
| 67 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 68 | public static String generateClassDefinition(int genFileTypes, String yangName) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 69 | |
| 70 | /** |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 71 | * 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] | 72 | * class / interface definition start. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 73 | */ |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 74 | if ((genFileTypes & INTERFACE_MASK) != 0) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 75 | return getInterfaceDefinition(yangName); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 76 | } else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 77 | return getBuilderClassDefinition(yangName); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 78 | } else if ((genFileTypes & IMPL_CLASS_MASK) != 0) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 79 | return getImplClassDefinition(yangName); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 80 | } else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 81 | return getBuilderInterfaceDefinition(yangName); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 82 | } else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 83 | return getTypeClassDefinition(yangName); |
| 84 | } else if ((genFileTypes & GENERATE_UNION_CLASS) != 0) { |
| 85 | return getTypeClassDefinition(yangName); |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 86 | } else if ((genFileTypes & GENERATE_ENUM_CLASS) != 0) { |
| 87 | return getEnumClassDefinition(yangName); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 88 | } else if ((genFileTypes & GENERATE_SERVICE_AND_MANAGER) != 0) { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 89 | return getRpcInterfaceDefinition(yangName); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 90 | } else if ((genFileTypes & GENERATE_EVENT_CLASS) != 0) { |
| 91 | return getEventDefinition(yangName); |
| 92 | } else if ((genFileTypes & GENERATE_EVENT_LISTENER_INTERFACE) != 0) { |
| 93 | return getEventListenerDefinition(yangName); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | /** |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 99 | * Returns enum file class definition. |
| 100 | * |
| 101 | * @param yangName class name |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 102 | * @return enum file class definition |
Bharat saraswal | 68fa0d1 | 2016-04-19 01:00:16 +0530 | [diff] [blame] | 103 | */ |
| 104 | private static String getEnumClassDefinition(String yangName) { |
| 105 | return PUBLIC + SPACE + ENUM + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 106 | } |
| 107 | |
| 108 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 109 | * Returns interface file class definition. |
| 110 | * |
| 111 | * @param yangName file name |
| 112 | * @return definition |
| 113 | */ |
| 114 | private static String getInterfaceDefinition(String yangName) { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 115 | if (!isExtendsList()) { |
| 116 | return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 117 | } |
| 118 | String def = PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE; |
| 119 | for (String extend : getExtendsList()) { |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 120 | def = def + extend + COMMA + SPACE; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 121 | } |
| 122 | def = trimAtLast(def, COMMA); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 123 | |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 124 | return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns builder interface file class definition. |
| 129 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 130 | * @param yangName java class name, corresponding to which the builder class |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 131 | * is being generated |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 132 | * @return definition |
| 133 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 134 | private static String getBuilderInterfaceDefinition(String yangName) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 135 | 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] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns builder file class definition. |
| 140 | * |
| 141 | * @param yangName file name |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 142 | * @param genFileTypes |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 143 | * @return definition |
| 144 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 145 | private static String getBuilderClassDefinition(String yangName) { |
| 146 | return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD |
| 147 | + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns impl file class definition. |
| 152 | * |
| 153 | * @param yangName file name |
| 154 | * @return definition |
| 155 | */ |
| 156 | private static String getImplClassDefinition(String yangName) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 157 | return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName |
| 158 | + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 159 | } |
| 160 | |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 161 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 162 | * Returns type file class definition. |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 163 | * |
| 164 | * @param yangName file name |
| 165 | * @return definition |
| 166 | */ |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 167 | private static String getTypeClassDefinition(String yangName) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 168 | 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] | 169 | } |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * Returns rpc file interface definition. |
| 173 | * |
| 174 | * @param yangName file name |
| 175 | * @return definition |
| 176 | */ |
| 177 | private static String getRpcInterfaceDefinition(String yangName) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 178 | if (yangName.contains(SERVICE)) { |
| 179 | return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 180 | } |
| 181 | return PUBLIC + SPACE + CLASS + SPACE + yangName + MANAGER + SPACE + IMPLEMENTS + SPACE + yangName + SERVICE |
| 182 | + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 183 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * Returns event class definition. |
| 187 | * |
| 188 | * @param javaName file name |
| 189 | * @return definition |
| 190 | */ |
| 191 | private static String getEventDefinition(String javaName) { |
| 192 | String classDef = PUBLIC + SPACE + CLASS + SPACE + javaName + SPACE + "extends AbstractEvent<" |
| 193 | + javaName + ".Type, " + javaName; |
| 194 | if (classDef.length() < 5) { |
| 195 | throw new RuntimeException("Event class name is error"); |
| 196 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 197 | classDef = classDef.substring(0, classDef.length() - 5); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 198 | classDef = classDef + ">" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 199 | |
| 200 | return classDef; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns event listener interface definition. |
| 205 | * |
| 206 | * @param javaName file name |
| 207 | * @return definition |
| 208 | */ |
| 209 | private static String getEventListenerDefinition(String javaName) { |
| 210 | String intfDef = PUBLIC + SPACE + INTERFACE + SPACE + javaName + SPACE + "extends EventListener<" |
| 211 | + javaName; |
| 212 | if (intfDef.length() < 8) { |
| 213 | throw new RuntimeException("Event listener interface name is error"); |
| 214 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 215 | intfDef = intfDef.substring(0, intfDef.length() - 8); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 216 | intfDef = intfDef + "Event>" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 217 | |
| 218 | return intfDef; |
| 219 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 220 | } |