blob: 0dca9a722b44d500325536fd4e44531ba299d316 [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;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053023import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053024import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053025import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053026import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053027
28import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Bharat saraswalb551aae2016-07-14 15:18:20 +053029import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeOfNode;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053030
31/**
32 * Represents union information extended to support java code generation.
33 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053034public class YangJavaUnion
35 extends YangUnion
36 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Gaurav Agrawal338735b2016-04-18 18:53:11 +053037
Bharat saraswal96dfef02016-06-16 00:29:12 +053038 private static final long serialVersionUID = 806201619L;
39
Gaurav Agrawal338735b2016-04-18 18:53:11 +053040 /**
41 * Contains the information of the java file being generated.
42 */
43 private JavaFileInfo javaFileInfo;
44
45 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053046 * File handle to maintain temporary java code fragments as per the code
47 * snippet types.
48 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053049 private transient TempJavaCodeFragmentFiles tempFileHandle;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053050
51 /**
52 * Creates an instance of YANG java union.
53 */
54 public YangJavaUnion() {
55 super();
56 setJavaFileInfo(new JavaFileInfo());
Gaurav Agrawal338735b2016-04-18 18:53:11 +053057 getJavaFileInfo().setGeneratedFileTypes(GENERATE_UNION_CLASS);
58 }
59
60 /**
61 * Returns the generated java file information.
62 *
63 * @return generated java file information
64 */
65 @Override
66 public JavaFileInfo getJavaFileInfo() {
67 if (javaFileInfo == null) {
68 throw new RuntimeException("Missing java info in java datamodel node");
69 }
70 return javaFileInfo;
71 }
72
73 /**
74 * Sets the java file info object.
75 *
76 * @param javaInfo java file info object
77 */
78 @Override
79 public void setJavaFileInfo(JavaFileInfo javaInfo) {
80 javaFileInfo = javaInfo;
81 }
82
83 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053084 * Returns the temporary file handle.
85 *
86 * @return temporary file handle
87 */
88 @Override
89 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Gaurav Agrawal338735b2016-04-18 18:53:11 +053090 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 saraswalab4c6ba2016-05-17 14:19:38 +0530108 * @throws TranslatorException when fails to translate
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530109 */
110 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530111 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
112 try {
113 generateCodeOfNode(this, yangPlugin);
114 } catch (IOException e) {
115 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530116 "Failed to prepare generate code entry for union node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530117 }
118
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530119 }
120
121 /**
122 * Creates a java file using the YANG union info.
123 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530124 * @throws TranslatorException when fails to translate
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530125 */
126 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530127 public void generateCodeExit() throws TranslatorException {
128 try {
129 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_UNION_CLASS, this);
130 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530131 throw new TranslatorException("Failed to generate code for union node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530132 }
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530133 }
134}