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