blob: aed817917bb5e55417c50244f539eddd76f30159 [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;
27import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053028import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053029import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar S38046502016-03-23 15:30:27 +053030
Bharat saraswal33dfa012016-05-17 19:59:16 +053031import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
32import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
33import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S38046502016-03-23 15:30:27 +053035import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053036import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfRootNode;
Bharat saraswalc0e04842016-05-12 13:16:57 +053037import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053038
39/**
Bharat saraswald9822e92016-04-05 15:13:44 +053040 * Represents module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053041 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042public class YangJavaModule
43 extends YangModule
44 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053045
46 /**
47 * Contains the information of the java file being generated.
48 */
49 private JavaFileInfo javaFileInfo;
50
51 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053052 * File handle to maintain temporary java code fragments as per the code
53 * snippet types.
54 */
55 private TempJavaCodeFragmentFiles tempFileHandle;
56
57 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053058 * List of notifications nodes.
59 */
60 private List<YangNode> notificationNodes;
61
62 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053063 * Creates a YANG node of module type.
Vinod Kumar S38046502016-03-23 15:30:27 +053064 */
65 public YangJavaModule() {
66 super();
67 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053068 setNotificationNodes(new ArrayList<>());
69 int gentype = GENERATE_SERVICE_AND_MANAGER;
70 if (isNotificationChildNodePresent(this)) {
71 gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_EVENT_SUBJECT_CLASS | GENERATE_EVENT_CLASS
72 | GENERATE_EVENT_LISTENER_INTERFACE;
73 }
74 getJavaFileInfo().setGeneratedFileTypes(gentype);
75
Vinod Kumar S38046502016-03-23 15:30:27 +053076 }
77
78 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053079 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053080 *
81 * @return generated java file information
82 */
83 @Override
84 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053085 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053086 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053087 }
88 return javaFileInfo;
89 }
90
91 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053092 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053093 *
94 * @param javaInfo java file info object
95 */
96 @Override
97 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053098 javaFileInfo = javaInfo;
99 }
100
101 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530102 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530103 *
104 * @return temporary file handle
105 */
106 @Override
107 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +0530108 return tempFileHandle;
109 }
110
111 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530112 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +0530113 *
114 * @param fileHandle temporary file handle
115 */
116 @Override
117 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530118 tempFileHandle = fileHandle;
119 }
120
121 /**
122 * Generates java code for module.
123 *
janani bde4ffab2016-04-15 16:18:30 +0530124 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530125 * @throws TranslatorException when fails to generate the source files
Vinod Kumar S38046502016-03-23 15:30:27 +0530126 */
127 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530128 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
janani bdd1314f2016-05-19 17:39:50 +0530129 String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate(),
130 yangPlugin.getConflictResolver());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530131 try {
132 generateCodeOfRootNode(this, yangPlugin, modulePkg);
133 } catch (IOException e) {
134 throw new TranslatorException(
135 "Failed to prepare generate code entry for module node " + this.getName());
136 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530137 }
138
Vidyashree Rama74453712016-04-18 12:29:39 +0530139 /**
140 * Creates a java file using the YANG module info.
141 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530142 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530143 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530144 /**
145 * As part of the notification support the following files needs to be generated.
146 * 1) Subject of the notification(event), this is simple interface with builder class.
147 * 2) Event class extending "AbstractEvent" and defining event type enum.
148 * 3) Event listener interface extending "EventListener".
149 * 4) Event subject class.
150 *
151 * The manager class needs to extend the "ListenerRegistry".
152 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530153 try {
154 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
155 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
156 getJavaFileInfo().getPackageFilePath());
157 } catch (IOException e) {
158 throw new TranslatorException("Failed to generate code for module node " + this.getName());
159 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530160 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530161
162 /**
163 * Returns notifications node list.
164 *
165 * @return notification nodes
166 */
167 public List<YangNode> getNotificationNodes() {
168 return notificationNodes;
169 }
170
171 /**
172 * Sets notifications list.
173 *
174 * @param notificationNodes notification list
175 */
176 private void setNotificationNodes(List<YangNode> notificationNodes) {
177 this.notificationNodes = notificationNodes;
178 }
179
180 /**
181 * Adds to notification node list.
182 *
183 * @param curNode notification node
184 */
185 private void addToNotificaitonList(YangNode curNode) {
186 getNotificationNodes().add(curNode);
187 }
188
189 /**
190 * Checks if there is any rpc defined in the module or sub-module.
191 *
192 * @param rootNode root node of the data model
193 * @return status of rpc's existence
194 */
195 public boolean isNotificationChildNodePresent(YangNode rootNode) {
196 YangNode childNode = rootNode.getChild();
197
198 while (childNode != null) {
199 if (childNode instanceof YangNotification) {
200 addToNotificaitonList(childNode);
201 }
202 childNode = childNode.getNextSibling();
203 }
204
205 if (!getNotificationNodes().isEmpty()) {
206 return true;
207 }
208 return false;
209 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530210}