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