Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangUnion; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 23 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 25 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
| 26 | |
| 27 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS; |
Gaurav Agrawal | 02a60de | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Represents union information extended to support java code generation. |
| 32 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 33 | public class YangJavaUnion |
| 34 | extends YangUnion |
| 35 | implements JavaCodeGeneratorInfo, JavaCodeGenerator { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 36 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 37 | private static final long serialVersionUID = 806201619L; |
| 38 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 39 | /** |
| 40 | * Contains the information of the java file being generated. |
| 41 | */ |
| 42 | private JavaFileInfo javaFileInfo; |
| 43 | |
| 44 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +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; |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Creates an instance of YANG java union. |
| 52 | */ |
| 53 | public YangJavaUnion() { |
| 54 | super(); |
| 55 | setJavaFileInfo(new JavaFileInfo()); |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 56 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_UNION_CLASS); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the generated java file information. |
| 61 | * |
| 62 | * @return generated java file information |
| 63 | */ |
| 64 | @Override |
| 65 | public JavaFileInfo getJavaFileInfo() { |
| 66 | if (javaFileInfo == null) { |
| 67 | throw new RuntimeException("Missing java info in java datamodel node"); |
| 68 | } |
| 69 | return javaFileInfo; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets the java file info object. |
| 74 | * |
| 75 | * @param javaInfo java file info object |
| 76 | */ |
| 77 | @Override |
| 78 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 79 | javaFileInfo = javaInfo; |
| 80 | } |
| 81 | |
| 82 | /** |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 83 | * Returns the temporary file handle. |
| 84 | * |
| 85 | * @return temporary file handle |
| 86 | */ |
| 87 | @Override |
| 88 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 89 | return tempFileHandle; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets temporary file handle. |
| 94 | * |
| 95 | * @param fileHandle temporary file handle |
| 96 | */ |
| 97 | @Override |
| 98 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 99 | tempFileHandle = fileHandle; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Prepare the information for java code generation corresponding to YANG |
| 104 | * union info. |
| 105 | * |
| 106 | * @param yangPlugin YANG plugin config |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 107 | * @throws TranslatorException when fails to translate |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +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 | generateCodeOfNode(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 union node " + getName()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 116 | } |
| 117 | |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Creates a java file using the YANG union info. |
| 122 | * |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 123 | * @throws TranslatorException when fails to translate |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 124 | */ |
| 125 | @Override |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 126 | public void generateCodeExit() throws TranslatorException { |
| 127 | try { |
| 128 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_UNION_CLASS, this); |
| 129 | } catch (IOException e) { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 130 | throw new TranslatorException("Failed to generate code for union node " + getName()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 131 | } |
Gaurav Agrawal | 97a5e1c | 2016-04-18 18:53:11 +0530 | [diff] [blame] | 132 | } |
| 133 | } |