blob: 835d7812c8a8a43c91bec485e6be6a6f448e953d [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 saraswal33dfa012016-05-17 19:59:16 +053034import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
35import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
36import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053038import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.generateCodeOfRootNode;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053039import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.isManagerCodeGenRequired;
40import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.isGenerationOfCodeReq;
41import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
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 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053059 * File handle to maintain temporary java code fragments as per the code
60 * snippet types.
61 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053062 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053063
64 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053065 * List of notifications nodes.
66 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +053067 private transient List<YangNode> notificationNodes = new ArrayList<>();
Bharat saraswal33dfa012016-05-17 19:59:16 +053068
69 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Creates YANG java sub module object.
Vinod Kumar S38046502016-03-23 15:30:27 +053071 */
72 public YangJavaSubModule() {
73 super();
74 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053075 int gentype = GENERATE_SERVICE_AND_MANAGER;
76 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);
Vinod Kumar S38046502016-03-23 15:30:27 +053081 }
82
83 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053084 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053085 *
86 * @return generated java file information
87 */
88 @Override
89 public JavaFileInfo getJavaFileInfo() {
90 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053091 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053092 }
93 return javaFileInfo;
94 }
95
96 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053097 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053098 *
99 * @param javaInfo java file info object
100 */
101 @Override
102 public void setJavaFileInfo(JavaFileInfo javaInfo) {
103 javaFileInfo = javaInfo;
104 }
105
106 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530107 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530108 *
109 * @return temporary file handle
110 */
111 @Override
112 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530113 return tempFileHandle;
114 }
115
116 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530117 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530118 *
119 * @param fileHandle temporary file handle
120 */
121 @Override
122 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
123 tempFileHandle = fileHandle;
124 }
125
126 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530127 * Returns the name space of the module to which the sub module belongs to.
Vinod Kumar S38046502016-03-23 15:30:27 +0530128 *
129 * @param belongsToInfo Information of the module to which the sub module
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530130 * belongs
Vinod Kumar S38046502016-03-23 15:30:27 +0530131 * @return the name space string of the module.
132 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530133 public String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530134 return ((YangModule) belongsToInfo.getModuleNode()).getNameSpace().getUri();
Vinod Kumar S38046502016-03-23 15:30:27 +0530135 }
136
137 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530138 * Prepares the information for java code generation corresponding to YANG
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530139 * submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530140 *
janani bde4ffab2016-04-15 16:18:30 +0530141 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530142 * @throws TranslatorException when fails to translate
Vinod Kumar S38046502016-03-23 15:30:27 +0530143 */
144 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530145 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530146 String subModulePkg = getRootPackage(getVersion(), getNameSpaceFromModule(getBelongsTo()),
janani bdd1314f2016-05-19 17:39:50 +0530147 getRevision().getRevDate(), yangPlugin.getConflictResolver());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530148 try {
149 generateCodeOfRootNode(this, yangPlugin, subModulePkg);
150 } catch (IOException e) {
151 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530152 "failed to prepare generate code entry for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530153 }
154
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 }
156
157 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530158 * Creates a java file using the YANG submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530159 */
160 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530161 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530162 /**
163 * As part of the notification support the following files needs to be generated.
164 * 1) Subject of the notification(event), this is simple interface with builder class.
165 * 2) Event class extending "AbstractEvent" and defining event type enum.
166 * 3) Event listener interface extending "EventListener".
167 * 4) Event subject class.
168 *
169 * The manager class needs to extend the "ListenerRegistry".
170 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530171 try {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530172 if (isManagerCodeGenRequired(this) && isGenerationOfCodeReq(getJavaFileInfo())) {
173 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
174 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530175 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
176 getJavaFileInfo().getPackageFilePath());
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530177 searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() +
178 getJavaFileInfo().getPackageFilePath());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530179 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530180 throw new TranslatorException("Failed to generate code for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530181 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530182 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530183
184 /**
185 * Returns notifications node list.
186 *
187 * @return notification nodes
188 */
189 public List<YangNode> getNotificationNodes() {
190 return notificationNodes;
191 }
192
193 /**
194 * Adds to notification node list.
195 *
196 * @param curNode notification node
197 */
198 private void addToNotificaitonList(YangNode curNode) {
199 getNotificationNodes().add(curNode);
200 }
201
202 /**
203 * Checks if there is any rpc defined in the module or sub-module.
204 *
205 * @param rootNode root node of the data model
206 * @return status of rpc's existence
207 */
208 public boolean isNotificationChildNodePresent(YangNode rootNode) {
209 YangNode childNode = rootNode.getChild();
210
211 while (childNode != null) {
212 if (childNode instanceof YangNotification) {
213 addToNotificaitonList(childNode);
214 }
215 childNode = childNode.getNextSibling();
216 }
217
218 if (!getNotificationNodes().isEmpty()) {
219 return true;
220 }
221 return false;
222 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530223}