blob: dd08265bb0e96c0cf79725349a311a6bd54b0bbd [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +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 */
16package org.onosproject.yangutils.translator.tojava.javamodel;
17
18import java.io.IOException;
Bharat saraswal33dfa012016-05-17 19:59:16 +053019import java.util.ArrayList;
20import java.util.List;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053021
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.YangModule;
Bharat saraswal33dfa012016-05-17 19:59:16 +053023import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.datamodel.YangNotification;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053025import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053026import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053027import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053028import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053029import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053030import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vinod Kumar S38046502016-03-23 15:30:27 +053031
Bharat saraswal33dfa012016-05-17 19:59:16 +053032import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
33import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
34import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
Bharat saraswalb551aae2016-07-14 15:18:20 +053035import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053036import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Bharat saraswalb551aae2016-07-14 15:18:20 +053037import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeOfRootNode;
38import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isManagerCodeGenRequired;
39import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isGenerationOfCodeReq;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053040import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswalc0e04842016-05-12 13:16:57 +053041import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053042import static org.onosproject.yangutils.utils.UtilConstants.SBI;
Vinod Kumar S38046502016-03-23 15:30:27 +053043
44/**
Bharat saraswald9822e92016-04-05 15:13:44 +053045 * Represents module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053046 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047public class YangJavaModule
48 extends YangModule
49 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053050
Bharat saraswal96dfef02016-06-16 00:29:12 +053051 private static final long serialVersionUID = 806201625L;
52
Vinod Kumar S38046502016-03-23 15:30:27 +053053 /**
54 * Contains the information of the java file being generated.
55 */
56 private JavaFileInfo javaFileInfo;
57
58 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053059 * File handle to maintain temporary java code fragments as per the code
60 * snippet types.
61 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053062 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053063
64 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053065 * List of notifications nodes.
66 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +053067 private transient List<YangNode> notificationNodes;
Bharat saraswal33dfa012016-05-17 19:59:16 +053068
69 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053070 * Creates a YANG node of module type.
Vinod Kumar S38046502016-03-23 15:30:27 +053071 */
72 public YangJavaModule() {
73 super();
74 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053075 setNotificationNodes(new ArrayList<>());
Bharat saraswalb551aae2016-07-14 15:18:20 +053076 int gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswal33dfa012016-05-17 19:59:16 +053077 if (isNotificationChildNodePresent(this)) {
78 gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_EVENT_SUBJECT_CLASS | GENERATE_EVENT_CLASS
79 | GENERATE_EVENT_LISTENER_INTERFACE;
80 }
81 getJavaFileInfo().setGeneratedFileTypes(gentype);
82
Vinod Kumar S38046502016-03-23 15:30:27 +053083 }
84
85 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053086 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053087 *
88 * @return generated java file information
89 */
90 @Override
91 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053092 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053093 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053094 }
95 return javaFileInfo;
96 }
97
98 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053099 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 *
101 * @param javaInfo java file info object
102 */
103 @Override
104 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 javaFileInfo = javaInfo;
106 }
107
108 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 *
111 * @return temporary file handle
112 */
113 @Override
114 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530115 return tempFileHandle;
116 }
117
118 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530119 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 *
121 * @param fileHandle temporary file handle
122 */
123 @Override
124 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530125 tempFileHandle = fileHandle;
126 }
127
128 /**
129 * Generates java code for module.
130 *
janani bde4ffab2016-04-15 16:18:30 +0530131 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530132 * @throws TranslatorException when fails to generate the source files
Vinod Kumar S38046502016-03-23 15:30:27 +0530133 */
134 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530135 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
janani bdd1314f2016-05-19 17:39:50 +0530136 String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate(),
137 yangPlugin.getConflictResolver());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530138 try {
139 generateCodeOfRootNode(this, yangPlugin, modulePkg);
140 } catch (IOException e) {
141 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530142 "Failed to prepare generate code entry for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530143 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530144 }
145
Vidyashree Rama74453712016-04-18 12:29:39 +0530146 /**
147 * Creates a java file using the YANG module info.
148 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530149 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530150 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530151 /**
152 * As part of the notification support the following files needs to be generated.
153 * 1) Subject of the notification(event), this is simple interface with builder class.
154 * 2) Event class extending "AbstractEvent" and defining event type enum.
155 * 3) Event listener interface extending "EventListener".
156 * 4) Event subject class.
157 *
158 * The manager class needs to extend the "ListenerRegistry".
159 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530160
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530161 try {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530162 if (isManagerCodeGenRequired(this) && isGenerationOfCodeReq(getJavaFileInfo())) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530163 if ((getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi() == null) ||
164 (!getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi().equals(SBI))) {
165 getTempJavaCodeFragmentFiles()
166 .generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
167 getTempJavaCodeFragmentFiles()
168 .generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
169 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530170 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530171 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
172 getJavaFileInfo().getPackageFilePath());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530173 searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() +
174 getJavaFileInfo().getPackageFilePath());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530175 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530176 throw new TranslatorException("Failed to generate code for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530177 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530178 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530179
180 /**
181 * Returns notifications node list.
182 *
183 * @return notification nodes
184 */
185 public List<YangNode> getNotificationNodes() {
186 return notificationNodes;
187 }
188
189 /**
190 * Sets notifications list.
191 *
192 * @param notificationNodes notification list
193 */
194 private void setNotificationNodes(List<YangNode> notificationNodes) {
195 this.notificationNodes = notificationNodes;
196 }
197
198 /**
199 * Adds to notification node list.
200 *
201 * @param curNode notification node
202 */
203 private void addToNotificaitonList(YangNode curNode) {
204 getNotificationNodes().add(curNode);
205 }
206
207 /**
208 * Checks if there is any rpc defined in the module or sub-module.
209 *
210 * @param rootNode root node of the data model
211 * @return status of rpc's existence
212 */
213 public boolean isNotificationChildNodePresent(YangNode rootNode) {
214 YangNode childNode = rootNode.getChild();
215
216 while (childNode != null) {
217 if (childNode instanceof YangNotification) {
218 addToNotificaitonList(childNode);
219 }
220 childNode = childNode.getNextSibling();
221 }
222
223 if (!getNotificationNodes().isEmpty()) {
224 return true;
225 }
226 return false;
227 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530228}