blob: 449820431b7f64da6781c5c252740fa5d626f294 [file] [log] [blame]
Gaurav Agrawal338735b2016-04-18 18:53:11 +05301/*
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 */
16package org.onosproject.yangutils.translator.tojava.javamodel;
17
18import java.io.IOException;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053019
Gaurav Agrawal338735b2016-04-18 18:53:11 +053020import org.onosproject.yangutils.datamodel.YangUnion;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053022import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
23import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053024import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
25import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
26
27import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Gaurav Agrawal56527662016-04-20 15:49:17 +053028import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053029
30/**
31 * Represents union information extended to support java code generation.
32 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053033public class YangJavaUnion
34 extends YangUnion
35 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Gaurav Agrawal338735b2016-04-18 18:53:11 +053036
37 /**
38 * Contains the information of the java file being generated.
39 */
40 private JavaFileInfo javaFileInfo;
41
42 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053043 * File handle to maintain temporary java code fragments as per the code
44 * snippet types.
45 */
46 private TempJavaCodeFragmentFiles tempFileHandle;
47
48 /**
49 * Creates an instance of YANG java union.
50 */
51 public YangJavaUnion() {
52 super();
53 setJavaFileInfo(new JavaFileInfo());
Gaurav Agrawal338735b2016-04-18 18:53:11 +053054 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
63 public JavaFileInfo getJavaFileInfo() {
64 if (javaFileInfo == null) {
65 throw new RuntimeException("Missing java info in java datamodel node");
66 }
67 return javaFileInfo;
68 }
69
70 /**
71 * Sets the java file info object.
72 *
73 * @param javaInfo java file info object
74 */
75 @Override
76 public void setJavaFileInfo(JavaFileInfo javaInfo) {
77 javaFileInfo = javaInfo;
78 }
79
80 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053081 * Returns the temporary file handle.
82 *
83 * @return temporary file handle
84 */
85 @Override
86 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Gaurav Agrawal338735b2016-04-18 18:53:11 +053087 return tempFileHandle;
88 }
89
90 /**
91 * Sets temporary file handle.
92 *
93 * @param fileHandle temporary file handle
94 */
95 @Override
96 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
97 tempFileHandle = fileHandle;
98 }
99
100 /**
101 * Prepare the information for java code generation corresponding to YANG
102 * union info.
103 *
104 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530105 * @throws TranslatorException when fails to translate
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530106 */
107 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530108 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
109 try {
110 generateCodeOfNode(this, yangPlugin);
111 } catch (IOException e) {
112 throw new TranslatorException(
113 "Failed to prepare generate code entry for union node " + this.getName());
114 }
115
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530116 }
117
118 /**
119 * Creates a java file using the YANG union info.
120 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530121 * @throws TranslatorException when fails to translate
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530122 */
123 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530124 public void generateCodeExit() throws TranslatorException {
125 try {
126 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_UNION_CLASS, this);
127 } catch (IOException e) {
128 throw new TranslatorException("Failed to generate code for union node " + this.getName());
129 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530130 }
131}