blob: 9fa13d49835d5742adbe5a9b2b172305a97ed33e [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 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;
Bharat saraswald72411a2016-04-19 01:00:16 +053021import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
Gaurav Agrawal56527662016-04-20 15:49:17 +053022import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_RPC_INTERFACE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053023import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053024import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053025import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
26import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053027import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList;
28import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.isExtendsList;
Bharat saraswale2d51d62016-03-23 19:40:35 +053029import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
30import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053031import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
Bharat saraswald72411a2016-04-19 01:00:16 +053032import static org.onosproject.yangutils.utils.UtilConstants.ENUM;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053033import static org.onosproject.yangutils.utils.UtilConstants.EXTEND;
Bharat saraswale2d51d62016-03-23 19:40:35 +053034import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
35import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
36import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
37import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
38import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
39import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
40import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
41import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
42import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053043import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Bharat saraswal870c56f2016-02-20 21:57:16 +053044
45/**
Bharat saraswald9822e92016-04-05 15:13:44 +053046 * Represents generator for class definition of generated files.
Bharat saraswal870c56f2016-02-20 21:57:16 +053047 */
48public final class ClassDefinitionGenerator {
49
50 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Creates an instance of class definition generator.
Bharat saraswal870c56f2016-02-20 21:57:16 +053052 */
53 private ClassDefinitionGenerator() {
54 }
55
56 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053057 * Based on the file type and the YANG name of the file, generate the class
58 * / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053059 *
60 * @param genFileTypes generated file type
Gaurav Agrawal338735b2016-04-18 18:53:11 +053061 * @param yangName class name
Bharat saraswal870c56f2016-02-20 21:57:16 +053062 * @return class definition
63 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053064 public static String generateClassDefinition(int genFileTypes, String yangName) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053065
66 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053067 * Based on the file type and the YANG name of the file, generate the
Vinod Kumar Sc4216002016-03-03 19:55:30 +053068 * class / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053069 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053070 if ((genFileTypes & INTERFACE_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053071 return getInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053072 } else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053073 return getBuilderClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053074 } else if ((genFileTypes & IMPL_CLASS_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053075 return getImplClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053076 } else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053077 return getBuilderInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053078 } else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +053079 return getTypeClassDefinition(yangName);
80 } else if ((genFileTypes & GENERATE_UNION_CLASS) != 0) {
81 return getTypeClassDefinition(yangName);
Bharat saraswald72411a2016-04-19 01:00:16 +053082 } else if ((genFileTypes & GENERATE_ENUM_CLASS) != 0) {
83 return getEnumClassDefinition(yangName);
Gaurav Agrawal56527662016-04-20 15:49:17 +053084 } else if ((genFileTypes & GENERATE_RPC_INTERFACE) != 0) {
85 return getRpcInterfaceDefinition(yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +053086 }
87 return null;
88 }
89
90 /**
Bharat saraswald72411a2016-04-19 01:00:16 +053091 * Returns enum file class definition.
92 *
93 * @param yangName class name
Gaurav Agrawal56527662016-04-20 15:49:17 +053094 * @return enum file class definition
Bharat saraswald72411a2016-04-19 01:00:16 +053095 */
96 private static String getEnumClassDefinition(String yangName) {
97 return PUBLIC + SPACE + ENUM + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
98 }
99
100 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530101 * Returns interface file class definition.
102 *
103 * @param yangName file name
104 * @return definition
105 */
106 private static String getInterfaceDefinition(String yangName) {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530107 if (!isExtendsList()) {
108 return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
109 }
110 String def = PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE;
111 for (String extend : getExtendsList()) {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530112 def = def + extend + COMMA + SPACE;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530113 }
114 def = trimAtLast(def, COMMA);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530115
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530116 return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530117 }
118
119 /**
120 * Returns builder interface file class definition.
121 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530122 * @param yangName java class name, corresponding to which the builder class
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530123 * is being generated
Bharat saraswal870c56f2016-02-20 21:57:16 +0530124 * @return definition
125 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530126 private static String getBuilderInterfaceDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530127 return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530128 }
129
130 /**
131 * Returns builder file class definition.
132 *
133 * @param yangName file name
134 * @return definition
135 */
136 private static String getBuilderClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530137 return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
138 + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530139 }
140
141 /**
142 * Returns impl file class definition.
143 *
144 * @param yangName file name
145 * @return definition
146 */
147 private static String getImplClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530148 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName
149 + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530150 }
151
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530152 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530153 * Returns type file class definition.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530154 *
155 * @param yangName file name
156 * @return definition
157 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530158 private static String getTypeClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530159 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530160 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530161
162 /**
163 * Returns rpc file interface definition.
164 *
165 * @param yangName file name
166 * @return definition
167 */
168 private static String getRpcInterfaceDefinition(String yangName) {
169 return INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
170 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530171}