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