blob: 0db41c9b6642a812bc40e7a2e72e4ce51920d7b7 [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;
Bharat saraswale2d51d62016-03-23 19:40:35 +053022import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053023import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053024import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
25import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053026import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList;
27import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.isExtendsList;
Bharat saraswale2d51d62016-03-23 19:40:35 +053028import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
29import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053030import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
Bharat saraswald72411a2016-04-19 01:00:16 +053031import static org.onosproject.yangutils.utils.UtilConstants.ENUM;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053032import static org.onosproject.yangutils.utils.UtilConstants.EXTEND;
Bharat saraswale2d51d62016-03-23 19:40:35 +053033import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
34import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
35import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
36import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
37import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
38import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
39import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
40import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
41import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053042import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Bharat saraswal870c56f2016-02-20 21:57:16 +053043
44/**
Bharat saraswald9822e92016-04-05 15:13:44 +053045 * Represents generator for class definition of generated files.
Bharat saraswal870c56f2016-02-20 21:57:16 +053046 */
47public final class ClassDefinitionGenerator {
48
49 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053050 * Creates an instance of class definition generator.
Bharat saraswal870c56f2016-02-20 21:57:16 +053051 */
52 private ClassDefinitionGenerator() {
53 }
54
55 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053056 * Based on the file type and the YANG name of the file, generate the class
57 * / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053058 *
59 * @param genFileTypes generated file type
Gaurav Agrawal338735b2016-04-18 18:53:11 +053060 * @param yangName class name
Bharat saraswal870c56f2016-02-20 21:57:16 +053061 * @return class definition
62 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053063 public static String generateClassDefinition(int genFileTypes, String yangName) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053064
65 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053066 * based on the file type and the YANG name of the file, generate the
67 * class / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053068 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053069 if ((genFileTypes & INTERFACE_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053070 return getInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053071 } else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053072 return getBuilderClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053073 } else if ((genFileTypes & IMPL_CLASS_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053074 return getImplClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053075 } else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053076 return getBuilderInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053077 } else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +053078 return getTypeClassDefinition(yangName);
79 } else if ((genFileTypes & GENERATE_UNION_CLASS) != 0) {
80 return getTypeClassDefinition(yangName);
Bharat saraswald72411a2016-04-19 01:00:16 +053081 } else if ((genFileTypes & GENERATE_ENUM_CLASS) != 0) {
82 return getEnumClassDefinition(yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +053083 }
84 return null;
85 }
86
87 /**
Bharat saraswald72411a2016-04-19 01:00:16 +053088 * Returns enum file class definition.
89 *
90 * @param yangName class name
91 * @return enum file class definiton
92 */
93 private static String getEnumClassDefinition(String yangName) {
94 return PUBLIC + SPACE + ENUM + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
95 }
96
97 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053098 * Returns interface file class definition.
99 *
100 * @param yangName file name
101 * @return definition
102 */
103 private static String getInterfaceDefinition(String yangName) {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530104 if (!isExtendsList()) {
105 return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
106 }
107 String def = PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE;
108 for (String extend : getExtendsList()) {
109 def = def + extend + COMMA;
110 }
111 def = trimAtLast(def, COMMA);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530112
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530113 return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530114 }
115
116 /**
117 * Returns builder interface file class definition.
118 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530119 * @param yangName java class name, corresponding to which the builder class
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530120 * is being generated
Bharat saraswal870c56f2016-02-20 21:57:16 +0530121 * @return definition
122 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530123 private static String getBuilderInterfaceDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530124 return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 }
126
127 /**
128 * Returns builder file class definition.
129 *
130 * @param yangName file name
131 * @return definition
132 */
133 private static String getBuilderClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530134 return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
135 + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530136 }
137
138 /**
139 * Returns impl file class definition.
140 *
141 * @param yangName file name
142 * @return definition
143 */
144 private static String getImplClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530145 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName
146 + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530147 }
148
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530149 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530150 * Returns type file class definition.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530151 *
152 * @param yangName file name
153 * @return definition
154 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530155 private static String getTypeClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530156 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530157 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530158}