blob: e778a8412a49a62ad2113ccb07584902401ed2d2 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal870c56f2016-02-20 21:57:16 +05303 *
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
17package org.onosproject.yangutils.translator.tojava.utils;
18
Bharat saraswalb1170bd2016-07-14 13:26:18 +053019import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswald50c6382016-07-14 21:57:13 +053020import org.onosproject.yangutils.datamodel.YangCase;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +053021import org.onosproject.yangutils.datamodel.YangIdentity;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053022import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053023import org.onosproject.yangutils.datamodel.YangNotification;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +053024import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053025import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
26import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053027import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
28import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053029
Bharat saraswale2d51d62016-03-23 19:40:35 +053030import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
31import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
Bharat saraswald72411a2016-04-19 01:00:16 +053032import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053033import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
34import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053035import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +053036import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_IDENTITY_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Bharat saraswale2d51d62016-03-23 19:40:35 +053038import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053039import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053040import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053042import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.OPERATION_BUILDER_CLASS_MASK;
43import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.OPERATION_CLASS_MASK;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053044import static org.onosproject.yangutils.utils.UtilConstants.ABSTRACT;
Bharat saraswale2d51d62016-03-23 19:40:35 +053045import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
46import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053047import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053048import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053049import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
50import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
51import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
Bharat saraswald72411a2016-04-19 01:00:16 +053052import static org.onosproject.yangutils.utils.UtilConstants.ENUM;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053053import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER_STRING;
54import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053055import static org.onosproject.yangutils.utils.UtilConstants.EXTEND;
Bharat saraswale2d51d62016-03-23 19:40:35 +053056import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
Bharat saraswale2d51d62016-03-23 19:40:35 +053057import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
58import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053059import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
60import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053061import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
Bharat saraswale2d51d62016-03-23 19:40:35 +053062import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
63import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
Bharat saraswald50c6382016-07-14 21:57:13 +053064import static org.onosproject.yangutils.utils.UtilConstants.OPERATION;
Bharat saraswale2d51d62016-03-23 19:40:35 +053065import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
66import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
janani bdd1314f2016-05-19 17:39:50 +053067import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053068import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053069import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053070import static org.onosproject.yangutils.utils.UtilConstants.SUBJECT;
Bharat saraswald50c6382016-07-14 21:57:13 +053071import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_OP_PARAM_INFO;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +053072import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053073import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Bharat saraswal870c56f2016-02-20 21:57:16 +053074
75/**
Bharat saraswald9822e92016-04-05 15:13:44 +053076 * Represents generator for class definition of generated files.
Bharat saraswal870c56f2016-02-20 21:57:16 +053077 */
78public final class ClassDefinitionGenerator {
79
80 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053081 * Creates an instance of class definition generator.
Bharat saraswal870c56f2016-02-20 21:57:16 +053082 */
83 private ClassDefinitionGenerator() {
84 }
85
86 /**
Bharat saraswald50c6382016-07-14 21:57:13 +053087 * Based on the file type and the YANG name of the file, generate the class / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053088 *
89 * @param genFileTypes generated file type
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053090 * @param yangName class name
Bharat saraswal870c56f2016-02-20 21:57:16 +053091 * @return class definition
92 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053093 public static String generateClassDefinition(int genFileTypes, String yangName) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053094
95 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053096 * Based on the file type and the YANG name of the file, generate the
Vinod Kumar Sc4216002016-03-03 19:55:30 +053097 * class / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053098 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053099 switch (genFileTypes) {
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530100 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 saraswal870c56f2016-02-20 21:57:16 +0530107 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530108 }
109
110 /**
Bharat saraswald50c6382016-07-14 21:57:13 +0530111 * Based on the file type and the YANG name of the file, generate the class / interface definition start.
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530112 *
113 * @param genFileTypes generated file type
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530114 * @param yangName class name
115 * @param curNode current YANG node
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530116 * @return class definition
117 */
118 public static String generateClassDefinition(int genFileTypes, String yangName, YangNode curNode) {
119
120 /**
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 Agrawal8a5af142016-06-15 13:58:01 +0530125 case INTERFACE_MASK:
126 return getInterfaceDefinition(yangName, curNode);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530127 case BUILDER_CLASS_MASK:
128 return getBuilderClassDefinition(yangName, curNode);
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530129 case OPERATION_BUILDER_CLASS_MASK:
130 return getOpParamBuilderClassDefinition(yangName, curNode);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530131 case IMPL_CLASS_MASK:
132 return getImplClassDefinition(yangName, curNode);
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530133 case OPERATION_CLASS_MASK:
Bharat saraswald50c6382016-07-14 21:57:13 +0530134 return getOpPramImplClassDefinition(yangName, curNode);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530135 case BUILDER_INTERFACE_MASK:
136 return getBuilderInterfaceDefinition(yangName, curNode);
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530137 case GENERATE_SERVICE_AND_MANAGER:
138 return getRpcInterfaceDefinition(yangName, curNode);
139 case GENERATE_EVENT_CLASS:
140 String eventName = yangName + SUBJECT;
141 return getEventDefinition(yangName, eventName);
142 case GENERATE_EVENT_LISTENER_INTERFACE:
143 return getEventListenerDefinition(yangName);
144 case GENERATE_EVENT_SUBJECT_CLASS:
145 return getClassDefinition(yangName);
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530146 case GENERATE_IDENTITY_CLASS:
147 return getIdentityClassDefinition(yangName, curNode);
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530148 default:
149 return null;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530150 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530151 }
152
153 /**
Bharat saraswald72411a2016-04-19 01:00:16 +0530154 * Returns enum file class definition.
155 *
156 * @param yangName class name
Gaurav Agrawal56527662016-04-20 15:49:17 +0530157 * @return enum file class definition
Bharat saraswald72411a2016-04-19 01:00:16 +0530158 */
159 private static String getEnumClassDefinition(String yangName) {
160 return PUBLIC + SPACE + ENUM + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
161 }
162
163 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530164 * Returns interface file class definition.
165 *
166 * @param yangName file name
167 * @return definition
168 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530169 private static String getInterfaceDefinition(String yangName, YangNode curNode) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530170
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530171 String clsDef = getClassDefinitionForWhenExtended(curNode, yangName, INTERFACE_MASK);
172 if (clsDef != null) {
173 return clsDef;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530174 }
175 return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530176 }
177
178 /**
179 * Returns builder interface file class definition.
180 *
Bharat saraswald50c6382016-07-14 21:57:13 +0530181 * @param yangName java class name, corresponding to which the builder class is being generated
Bharat saraswal870c56f2016-02-20 21:57:16 +0530182 * @return definition
183 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530184 private static String getBuilderInterfaceDefinition(String yangName, YangNode curNode) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530185 if (!(curNode instanceof YangCase)) {
186 String clsDef = getClassDefinitionForWhenExtended(curNode, yangName, BUILDER_INTERFACE_MASK);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530187 if (clsDef != null) {
188 return clsDef;
189 }
190 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530191 return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530192 }
193
194 /**
195 * Returns builder file class definition.
196 *
197 * @param yangName file name
198 * @return definition
199 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530200 private static String getBuilderClassDefinition(String yangName, YangNode curNode) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530201 if (!(curNode instanceof YangCase)) {
202 String clsDef = getClassDefinitionForWhenExtended(curNode, yangName, BUILDER_CLASS_MASK);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530203 if (clsDef != null) {
204 return clsDef;
205 }
206 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530207 return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
208 + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530209 }
210
211 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530212 * Returns operation param builder file class definition.
213 *
214 * @param yangName class name
Bharat saraswald50c6382016-07-14 21:57:13 +0530215 * @param curNode YANG node
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530216 * @return definition returns operation param builder file class definition
217 */
218 private static String getOpParamBuilderClassDefinition(String yangName, YangNode curNode) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530219 if (!(curNode instanceof YangCase)) {
220 String clsDef = getClassDefinitionForWhenExtended(curNode, yangName, OPERATION_BUILDER_CLASS_MASK);
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530221 if (clsDef != null) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530222 return clsDef + OPEN_CURLY_BRACKET + NEW_LINE;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530223 }
224 }
225 return PUBLIC + SPACE + CLASS + SPACE + yangName + OPERATION + BUILDER + SPACE + EXTEND +
226 SPACE + getCapitalCase(DEFAULT) + yangName + PERIOD + yangName + BUILDER + SPACE +
227 OPEN_CURLY_BRACKET + NEW_LINE;
228 }
229
230 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530231 * Returns impl file class definition.
232 *
233 * @param yangName file name
234 * @return definition
235 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530236 private static String getImplClassDefinition(String yangName, YangNode curNode) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530237 if (!(curNode instanceof YangCase)) {
238 String clsDef = getClassDefinitionForWhenExtended(curNode, yangName, IMPL_CLASS_MASK);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530239 if (clsDef != null) {
240 return clsDef;
241 }
242 }
243 return PUBLIC + SPACE + CLASS + SPACE + getCapitalCase(DEFAULT) + yangName + SPACE + IMPLEMENTS + SPACE
Bharat saraswal33dfa012016-05-17 19:59:16 +0530244 + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
245 }
246
247 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530248 * Returns operation param file class definition.
249 *
250 * @param yangName class name
Bharat saraswald50c6382016-07-14 21:57:13 +0530251 * @param curNode YANG node
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530252 * @return definition returns operation param file class definition
253 */
Bharat saraswald50c6382016-07-14 21:57:13 +0530254 private static String getOpPramImplClassDefinition(String yangName, YangNode curNode) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530255 String clsDef = "";
Bharat saraswald50c6382016-07-14 21:57:13 +0530256 if (!(curNode instanceof YangCase)) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530257 clsDef = getClassDefinitionForWhenExtended(curNode, yangName, OPERATION_CLASS_MASK);
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530258 }
259
Bharat saraswald50c6382016-07-14 21:57:13 +0530260 if (clsDef.equals("")) {
261 clsDef = PUBLIC + SPACE + CLASS + SPACE + yangName + OPERATION + SPACE + EXTEND + SPACE
262 + getCapitalCase(DEFAULT) + yangName + SPACE;
263 }
264 if (curNode instanceof YangAugment) {
265 clsDef = clsDef + IMPLEMENTS + SPACE + YANG_AUGMENTED_OP_PARAM_INFO + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
266 } else {
267 clsDef = clsDef + OPEN_CURLY_BRACKET + NEW_LINE;
268 }
269
270 return clsDef;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530271 }
272
273 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530274 * Returns impl file class definition.
275 *
276 * @param yangName file name
277 * @return definition
278 */
279 private static String getClassDefinition(String yangName) {
280 return PUBLIC + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530281 }
282
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530283 /**
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530284 * Returns implementation file identity class definition.
285 *
286 * @param yangName file name
287 * @return identity class definition
288 */
289 private static String getIdentityClassDefinition(String yangName, YangNode curNode) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530290 if (!(curNode instanceof YangIdentity)) {
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530291 throw new TranslatorException("Expected java identity instance node");
292 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530293 YangIdentity identity = (YangIdentity) curNode;
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530294 if (identity.getBaseNode() != null) {
295 YangIdentity baseIdentity = identity.getBaseNode().getReferredIdentity();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530296 if (!(baseIdentity instanceof YangIdentity)) {
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530297 throw new TranslatorException("Expected java identity instance node");
298 }
299
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530300 JavaFileInfo fileInfo = ((JavaFileInfoContainer) baseIdentity).getJavaFileInfo();
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530301 return PUBLIC + SPACE + ABSTRACT + SPACE + CLASS + SPACE + yangName + SPACE + EXTEND + SPACE
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530302 + getCapitalCase(fileInfo.getJavaName()) + SPACE +
Shankara-Huaweidf7b9ca2016-07-14 11:35:34 +0530303 OPEN_CURLY_BRACKET + NEW_LINE;
304 }
305
306 return PUBLIC + SPACE + ABSTRACT + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
307 }
308
309 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530310 * Returns type file class definition.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530311 *
312 * @param yangName file name
313 * @return definition
314 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530315 private static String getTypeClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530316 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530317 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530318
319 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530320 * Returns RPC file interface definition.
Gaurav Agrawal56527662016-04-20 15:49:17 +0530321 *
322 * @param yangName file name
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530323 * @param curNode current YANG node
Gaurav Agrawal56527662016-04-20 15:49:17 +0530324 * @return definition
325 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530326 private static String getRpcInterfaceDefinition(String yangName, YangNode curNode) {
327 JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) curNode)
328 .getTempJavaCodeFragmentFiles().getServiceTempFiles().getJavaExtendsListHolder();
329 if (holder.getExtendsList() != null && !holder.getExtendsList().isEmpty()) {
330 curNode = curNode.getChild();
331 while (curNode != null) {
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530332 if (curNode instanceof YangNotification) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530333 return getRpcInterfaceDefinitionWhenItExtends(yangName);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530334 }
335 curNode = curNode.getNextSibling();
336 }
337 }
janani bdd1314f2016-05-19 17:39:50 +0530338 if (yangName.matches(REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE)) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530339 return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
340 }
janani bdd1314f2016-05-19 17:39:50 +0530341 return PUBLIC + SPACE + CLASS + SPACE + yangName + SPACE + IMPLEMENTS + SPACE
342 + yangName.substring(0, yangName.length() - 7) + SERVICE + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530343 }
344
345 /* Provides class definition when RPC interface needs to extends any event.*/
Bharat saraswalb551aae2016-07-14 15:18:20 +0530346 private static String getRpcInterfaceDefinitionWhenItExtends(String yangName) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530347
janani bdd1314f2016-05-19 17:39:50 +0530348 if (yangName.matches(REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE)) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530349 String[] strArray = yangName.split(SERVICE);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530350 return PUBLIC + SPACE + INTERFACE + SPACE + yangName + NEW_LINE + EIGHT_SPACE_INDENTATION
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530351 + EXTEND + SPACE + LISTENER_SERVICE + DIAMOND_OPEN_BRACKET + strArray[0] + EVENT_STRING + COMMA
352 + SPACE + strArray[0] + EVENT_LISTENER_STRING + DIAMOND_CLOSE_BRACKET + SPACE
353 + OPEN_CURLY_BRACKET + NEW_LINE;
354 }
janani bdd1314f2016-05-19 17:39:50 +0530355 yangName = yangName.substring(0, yangName.length() - 7);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530356 return PUBLIC + SPACE + CLASS + SPACE + yangName + MANAGER + NEW_LINE + EIGHT_SPACE_INDENTATION
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530357 + EXTEND + SPACE + LISTENER_REG + DIAMOND_OPEN_BRACKET + yangName + EVENT_STRING + COMMA + SPACE
Bharat saraswal33dfa012016-05-17 19:59:16 +0530358 + yangName + EVENT_LISTENER_STRING + DIAMOND_CLOSE_BRACKET + NEW_LINE
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530359 + EIGHT_SPACE_INDENTATION + IMPLEMENTS + SPACE + yangName + SERVICE + SPACE + OPEN_CURLY_BRACKET
360 + NEW_LINE;
Gaurav Agrawal56527662016-04-20 15:49:17 +0530361 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530362
363 /**
364 * Returns event class definition.
365 *
366 * @param javaName file name
367 * @return definition
368 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530369 private static String getEventDefinition(String javaName, String eventName) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530370 String classDef = PUBLIC + SPACE + CLASS + SPACE + javaName + SPACE + "extends AbstractEvent<"
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530371 + javaName + ".Type, " + eventName + ">" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530372
373 return classDef;
374 }
375
376 /**
377 * Returns event listener interface definition.
378 *
379 * @param javaName file name
380 * @return definition
381 */
382 private static String getEventListenerDefinition(String javaName) {
383 String intfDef = PUBLIC + SPACE + INTERFACE + SPACE + javaName + SPACE + "extends EventListener<"
384 + javaName;
385 if (intfDef.length() < 8) {
386 throw new RuntimeException("Event listener interface name is error");
387 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530388 intfDef = intfDef.substring(0, intfDef.length() - 8);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530389 intfDef = intfDef + "Event>" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
390
391 return intfDef;
392 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530393
394 /**
395 * Returns class definition when class is extending another class.
396 *
397 * @param curNode current node
398 * @param yangName name
399 * @param genFileTypes gen file type
400 * @return class definition
401 */
402 private static String getClassDefinitionForWhenExtended(YangNode curNode, String yangName, int genFileTypes) {
403 JavaExtendsListHolder holder = ((TempJavaCodeFragmentFilesContainer) curNode)
404 .getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder();
405
406 if (holder.getExtendsList() != null && !holder.getExtendsList().isEmpty()) {
407 String def = PUBLIC + SPACE;
408 switch (genFileTypes) {
409 case INTERFACE_MASK:
410 def = def + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE;
Bharat saraswald50c6382016-07-14 21:57:13 +0530411 def = getDefinitionString(def, holder);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530412 return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
413 case BUILDER_INTERFACE_MASK:
414 String builderDef = INTERFACE + SPACE + yangName + BUILDER + SPACE + EXTEND + SPACE;
Bharat saraswald50c6382016-07-14 21:57:13 +0530415 builderDef = getDefinitionString(builderDef, holder);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530416 return builderDef + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
417 case BUILDER_CLASS_MASK:
418 def = def + CLASS + SPACE + yangName + BUILDER + SPACE + EXTEND + SPACE;
Bharat saraswald50c6382016-07-14 21:57:13 +0530419 def = getDefinitionString(def, holder);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530420 return def + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
421 + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
422
423 case IMPL_CLASS_MASK:
424 def = def + SPACE + CLASS + SPACE + getCapitalCase(DEFAULT) + yangName + SPACE + EXTEND + SPACE;
Bharat saraswald50c6382016-07-14 21:57:13 +0530425 def = getDefinitionString(def, holder);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530426 return def + SPACE + IMPLEMENTS + SPACE
427 + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
428
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530429 case OPERATION_CLASS_MASK:
Bharat saraswald50c6382016-07-14 21:57:13 +0530430 def = def + CLASS + SPACE + yangName + OPERATION + SPACE + EXTEND + SPACE;
431 def = getDefinitionString(def, holder);
432 return def + SPACE;
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530433 default:
434 return null;
435 }
436 }
437 return null;
438 }
439
440 /**
441 * Returns updated class definition.
442 *
443 * @param def current definition
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530444 * @param holder extend list holder
445 * @return updated class definition
446 */
Bharat saraswald50c6382016-07-14 21:57:13 +0530447 private static String getDefinitionString(String def, JavaExtendsListHolder holder) {
448 for (JavaQualifiedTypeInfo info : holder.getExtendsList()) {
449 if (!holder.getExtendedClassStore().get(info)) {
450 def = def + info.getClassInfo() + COMMA + SPACE;
451 } else {
452 def = def + info.getPkgInfo() + PERIOD + info.getClassInfo() + COMMA + SPACE;
453 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530454 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530455 return trimAtLast(def, COMMA);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530456 }
457
Bharat saraswal870c56f2016-02-20 21:57:16 +0530458}