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; |
| 21 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangBelongsTo; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangModule; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.datamodel.YangNotification; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangSubModule; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 33 | |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ALL_EVENT_CLASS_MASK; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeOfRootNode; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isGenerationOfCodeReq; |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.isManagerCodeGenRequired; |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage; |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.SBI; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 43 | |
| 44 | /** |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 45 | * Represents sub module information extended to support java code generation. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 46 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 47 | public class YangJavaSubModule |
| 48 | extends YangSubModule |
| 49 | implements JavaCodeGeneratorInfo, JavaCodeGenerator { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 50 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 51 | private static final long serialVersionUID = 806201621L; |
| 52 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 53 | /** |
| 54 | * Contains the information of the java file being generated. |
| 55 | */ |
| 56 | private JavaFileInfo javaFileInfo; |
| 57 | |
| 58 | /** |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 59 | * File handle to maintain temporary java code fragments as per the code snippet types. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 60 | */ |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 61 | private transient TempJavaCodeFragmentFiles tempFileHandle; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 62 | |
| 63 | /** |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 64 | * List of notifications nodes. |
| 65 | */ |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 66 | private transient List<YangNode> notificationNodes = new ArrayList<>(); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 67 | |
| 68 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 69 | * Creates YANG java sub module object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 70 | */ |
| 71 | public YangJavaSubModule() { |
| 72 | super(); |
| 73 | setJavaFileInfo(new JavaFileInfo()); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 74 | int gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_INTERFACE_WITH_BUILDER; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 75 | if (isNotificationChildNodePresent(this)) { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 76 | gentype = GENERATE_SERVICE_AND_MANAGER | GENERATE_ALL_EVENT_CLASS_MASK; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 77 | } |
| 78 | getJavaFileInfo().setGeneratedFileTypes(gentype); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 82 | * Returns the generated java file information. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 83 | * |
| 84 | * @return generated java file information |
| 85 | */ |
| 86 | @Override |
| 87 | public JavaFileInfo getJavaFileInfo() { |
| 88 | if (javaFileInfo == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 89 | throw new TranslatorException("Missing java info in java datamodel node"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 90 | } |
| 91 | return javaFileInfo; |
| 92 | } |
| 93 | |
| 94 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 95 | * Sets the java file info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 96 | * |
| 97 | * @param javaInfo java file info object |
| 98 | */ |
| 99 | @Override |
| 100 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 101 | javaFileInfo = javaInfo; |
| 102 | } |
| 103 | |
| 104 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 105 | * Returns the temporary file handle. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 106 | * |
| 107 | * @return temporary file handle |
| 108 | */ |
| 109 | @Override |
| 110 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 111 | return tempFileHandle; |
| 112 | } |
| 113 | |
| 114 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 115 | * Sets temporary file handle. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 116 | * |
| 117 | * @param fileHandle temporary file handle |
| 118 | */ |
| 119 | @Override |
| 120 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 121 | tempFileHandle = fileHandle; |
| 122 | } |
| 123 | |
| 124 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 125 | * Returns the name space of the module to which the sub module belongs to. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 126 | * |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 127 | * @param belongsToInfo Information of the module to which the sub module belongs |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 128 | * @return the name space string of the module. |
| 129 | */ |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 130 | public String getNameSpaceFromModule(YangBelongsTo belongsToInfo) { |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 131 | return ((YangModule) belongsToInfo.getModuleNode()).getNameSpace().getUri(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 135 | * Prepares the information for java code generation corresponding to YANG submodule info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 136 | * |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 137 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 138 | * @throws TranslatorException when fails to translate |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 139 | */ |
| 140 | @Override |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 141 | public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 142 | String subModulePkg = getRootPackage(getVersion(), getNameSpaceFromModule(getBelongsTo()), |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 143 | getRevision().getRevDate(), yangPlugin.getConflictResolver()); |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 144 | |
| 145 | if (isNotificationChildNodePresent(this)) { |
| 146 | getJavaFileInfo().setGeneratedFileTypes(getJavaFileInfo().getGeneratedFileTypes() |
| 147 | | GENERATE_ALL_EVENT_CLASS_MASK); |
| 148 | } |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 149 | try { |
| 150 | generateCodeOfRootNode(this, yangPlugin, subModulePkg); |
| 151 | } catch (IOException e) { |
| 152 | throw new TranslatorException( |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 153 | "failed to prepare generate code entry for submodule node " + getName()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 154 | } |
| 155 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 159 | * Creates a java file using the YANG submodule info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 160 | */ |
| 161 | @Override |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 162 | public void generateCodeExit() throws TranslatorException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 163 | /** |
| 164 | * As part of the notification support the following files needs to be generated. |
| 165 | * 1) Subject of the notification(event), this is simple interface with builder class. |
| 166 | * 2) Event class extending "AbstractEvent" and defining event type enum. |
| 167 | * 3) Event listener interface extending "EventListener". |
| 168 | * 4) Event subject class. |
| 169 | * |
| 170 | * The manager class needs to extend the "ListenerRegistry". |
| 171 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 172 | try { |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 173 | if ((getJavaFileInfo().getGeneratedFileTypes() & GENERATE_ALL_EVENT_CLASS_MASK) != 0) { |
| 174 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_ALL_EVENT_CLASS_MASK, this); |
| 175 | } |
| 176 | getTempJavaCodeFragmentFiles() |
| 177 | .generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this); |
| 178 | if (isManagerCodeGenRequired(this)) { |
| 179 | if (isGenerationOfCodeReq(getJavaFileInfo())) { |
| 180 | if ((getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi() == null) |
| 181 | || (!getJavaFileInfo().getPluginConfig().getCodeGenerateForsbi().equals(SBI))) { |
| 182 | getTempJavaCodeFragmentFiles().getServiceTempFiles().setManagerNeedToBeGenerated(true); |
| 183 | } |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 184 | } |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 185 | } |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 186 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this); |
| 187 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 188 | searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() + |
| 189 | getJavaFileInfo().getPackageFilePath()); |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 190 | searchAndDeleteTempDir(getJavaFileInfo().getPluginConfig().getCodeGenDir() + |
| 191 | getJavaFileInfo().getPackageFilePath()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 192 | } catch (IOException e) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 193 | throw new TranslatorException("Failed to generate code for submodule node " + getName()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 194 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 195 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 196 | |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 197 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 198 | /** |
| 199 | * Returns notifications node list. |
| 200 | * |
| 201 | * @return notification nodes |
| 202 | */ |
| 203 | public List<YangNode> getNotificationNodes() { |
| 204 | return notificationNodes; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Adds to notification node list. |
| 209 | * |
| 210 | * @param curNode notification node |
| 211 | */ |
| 212 | private void addToNotificaitonList(YangNode curNode) { |
| 213 | getNotificationNodes().add(curNode); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Checks if there is any rpc defined in the module or sub-module. |
| 218 | * |
| 219 | * @param rootNode root node of the data model |
| 220 | * @return status of rpc's existence |
| 221 | */ |
| 222 | public boolean isNotificationChildNodePresent(YangNode rootNode) { |
| 223 | YangNode childNode = rootNode.getChild(); |
| 224 | |
| 225 | while (childNode != null) { |
| 226 | if (childNode instanceof YangNotification) { |
| 227 | addToNotificaitonList(childNode); |
| 228 | } |
| 229 | childNode = childNode.getNextSibling(); |
| 230 | } |
| 231 | |
| 232 | if (!getNotificationNodes().isEmpty()) { |
| 233 | return true; |
| 234 | } |
| 235 | return false; |
| 236 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 237 | } |