blob: 1413f9ca2418614d4dceebac43edb6b20e6c7749 [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
Bharat saraswal96dfef02016-06-16 00:29:12 +053046 private static final long serialVersionUID = 806201625L;
47
Vinod Kumar S38046502016-03-23 15:30:27 +053048 /**
49 * Contains the information of the java file being generated.
50 */
51 private JavaFileInfo javaFileInfo;
52
53 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053054 * File handle to maintain temporary java code fragments as per the code
55 * snippet types.
56 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053057 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053058
59 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053060 * List of notifications nodes.
61 */
62 private List<YangNode> notificationNodes;
63
64 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053065 * Creates a YANG node of module type.
Vinod Kumar S38046502016-03-23 15:30:27 +053066 */
67 public YangJavaModule() {
68 super();
69 setJavaFileInfo(new JavaFileInfo());
Bharat saraswal33dfa012016-05-17 19:59:16 +053070 setNotificationNodes(new ArrayList<>());
71 int gentype = GENERATE_SERVICE_AND_MANAGER;
72 if (isNotificationChildNodePresent(this)) {
73 gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_EVENT_SUBJECT_CLASS | GENERATE_EVENT_CLASS
74 | GENERATE_EVENT_LISTENER_INTERFACE;
75 }
76 getJavaFileInfo().setGeneratedFileTypes(gentype);
77
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() {
Vinod Kumar S38046502016-03-23 15:30:27 +053087 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) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 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) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 tempFileHandle = fileHandle;
121 }
122
123 /**
124 * Generates java code for module.
125 *
janani bde4ffab2016-04-15 16:18:30 +0530126 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530127 * @throws TranslatorException when fails to generate the source files
Vinod Kumar S38046502016-03-23 15:30:27 +0530128 */
129 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530130 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
janani bdd1314f2016-05-19 17:39:50 +0530131 String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate(),
132 yangPlugin.getConflictResolver());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530133 try {
134 generateCodeOfRootNode(this, yangPlugin, modulePkg);
135 } catch (IOException e) {
136 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530137 "Failed to prepare generate code entry for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530138 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530139 }
140
Vidyashree Rama74453712016-04-18 12:29:39 +0530141 /**
142 * Creates a java file using the YANG module info.
143 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530144 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530145 public void generateCodeExit() throws TranslatorException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530146 /**
147 * As part of the notification support the following files needs to be generated.
148 * 1) Subject of the notification(event), this is simple interface with builder class.
149 * 2) Event class extending "AbstractEvent" and defining event type enum.
150 * 3) Event listener interface extending "EventListener".
151 * 4) Event subject class.
152 *
153 * The manager class needs to extend the "ListenerRegistry".
154 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530155 try {
156 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
157 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
158 getJavaFileInfo().getPackageFilePath());
159 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530160 throw new TranslatorException("Failed to generate code for module node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530161 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530162 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530163
164 /**
165 * Returns notifications node list.
166 *
167 * @return notification nodes
168 */
169 public List<YangNode> getNotificationNodes() {
170 return notificationNodes;
171 }
172
173 /**
174 * Sets notifications list.
175 *
176 * @param notificationNodes notification list
177 */
178 private void setNotificationNodes(List<YangNode> notificationNodes) {
179 this.notificationNodes = notificationNodes;
180 }
181
182 /**
183 * Adds to notification node list.
184 *
185 * @param curNode notification node
186 */
187 private void addToNotificaitonList(YangNode curNode) {
188 getNotificationNodes().add(curNode);
189 }
190
191 /**
192 * Checks if there is any rpc defined in the module or sub-module.
193 *
194 * @param rootNode root node of the data model
195 * @return status of rpc's existence
196 */
197 public boolean isNotificationChildNodePresent(YangNode rootNode) {
198 YangNode childNode = rootNode.getChild();
199
200 while (childNode != null) {
201 if (childNode instanceof YangNotification) {
202 addToNotificaitonList(childNode);
203 }
204 childNode = childNode.getNextSibling();
205 }
206
207 if (!getNotificationNodes().isEmpty()) {
208 return true;
209 }
210 return false;
211 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530212}