blob: 73d7583479053bcbd73c4f7f8cc70852c0df2cbb [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;
21
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.YangBelongsTo;
Vidyashree Rama1db15562016-05-17 16:16:15 +053023import org.onosproject.yangutils.datamodel.YangModule;
Bharat saraswal33dfa012016-05-17 19:59:16 +053024import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.datamodel.YangNotification;
Vinod Kumar S38046502016-03-23 15:30:27 +053026import org.onosproject.yangutils.datamodel.YangSubModule;
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.JavaFileInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053031import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053032import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vinod Kumar S38046502016-03-23 15:30:27 +053033
Bharat saraswald50c6382016-07-14 21:57:13 +053034import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ALL_EVENT_CLASS_MASK;
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;
Bharat saraswalb551aae2016-07-14 15:18:20 +053038import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isGenerationOfCodeReq;
Bharat saraswald50c6382016-07-14 21:57:13 +053039import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isManagerCodeGenRequired;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053040import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053041import static org.onosproject.yangutils.utils.UtilConstants.SBI;
Bharat saraswalc0e04842016-05-12 13:16:57 +053042import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053043
44/**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053045 * Represents sub 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 YangJavaSubModule
48 extends YangSubModule
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 = 806201621L;
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 /**
Bharat saraswald50c6382016-07-14 21:57:13 +053059 * File handle to maintain temporary java code fragments as per the code snippet types.
Vinod Kumar S38046502016-03-23 15:30:27 +053060 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053061 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053062
63 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053064 * List of notifications nodes.
65 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +053066 private transient List<YangNode> notificationNodes = new ArrayList<>();
Bharat saraswal33dfa012016-05-17 19:59:16 +053067
68 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053069 * Creates YANG java sub module object.
Vinod Kumar S38046502016-03-23 15:30:27 +053070 */
71 public YangJavaSubModule() {
72 super();
73 setJavaFileInfo(new JavaFileInfo());
Bharat saraswalb551aae2016-07-14 15:18:20 +053074 int gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswal33dfa012016-05-17 19:59:16 +053075 if (isNotificationChildNodePresent(this)) {
Bharat saraswald50c6382016-07-14 21:57:13 +053076 gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_ALL_EVENT_CLASS_MASK;
Bharat saraswal33dfa012016-05-17 19:59:16 +053077 }
78 getJavaFileInfo().setGeneratedFileTypes(gentype);
Vinod Kumar S38046502016-03-23 15:30:27 +053079 }
80
81 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053082 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053083 *
84 * @return generated java file information
85 */
86 @Override
87 public JavaFileInfo getJavaFileInfo() {
88 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053089 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053090 }
91 return javaFileInfo;
92 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053096 *
97 * @param javaInfo java file info object
98 */
99 @Override
100 public void setJavaFileInfo(JavaFileInfo javaInfo) {
101 javaFileInfo = javaInfo;
102 }
103
104 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530105 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 *
107 * @return temporary file handle
108 */
109 @Override
110 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530111 return tempFileHandle;
112 }
113
114 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530115 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530116 *
117 * @param fileHandle temporary file handle
118 */
119 @Override
120 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
121 tempFileHandle = fileHandle;
122 }
123
124 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530125 * Returns the name space of the module to which the sub module belongs to.
Vinod Kumar S38046502016-03-23 15:30:27 +0530126 *
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530127 * @param belongsToInfo Information of the module to which the sub module belongs
Vinod Kumar S38046502016-03-23 15:30:27 +0530128 * @return the name space string of the module.
129 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530130 public String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530131 return ((YangModule) belongsToInfo.getModuleNode()).getNameSpace().getUri();
Vinod Kumar S38046502016-03-23 15:30:27 +0530132 }
133
134 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530135 * Prepares the information for java code generation corresponding to YANG submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530136 *
janani bde4ffab2016-04-15 16:18:30 +0530137 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530138 * @throws TranslatorException when fails to translate
Vinod Kumar S38046502016-03-23 15:30:27 +0530139 */
140 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530141 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530142 String subModulePkg = getRootPackage(getVersion(), getNameSpaceFromModule(getBelongsTo()),
janani bdd1314f2016-05-19 17:39:50 +0530143 getRevision().getRevDate(), yangPlugin.getConflictResolver());
Bharat saraswald50c6382016-07-14 21:57:13 +0530144
145 if (isNotificationChildNodePresent(this)) {
146 getJavaFileInfo().setGeneratedFileTypes(getJavaFileInfo().getGeneratedFileTypes()
147 | GENERATE_ALL_EVENT_CLASS_MASK);
148 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530149 try {
150 generateCodeOfRootNode(this, yangPlugin, subModulePkg);
151 } catch (IOException e) {
152 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530153 "failed to prepare generate code entry for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530154 }
155
Vinod Kumar S38046502016-03-23 15:30:27 +0530156 }
157
158 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530159 * Creates a java file using the YANG submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530160 */
161 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530162 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530163 /**
164 * As part of the notification support the following files needs to be generated.
165 * 1) Subject of the notification(event), this is simple interface with builder class.
166 * 2) Event class extending "AbstractEvent" and defining event type enum.
167 * 3) Event listener interface extending "EventListener".
168 * 4) Event subject class.
169 *
170 * The manager class needs to extend the "ListenerRegistry".
171 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530172 try {
Bharat saraswald50c6382016-07-14 21:57:13 +0530173 if ((getJavaFileInfo().getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
174 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, this);
175 }
176 getTempJavaCodeFragmentFiles()
177 .generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
178 if (isManagerCodeGenRequired(this)) {
179 if (isGenerationOfCodeReq(getJavaFileInfo())) {
180 if ((getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi() == null)
181 || (!getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi().equals(SBI))) {
182 getTempJavaCodeFragmentFiles().getServiceTempFiles().setManagerNeedToBeGenerated(true);
183 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530184 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530185 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530186 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
187
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530188 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
189 getJavaFileInfo().getPackageFilePath());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530190 searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() +
191 getJavaFileInfo().getPackageFilePath());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530192 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530193 throw new TranslatorException("Failed to generate code for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530194 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530195 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530196
197 /**
198 * Returns notifications node list.
199 *
200 * @return notification nodes
201 */
202 public List<YangNode> getNotificationNodes() {
203 return notificationNodes;
204 }
205
206 /**
207 * Adds to notification node list.
208 *
209 * @param curNode notification node
210 */
211 private void addToNotificaitonList(YangNode curNode) {
212 getNotificationNodes().add(curNode);
213 }
214
215 /**
216 * Checks if there is any rpc defined in the module or sub-module.
217 *
218 * @param rootNode root node of the data model
219 * @return status of rpc's existence
220 */
Bharat saraswale707f032016-07-14 23:33:55 +0530221 private boolean isNotificationChildNodePresent(YangNode rootNode) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530222 YangNode childNode = rootNode.getChild();
223
224 while (childNode != null) {
225 if (childNode instanceof YangNotification) {
226 addToNotificaitonList(childNode);
227 }
228 childNode = childNode.getNextSibling();
229 }
230
Bharat saraswale707f032016-07-14 23:33:55 +0530231 return !getNotificationNodes().isEmpty();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530232 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530233}