blob: 5c84ddc6d6ffc15dd87a8de5611bc8683e4f2126 [file] [log] [blame]
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +05303 *
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
17package org.onosproject.yangutils.translator.tojava.javamodel;
18
19import java.io.IOException;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053021import org.onosproject.yangutils.datamodel.YangOutput;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053023import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
24import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053025import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053026import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053027
28import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswalcad0e652016-05-26 23:48:38 +053029import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfAugmentableNode;
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053030
31/**
Bharat saraswald9822e92016-04-05 15:13:44 +053032 * Represents output information extended to support java code generation.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053033 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053034public class YangJavaOutput
35 extends YangOutput
36 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053037
38 /**
39 * Contains information of the java file being generated.
40 */
41 private JavaFileInfo javaFileInfo;
42
43 /**
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053044 * File handle to maintain temporary java code fragments as per the code
45 * snippet types.
46 */
47 private TempJavaCodeFragmentFiles tempFileHandle;
48
Bharat saraswalcad0e652016-05-26 23:48:38 +053049 private boolean codeGenFlag;
50
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053051 /**
52 * Creates an instance of java output.
53 */
54 public YangJavaOutput() {
55 super();
56 setJavaFileInfo(new JavaFileInfo());
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053057 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
58 }
59
60 /**
61 * Returns the generated java file information.
62 *
63 * @return generated java file information
64 */
65 @Override
66 public JavaFileInfo getJavaFileInfo() {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053067 if (javaFileInfo == null) {
Gaurav Agrawal56527662016-04-20 15:49:17 +053068 throw new TranslatorException("missing java info in java datamodel node");
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053069 }
70 return javaFileInfo;
71 }
72
73 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053074 * Sets the java file info object.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053075 *
76 * @param javaInfo java file info object
77 */
78 @Override
79 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053080 javaFileInfo = javaInfo;
81 }
82
83 /**
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053084 * Returns the temporary file handle.
85 *
86 * @return temporary file handle
87 */
88 @Override
89 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053090 return tempFileHandle;
91 }
92
93 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Sets temporary file handle.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +053095 *
96 * @param fileHandle temporary file handle
97 */
98 @Override
99 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530100 tempFileHandle = fileHandle;
101 }
102
103 /**
104 * Prepare the information for java code generation corresponding to YANG
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530105 * output info.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530106 *
janani bde4ffab2016-04-15 16:18:30 +0530107 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530108 * @throws TranslatorException translator operation fail
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530109 */
110 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530111 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
112 try {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530113 generateCodeOfAugmentableNode(this, yangPlugin);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530114 } catch (IOException e) {
115 throw new TranslatorException(
116 "Failed to prepare generate code entry for output node " + this.getName());
117 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530119 }
120
121 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530122 * Creates a java file using the YANG output info.
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530123 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530124 * @throws TranslatorException translator operation fail
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530125 */
126 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530127 public void generateCodeExit() throws TranslatorException {
128 try {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530129 if (isCodeGenFlag()) {
130 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
131 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530132 } catch (IOException e) {
133 throw new TranslatorException(
134 "Failed to prepare generate code exit for output node " + this.getName());
135 }
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530136 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530137
138 /**
139 * Returns code generator flag.
140 *
141 * @return code generator flag
142 */
143 public boolean isCodeGenFlag() {
144 return codeGenFlag;
145 }
146
147 /**
148 * Sets code generator flag.
149 *
150 * @param codeGenFlag code generator flag
151 */
152 public void setCodeGenFlag(boolean codeGenFlag) {
153 this.codeGenFlag = codeGenFlag;
154 }
Gaurav Agrawale3ed0d92016-03-23 19:04:17 +0530155}