blob: 6837aa4a78fe59c16ba115d0b0081f9efe031925 [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 saraswal33dfa012016-05-17 19:59:16 +053032import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
33import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
34import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
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;
38import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isManagerCodeGenRequired;
39import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isGenerationOfCodeReq;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053040import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
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/**
Bharat saraswald9822e92016-04-05 15:13:44 +053044 * Represents module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053045 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046public class YangJavaModule
47 extends YangModule
48 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 = 806201625L;
51
Vinod Kumar S38046502016-03-23 15:30:27 +053052 /**
53 * Contains the information of the java file being generated.
54 */
55 private JavaFileInfo javaFileInfo;
56
57 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053058 * File handle to maintain temporary java code fragments as per the code
59 * snippet types.
60 */
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;
Bharat saraswal33dfa012016-05-17 19:59:16 +053067
68 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053069 * Creates a YANG node of module type.
Vinod Kumar S38046502016-03-23 15:30:27 +053070 */
71 public YangJavaModule() {
72 super();
73 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053074 setNotificationNodes(new ArrayList<>());
Bharat saraswalb551aae2016-07-14 15:18:20 +053075 int gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswal33dfa012016-05-17 19:59:16 +053076 if (isNotificationChildNodePresent(this)) {
77 gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_EVENT_SUBJECT_CLASS | GENERATE_EVENT_CLASS
78 | GENERATE_EVENT_LISTENER_INTERFACE;
79 }
80 getJavaFileInfo().setGeneratedFileTypes(gentype);
81
Vinod Kumar S38046502016-03-23 15:30:27 +053082 }
83
84 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053085 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053086 *
87 * @return generated java file information
88 */
89 @Override
90 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053091 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053092 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053093 }
94 return javaFileInfo;
95 }
96
97 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053098 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053099 *
100 * @param javaInfo java file info object
101 */
102 @Override
103 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530104 javaFileInfo = javaInfo;
105 }
106
107 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530108 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530109 *
110 * @return temporary file handle
111 */
112 @Override
113 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530114 return tempFileHandle;
115 }
116
117 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530118 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530119 *
120 * @param fileHandle temporary file handle
121 */
122 @Override
123 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530124 tempFileHandle = fileHandle;
125 }
126
127 /**
128 * Generates java code for module.
129 *
janani bde4ffab2016-04-15 16:18:30 +0530130 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530131 * @throws TranslatorException when fails to generate the source files
Vinod Kumar S38046502016-03-23 15:30:27 +0530132 */
133 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530134 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
janani bdd1314f2016-05-19 17:39:50 +0530135 String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate(),
136 yangPlugin.getConflictResolver());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530137 try {
138 generateCodeOfRootNode(this, yangPlugin, modulePkg);
139 } catch (IOException e) {
140 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530141 "Failed to prepare generate code entry for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530142 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530143 }
144
Vidyashree Rama74453712016-04-18 12:29:39 +0530145 /**
146 * Creates a java file using the YANG module info.
147 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530148 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530149 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530150 /**
151 * As part of the notification support the following files needs to be generated.
152 * 1) Subject of the notification(event), this is simple interface with builder class.
153 * 2) Event class extending "AbstractEvent" and defining event type enum.
154 * 3) Event listener interface extending "EventListener".
155 * 4) Event subject class.
156 *
157 * The manager class needs to extend the "ListenerRegistry".
158 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530159
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530160 try {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530161 if (isManagerCodeGenRequired(this) && isGenerationOfCodeReq(getJavaFileInfo())) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530162 getTempJavaCodeFragmentFiles()
163 .generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
164 getTempJavaCodeFragmentFiles()
165 .generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530166 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530167 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
168 getJavaFileInfo().getPackageFilePath());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530169 searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() +
170 getJavaFileInfo().getPackageFilePath());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530171 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530172 throw new TranslatorException("Failed to generate code for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530173 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530174 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530175
176 /**
177 * Returns notifications node list.
178 *
179 * @return notification nodes
180 */
181 public List<YangNode> getNotificationNodes() {
182 return notificationNodes;
183 }
184
185 /**
186 * Sets notifications list.
187 *
188 * @param notificationNodes notification list
189 */
190 private void setNotificationNodes(List<YangNode> notificationNodes) {
191 this.notificationNodes = notificationNodes;
192 }
193
194 /**
195 * Adds to notification node list.
196 *
197 * @param curNode notification node
198 */
199 private void addToNotificaitonList(YangNode curNode) {
200 getNotificationNodes().add(curNode);
201 }
202
203 /**
204 * Checks if there is any rpc defined in the module or sub-module.
205 *
206 * @param rootNode root node of the data model
207 * @return status of rpc's existence
208 */
209 public boolean isNotificationChildNodePresent(YangNode rootNode) {
210 YangNode childNode = rootNode.getChild();
211
212 while (childNode != null) {
213 if (childNode instanceof YangNotification) {
214 addToNotificaitonList(childNode);
215 }
216 childNode = childNode.getNextSibling();
217 }
218
219 if (!getNotificationNodes().isEmpty()) {
220 return true;
221 }
222 return false;
223 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530224}