blob: 5c3a144340d8dd52bee6f2f2d26de657dd061c24 [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;
21import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
22import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053023import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
24import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
25
26import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Gaurav Agrawal56527662016-04-20 15:49:17 +053027import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053028
29/**
30 * Represents union information extended to support java code generation.
31 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053032public class YangJavaUnion
33 extends YangUnion
34 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Gaurav Agrawal338735b2016-04-18 18:53:11 +053035
36 /**
37 * Contains the information of the java file being generated.
38 */
39 private JavaFileInfo javaFileInfo;
40
41 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053042 * File handle to maintain temporary java code fragments as per the code
43 * snippet types.
44 */
45 private TempJavaCodeFragmentFiles tempFileHandle;
46
47 /**
48 * Creates an instance of YANG java union.
49 */
50 public YangJavaUnion() {
51 super();
52 setJavaFileInfo(new JavaFileInfo());
Gaurav Agrawal338735b2016-04-18 18:53:11 +053053 getJavaFileInfo().setGeneratedFileTypes(GENERATE_UNION_CLASS);
54 }
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 RuntimeException("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 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +053080 * Returns the temporary file handle.
81 *
82 * @return temporary file handle
83 */
84 @Override
85 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
86 if (tempFileHandle == null) {
87 throw new RuntimeException("Missing temp file hand for current node "
88 + getJavaFileInfo().getJavaName());
89 }
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
108 * @throws IOException IO operations fails
109 */
110 @Override
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530111 public void generateCodeEntry(YangPluginConfig yangPlugin)
112 throws IOException {
Gaurav Agrawal56527662016-04-20 15:49:17 +0530113 generateCodeOfNode(this, yangPlugin);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530114 }
115
116 /**
117 * Creates a java file using the YANG union info.
118 *
119 * @throws IOException IO operations fails
120 */
121 @Override
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530122 public void generateCodeExit()
123 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530124 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_UNION_CLASS, this);
125 }
126}