blob: d838682a80d8cad3d951df4f14e2772dbc8511fe [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;
29import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053030import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053031import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar S38046502016-03-23 15:30:27 +053032
Bharat saraswal33dfa012016-05-17 19:59:16 +053033import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
34import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
35import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053036import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S38046502016-03-23 15:30:27 +053037import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053038import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfRootNode;
Bharat saraswalc0e04842016-05-12 13:16:57 +053039import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053040
41/**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053042 * Represents sub module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053043 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044public class YangJavaSubModule
45 extends YangSubModule
46 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 = 806201621L;
49
Vinod Kumar S38046502016-03-23 15:30:27 +053050 /**
51 * Contains the information of the java file being generated.
52 */
53 private JavaFileInfo javaFileInfo;
54
55 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053056 * File handle to maintain temporary java code fragments as per the code
57 * snippet types.
58 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053059 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053060
61 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053062 * List of notifications nodes.
63 */
64 private List<YangNode> notificationNodes = new ArrayList<>();
65
66 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053067 * Creates YANG java sub module object.
Vinod Kumar S38046502016-03-23 15:30:27 +053068 */
69 public YangJavaSubModule() {
70 super();
71 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053072 int gentype = GENERATE_SERVICE_AND_MANAGER;
73 if (isNotificationChildNodePresent(this)) {
74 gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_EVENT_SUBJECT_CLASS | GENERATE_EVENT_CLASS
75 | GENERATE_EVENT_LISTENER_INTERFACE;
76 }
77 getJavaFileInfo().setGeneratedFileTypes(gentype);
Vinod Kumar S38046502016-03-23 15:30:27 +053078 }
79
80 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053081 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053082 *
83 * @return generated java file information
84 */
85 @Override
86 public JavaFileInfo getJavaFileInfo() {
87 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053088 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053089 }
90 return javaFileInfo;
91 }
92
93 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053095 *
96 * @param javaInfo java file info object
97 */
98 @Override
99 public void setJavaFileInfo(JavaFileInfo javaInfo) {
100 javaFileInfo = javaInfo;
101 }
102
103 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 *
106 * @return temporary file handle
107 */
108 @Override
109 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 return tempFileHandle;
111 }
112
113 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530114 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530115 *
116 * @param fileHandle temporary file handle
117 */
118 @Override
119 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
120 tempFileHandle = fileHandle;
121 }
122
123 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530124 * Returns the name space of the module to which the sub module belongs to.
Vinod Kumar S38046502016-03-23 15:30:27 +0530125 *
126 * @param belongsToInfo Information of the module to which the sub module
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530127 * belongs
Vinod Kumar S38046502016-03-23 15:30:27 +0530128 * @return the name space string of the module.
129 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530130 public String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530131 return ((YangModule) belongsToInfo.getModuleNode()).getNameSpace().getUri();
Vinod Kumar S38046502016-03-23 15:30:27 +0530132 }
133
134 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530135 * Prepares the information for java code generation corresponding to YANG
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530136 * submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530137 *
janani bde4ffab2016-04-15 16:18:30 +0530138 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530139 * @throws TranslatorException when fails to translate
Vinod Kumar S38046502016-03-23 15:30:27 +0530140 */
141 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530142 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530143 String subModulePkg = getRootPackage(getVersion(), getNameSpaceFromModule(getBelongsTo()),
janani bdd1314f2016-05-19 17:39:50 +0530144 getRevision().getRevDate(), yangPlugin.getConflictResolver());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530145 try {
146 generateCodeOfRootNode(this, yangPlugin, subModulePkg);
147 } catch (IOException e) {
148 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530149 "failed to prepare generate code entry for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530150 }
151
Vinod Kumar S38046502016-03-23 15:30:27 +0530152 }
153
154 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530155 * Creates a java file using the YANG submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530156 */
157 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530158 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530159 /**
160 * As part of the notification support the following files needs to be generated.
161 * 1) Subject of the notification(event), this is simple interface with builder class.
162 * 2) Event class extending "AbstractEvent" and defining event type enum.
163 * 3) Event listener interface extending "EventListener".
164 * 4) Event subject class.
165 *
166 * The manager class needs to extend the "ListenerRegistry".
167 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530168 try {
169 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
170 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
171 getJavaFileInfo().getPackageFilePath());
172 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530173 throw new TranslatorException("Failed to generate code for submodule node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530174 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530175 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530176
177 /**
178 * Returns notifications node list.
179 *
180 * @return notification nodes
181 */
182 public List<YangNode> getNotificationNodes() {
183 return notificationNodes;
184 }
185
186 /**
187 * Adds to notification node list.
188 *
189 * @param curNode notification node
190 */
191 private void addToNotificaitonList(YangNode curNode) {
192 getNotificationNodes().add(curNode);
193 }
194
195 /**
196 * Checks if there is any rpc defined in the module or sub-module.
197 *
198 * @param rootNode root node of the data model
199 * @return status of rpc's existence
200 */
201 public boolean isNotificationChildNodePresent(YangNode rootNode) {
202 YangNode childNode = rootNode.getChild();
203
204 while (childNode != null) {
205 if (childNode instanceof YangNotification) {
206 addToNotificaitonList(childNode);
207 }
208 childNode = childNode.getNextSibling();
209 }
210
211 if (!getNotificationNodes().isEmpty()) {
212 return true;
213 }
214 return false;
215 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530216}