blob: 06715b1dd893fa88c66bba2d405093a7d68e6721 [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
Bharat saraswal33dfa012016-05-17 19:59:16 +053022import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.YangNotification;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053024import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
25import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaModule;
26import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053027import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053028import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053029import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053030import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
31
Bharat saraswald50c6382016-07-14 21:57:13 +053032import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ALL_EVENT_CLASS_MASK;
Bharat saraswalb551aae2016-07-14 15:18:20 +053033import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Bharat saraswalb551aae2016-07-14 15:18:20 +053035import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeOfRootNode;
Bharat saraswalb551aae2016-07-14 15:18:20 +053036import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isGenerationOfCodeReq;
Bharat saraswald50c6382016-07-14 21:57:13 +053037import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isManagerCodeGenRequired;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053038import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053039import static org.onosproject.yangutils.utils.UtilConstants.SBI;
Bharat saraswale707f032016-07-14 23:33:55 +053040import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053041
42/**
Bharat saraswald9822e92016-04-05 15:13:44 +053043 * Represents module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053044 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053045public class YangJavaModuleTranslator
46 extends YangJavaModule
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053048
Bharat saraswal96dfef02016-06-16 00:29:12 +053049 private static final long serialVersionUID = 806201625L;
50
Vinod Kumar S38046502016-03-23 15:30:27 +053051 /**
Bharat saraswald50c6382016-07-14 21:57:13 +053052 * File handle to maintain temporary java code fragments as per the code snippet types.
Vinod Kumar S38046502016-03-23 15:30:27 +053053 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053054 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053055
56 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053057 * List of notifications nodes.
58 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +053059 private transient List<YangNode> notificationNodes;
Bharat saraswal33dfa012016-05-17 19:59:16 +053060
61 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053062 * Creates a YANG node of module type.
Vinod Kumar S38046502016-03-23 15:30:27 +053063 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053064 public YangJavaModuleTranslator() {
Vinod Kumar S38046502016-03-23 15:30:27 +053065 super();
66 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053067 setNotificationNodes(new ArrayList<>());
Bharat saraswald50c6382016-07-14 21:57:13 +053068 getJavaFileInfo().setGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER | GENERATE_INTERFACE_WITH_BUILDER);
Bharat saraswal33dfa012016-05-17 19:59:16 +053069
Vinod Kumar S38046502016-03-23 15:30:27 +053070 }
71
72 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053073 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053074 *
75 * @return generated java file information
76 */
77 @Override
78 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053079 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053080 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053081 }
82 return javaFileInfo;
83 }
84
85 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053086 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053087 *
88 * @param javaInfo java file info object
89 */
90 @Override
91 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053092 javaFileInfo = javaInfo;
93 }
94
95 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053096 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +053097 *
98 * @return temporary file handle
99 */
100 @Override
101 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530102 return tempFileHandle;
103 }
104
105 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530106 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530107 *
108 * @param fileHandle temporary file handle
109 */
110 @Override
111 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 tempFileHandle = fileHandle;
113 }
114
115 /**
116 * Generates java code for module.
117 *
janani bde4ffab2016-04-15 16:18:30 +0530118 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530119 * @throws TranslatorException when fails to generate the source files
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 */
121 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530122 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
janani bdd1314f2016-05-19 17:39:50 +0530123 String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate(),
124 yangPlugin.getConflictResolver());
Bharat saraswald50c6382016-07-14 21:57:13 +0530125
126 if (isNotificationChildNodePresent(this)) {
127 getJavaFileInfo().setGeneratedFileTypes(getJavaFileInfo().getGeneratedFileTypes()
128 | GENERATE_ALL_EVENT_CLASS_MASK);
129 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530130 try {
131 generateCodeOfRootNode(this, yangPlugin, modulePkg);
132 } catch (IOException e) {
133 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530134 "Failed to prepare generate code entry for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530135 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530136 }
137
Vidyashree Rama74453712016-04-18 12:29:39 +0530138 /**
139 * Creates a java file using the YANG module info.
140 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530141 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530142 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530143 /**
144 * As part of the notification support the following files needs to be generated.
145 * 1) Subject of the notification(event), this is simple interface with builder class.
146 * 2) Event class extending "AbstractEvent" and defining event type enum.
147 * 3) Event listener interface extending "EventListener".
148 * 4) Event subject class.
149 *
150 * The manager class needs to extend the "ListenerRegistry".
151 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530152 try {
Bharat saraswald50c6382016-07-14 21:57:13 +0530153 if ((getJavaFileInfo().getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
154 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, this);
155 }
156 getTempJavaCodeFragmentFiles()
157 .generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
158 if (isManagerCodeGenRequired(this)) {
159 if (isGenerationOfCodeReq(getJavaFileInfo())) {
160 if ((getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi() == null)
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530161 || (!getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi().equals(SBI))) {
162 getTempJavaCodeFragmentFiles().getServiceTempFiles().setManagerNeedToBeGenerated(true);
163 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530164 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530165 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530166 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
167
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530168 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
169 getJavaFileInfo().getPackageFilePath());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530170 searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() +
171 getJavaFileInfo().getPackageFilePath());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530172 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530173 throw new TranslatorException("Failed to generate code for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530174 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530175 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530176
177 /**
178 * Returns notifications node list.
179 *
180 * @return notification nodes
181 */
182 public List<YangNode> getNotificationNodes() {
183 return notificationNodes;
184 }
185
186 /**
187 * Sets notifications list.
188 *
189 * @param notificationNodes notification list
190 */
191 private void setNotificationNodes(List<YangNode> notificationNodes) {
192 this.notificationNodes = notificationNodes;
193 }
194
195 /**
196 * Adds to notification node list.
197 *
198 * @param curNode notification node
199 */
200 private void addToNotificaitonList(YangNode curNode) {
201 getNotificationNodes().add(curNode);
202 }
203
204 /**
205 * Checks if there is any rpc defined in the module or sub-module.
206 *
207 * @param rootNode root node of the data model
208 * @return status of rpc's existence
209 */
Bharat saraswale707f032016-07-14 23:33:55 +0530210 private boolean isNotificationChildNodePresent(YangNode rootNode) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530211 YangNode childNode = rootNode.getChild();
212
213 while (childNode != null) {
214 if (childNode instanceof YangNotification) {
215 addToNotificaitonList(childNode);
216 }
217 childNode = childNode.getNextSibling();
218 }
219
Bharat saraswale707f032016-07-14 23:33:55 +0530220 return !getNotificationNodes().isEmpty();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530221 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530222}