blob: 2ab8bc3c37e4246202157e9df14f8169019a9e7e [file] [log] [blame]
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.yangutils.translator.tojava.utils;
18
Bharat saraswal0663aff2016-10-18 23:16:14 +053019import org.onosproject.yangutils.datamodel.YangAugment;
20import org.onosproject.yangutils.datamodel.YangChoice;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053021import org.onosproject.yangutils.datamodel.YangCompilerAnnotation;
Bharat saraswal0663aff2016-10-18 23:16:14 +053022import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053023import org.onosproject.yangutils.datamodel.YangType;
24import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
25import org.onosproject.yangutils.translator.exception.TranslatorException;
Bharat saraswal0663aff2016-10-18 23:16:14 +053026import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator;
27import org.onosproject.yangutils.utils.io.YangPluginConfig;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053028
Bharat saraswal0663aff2016-10-18 23:16:14 +053029import java.util.ArrayList;
janani bc27947a2016-10-19 01:17:40 +053030import java.util.Arrays;
31import java.util.HashSet;
Bharat saraswal0663aff2016-10-18 23:16:14 +053032import java.util.List;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053033import java.util.Map;
janani bc27947a2016-10-19 01:17:40 +053034import java.util.Set;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053035
janani bc27947a2016-10-19 01:17:40 +053036import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BOOLEAN;
37import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DECIMAL64;
38import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.EMPTY;
39import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT16;
40import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
41import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64;
42import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT8;
43import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16;
44import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32;
45import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT64;
46import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT8;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053047import static org.onosproject.yangutils.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET;
48import static org.onosproject.yangutils.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET_WITH_VALUE;
49import static org.onosproject.yangutils.translator.tojava.utils.BracketType.OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE;
50import static org.onosproject.yangutils.translator.tojava.utils.MethodClassTypes.CLASS_TYPE;
51import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getIfConditionForAddToListMethod;
Bharat saraswal0663aff2016-10-18 23:16:14 +053052import static org.onosproject.yangutils.translator.tojava.utils.SubtreeFilteringMethodsGenerator.getQualifiedInfo;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053053import static org.onosproject.yangutils.utils.UtilConstants.ADD_STRING;
54import static org.onosproject.yangutils.utils.UtilConstants.AND;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053055import static org.onosproject.yangutils.utils.UtilConstants.APPEND;
Bharat saraswal0663aff2016-10-18 23:16:14 +053056import static org.onosproject.yangutils.utils.UtilConstants.APP_INSTANCE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053057import static org.onosproject.yangutils.utils.UtilConstants.AT;
58import static org.onosproject.yangutils.utils.UtilConstants.BIG_DECIMAL;
59import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053060import static org.onosproject.yangutils.utils.UtilConstants.BITSET;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053061import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
62import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER;
63import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
64import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_LOWER_CASE;
65import static org.onosproject.yangutils.utils.UtilConstants.BYTE;
66import static org.onosproject.yangutils.utils.UtilConstants.BYTE_WRAPPER;
67import static org.onosproject.yangutils.utils.UtilConstants.CATCH;
68import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING;
69import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
70import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
71import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
72import static org.onosproject.yangutils.utils.UtilConstants.COLON;
73import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
74import static org.onosproject.yangutils.utils.UtilConstants.COMPARE_TO;
75import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_CAPS;
76import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
77import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
78import static org.onosproject.yangutils.utils.UtilConstants.DOUBLE;
79import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
80import static org.onosproject.yangutils.utils.UtilConstants.ELSE;
81import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
82import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
Bharat saraswal0663aff2016-10-18 23:16:14 +053083import static org.onosproject.yangutils.utils.UtilConstants.EQUALS_STRING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053084import static org.onosproject.yangutils.utils.UtilConstants.EXCEPTION;
85import static org.onosproject.yangutils.utils.UtilConstants.EXCEPTION_VAR;
86import static org.onosproject.yangutils.utils.UtilConstants.EXTEND;
87import static org.onosproject.yangutils.utils.UtilConstants.FALSE;
88import static org.onosproject.yangutils.utils.UtilConstants.FOR;
89import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
90import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_METHOD_NAME;
91import static org.onosproject.yangutils.utils.UtilConstants.GET;
Bharat saraswal94844d62016-10-13 13:28:03 +053092import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_METHOD_STATIC_STRING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053093import static org.onosproject.yangutils.utils.UtilConstants.IF;
94import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
95import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
96import static org.onosproject.yangutils.utils.UtilConstants.IN;
97import static org.onosproject.yangutils.utils.UtilConstants.INT;
98import static org.onosproject.yangutils.utils.UtilConstants.INTEGER_WRAPPER;
99import static org.onosproject.yangutils.utils.UtilConstants.INT_MAX_RANGE;
100import static org.onosproject.yangutils.utils.UtilConstants.INT_MIN_RANGE;
Bharat saraswal0663aff2016-10-18 23:16:14 +0530101import static org.onosproject.yangutils.utils.UtilConstants.LEAF_IDENTIFIER;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530102import static org.onosproject.yangutils.utils.UtilConstants.LIST;
103import static org.onosproject.yangutils.utils.UtilConstants.LONG;
104import static org.onosproject.yangutils.utils.UtilConstants.LONG_MAX_RANGE;
105import static org.onosproject.yangutils.utils.UtilConstants.LONG_MIN_RANGE;
106import static org.onosproject.yangutils.utils.UtilConstants.LONG_WRAPPER;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530107import static org.onosproject.yangutils.utils.UtilConstants.MORE_OBJ_ATTR;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530108import static org.onosproject.yangutils.utils.UtilConstants.NEW;
109import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
110import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
111import static org.onosproject.yangutils.utils.UtilConstants.OBJECT_STRING;
112import static org.onosproject.yangutils.utils.UtilConstants.OF;
113import static org.onosproject.yangutils.utils.UtilConstants.OMIT_NULL_VALUE_STRING;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530114import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CLOSE_BRACKET_STRING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530115import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
116import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
117import static org.onosproject.yangutils.utils.UtilConstants.OVERRIDE;
118import static org.onosproject.yangutils.utils.UtilConstants.PARSE_BOOLEAN;
119import static org.onosproject.yangutils.utils.UtilConstants.PARSE_BYTE;
120import static org.onosproject.yangutils.utils.UtilConstants.PARSE_INT;
121import static org.onosproject.yangutils.utils.UtilConstants.PARSE_LONG;
122import static org.onosproject.yangutils.utils.UtilConstants.PARSE_SHORT;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530123import static org.onosproject.yangutils.utils.UtilConstants.PATTERN;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530124import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
125import static org.onosproject.yangutils.utils.UtilConstants.PUT;
126import static org.onosproject.yangutils.utils.UtilConstants.QUEUE;
127import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530128import static org.onosproject.yangutils.utils.UtilConstants.QUOTE_STRING;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530129import static org.onosproject.yangutils.utils.UtilConstants.RETURN;
130import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLON;
131import static org.onosproject.yangutils.utils.UtilConstants.SET;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530132import static org.onosproject.yangutils.utils.UtilConstants.SET_VALUE_PARA;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530133import static org.onosproject.yangutils.utils.UtilConstants.SHORT;
134import static org.onosproject.yangutils.utils.UtilConstants.SHORT_MAX_RANGE;
135import static org.onosproject.yangutils.utils.UtilConstants.SHORT_MIN_RANGE;
136import static org.onosproject.yangutils.utils.UtilConstants.SHORT_WRAPPER;
137import static org.onosproject.yangutils.utils.UtilConstants.SIXTEEN_SPACE_INDENTATION;
138import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530139import static org.onosproject.yangutils.utils.UtilConstants.STRING_BUILDER;
140import static org.onosproject.yangutils.utils.UtilConstants.STRING_BUILDER_VAR;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530141import static org.onosproject.yangutils.utils.UtilConstants.THIS;
142import static org.onosproject.yangutils.utils.UtilConstants.TMP_VAL;
Bharat saraswal94844d62016-10-13 13:28:03 +0530143import static org.onosproject.yangutils.utils.UtilConstants.TO_STRING_METHOD;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530144import static org.onosproject.yangutils.utils.UtilConstants.TRY;
145import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
146import static org.onosproject.yangutils.utils.UtilConstants.TWENTY_SPACE_INDENTATION;
147import static org.onosproject.yangutils.utils.UtilConstants.UINT8_MAX_RANGE;
148import static org.onosproject.yangutils.utils.UtilConstants.UINT8_MIN_RANGE;
149import static org.onosproject.yangutils.utils.UtilConstants.UINT_MAX_RANGE;
150import static org.onosproject.yangutils.utils.UtilConstants.UINT_MIN_RANGE;
151import static org.onosproject.yangutils.utils.UtilConstants.ULONG_MAX_RANGE;
152import static org.onosproject.yangutils.utils.UtilConstants.ULONG_MIN_RANGE;
153import static org.onosproject.yangutils.utils.UtilConstants.VALIDATE_RANGE;
154import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
Bharat saraswal94844d62016-10-13 13:28:03 +0530155import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_MAP;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530156import static org.onosproject.yangutils.utils.UtilConstants.YANG_UTILS_TODO;
157import static org.onosproject.yangutils.utils.UtilConstants.ZERO;
158import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
159
160/**
161 * Represents string generator for translator.
162 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530163public final class StringGenerator {
164
janani bc27947a2016-10-19 01:17:40 +0530165 private static final Set<YangDataTypes> PRIMITIVE_TYPES =
166 new HashSet<>(Arrays.asList(INT8, INT16, INT32, INT64, UINT8,
167 UINT16, UINT32, UINT64, DECIMAL64,
168 BOOLEAN, EMPTY));
169
170 // No instantiation.
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530171 private StringGenerator() {
172 }
173
174 /**
175 * Returns compare to string.
176 *
177 * @return compare to string
178 */
179 static String getCompareToString() {
180 return PERIOD + COMPARE_TO;
181 }
182
183 /**
184 * Returns lesser than and equals condition.
185 *
186 * @return lesser than and equals condition
187 */
188 static String getLesserThanCondition() {
189 return SPACE + DIAMOND_OPEN_BRACKET + EQUAL + SPACE;
190 }
191
192 /**
193 * Returns greater than and equals condition.
194 *
195 * @return greater than and equals condition
196 */
197 static String getGreaterThanCondition() {
198 return SPACE + DIAMOND_CLOSE_BRACKET + EQUAL + SPACE;
199 }
200
201 /**
202 * Returns && conditional string.
203 *
204 * @param cond1 condition one
205 * @param cond2 condition two
206 * @return && conditional string
207 */
208 static String ifAndAndCondition(String cond1, String cond2) {
209 return cond1 + SPACE + AND + AND + SPACE + cond2;
210 }
211
212 /**
213 * Returns equal equal conditional string.
214 *
215 * @param cond1 condition one
216 * @param cond2 condition two
217 * @return equal equal conditional string
218 */
219 static String ifEqualEqualCondition(String cond1, String cond2) {
220 return cond1 + SPACE + EQUAL + EQUAL + SPACE + cond2;
221 }
222
223 /**
224 * Returns new instance string.
225 *
226 * @param returnType return type
227 * @param varName variable name
228 * @param space spaces
229 * @param value value
230 * @return new instance string
231 */
232 static String getNewInstance(String returnType, String varName,
233 String space, String value) {
234 return space + returnType + SPACE + varName + SPACE + EQUAL + SPACE +
Bharat saraswal0663aff2016-10-18 23:16:14 +0530235 NEW + SPACE + returnType + getOpenCloseParaWithValue(value) +
236 signatureClose();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530237 }
238
239 /**
240 * Returns return string.
241 *
242 * @param value value to be returned
243 * @param space spaces
244 * @return return string
245 */
246 static String getReturnString(String value, String space) {
247 return space + RETURN + SPACE + value;
248 }
249
250 /**
251 * Returns new line string with spaces.
252 *
253 * @param space spaces
254 * @return new line string with spaces
255 */
256 static String getNewLineAndSpace(String space) {
257 return NEW_LINE + space;
258 }
259
260 /**
261 * Returns method close string.
262 *
263 * @param type indentation type
264 * @return method close string
265 */
Bharat saraswal94844d62016-10-13 13:28:03 +0530266 public static String methodClose(IndentationType type) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530267 switch (type) {
268 case EIGHT_SPACE:
269 return EIGHT_SPACE_INDENTATION + CLOSE_CURLY_BRACKET +
270 NEW_LINE;
271 case TWELVE_SPACE:
272 return TWELVE_SPACE_INDENTATION + CLOSE_CURLY_BRACKET +
273 NEW_LINE;
274 case SIXTEEN_SPACE:
275 return SIXTEEN_SPACE_INDENTATION + CLOSE_CURLY_BRACKET +
276 NEW_LINE;
277 case TWENTY_SPACE:
278 return TWENTY_SPACE_INDENTATION + CLOSE_CURLY_BRACKET +
279 NEW_LINE;
280 case TWENTY_EIGHT_SPACE:
Bharat saraswal0663aff2016-10-18 23:16:14 +0530281 return TWENTY_SPACE_INDENTATION + EIGHT_SPACE_INDENTATION +
282 CLOSE_CURLY_BRACKET + NEW_LINE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530283 case TWENTY_FOUR_SPACE:
Bharat saraswal0663aff2016-10-18 23:16:14 +0530284 return TWENTY_SPACE_INDENTATION + FOUR_SPACE_INDENTATION +
285 CLOSE_CURLY_BRACKET + NEW_LINE;
286 case FOUR_SPACE:
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530287 return FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET +
288 NEW_LINE;
289 default:
Bharat saraswal0663aff2016-10-18 23:16:14 +0530290 return CLOSE_CURLY_BRACKET + NEW_LINE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530291 }
292 }
293
294 /**
295 * Returns body of the method.
296 *
297 * @param type type of method body
298 * @param paraName parameter name
299 * @param methodName method name
300 * @param space space to be given before body
301 * @param prefix prefix for internal method
302 * @param paramType parameter type
303 * @param isBuilderSetter is for builder setter
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530304 * @param setterVal value to set in setter
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530305 * @return body of the method
306 */
307 static String methodBody(MethodBodyTypes type, String paraName,
308 String methodName,
309 String space, String prefix,
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530310 String paramType, boolean isBuilderSetter, String setterVal) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530311 StringBuilder builder = new StringBuilder();
312 String body;
Bharat saraswal94844d62016-10-13 13:28:03 +0530313 String cond;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530314 switch (type) {
315 case GETTER:
316 return getReturnString(paraName, space);
317 case SETTER:
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530318 if (setterVal == null) {
319 setterVal = paraName;
320 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530321 body = space + THIS + PERIOD + paraName + SPACE + EQUAL + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530322 setterVal + signatureClose();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530323 builder.append(body);
324 if (isBuilderSetter) {
325 body = getReturnString(THIS + signatureClose(), space);
326 builder.append(body);
327 }
328 return builder.toString();
329 case BUILD:
330 return getReturnString(
Bharat saraswal0663aff2016-10-18 23:16:14 +0530331 NEW + SPACE + paraName + getOpenCloseParaWithValue(
332 THIS) + signatureClose(), space);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530333 case CONSTRUCTOR:
334 return space + THIS + PERIOD + paraName + SPACE +
335 EQUAL + SPACE + BUILDER_LOWER_CASE + OBJECT + PERIOD +
336 prefix + methodName +
337 brackets(OPEN_CLOSE_BRACKET, null, null) +
338 signatureClose();
339 case DEFAULT_CONSTRUCTOR:
340 return EMPTY_STRING;
341 case ADD_TO_LIST:
342 return space + getIfConditionForAddToListMethod(paraName) +
343 space + paraName +
344 brackets(OPEN_CLOSE_BRACKET, null, null) + PERIOD +
Bharat saraswal0663aff2016-10-18 23:16:14 +0530345 ADD_STRING + getOpenCloseParaWithValue(VALUE) +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530346 signatureClose() + getReturnString(
347 THIS + signatureClose(), space);
348 case AUGMENTED_MAP_ADD:
Bharat saraswal94844d62016-10-13 13:28:03 +0530349 return space + YANG_AUGMENTED_INFO_MAP +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530350 PERIOD + PUT + OPEN_PARENTHESIS + CLASS +
351 OBJECT_STRING + COMMA + SPACE + VALUE +
352 CLOSE_PARENTHESIS + signatureClose();
353 case AUGMENTED_MAP_GET_VALUE:
354 return getReturnString(
Bharat saraswal0663aff2016-10-18 23:16:14 +0530355 YANG_AUGMENTED_INFO_MAP + PERIOD + GET + getOpenCloseParaWithValue(
356 CLASS + OBJECT_STRING) + signatureClose(), space);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530357 case AUGMENTED_MAP_GETTER:
Bharat saraswal94844d62016-10-13 13:28:03 +0530358 return getReturnString(YANG_AUGMENTED_INFO_MAP +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530359 signatureClose(), space);
360 case MANAGER_METHODS:
361 body = space + YANG_UTILS_TODO + NEW_LINE;
362 builder.append(body);
363 if (paramType != null) {
364 body = getReturnString(parseTypeForReturnValue(paramType),
365 space);
366 builder.append(body)
367 .append(signatureClose());
368 }
369 return builder.toString();
370 case OF_METHOD:
371 return getReturnString(
Bharat saraswal0663aff2016-10-18 23:16:14 +0530372 NEW + SPACE + paraName + getOpenCloseParaWithValue(
373 VALUE) + signatureClose(), space);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530374 case TO_STRING:
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530375 return getToStringMethodsAddString(space, paraName) + paraName +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530376 CLOSE_PARENTHESIS;
377 case EQUALS_METHOD:
378 default:
379 return null;
380 }
381 }
382
383 /**
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530384 * Returns to string method's add string.
385 *
386 * @param space indentation
387 * @param paraName parameter name
388 * @return to string method's add string
389 */
390 static String getToStringMethodsAddString(String space, String paraName) {
391 return space + PERIOD + ADD_STRING + OPEN_PARENTHESIS +
392 getQuotedString(paraName) + COMMA + SPACE;
393 }
394
395 /**
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530396 * Returns end of line.
397 *
398 * @return end of line
399 */
400 static String signatureClose() {
401 return SEMI_COLON + NEW_LINE;
402 }
403
Bharat saraswal0663aff2016-10-18 23:16:14 +0530404
405 /**
406 * Returns value assignment.
407 *
408 * @return value assignment
409 */
410 static String valueAssign(String param, String value, String indentation) {
411 return indentation + param + SPACE + EQUAL + SPACE + value +
412 signatureClose();
413 }
414
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530415 /**
416 * Returns method signature close for method class type.
417 *
418 * @param type method class type
419 * @return method signature close for method class type
420 */
421 static String methodSignatureClose(MethodClassTypes type) {
422 switch (type) {
423 case INTERFACE_TYPE:
424 return signatureClose();
425 case CLASS_TYPE:
426 return SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
427 default:
428 return null;
429 }
430 }
431
432 /**
433 * Returns method param.
434 *
435 * @param type type of param
436 * @param name name of param
437 * @return method param
438 */
439 private static String methodParam(String type, String name) {
440 return type + SPACE + name;
441 }
442
443 /**
444 * Returns comma followed by a space.
445 *
446 * @return comma followed by a space
447 */
448 private static String commaWithSpace() {
449 return COMMA + SPACE;
450 }
451
452 /**
453 * Returns bracket string for the given type.
454 *
455 * @param type bracket type
456 * @param value value to be added in brackets
457 * @param returnType returns type to be added for value
458 * @return bracket for the given type.
459 */
460 static String brackets(BracketType type, String value,
461 String returnType) {
462 String ret = EMPTY_STRING;
463 switch (type) {
464 case OPEN_CLOSE_BRACKET:
465 return OPEN_PARENTHESIS + CLOSE_PARENTHESIS;
466 case OPEN_CLOSE_BRACKET_WITH_VALUE:
467 return OPEN_PARENTHESIS + value + CLOSE_PARENTHESIS;
468 case OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE:
469 if (returnType != null) {
470 ret = returnType + SPACE;
471 }
472 return OPEN_PARENTHESIS + ret + value +
473 CLOSE_PARENTHESIS;
474 case OPEN_BRACKET_WITH_VALUE:
475 ret = EMPTY_STRING;
476 if (returnType != null) {
477 ret = returnType + SPACE;
478 }
479 return OPEN_PARENTHESIS + ret + value +
480 COMMA;
481 case CLOSE_BRACKET_WITH_VALUE:
482 ret = EMPTY_STRING;
483 if (returnType != null) {
484 ret = returnType + SPACE;
485 }
486 return SPACE + ret + value +
487 CLOSE_PARENTHESIS;
488 case OPEN_CLOSE_DIAMOND:
489 return DIAMOND_OPEN_BRACKET + DIAMOND_CLOSE_BRACKET;
490 case OPEN_CLOSE_DIAMOND_WITH_VALUE:
491 return DIAMOND_OPEN_BRACKET + value + DIAMOND_CLOSE_BRACKET;
492 default:
493 return null;
494 }
495 }
496
497 /**
498 * Returns method signature for multi attribute methods.
499 *
500 * @param methodName method name
501 * @param prefix prefix for method
502 * @param modifier modifier for method
503 * @param methodReturnType method's return type
504 * @param params parameters
505 * @param type type of method
506 * @return method signature for multi attribute methods
507 */
508 static String multiAttrMethodSignature(String methodName, String
509 prefix, String modifier, String methodReturnType,
510 Map<String, String> params,
511 MethodClassTypes type) {
512 StringBuilder methodBuilder = new StringBuilder(FOUR_SPACE_INDENTATION);
513 String method = EMPTY_STRING;
514 if (modifier != null) {
515 method = modifier + SPACE;
516 }
517 methodBuilder.append(method);
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530518 if (prefix == null) {
519 prefix = EMPTY_STRING;
520 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530521 if (methodReturnType != null) {
522 method = methodReturnType + SPACE + prefix + methodName;
523 } else {
524 method = prefix + methodName;
525 }
526 //Append method beginning.
527 methodBuilder.append(method)
528 .append(OPEN_PARENTHESIS);
529 for (Map.Entry<String, String> param : params.entrySet()) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530530 methodBuilder.append(methodParam(param.getValue(), param.getKey()));
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530531 methodBuilder.append(commaWithSpace());
532 }
533 String para = methodBuilder.toString();
534 String[] array = {SPACE, COMMA};
535 para = trimAtLast(para, array);
536 methodBuilder = new StringBuilder(para)
537 .append(CLOSE_PARENTHESIS)
538 .append(methodSignatureClose(type));
539
540 return methodBuilder.toString();
541 }
542
543 /**
544 * Returns method signature for interface and implementation classes.
545 *
546 * @param methodName name of the method
547 * @param prefix prefix which needs to be added for method
548 * @param modifier modifier which needs to be added for method
549 * @param paraVal value which needs to be added as parameter
550 * @param methodReturnType returns type to be added for method
551 * @param paraReturnType return type to be added for parameter
552 * @param type method class type
553 * @return method signature for interface and implementation classes
554 */
555 static String methodSignature(
556 String methodName, String prefix, String modifier, String paraVal,
557 String methodReturnType, String paraReturnType,
558 MethodClassTypes type) {
559 StringBuilder methodBuilder = new StringBuilder(FOUR_SPACE_INDENTATION);
560 String method = EMPTY_STRING;
561 if (modifier != null) {
562 method = modifier + SPACE;
563 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530564 if (prefix == null) {
565 prefix = EMPTY_STRING;
566 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530567 methodBuilder.append(method);
568 if (methodReturnType != null) {
569 method = methodReturnType + SPACE + prefix + methodName;
570 } else {
571 method = prefix + methodName;
572 }
573 //Append method beginning.
574 methodBuilder.append(method);
575
576 if (paraVal != null) {
577 methodBuilder.append(brackets(
578 OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE,
579 paraVal, paraReturnType));
580 } else {
581 methodBuilder.append(brackets(OPEN_CLOSE_BRACKET, null,
582 null));
583 }
584
585 methodBuilder.append(methodSignatureClose(type));
586
587 return methodBuilder.toString();
588 }
589
590 /**
591 * Returns list attribute.
592 *
593 * @param attrType attribute type
594 * @param compilerAnnotation compiler annotations
595 * @return list attribute
596 */
597
598 static String getListAttribute(String attrType,
599 YangCompilerAnnotation compilerAnnotation) {
600 String listAttr;
601 if (compilerAnnotation != null &&
602 compilerAnnotation.getYangAppDataStructure() != null) {
603 switch (compilerAnnotation.getYangAppDataStructure()
604 .getDataStructure()) {
605 case QUEUE: {
606 listAttr = QUEUE + DIAMOND_OPEN_BRACKET + attrType +
607 DIAMOND_CLOSE_BRACKET;
608 break;
609 }
610 case SET: {
611 listAttr = SET + DIAMOND_OPEN_BRACKET + attrType +
612 DIAMOND_CLOSE_BRACKET;
613 break;
614 }
615 case LIST: {
616 listAttr = getListString() + attrType +
617 DIAMOND_CLOSE_BRACKET;
618 break;
619 }
620 default: {
621 listAttr = getListString() + attrType +
622 DIAMOND_CLOSE_BRACKET;
623 }
624 }
625 } else {
626 listAttr = getListString() + attrType + DIAMOND_CLOSE_BRACKET;
627 }
628 return listAttr;
629 }
630
631 /**
632 * Returns getters for value and select leaf.
633 *
634 * @return getters for value and select leaf
635 */
Bharat saraswal94844d62016-10-13 13:28:03 +0530636 static String getIsValueLeafSet() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530637 return "\n" +
638 " @Override\n" +
639 " public boolean isLeafValueSet(LeafIdentifier leaf) {\n" +
Bharat saraswal94844d62016-10-13 13:28:03 +0530640 " return valueLeafFlags.get(leaf.getLeafIndex());\n" +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530641 " }\n" +
Bharat saraswal94844d62016-10-13 13:28:03 +0530642 "\n";
643 }
644
645 /**
646 * Returns is select leaf set.
647 *
648 * @return is select leaf set
649 */
650 static String getIsSelectLeafSet() {
651 return " @Override\n" +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530652 " public boolean isSelectLeaf(LeafIdentifier leaf) {\n" +
Bharat saraswal94844d62016-10-13 13:28:03 +0530653 " return selectLeafFlags.get(leaf.getLeafIndex());\n" +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530654 " }\n";
655 }
656
657 /**
658 * Returns getter methods for operation attributes.
659 *
660 * @return getter methods for operation attributes
661 */
Bharat saraswal94844d62016-10-13 13:28:03 +0530662 static String getValueLeafGetters() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530663 return "\n" +
664 " /**\n" +
665 " * Returns the valueLeafFlags.\n" +
666 " *\n" +
667 " * @return value of valueLeafFlags\n" +
668 " */\n" +
669 " public BitSet getValueLeafFlags() {\n" +
670 " return valueLeafFlags;\n" +
671 " }\n" +
Bharat saraswal94844d62016-10-13 13:28:03 +0530672 "\n";
673 }
674
675 /**
676 * Returns getter methods for operation attributes.
677 *
678 * @return getter methods for operation attributes
679 */
680 static String getSelectLeafGetters() {
681 return " /**\n" +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530682 " * Returns the selectLeafFlags.\n" +
683 " *\n" +
684 " * @return value of selectLeafFlags\n" +
685 " */\n" +
686 " public BitSet getSelectLeafFlags() {\n" +
687 " return selectLeafFlags;\n" +
688 " }\n" +
689 "\n";
690 }
691
692 /**
693 * Returns interface leaf identifier enum method.
694 *
695 * @return interface leaf identifier enum method
696 */
697 static String getInterfaceLeafIdEnumMethods() {
698 return "\n\n private int leafIndex;\n" +
699 "\n" +
700 " public int getLeafIndex() {\n" +
701 " return leafIndex;\n" +
702 " }\n" +
703 "\n" +
704 " LeafIdentifier(int value) {\n" +
705 " this.leafIndex = value;\n" +
706 " }\n" +
707 " }\n";
708 }
709
710 /**
711 * Returns if condition string for typedef constructor.
712 *
713 * @param type type of conflict
714 * @param addFirst true int/long need to be added first
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530715 * @param val value to set
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530716 * @return if condition string for typedef constructor
717 */
718 static String ifConditionForIntInTypeDefConstructor(ValidatorTypeForUnionTypes type,
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530719 boolean addFirst, String val) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530720 String condition =
721 EIGHT_SPACE_INDENTATION + IF + SPACE + OPEN_PARENTHESIS +
722 VALIDATE_RANGE + OPEN_PARENTHESIS;
723
724 switch (type) {
725 case INT_TYPE_CONFLICT:
726 if (addFirst) {
727 condition = condition + INT_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530728 INT_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530729 } else {
730 condition = condition + UINT_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530731 UINT_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530732 }
733 break;
734 case LONG_TYPE_CONFLICT:
735 if (addFirst) {
736 condition = condition + LONG_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530737 LONG_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530738 } else {
739 condition = condition + ULONG_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530740 ULONG_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530741 }
742 break;
743 case SHORT_TYPE_CONFLICT:
744 if (addFirst) {
745 condition = condition + SHORT_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530746 SHORT_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530747 } else {
748 condition = condition + UINT8_MIN_RANGE + COMMA + SPACE +
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530749 UINT8_MAX_RANGE + COMMA + SPACE + val;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530750 }
751 break;
752 default:
753 return null;
754 }
755
756 return condition + CLOSE_PARENTHESIS + CLOSE_PARENTHESIS + SPACE +
757 OPEN_CURLY_BRACKET + NEW_LINE;
758 }
759
760 /**
761 * Returns from string method parsed string.
762 *
763 * @param targetDataType target data type
764 * @param yangType YANG type
765 * @return parsed string
766 */
767 static String getParseFromStringMethod(String targetDataType,
768 YangType<?> yangType) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530769
janani bc27947a2016-10-19 01:17:40 +0530770 YangDataTypes type = yangType.getDataType();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530771 switch (type) {
772 case INT8:
773 return BYTE_WRAPPER + PERIOD + PARSE_BYTE;
janani bc27947a2016-10-19 01:17:40 +0530774
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530775 case INT16:
776 return SHORT_WRAPPER + PERIOD + PARSE_SHORT;
janani bc27947a2016-10-19 01:17:40 +0530777
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530778 case INT32:
779 return INTEGER_WRAPPER + PERIOD + PARSE_INT;
janani bc27947a2016-10-19 01:17:40 +0530780
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530781 case INT64:
782 return LONG_WRAPPER + PERIOD + PARSE_LONG;
janani bc27947a2016-10-19 01:17:40 +0530783
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530784 case UINT8:
785 return SHORT_WRAPPER + PERIOD + PARSE_SHORT;
janani bc27947a2016-10-19 01:17:40 +0530786
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530787 case UINT16:
788 return INTEGER_WRAPPER + PERIOD + PARSE_INT;
janani bc27947a2016-10-19 01:17:40 +0530789
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530790 case UINT32:
791 return LONG_WRAPPER + PERIOD + PARSE_LONG;
janani bc27947a2016-10-19 01:17:40 +0530792
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530793 case UINT64:
794 return NEW + SPACE + BIG_INTEGER;
janani bc27947a2016-10-19 01:17:40 +0530795
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530796 case DECIMAL64:
797 return NEW + SPACE + BIG_DECIMAL;
janani bc27947a2016-10-19 01:17:40 +0530798
799 case INSTANCE_IDENTIFIER:
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530800 case STRING:
801 case IDENTITYREF:
802 return EMPTY_STRING;
janani bc27947a2016-10-19 01:17:40 +0530803
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530804 case EMPTY:
805 case BOOLEAN:
806 return BOOLEAN_WRAPPER + PERIOD + PARSE_BOOLEAN;
janani bc27947a2016-10-19 01:17:40 +0530807
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530808 case ENUMERATION:
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530809 return targetDataType + PERIOD + OF;
janani bc27947a2016-10-19 01:17:40 +0530810
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530811 case DERIVED:
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530812 case UNION:
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530813 return targetDataType + PERIOD + FROM_STRING_METHOD_NAME;
janani bc27947a2016-10-19 01:17:40 +0530814
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530815 default:
janani bc27947a2016-10-19 01:17:40 +0530816 throw new TranslatorException(
817 "Given data type is not supported. " +
818 yangType.getDataTypeName() + " in " +
819 yangType.getLineNumber() + " at " +
820 yangType.getCharPosition() + " in " +
821 yangType.getFileName());
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530822 }
823 }
824
825 /**
826 * Returns sub string with catch statement for union's from string method.
827 *
828 * @return sub string with catch statement for union's from string method
829 */
830 static String getCatchSubString() {
831 return CLOSE_CURLY_BRACKET + SPACE + CATCH + SPACE +
832 brackets(OPEN_CLOSE_BRACKET_WITH_VALUE_AND_RETURN_TYPE, EXCEPTION_VAR,
833 EXCEPTION) + SPACE + OPEN_CURLY_BRACKET;
834 }
835
836 /**
837 * Returns sub string with return statement for union's from string method.
838 *
839 * @return sub string with return statement for union's from string method
840 */
841 static String getReturnOfSubString() {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530842 return getReturnString(OF, TWELVE_SPACE_INDENTATION) +
Bharat saraswal0663aff2016-10-18 23:16:14 +0530843 getOpenCloseParaWithValue(TMP_VAL) + signatureClose();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530844 }
845
846 /**
847 * Returns sub string with try statement for union's from string method.
848 *
849 * @return sub string with try statement for union's from string method
850 */
851 static String getTrySubString() {
852 return TRY + SPACE + OPEN_CURLY_BRACKET;
853 }
854
855 /*
856 * Returns omit null value string.
857 *
858 * @return omit null value string
859 */
860 static String getOmitNullValueString() {
861 return TWELVE_SPACE_INDENTATION + PERIOD + OMIT_NULL_VALUE_STRING +
862 NEW_LINE;
863 }
864
865 /**
866 * Returns collection's iterator method.
867 *
868 * @param indentation indentation
869 * @param loopVar loop variable
870 * @param collection collection
871 * @return collection's iterator method
872 */
873 static String getCollectionIteratorForLoopBegin(String indentation,
874 String loopVar,
875 String collection) {
876 return indentation + FOR + SPACE + OPEN_PARENTHESIS + loopVar + SPACE +
877 COLON + SPACE + collection + CLOSE_PARENTHESIS + SPACE +
878 OPEN_CURLY_BRACKET + NEW_LINE;
879 }
880
881 /**
882 * Returns if else condition's signature.
883 *
884 * @param indentation indentation
885 * @param condition conditions
886 * @return if else condition's signature
887 */
888 static String getElseIfConditionBegin(String indentation,
889 String condition) {
890 return indentation + CLOSE_CURLY_BRACKET + SPACE + ELSE + SPACE +
891 getIfConditionBegin(EMPTY_STRING, condition);
892 }
893
894 /**
895 * Returns if condition's signature.
896 *
897 * @param indentation indentation
898 * @param condition conditions
899 * @return if condition's signature
900 */
901 static String getIfConditionBegin(String indentation, String condition) {
Bharat saraswal0663aff2016-10-18 23:16:14 +0530902 return indentation + IF + SPACE + getOpenCloseParaWithValue(condition) +
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530903 methodSignatureClose(CLASS_TYPE);
904 }
905
906 /**
janani bc27947a2016-10-19 01:17:40 +0530907 * Returns true, if the data type is primitive; false otherwise.
908 *
909 * @param dataType data type
910 * @return true if the data type is primitive; false otherwise
911 */
912 public static boolean isPrimitiveDataType(YangDataTypes dataType) {
913 return PRIMITIVE_TYPES.contains(dataType);
914 }
915
916 /**
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530917 * Returns list string.
918 *
919 * @return list string
920 */
921 private static String getListString() {
922 return LIST + DIAMOND_OPEN_BRACKET;
923 }
924
925 /**
926 * Returns override string.
927 *
928 * @return override string
929 */
930 public static String getOverRideString() {
931 return NEW_LINE + FOUR_SPACE_INDENTATION + OVERRIDE + NEW_LINE;
932 }
933
934 /**
935 * Returns value leaf flag setter.
936 *
Bharat saraswal0663aff2016-10-18 23:16:14 +0530937 * @param name name of leaf
938 * @param flag flag to set values
939 * @param indentation indentation
940 * @param prefix prefix of method
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530941 * @return value leaf flag setter
942 */
Bharat saraswal0663aff2016-10-18 23:16:14 +0530943 static String getLeafFlagSetString(String name, String flag, String indentation, String prefix) {
944 return indentation + flag + PERIOD + prefix +
945 getOpenCloseParaWithValue(LEAF_IDENTIFIER + PERIOD + name
946 .toUpperCase() + ".getLeafIndex()");
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530947 }
948
949 /*Provides string to return for type.*/
950 private static String parseTypeForReturnValue(String type) {
951 switch (type) {
952 case BYTE:
953 case INT:
954 case SHORT:
955 case LONG:
956 case DOUBLE:
957 return ZERO;
958 case BOOLEAN_DATA_TYPE:
959 return FALSE;
960 default:
961 return null;
962 }
963 }
964
965 /**
966 * Returns check not null string.
967 *
968 * @param name attribute name
969 * @return check not null string
970 */
971 static String getCheckNotNull(String name) {
972 return EIGHT_SPACE_INDENTATION + CHECK_NOT_NULL_STRING +
973 OPEN_PARENTHESIS + name + COMMA + SPACE + name +
974 CLOSE_PARENTHESIS + SEMI_COLON + NEW_LINE;
975 }
976
977 /**
978 * Returns definition close string.
979 *
980 * @return definition close string
981 */
982 private static String defCloseString() {
Bharat saraswal94844d62016-10-13 13:28:03 +0530983 return SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530984 }
985
986 /**
987 * Returns default class definition for java file when extends a
988 * interface.
989 *
990 * @param classType class type
991 * @param name name of class
992 * @param modifier modifier for class
993 * @param extend extends class name
994 * @return class definition
995 */
996 static String getDefaultDefinitionWithExtends(String classType,
997 String name, String
998 modifier,
999 String extend) {
1000 String mod = EMPTY_STRING;
1001 if (modifier != null) {
1002 mod = modifier + SPACE;
1003 }
1004 return mod + classType + SPACE + name + SPACE + EXTEND + SPACE
1005 + extend + defCloseString();
1006 }
1007
1008 /**
1009 * Returns default class definition for java file when implements a
1010 * interface.
1011 *
1012 * @param classType class type
1013 * @param name name of class
1014 * @param modifier modifier for class
1015 * @param impl implements class name
1016 * @return class definition
1017 */
1018 static String getDefaultDefinitionWithImpl(String classType,
1019 String name, String
1020 modifier,
1021 String impl) {
1022 String mod = EMPTY_STRING;
1023 if (modifier != null) {
1024 mod = modifier + SPACE;
1025 }
1026 return mod + classType + SPACE + name + SPACE + IMPLEMENTS +
1027 SPACE + impl + defCloseString();
1028 }
1029
1030 /**
1031 * Returns default class definition for java file.
1032 *
1033 * @param classType class type
1034 * @param name name of class
1035 * @param modifier modifier for class
1036 * @return class definition
1037 */
1038 static String getDefaultDefinition(String classType,
1039 String name, String modifier) {
1040 String mod = EMPTY_STRING;
1041 if (modifier != null) {
1042 mod = modifier + SPACE;
1043 }
1044 return mod + classType + SPACE + name + defCloseString();
1045 }
1046
1047 /**
1048 * Returns string for service class.
1049 *
1050 * @param name1 name of even listener class
1051 * @param name2 name of even class
1052 * @return listener service string for service class
1053 */
1054 static String getEventExtendsString(String name1, String type,
1055 String name2) {
1056 StringBuilder builder = new StringBuilder();
1057 builder.append(type).append(DIAMOND_OPEN_BRACKET).append(name1)
1058 .append(COMMA).append(SPACE).append(name2).append
1059 (DIAMOND_CLOSE_BRACKET);
1060 return builder.toString();
1061 }
1062
1063 /**
1064 * Returns import string.
1065 *
1066 * @param pkg package
1067 * @param cls class
1068 * @return import string
1069 */
1070 public static String getImportString(String pkg, String cls) {
1071 StringBuilder builder = new StringBuilder()
1072 .append(IMPORT).append(pkg).append(PERIOD).append(cls)
1073 .append(signatureClose());
1074 return builder.toString();
1075 }
1076
1077 /**
1078 * Returns static modifier string.
1079 *
1080 * @param modifier modifier
1081 * @return static modifier string
1082 */
1083 static String getSpecificModifier(String modifier, String keyWord) {
1084 return new StringBuilder(modifier).append(SPACE).append(keyWord)
1085 .toString();
1086 }
1087
1088 /**
1089 * Returns builder string for class definition.
1090 *
1091 * @param yangName class name
1092 * @return builder string for class definition
1093 */
1094 static String getBuilderImplStringClassDef(String yangName) {
1095 return new StringBuilder(yangName).append(PERIOD).append(yangName)
1096 .append(BUILDER).toString();
1097 }
1098
1099 /**
1100 * Returns default name string.
1101 *
1102 * @param yangName class name
1103 * @return default name string
1104 */
1105 static String getDefaultName(String yangName) {
1106 return new StringBuilder(DEFAULT_CAPS).append(yangName).toString();
1107 }
1108
1109 /**
1110 * Returns suffixed name string.
1111 *
1112 * @param yangName class name
1113 * @param suffix suffix to append to name
1114 * @return suffixed name string
1115 */
1116 static String getSuffixedName(String yangName, String suffix) {
1117 return new StringBuilder(yangName).append(suffix).toString();
1118 }
1119
1120 /**
1121 * Returns error msg string.
1122 *
1123 * @param msg message
1124 * @param name name of node
1125 * @param line line number
1126 * @param position char position
1127 * @param fileName file name
1128 * @return error message string
1129 */
1130 static String getErrorMsg(String msg, String name, int line, int position,
1131 String fileName) {
1132 return new StringBuilder().append(msg).append(name).append(IN)
1133 .append(line).append(AT).append(position).append(IN)
1134 .append(fileName).toString();
1135 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301136
1137 /**
1138 * Returns string builder attribute string;
1139 *
1140 * @param init first param to be appended to string builder
1141 * @param space indentation space
1142 * @return string builder attribute
1143 */
1144 static String getStringBuilderAttr(String init, String space) {
1145 StringBuilder builder = new StringBuilder(space);
1146 builder.append(STRING_BUILDER).append(SPACE).append(STRING_BUILDER_VAR)
1147 .append(SPACE).append(EQUAL).append(SPACE).append(NEW)
1148 .append(SPACE).append(STRING_BUILDER).append(
Bharat saraswal0663aff2016-10-18 23:16:14 +05301149 getOpenCloseParaWithValue(getQuotedString(init)))
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301150 .append(signatureClose());
1151 return builder.toString();
1152 }
1153
1154 /**
1155 * Returns quoted string.
1156 *
1157 * @param name name to be quoted
1158 * @return quoted string
1159 */
1160 static String getQuotedString(String name) {
1161 return QUOTES + name + QUOTES;
1162 }
1163
1164 /**
1165 * Returns string builder's append string.
1166 *
1167 * @param append data to be append
1168 * @param space indentation
1169 * @return string builder's append string
1170 */
1171 static String getStringBuilderAppendString(String append, String space) {
1172 return space + STRING_BUILDER_VAR + PERIOD + APPEND + OPEN_PARENTHESIS +
1173 append + CLOSE_PARENTHESIS + signatureClose();
1174 }
1175
1176 /**
1177 * Returns pattern quote string.
1178 *
1179 * @param type type for pattern is needed
1180 * @return pattern quote string
1181 */
1182 static String getPatternQuoteString(String type) {
Bharat saraswal0663aff2016-10-18 23:16:14 +05301183 return PATTERN + PERIOD + QUOTE_STRING + getOpenCloseParaWithValue(
1184 getQuotedString(type));
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301185 }
1186
1187 /**
1188 * Returns bitset attribute.
1189 *
1190 * @param indentation indentation
1191 * @return bitset attribute
1192 */
1193 static String getBitSetAttr(String indentation) {
1194 StringBuilder builder = new StringBuilder(indentation);
1195 return builder.append(BITSET).append(SPACE).append(TMP_VAL)
1196 .append(SPACE).append(EQUAL).append(SPACE).append(NEW)
1197 .append(SPACE).append(BITSET).append(OPEN_CLOSE_BRACKET_STRING)
1198 .append(signatureClose()).toString();
1199 }
1200
1201 /**
1202 * Returns for loop string
1203 *
1204 * @param space indentation
1205 * @param type data type
1206 * @param var variable
1207 * @param data data variable/collection
1208 * @return for loop string
1209 */
1210 static String getForLoopString(String space, String type, String var,
1211 String data) {
1212 return space + FOR + SPACE + OPEN_PARENTHESIS + type + SPACE + var +
1213 SPACE + COLON + SPACE + data + CLOSE_PARENTHESIS +
1214 methodSignatureClose(CLASS_TYPE);
1215 }
1216
1217 /**
1218 * Returns set value parameter's get string for union to string method.
1219 *
1220 * @param count count of type
1221 * @return get string
1222 */
1223 static String getSetValueParaCondition(int count) {
Bharat saraswal0663aff2016-10-18 23:16:14 +05301224 return SET_VALUE_PARA + PERIOD + GET + getOpenCloseParaWithValue(
1225 count + EMPTY_STRING);
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301226 }
1227
1228 /**
1229 * Returns more object attr for union to string method.
1230 *
Bharat saraswal94844d62016-10-13 13:28:03 +05301231 * @param name name of generate class
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301232 * @return more object attr for union to string method
1233 */
Bharat saraswal94844d62016-10-13 13:28:03 +05301234 static String getMoreObjectAttr(String name) {
1235 String cls = name + PERIOD + CLASS;
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301236 StringBuilder attr = new StringBuilder(EIGHT_SPACE_INDENTATION);
1237 String[] array = {NEW_LINE};
Bharat saraswal94844d62016-10-13 13:28:03 +05301238 attr.append(MORE_OBJ_ATTR).append(GOOGLE_MORE_OBJECT_METHOD_STATIC_STRING)
Bharat saraswal0663aff2016-10-18 23:16:14 +05301239 .append(getOpenCloseParaWithValue(cls)).append(NEW_LINE)
1240 .append(FOUR_SPACE_INDENTATION).append(trimAtLast(
Bharat saraswal54e4bab2016-10-05 23:32:14 +05301241 getOmitNullValueString(), array)).append(signatureClose());
1242 return attr.toString();
1243 }
Bharat saraswal94844d62016-10-13 13:28:03 +05301244
1245 /**
1246 * Returns to string call.
1247 *
1248 * @param name name of attribute
1249 * @return to string call for attribute
1250 */
1251 static String getToStringCall(String name) {
1252 return name + PERIOD +
1253 TO_STRING_METHOD + OPEN_CLOSE_BRACKET_STRING;
1254 }
Bharat saraswal0663aff2016-10-18 23:16:14 +05301255
1256 /**
1257 * Returns value in brackets.
1258 *
1259 * @param name value
1260 * @return value in brackets
1261 */
1262 static String getOpenCloseParaWithValue(String name) {
1263 return brackets(OPEN_CLOSE_BRACKET_WITH_VALUE, name, null);
1264 }
1265
1266 /**
1267 * Returns equals comparision.
1268 *
1269 * @param para1 param
1270 * @param para2 param
1271 * @return equals comparision
1272 */
1273 static String getTwoParaEqualsString(String para1, String para2) {
1274 return para1 + PERIOD + EQUALS_STRING + getOpenCloseParaWithValue(para2);
1275 }
1276
1277 /**
1278 * Returns equal equal condition.
1279 *
1280 * @param para param
1281 * @param val value
1282 * @return equal equal condition
1283 */
1284 static String getEqualEqualString(String para, String val) {
1285 return para + SPACE + EQUAL + EQUAL + SPACE + val;
1286 }
1287
1288 /**
1289 * Returns app instance method call.
1290 *
1291 * @param name attr name
1292 * @return app instance method call
1293 */
1294 static String getAppInstanceAttrString(String name) {
1295 return APP_INSTANCE + PERIOD + name + OPEN_CLOSE_BRACKET_STRING;
1296 }
1297
1298 /**
1299 * Returns qualified name.
1300 *
1301 * @param pkg package
1302 * @param cls class info
1303 * @return qualified name
1304 */
1305 static String getQualifiedString(String pkg, String cls) {
1306 return pkg + PERIOD + cls;
1307 }
1308
1309 /**
1310 * Returns import list for node list.
1311 *
1312 * @param nodes node list
1313 * @param config plugin config
1314 * @return import list
1315 */
1316 public static List<JavaQualifiedTypeInfoTranslator> getNodesImports(List<YangNode> nodes,
1317 YangPluginConfig config) {
1318 List<JavaQualifiedTypeInfoTranslator> imports = new ArrayList<>();
1319 for (YangNode node : nodes) {
1320 JavaQualifiedTypeInfoTranslator qInfo = getQualifiedInfo(node,
1321 config);
1322 imports.add(qInfo);
1323 }
1324 return imports;
1325 }
1326
1327 /**
1328 * Returns list of child node for choice.
1329 *
1330 * @param choice choice node
1331 * @return list of child nodes
1332 */
1333 public static List<YangNode> getChoiceChildNodes(YangChoice choice) {
1334 List<YangNode> childs = new ArrayList<>();
1335 YangNode child = choice.getChild();
1336 while (child != null) {
1337 childs.add(child);
1338 child = child.getNextSibling();
1339 }
1340
1341 List<YangAugment> augments = choice.getAugmentedInfoList();
1342 YangNode augmentCase;
1343 for (YangAugment augment : augments) {
1344 augmentCase = augment.getChild();
1345 while (augmentCase != null) {
1346 childs.add(augmentCase);
1347 augmentCase = augmentCase.getNextSibling();
1348 }
1349 }
1350 return childs;
1351 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +05301352}