blob: b18558845f5115838913f269d103863ef59009cf [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
Bharat saraswal33dfa012016-05-17 19:59:16 +053022import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.YangNotification;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053024import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
25import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaModule;
26import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
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.TempJavaCodeFragmentFiles;
31
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 saraswal2d90b0c2016-08-04 02:00:03 +053036import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isRootNodesCodeGenRequired;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053037import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053038import static org.onosproject.yangutils.utils.UtilConstants.SBI;
Bharat saraswale707f032016-07-14 23:33:55 +053039import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053040
41/**
Bharat saraswald9822e92016-04-05 15:13:44 +053042 * Represents module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053043 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053044public class YangJavaModuleTranslator
45 extends YangJavaModule
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053047
Bharat saraswal96dfef02016-06-16 00:29:12 +053048 private static final long serialVersionUID = 806201625L;
49
Vinod Kumar S38046502016-03-23 15:30:27 +053050 /**
Bharat saraswald50c6382016-07-14 21:57:13 +053051 * File handle to maintain temporary java code fragments as per the code snippet types.
Vinod Kumar S38046502016-03-23 15:30:27 +053052 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053053 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053054
55 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053056 * List of notifications nodes.
57 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +053058 private transient List<YangNode> notificationNodes;
Bharat saraswal33dfa012016-05-17 19:59:16 +053059
60 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053061 * Creates a YANG node of module type.
Vinod Kumar S38046502016-03-23 15:30:27 +053062 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053063 public YangJavaModuleTranslator() {
Vinod Kumar S38046502016-03-23 15:30:27 +053064 super();
65 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053066 setNotificationNodes(new ArrayList<>());
Bharat saraswald50c6382016-07-14 21:57:13 +053067 getJavaFileInfo().setGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER | GENERATE_INTERFACE_WITH_BUILDER);
Bharat saraswal33dfa012016-05-17 19:59:16 +053068
Vinod Kumar S38046502016-03-23 15:30:27 +053069 }
70
71 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053072 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053073 *
74 * @return generated java file information
75 */
76 @Override
77 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053078 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053079 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053080 }
81 return javaFileInfo;
82 }
83
84 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053085 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053086 *
87 * @param javaInfo java file info object
88 */
89 @Override
90 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053091 javaFileInfo = javaInfo;
92 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +053096 *
97 * @return temporary file handle
98 */
99 @Override
100 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530101 return tempFileHandle;
102 }
103
104 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530105 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 *
107 * @param fileHandle temporary file handle
108 */
109 @Override
110 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530111 tempFileHandle = fileHandle;
112 }
113
114 /**
115 * Generates java code for module.
116 *
janani bde4ffab2016-04-15 16:18:30 +0530117 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530118 * @throws TranslatorException when fails to generate the source files
Vinod Kumar S38046502016-03-23 15:30:27 +0530119 */
120 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530121 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
janani bdd1314f2016-05-19 17:39:50 +0530122 String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate(),
123 yangPlugin.getConflictResolver());
Bharat saraswald50c6382016-07-14 21:57:13 +0530124
125 if (isNotificationChildNodePresent(this)) {
126 getJavaFileInfo().setGeneratedFileTypes(getJavaFileInfo().getGeneratedFileTypes()
127 | GENERATE_ALL_EVENT_CLASS_MASK);
128 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530129 try {
130 generateCodeOfRootNode(this, yangPlugin, modulePkg);
131 } catch (IOException e) {
132 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530133 "Failed to prepare generate code entry for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530134 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530135 }
136
Vidyashree Rama74453712016-04-18 12:29:39 +0530137 /**
138 * Creates a java file using the YANG module info.
139 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530140 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530141 public void generateCodeExit() throws TranslatorException {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530142 /*
Bharat saraswal33dfa012016-05-17 19:59:16 +0530143 * As part of the notification support the following files needs to be generated.
144 * 1) Subject of the notification(event), this is simple interface with builder class.
145 * 2) Event class extending "AbstractEvent" and defining event type enum.
146 * 3) Event listener interface extending "EventListener".
147 * 4) Event subject class.
148 *
149 * The manager class needs to extend the "ListenerRegistry".
150 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530151 try {
Bharat saraswald50c6382016-07-14 21:57:13 +0530152 if ((getJavaFileInfo().getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) {
153 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, this);
154 }
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530155
156 if (isRootNodesCodeGenRequired(this)) {
157 getTempJavaCodeFragmentFiles()
158 .generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
159 if ((getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi() == null)
160 || (!getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi().equals(SBI))) {
161 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530162 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530163 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530164
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530165 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
166 getJavaFileInfo().getPackageFilePath());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530167 searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() +
168 getJavaFileInfo().getPackageFilePath());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530169 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530170 throw new TranslatorException("Failed to generate code for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530171 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530172 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530173
174 /**
175 * Returns notifications node list.
176 *
177 * @return notification nodes
178 */
179 public List<YangNode> getNotificationNodes() {
180 return notificationNodes;
181 }
182
183 /**
184 * Sets notifications list.
185 *
186 * @param notificationNodes notification list
187 */
188 private void setNotificationNodes(List<YangNode> notificationNodes) {
189 this.notificationNodes = notificationNodes;
190 }
191
192 /**
193 * Adds to notification node list.
194 *
195 * @param curNode notification node
196 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530197 private void addToNotificationList(YangNode curNode) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530198 getNotificationNodes().add(curNode);
199 }
200
201 /**
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530202 * Checks if there is any notification node present.
Bharat saraswal33dfa012016-05-17 19:59:16 +0530203 *
204 * @param rootNode root node of the data model
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530205 * @return status of notification's existence
Bharat saraswal33dfa012016-05-17 19:59:16 +0530206 */
Bharat saraswale707f032016-07-14 23:33:55 +0530207 private boolean isNotificationChildNodePresent(YangNode rootNode) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530208 YangNode childNode = rootNode.getChild();
209
210 while (childNode != null) {
211 if (childNode instanceof YangNotification) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530212 addToNotificationList(childNode);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530213 }
214 childNode = childNode.getNextSibling();
215 }
216
Bharat saraswale707f032016-07-14 23:33:55 +0530217 return !getNotificationNodes().isEmpty();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530218 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530219}