Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.yangutils.translator.tojava.javamodel; |
| 17 | |
| 18 | import java.io.IOException; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 21 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangModule; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | import org.onosproject.yangutils.datamodel.YangNotification; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 27 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 30 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 31 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS; |
| 32 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE; |
| 33 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfRootNode; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 38 | |
| 39 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 40 | * Represents module information extended to support java code generation. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 41 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 42 | public class YangJavaModule |
| 43 | extends YangModule |
| 44 | implements JavaCodeGeneratorInfo, JavaCodeGenerator { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Contains the information of the java file being generated. |
| 48 | */ |
| 49 | private JavaFileInfo javaFileInfo; |
| 50 | |
| 51 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 52 | * File handle to maintain temporary java code fragments as per the code |
| 53 | * snippet types. |
| 54 | */ |
| 55 | private TempJavaCodeFragmentFiles tempFileHandle; |
| 56 | |
| 57 | /** |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 58 | * List of notifications nodes. |
| 59 | */ |
| 60 | private List<YangNode> notificationNodes; |
| 61 | |
| 62 | /** |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 63 | * Creates a YANG node of module type. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 64 | */ |
| 65 | public YangJavaModule() { |
| 66 | super(); |
| 67 | setJavaFileInfo(new JavaFileInfo()); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 68 | 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 79 | * Returns the generated java file information. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 80 | * |
| 81 | * @return generated java file information |
| 82 | */ |
| 83 | @Override |
| 84 | public JavaFileInfo getJavaFileInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 85 | if (javaFileInfo == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 86 | throw new TranslatorException("Missing java info in java datamodel node"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 87 | } |
| 88 | return javaFileInfo; |
| 89 | } |
| 90 | |
| 91 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 92 | * Sets the java file info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 93 | * |
| 94 | * @param javaInfo java file info object |
| 95 | */ |
| 96 | @Override |
| 97 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 98 | javaFileInfo = javaInfo; |
| 99 | } |
| 100 | |
| 101 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 102 | * Returns the temporary file handle. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 103 | * |
| 104 | * @return temporary file handle |
| 105 | */ |
| 106 | @Override |
| 107 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 108 | return tempFileHandle; |
| 109 | } |
| 110 | |
| 111 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 112 | * Sets temporary file handle. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 113 | * |
| 114 | * @param fileHandle temporary file handle |
| 115 | */ |
| 116 | @Override |
| 117 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 118 | tempFileHandle = fileHandle; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Generates java code for module. |
| 123 | * |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 124 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 125 | * @throws TranslatorException when fails to generate the source files |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 126 | */ |
| 127 | @Override |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 128 | public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 129 | String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 130 | try { |
| 131 | generateCodeOfRootNode(this, yangPlugin, modulePkg); |
| 132 | } catch (IOException e) { |
| 133 | throw new TranslatorException( |
| 134 | "Failed to prepare generate code entry for module node " + this.getName()); |
| 135 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 136 | } |
| 137 | |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 138 | /** |
| 139 | * Creates a java file using the YANG module info. |
| 140 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 141 | @Override |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 142 | public void generateCodeExit() throws TranslatorException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 143 | /** |
| 144 | * As part of the notification support the following files needs to be generated. |
| 145 | * 1) Subject of the notification(event), this is simple interface with builder class. |
| 146 | * 2) Event class extending "AbstractEvent" and defining event type enum. |
| 147 | * 3) Event listener interface extending "EventListener". |
| 148 | * 4) Event subject class. |
| 149 | * |
| 150 | * The manager class needs to extend the "ListenerRegistry". |
| 151 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 152 | try { |
| 153 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this); |
| 154 | searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() + |
| 155 | getJavaFileInfo().getPackageFilePath()); |
| 156 | } catch (IOException e) { |
| 157 | throw new TranslatorException("Failed to generate code for module node " + this.getName()); |
| 158 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 159 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame^] | 160 | |
| 161 | /** |
| 162 | * Returns notifications node list. |
| 163 | * |
| 164 | * @return notification nodes |
| 165 | */ |
| 166 | public List<YangNode> getNotificationNodes() { |
| 167 | return notificationNodes; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Sets notifications list. |
| 172 | * |
| 173 | * @param notificationNodes notification list |
| 174 | */ |
| 175 | private void setNotificationNodes(List<YangNode> notificationNodes) { |
| 176 | this.notificationNodes = notificationNodes; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Adds to notification node list. |
| 181 | * |
| 182 | * @param curNode notification node |
| 183 | */ |
| 184 | private void addToNotificaitonList(YangNode curNode) { |
| 185 | getNotificationNodes().add(curNode); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Checks if there is any rpc defined in the module or sub-module. |
| 190 | * |
| 191 | * @param rootNode root node of the data model |
| 192 | * @return status of rpc's existence |
| 193 | */ |
| 194 | public boolean isNotificationChildNodePresent(YangNode rootNode) { |
| 195 | YangNode childNode = rootNode.getChild(); |
| 196 | |
| 197 | while (childNode != null) { |
| 198 | if (childNode instanceof YangNotification) { |
| 199 | addToNotificaitonList(childNode); |
| 200 | } |
| 201 | childNode = childNode.getNextSibling(); |
| 202 | } |
| 203 | |
| 204 | if (!getNotificationNodes().isEmpty()) { |
| 205 | return true; |
| 206 | } |
| 207 | return false; |
| 208 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 209 | } |