blob: b53ec9208e21257e22217205235e4c3d8bca49e9 [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;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053035import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S38046502016-03-23 15:30:27 +053036import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053037import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.generateCodeOfRootNode;
Bharat saraswalc0e04842016-05-12 13:16:57 +053038import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053039
40/**
Bharat saraswald9822e92016-04-05 15:13:44 +053041 * Represents module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053042 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053043public class YangJavaModule
44 extends YangModule
45 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053046
Bharat saraswal96dfef02016-06-16 00:29:12 +053047 private static final long serialVersionUID = 806201625L;
48
Vinod Kumar S38046502016-03-23 15:30:27 +053049 /**
50 * Contains the information of the java file being generated.
51 */
52 private JavaFileInfo javaFileInfo;
53
54 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053055 * File handle to maintain temporary java code fragments as per the code
56 * snippet types.
57 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053058 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053059
60 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053061 * List of notifications nodes.
62 */
63 private List<YangNode> notificationNodes;
64
65 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053066 * Creates a YANG node of module type.
Vinod Kumar S38046502016-03-23 15:30:27 +053067 */
68 public YangJavaModule() {
69 super();
70 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053071 setNotificationNodes(new ArrayList<>());
72 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);
78
Vinod Kumar S38046502016-03-23 15:30:27 +053079 }
80
81 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053082 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053083 *
84 * @return generated java file information
85 */
86 @Override
87 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053088 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053089 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053090 }
91 return javaFileInfo;
92 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053096 *
97 * @param javaInfo java file info object
98 */
99 @Override
100 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530101 javaFileInfo = javaInfo;
102 }
103
104 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530105 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 *
107 * @return temporary file handle
108 */
109 @Override
110 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530111 return tempFileHandle;
112 }
113
114 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530115 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530116 *
117 * @param fileHandle temporary file handle
118 */
119 @Override
120 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 tempFileHandle = fileHandle;
122 }
123
124 /**
125 * Generates java code for module.
126 *
janani bde4ffab2016-04-15 16:18:30 +0530127 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530128 * @throws TranslatorException when fails to generate the source files
Vinod Kumar S38046502016-03-23 15:30:27 +0530129 */
130 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530131 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
janani bdd1314f2016-05-19 17:39:50 +0530132 String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate(),
133 yangPlugin.getConflictResolver());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530134 try {
135 generateCodeOfRootNode(this, yangPlugin, modulePkg);
136 } catch (IOException e) {
137 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530138 "Failed to prepare generate code entry for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530139 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530140 }
141
Vidyashree Rama74453712016-04-18 12:29:39 +0530142 /**
143 * Creates a java file using the YANG module info.
144 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530145 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530146 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530147 /**
148 * As part of the notification support the following files needs to be generated.
149 * 1) Subject of the notification(event), this is simple interface with builder class.
150 * 2) Event class extending "AbstractEvent" and defining event type enum.
151 * 3) Event listener interface extending "EventListener".
152 * 4) Event subject class.
153 *
154 * The manager class needs to extend the "ListenerRegistry".
155 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530156 try {
157 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
158 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
159 getJavaFileInfo().getPackageFilePath());
160 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530161 throw new TranslatorException("Failed to generate code for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530162 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530163 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530164
165 /**
166 * Returns notifications node list.
167 *
168 * @return notification nodes
169 */
170 public List<YangNode> getNotificationNodes() {
171 return notificationNodes;
172 }
173
174 /**
175 * Sets notifications list.
176 *
177 * @param notificationNodes notification list
178 */
179 private void setNotificationNodes(List<YangNode> notificationNodes) {
180 this.notificationNodes = notificationNodes;
181 }
182
183 /**
184 * Adds to notification node list.
185 *
186 * @param curNode notification node
187 */
188 private void addToNotificaitonList(YangNode curNode) {
189 getNotificationNodes().add(curNode);
190 }
191
192 /**
193 * Checks if there is any rpc defined in the module or sub-module.
194 *
195 * @param rootNode root node of the data model
196 * @return status of rpc's existence
197 */
198 public boolean isNotificationChildNodePresent(YangNode rootNode) {
199 YangNode childNode = rootNode.getChild();
200
201 while (childNode != null) {
202 if (childNode instanceof YangNotification) {
203 addToNotificaitonList(childNode);
204 }
205 childNode = childNode.getNextSibling();
206 }
207
208 if (!getNotificationNodes().isEmpty()) {
209 return true;
210 }
211 return false;
212 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530213}