blob: 70c4cb8efe2e8288c8df35c032a5183d4dc8bb70 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
2 * Copyright 2016 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 saraswale2d51d62016-03-23 19:40:35 +053019import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
20import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
21import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
22import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
23import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
24import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
25import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
26import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
27import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
28import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
29import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
30import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
31import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
32import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
33import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
34import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswal870c56f2016-02-20 21:57:16 +053035
36/**
37 * Generates class definition for generated files.
38 */
39public final class ClassDefinitionGenerator {
40
41 /**
42 * Default constructor.
43 */
44 private ClassDefinitionGenerator() {
45 }
46
47 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053048 * Based on the file type and the YANG name of the file, generate the class
49 * / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053050 *
51 * @param genFileTypes generated file type
52 * @param yangName class name
53 * @return class definition
54 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053055 public static String generateClassDefinition(int genFileTypes, String yangName) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053056
57 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053058 * based on the file type and the YANG name of the file, generate the
59 * class / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053060 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053061 if ((genFileTypes & INTERFACE_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053062
63 return getInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053064 } else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053065
66 return getBuilderClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053067 } else if ((genFileTypes & IMPL_CLASS_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053068
69 return getImplClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053070 } else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053071
Vinod Kumar Sc4216002016-03-03 19:55:30 +053072 return getBuilderInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053073 } else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053074
75 return getTypeDefClassDefinition(yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +053076 }
77 return null;
78 }
79
80 /**
81 * Returns interface file class definition.
82 *
83 * @param yangName file name
84 * @return definition
85 */
86 private static String getInterfaceDefinition(String yangName) {
87
Bharat saraswale2d51d62016-03-23 19:40:35 +053088 return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +053089 }
90
91 /**
92 * Returns builder interface file class definition.
93 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053094 * @param yangName java class name, corresponding to which the builder class
95 * is being generated
Bharat saraswal870c56f2016-02-20 21:57:16 +053096 * @return definition
97 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053098 private static String getBuilderInterfaceDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +053099
100 return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530101 }
102
103 /**
104 * Returns builder file class definition.
105 *
106 * @param yangName file name
107 * @return definition
108 */
109 private static String getBuilderClassDefinition(String yangName) {
110
Bharat saraswale2d51d62016-03-23 19:40:35 +0530111 return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
112 + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530113 }
114
115 /**
116 * Returns impl file class definition.
117 *
118 * @param yangName file name
119 * @return definition
120 */
121 private static String getImplClassDefinition(String yangName) {
122
Bharat saraswale2d51d62016-03-23 19:40:35 +0530123 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName
124 + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 }
126
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530127 /**
128 * Returns typeDef file class definition.
129 *
130 * @param yangName file name
131 * @return definition
132 */
133 private static String getTypeDefClassDefinition(String yangName) {
134
Bharat saraswale2d51d62016-03-23 19:40:35 +0530135 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530136 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530137}