blob: 5dae6142024dea4a27da048466e3b897514b1675 [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 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;
Bharat saraswalc0e04842016-05-12 13:16:57 +053039import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053040import static org.onosproject.yangutils.utils.UtilConstants.SBI;
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 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053045public class YangJavaModule
46 extends YangModule
47 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 /**
52 * Contains the information of the java file being generated.
53 */
54 private JavaFileInfo javaFileInfo;
55
56 /**
Bharat saraswald50c6382016-07-14 21:57:13 +053057 * File handle to maintain temporary java code fragments as per the code snippet types.
Vinod Kumar S38046502016-03-23 15:30:27 +053058 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053059 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053060
61 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053062 * List of notifications nodes.
63 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +053064 private transient List<YangNode> notificationNodes;
Bharat saraswal33dfa012016-05-17 19:59:16 +053065
66 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053067 * Creates a YANG node of module type.
Vinod Kumar S38046502016-03-23 15:30:27 +053068 */
69 public YangJavaModule() {
70 super();
71 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053072 setNotificationNodes(new ArrayList<>());
Bharat saraswald50c6382016-07-14 21:57:13 +053073 getJavaFileInfo().setGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER | GENERATE_INTERFACE_WITH_BUILDER);
Bharat saraswal33dfa012016-05-17 19:59:16 +053074
Vinod Kumar S38046502016-03-23 15:30:27 +053075 }
76
77 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053078 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053079 *
80 * @return generated java file information
81 */
82 @Override
83 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053084 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053085 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053086 }
87 return javaFileInfo;
88 }
89
90 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053091 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053092 *
93 * @param javaInfo java file info object
94 */
95 @Override
96 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053097 javaFileInfo = javaInfo;
98 }
99
100 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530101 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530102 *
103 * @return temporary file handle
104 */
105 @Override
106 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530107 return tempFileHandle;
108 }
109
110 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530111 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 *
113 * @param fileHandle temporary file handle
114 */
115 @Override
116 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530117 tempFileHandle = fileHandle;
118 }
119
120 /**
121 * Generates java code for module.
122 *
janani bde4ffab2016-04-15 16:18:30 +0530123 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530124 * @throws TranslatorException when fails to generate the source files
Vinod Kumar S38046502016-03-23 15:30:27 +0530125 */
126 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530127 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
janani bdd1314f2016-05-19 17:39:50 +0530128 String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate(),
129 yangPlugin.getConflictResolver());
Bharat saraswald50c6382016-07-14 21:57:13 +0530130
131 if (isNotificationChildNodePresent(this)) {
132 getJavaFileInfo().setGeneratedFileTypes(getJavaFileInfo().getGeneratedFileTypes()
133 | GENERATE_ALL_EVENT_CLASS_MASK);
134 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530135 try {
136 generateCodeOfRootNode(this, yangPlugin, modulePkg);
137 } catch (IOException e) {
138 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530139 "Failed to prepare generate code entry for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530140 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530141 }
142
Vidyashree Rama74453712016-04-18 12:29:39 +0530143 /**
144 * Creates a java file using the YANG module info.
145 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530146 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530147 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530148 /**
149 * As part of the notification support the following files needs to be generated.
150 * 1) Subject of the notification(event), this is simple interface with builder class.
151 * 2) Event class extending "AbstractEvent" and defining event type enum.
152 * 3) Event listener interface extending "EventListener".
153 * 4) Event subject class.
154 *
155 * The manager class needs to extend the "ListenerRegistry".
156 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530157 try {
Bharat saraswald50c6382016-07-14 21:57:13 +0530158 if ((getJavaFileInfo().getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
159 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, this);
160 }
161 getTempJavaCodeFragmentFiles()
162 .generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
163 if (isManagerCodeGenRequired(this)) {
164 if (isGenerationOfCodeReq(getJavaFileInfo())) {
165 if ((getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi() == null)
166 || (!getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi().equals(SBI))) {
167 getTempJavaCodeFragmentFiles().getServiceTempFiles().setManagerNeedToBeGenerated(true);
168 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530169 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530170 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530171 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
172
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530173 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
174 getJavaFileInfo().getPackageFilePath());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530175 searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() +
176 getJavaFileInfo().getPackageFilePath());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530177 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530178 throw new TranslatorException("Failed to generate code for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530179 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530180 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530181
182 /**
183 * Returns notifications node list.
184 *
185 * @return notification nodes
186 */
187 public List<YangNode> getNotificationNodes() {
188 return notificationNodes;
189 }
190
191 /**
192 * Sets notifications list.
193 *
194 * @param notificationNodes notification list
195 */
196 private void setNotificationNodes(List<YangNode> notificationNodes) {
197 this.notificationNodes = notificationNodes;
198 }
199
200 /**
201 * Adds to notification node list.
202 *
203 * @param curNode notification node
204 */
205 private void addToNotificaitonList(YangNode curNode) {
206 getNotificationNodes().add(curNode);
207 }
208
209 /**
210 * Checks if there is any rpc defined in the module or sub-module.
211 *
212 * @param rootNode root node of the data model
213 * @return status of rpc's existence
214 */
215 public boolean isNotificationChildNodePresent(YangNode rootNode) {
216 YangNode childNode = rootNode.getChild();
217
218 while (childNode != null) {
219 if (childNode instanceof YangNotification) {
220 addToNotificaitonList(childNode);
221 }
222 childNode = childNode.getNextSibling();
223 }
224
225 if (!getNotificationNodes().isEmpty()) {
226 return true;
227 }
228 return false;
229 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530230}