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 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 18 | import java.io.IOException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 22 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
| 23 | import org.onosproject.yangutils.translator.tojava.JavaImportData; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 26 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 29 | |
| 30 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 31 | * Represents type define information extended to support java code generation. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 32 | */ |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 33 | public class YangJavaTypeDef extends YangTypeDef implements JavaCodeGeneratorInfo, JavaCodeGenerator { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Contains the information of the java file being generated. |
| 37 | */ |
| 38 | private JavaFileInfo javaFileInfo; |
| 39 | |
| 40 | /** |
| 41 | * Contains information of the imports to be inserted in the java file |
| 42 | * generated. |
| 43 | */ |
| 44 | private JavaImportData javaImportData; |
| 45 | |
| 46 | /** |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 47 | * File handle to maintain temporary java code fragments as per the code |
| 48 | * snippet types. |
| 49 | */ |
| 50 | private TempJavaCodeFragmentFiles tempFileHandle; |
| 51 | |
| 52 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 53 | * Creates a YANG java typedef object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 54 | */ |
| 55 | public YangJavaTypeDef() { |
| 56 | super(); |
| 57 | setJavaFileInfo(new JavaFileInfo()); |
| 58 | setJavaImportData(new JavaImportData()); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 59 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_TYPEDEF_CLASS); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 63 | * Returns the generated java file information. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 64 | * |
| 65 | * @return generated java file information |
| 66 | */ |
| 67 | @Override |
| 68 | public JavaFileInfo getJavaFileInfo() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 69 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 70 | if (javaFileInfo == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 71 | throw new TranslatorException("Missing java info in java datamodel node"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 72 | } |
| 73 | return javaFileInfo; |
| 74 | } |
| 75 | |
| 76 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 77 | * Sets the java file info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 78 | * |
| 79 | * @param javaInfo java file info object |
| 80 | */ |
| 81 | @Override |
| 82 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 83 | javaFileInfo = javaInfo; |
| 84 | } |
| 85 | |
| 86 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 87 | * Returns the data of java imports to be included in generated file. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 88 | * |
| 89 | * @return data of java imports to be included in generated file |
| 90 | */ |
| 91 | @Override |
| 92 | public JavaImportData getJavaImportData() { |
| 93 | return javaImportData; |
| 94 | } |
| 95 | |
| 96 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 97 | * Sets the data of java imports to be included in generated file. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 98 | * |
| 99 | * @param javaImportData data of java imports to be included in generated |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 100 | * file |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 101 | */ |
| 102 | @Override |
| 103 | public void setJavaImportData(JavaImportData javaImportData) { |
| 104 | this.javaImportData = javaImportData; |
| 105 | } |
| 106 | |
| 107 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 108 | * Returns the temporary file handle. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 109 | * |
| 110 | * @return temporary file handle |
| 111 | */ |
| 112 | @Override |
| 113 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 114 | return tempFileHandle; |
| 115 | } |
| 116 | |
| 117 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 118 | * Sets temporary file handle. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 119 | * |
| 120 | * @param fileHandle temporary file handle |
| 121 | */ |
| 122 | @Override |
| 123 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 124 | tempFileHandle = fileHandle; |
| 125 | } |
| 126 | |
| 127 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 128 | * Prepare the information for java code generation corresponding to YANG |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 129 | * typedef info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 130 | * |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 131 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 132 | * @throws IOException IO operations fails |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 133 | */ |
| 134 | @Override |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 135 | public void generateCodeEntry(YangPluginConfig yangPlugin) throws IOException { |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 136 | generateCodeOfNode(this, yangPlugin, false); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 140 | * Create a java file using the YANG typedef info. |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 141 | * |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 142 | * @throws IOException IO operations fails |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 143 | */ |
| 144 | @Override |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 145 | public void generateCodeExit() throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 146 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_TYPEDEF_CLASS, this); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | } |