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; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 19 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangCase; |
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; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +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 | |
| 27 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfAugmentableNode; |
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 case information extended to support java code generation. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 32 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 33 | public class YangJavaCase |
| 34 | extends YangCase |
| 35 | implements JavaCodeGeneratorInfo, JavaCodeGenerator { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 36 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 37 | private static final long serialVersionUID = 806201631L; |
| 38 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 39 | /** |
| 40 | * Contains the information of the java file being generated. |
| 41 | */ |
| 42 | private JavaFileInfo javaFileInfo; |
| 43 | |
| 44 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 45 | * File handle to maintain temporary java code fragments as per the code |
| 46 | * snippet types. |
| 47 | */ |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 48 | private transient TempJavaCodeFragmentFiles tempFileHandle; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 49 | |
| 50 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 51 | * Creates YANG java case object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 52 | */ |
| 53 | public YangJavaCase() { |
| 54 | super(); |
| 55 | setJavaFileInfo(new JavaFileInfo()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 56 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER); |
| 57 | } |
| 58 | |
| 59 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 60 | * Returns the generated java file information. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 61 | * |
| 62 | * @return generated java file information |
| 63 | */ |
| 64 | @Override |
| 65 | public JavaFileInfo getJavaFileInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 66 | if (javaFileInfo == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 67 | throw new TranslatorException("Missing java info in java datamodel node"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 68 | } |
| 69 | return javaFileInfo; |
| 70 | } |
| 71 | |
| 72 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 73 | * Sets the java file info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 74 | * |
| 75 | * @param javaInfo java file info object |
| 76 | */ |
| 77 | @Override |
| 78 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 79 | javaFileInfo = javaInfo; |
| 80 | } |
| 81 | |
| 82 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 83 | * Returns the temporary file handle. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 84 | * |
| 85 | * @return temporary file handle |
| 86 | */ |
| 87 | @Override |
| 88 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 89 | return tempFileHandle; |
| 90 | } |
| 91 | |
| 92 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 93 | * Sets temporary file handle. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 94 | * |
| 95 | * @param fileHandle temporary file handle |
| 96 | */ |
| 97 | @Override |
| 98 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 99 | tempFileHandle = fileHandle; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Prepare the information for java code generation corresponding to YANG |
| 104 | * case info. |
| 105 | * |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 106 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 107 | * @throws TranslatorException translator operation fail |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 108 | */ |
| 109 | @Override |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 110 | public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException { |
| 111 | try { |
| 112 | generateCodeOfAugmentableNode(this, yangPlugin); |
| 113 | } catch (IOException e) { |
| 114 | throw new TranslatorException( |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 115 | "Failed to prepare generate code entry for case node " + getName()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 116 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 120 | * Creates a java file using the YANG case info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 121 | */ |
| 122 | @Override |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 123 | public void generateCodeExit() throws TranslatorException { |
| 124 | try { |
| 125 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this); |
| 126 | } catch (IOException e) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 127 | throw new TranslatorException("Failed to generate code for case node " + getName()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 128 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 129 | } |
| 130 | } |