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