blob: 10aab9387cf1f6bd34e256a797f4f662f0cc3412 [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 saraswal2d90b0c2016-08-04 02:00:03 +053038import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isRootNodesCodeGenRequired;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053039import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053040import static org.onosproject.yangutils.utils.UtilConstants.SBI;
Bharat saraswalc0e04842016-05-12 13:16:57 +053041import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053042
43/**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053044 * Represents sub module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053045 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053046public class YangJavaSubModuleTranslator
47 extends YangJavaSubModule
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053048 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053049
Bharat saraswal96dfef02016-06-16 00:29:12 +053050 private static final long serialVersionUID = 806201621L;
51
Vinod Kumar S38046502016-03-23 15:30:27 +053052 /**
Bharat saraswald50c6382016-07-14 21:57:13 +053053 * File handle to maintain temporary java code fragments as per the code snippet types.
Vinod Kumar S38046502016-03-23 15:30:27 +053054 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053055 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053056
57 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053058 * List of notifications nodes.
59 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +053060 private transient List<YangNode> notificationNodes = new ArrayList<>();
Bharat saraswal33dfa012016-05-17 19:59:16 +053061
62 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053063 * Creates YANG java sub module object.
Vinod Kumar S38046502016-03-23 15:30:27 +053064 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053065 public YangJavaSubModuleTranslator() {
Vinod Kumar S38046502016-03-23 15:30:27 +053066 super();
67 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053068 int genType = GENERATE_SERVICE_AND_MANAGER | GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswal33dfa012016-05-17 19:59:16 +053069 if (isNotificationChildNodePresent(this)) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053070 genType = GENERATE_SERVICE_AND_MANAGER | GENERATE_ALL_EVENT_CLASS_MASK;
Bharat saraswal33dfa012016-05-17 19:59:16 +053071 }
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053072 getJavaFileInfo().setGeneratedFileTypes(genType);
Vinod Kumar S38046502016-03-23 15:30:27 +053073 }
74
75 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053076 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053077 *
78 * @return generated java file information
79 */
80 @Override
81 public JavaFileInfo getJavaFileInfo() {
82 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053083 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053084 }
85 return javaFileInfo;
86 }
87
88 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053089 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053090 *
91 * @param javaInfo java file info object
92 */
93 @Override
94 public void setJavaFileInfo(JavaFileInfo javaInfo) {
95 javaFileInfo = javaInfo;
96 }
97
98 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053099 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 *
101 * @return temporary file handle
102 */
103 @Override
104 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 return tempFileHandle;
106 }
107
108 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 *
111 * @param fileHandle temporary file handle
112 */
113 @Override
114 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
115 tempFileHandle = fileHandle;
116 }
117
118 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530119 * Returns the name space of the module to which the sub module belongs to.
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 *
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530121 * @param belongsToInfo Information of the module to which the sub module belongs
Vinod Kumar S38046502016-03-23 15:30:27 +0530122 * @return the name space string of the module.
123 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530124 public String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530125 return ((YangModule) belongsToInfo.getModuleNode()).getNameSpace().getUri();
Vinod Kumar S38046502016-03-23 15:30:27 +0530126 }
127
128 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530129 * Prepares the information for java code generation corresponding to YANG submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530130 *
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 translate
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 {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530136 String subModulePkg = getRootPackage(getVersion(), getNameSpaceFromModule(getBelongsTo()),
janani bdd1314f2016-05-19 17:39:50 +0530137 getRevision().getRevDate(), yangPlugin.getConflictResolver());
Bharat saraswald50c6382016-07-14 21:57:13 +0530138
139 if (isNotificationChildNodePresent(this)) {
140 getJavaFileInfo().setGeneratedFileTypes(getJavaFileInfo().getGeneratedFileTypes()
141 | GENERATE_ALL_EVENT_CLASS_MASK);
142 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530143 try {
144 generateCodeOfRootNode(this, yangPlugin, subModulePkg);
145 } catch (IOException e) {
146 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530147 "failed to prepare generate code entry for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530148 }
149
Vinod Kumar S38046502016-03-23 15:30:27 +0530150 }
151
152 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530153 * Creates a java file using the YANG submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530154 */
155 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530156 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530157 /**
158 * As part of the notification support the following files needs to be generated.
159 * 1) Subject of the notification(event), this is simple interface with builder class.
160 * 2) Event class extending "AbstractEvent" and defining event type enum.
161 * 3) Event listener interface extending "EventListener".
162 * 4) Event subject class.
163 *
164 * The manager class needs to extend the "ListenerRegistry".
165 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530166 try {
Bharat saraswald50c6382016-07-14 21:57:13 +0530167 if ((getJavaFileInfo().getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
168 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, this);
169 }
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530170 if (isRootNodesCodeGenRequired(this)) {
171 getTempJavaCodeFragmentFiles()
172 .generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
173 if ((getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi() == null)
174 || (!getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi().equals(SBI))) {
175 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530176 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530177 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530178
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530179 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
180 getJavaFileInfo().getPackageFilePath());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530181 searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() +
182 getJavaFileInfo().getPackageFilePath());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530183 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530184 throw new TranslatorException("Failed to generate code for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530185 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530186 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530187
188 /**
189 * Returns notifications node list.
190 *
191 * @return notification nodes
192 */
193 public List<YangNode> getNotificationNodes() {
194 return notificationNodes;
195 }
196
197 /**
198 * Adds to notification node list.
199 *
200 * @param curNode notification node
201 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530202 private void addToNotificationList(YangNode curNode) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530203 getNotificationNodes().add(curNode);
204 }
205
206 /**
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530207 * Checks if there is any notification node present.
Bharat saraswal33dfa012016-05-17 19:59:16 +0530208 *
209 * @param rootNode root node of the data model
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530210 * @return status of notification's existence
Bharat saraswal33dfa012016-05-17 19:59:16 +0530211 */
Bharat saraswale707f032016-07-14 23:33:55 +0530212 private boolean isNotificationChildNodePresent(YangNode rootNode) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530213 YangNode childNode = rootNode.getChild();
214
215 while (childNode != null) {
216 if (childNode instanceof YangNotification) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530217 addToNotificationList(childNode);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530218 }
219 childNode = childNode.getNextSibling();
220 }
221
Bharat saraswale707f032016-07-14 23:33:55 +0530222 return !getNotificationNodes().isEmpty();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530223 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530224}