blob: a089a99956c466b2ae5b2759eb5d07a601bec6b3 [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;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053026import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
27import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaSubModule;
28import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053029import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053030import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053031import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053032import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
33
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 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053047public class YangJavaSubModuleTranslator
48 extends YangJavaSubModule
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049 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 /**
Bharat saraswald50c6382016-07-14 21:57:13 +053054 * File handle to maintain temporary java code fragments as per the code snippet types.
Vinod Kumar S38046502016-03-23 15:30:27 +053055 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053056 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053057
58 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053059 * List of notifications nodes.
60 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +053061 private transient List<YangNode> notificationNodes = new ArrayList<>();
Bharat saraswal33dfa012016-05-17 19:59:16 +053062
63 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053064 * Creates YANG java sub module object.
Vinod Kumar S38046502016-03-23 15:30:27 +053065 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053066 public YangJavaSubModuleTranslator() {
Vinod Kumar S38046502016-03-23 15:30:27 +053067 super();
68 setJavaFileInfo(new JavaFileInfo());
Bharat saraswalb551aae2016-07-14 15:18:20 +053069 int gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswal33dfa012016-05-17 19:59:16 +053070 if (isNotificationChildNodePresent(this)) {
Bharat saraswald50c6382016-07-14 21:57:13 +053071 gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_ALL_EVENT_CLASS_MASK;
Bharat saraswal33dfa012016-05-17 19:59:16 +053072 }
73 getJavaFileInfo().setGeneratedFileTypes(gentype);
Vinod Kumar S38046502016-03-23 15:30:27 +053074 }
75
76 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053077 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053078 *
79 * @return generated java file information
80 */
81 @Override
82 public JavaFileInfo getJavaFileInfo() {
83 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053084 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053085 }
86 return javaFileInfo;
87 }
88
89 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053090 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053091 *
92 * @param javaInfo java file info object
93 */
94 @Override
95 public void setJavaFileInfo(JavaFileInfo javaInfo) {
96 javaFileInfo = javaInfo;
97 }
98
99 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530100 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530101 *
102 * @return temporary file handle
103 */
104 @Override
105 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 return tempFileHandle;
107 }
108
109 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530110 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530111 *
112 * @param fileHandle temporary file handle
113 */
114 @Override
115 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
116 tempFileHandle = fileHandle;
117 }
118
119 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530120 * Returns the name space of the module to which the sub module belongs to.
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 *
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530122 * @param belongsToInfo Information of the module to which the sub module belongs
Vinod Kumar S38046502016-03-23 15:30:27 +0530123 * @return the name space string of the module.
124 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530125 public String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530126 return ((YangModule) belongsToInfo.getModuleNode()).getNameSpace().getUri();
Vinod Kumar S38046502016-03-23 15:30:27 +0530127 }
128
129 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530130 * Prepares the information for java code generation corresponding to YANG submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530131 *
janani bde4ffab2016-04-15 16:18:30 +0530132 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530133 * @throws TranslatorException when fails to translate
Vinod Kumar S38046502016-03-23 15:30:27 +0530134 */
135 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530136 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530137 String subModulePkg = getRootPackage(getVersion(), getNameSpaceFromModule(getBelongsTo()),
janani bdd1314f2016-05-19 17:39:50 +0530138 getRevision().getRevDate(), yangPlugin.getConflictResolver());
Bharat saraswald50c6382016-07-14 21:57:13 +0530139
140 if (isNotificationChildNodePresent(this)) {
141 getJavaFileInfo().setGeneratedFileTypes(getJavaFileInfo().getGeneratedFileTypes()
142 | GENERATE_ALL_EVENT_CLASS_MASK);
143 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530144 try {
145 generateCodeOfRootNode(this, yangPlugin, subModulePkg);
146 } catch (IOException e) {
147 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530148 "failed to prepare generate code entry for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530149 }
150
Vinod Kumar S38046502016-03-23 15:30:27 +0530151 }
152
153 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530154 * Creates a java file using the YANG submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 */
156 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530157 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530158 /**
159 * As part of the notification support the following files needs to be generated.
160 * 1) Subject of the notification(event), this is simple interface with builder class.
161 * 2) Event class extending "AbstractEvent" and defining event type enum.
162 * 3) Event listener interface extending "EventListener".
163 * 4) Event subject class.
164 *
165 * The manager class needs to extend the "ListenerRegistry".
166 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530167 try {
Bharat saraswald50c6382016-07-14 21:57:13 +0530168 if ((getJavaFileInfo().getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
169 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, this);
170 }
171 getTempJavaCodeFragmentFiles()
172 .generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
173 if (isManagerCodeGenRequired(this)) {
174 if (isGenerationOfCodeReq(getJavaFileInfo())) {
175 if ((getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi() == null)
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530176 || (!getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi().equals(SBI))) {
177 getTempJavaCodeFragmentFiles().getServiceTempFiles().setManagerNeedToBeGenerated(true);
178 }
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530179 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530180 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530181 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
182
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530183 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
184 getJavaFileInfo().getPackageFilePath());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530185 searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() +
186 getJavaFileInfo().getPackageFilePath());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530187 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530188 throw new TranslatorException("Failed to generate code for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530189 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530190 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530191
192 /**
193 * Returns notifications node list.
194 *
195 * @return notification nodes
196 */
197 public List<YangNode> getNotificationNodes() {
198 return notificationNodes;
199 }
200
201 /**
202 * Adds to notification node list.
203 *
204 * @param curNode notification node
205 */
206 private void addToNotificaitonList(YangNode curNode) {
207 getNotificationNodes().add(curNode);
208 }
209
210 /**
211 * Checks if there is any rpc defined in the module or sub-module.
212 *
213 * @param rootNode root node of the data model
214 * @return status of rpc's existence
215 */
Bharat saraswale707f032016-07-14 23:33:55 +0530216 private boolean isNotificationChildNodePresent(YangNode rootNode) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530217 YangNode childNode = rootNode.getChild();
218
219 while (childNode != null) {
220 if (childNode instanceof YangNotification) {
221 addToNotificaitonList(childNode);
222 }
223 childNode = childNode.getNextSibling();
224 }
225
Bharat saraswale707f032016-07-14 23:33:55 +0530226 return !getNotificationNodes().isEmpty();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530227 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530228}