blob: ce04f68c912207e70ecc43db9c30066dc2d8e1fb [file] [log] [blame]
Gaurav Agrawal97a5e1c2016-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 S79a374b2016-04-30 21:09:15 +053019
Shankara-Huaweib7564772016-08-02 18:13:13 +053020import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaUnion;
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +053021import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053023import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053024import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswale3175d32016-08-31 17:50:11 +053025import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053026import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Bharat saraswale3175d32016-08-31 17:50:11 +053027import org.onosproject.yangutils.utils.io.YangPluginConfig;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053028
29import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Bharat saraswalaf413b82016-07-14 15:18:20 +053030import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeOfNode;
Gaurav Agrawald30f79b2016-10-06 00:58:57 +053031import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateJava;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053032
33/**
34 * Represents union information extended to support java code generation.
35 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053036public class YangJavaUnionTranslator
37 extends YangJavaUnion
Vinod Kumar S79a374b2016-04-30 21:09:15 +053038 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053039
Bharat saraswalc2d3be12016-06-16 00:29:12 +053040 private static final long serialVersionUID = 806201619L;
41
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053042 /**
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053043 * File handle to maintain temporary java code fragments as per the code
44 * snippet types.
45 */
Bharat saraswalc2d3be12016-06-16 00:29:12 +053046 private transient TempJavaCodeFragmentFiles tempFileHandle;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053047
48 /**
49 * Creates an instance of YANG java union.
50 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053051 public YangJavaUnionTranslator() {
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053052 super();
Bharat saraswale50edca2016-08-05 01:58:25 +053053 setJavaFileInfo(new JavaFileInfoTranslator());
Gaurav Agrawal97a5e1c2016-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
Bharat saraswale50edca2016-08-05 01:58:25 +053063 public JavaFileInfoTranslator getJavaFileInfo() {
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053064 if (javaFileInfo == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +053065 throw new RuntimeException("Missing java info in java datamodel node " + getName() + " in " +
66 getLineNumber() + " at " +
67 getCharPosition()
68 + " in " + getFileName());
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053069 }
Bharat saraswale50edca2016-08-05 01:58:25 +053070 return (JavaFileInfoTranslator) javaFileInfo;
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053071 }
72
73 /**
74 * Sets the java file info object.
75 *
76 * @param javaInfo java file info object
77 */
78 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053079 public void setJavaFileInfo(JavaFileInfoTranslator javaInfo) {
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +053080 javaFileInfo = javaInfo;
81 }
82
83 /**
Gaurav Agrawal97a5e1c2016-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 Agrawal97a5e1c2016-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 saraswal4aaab4d2016-05-17 14:19:38 +0530108 * @throws TranslatorException when fails to translate
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530109 */
110 @Override
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530111 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
112 try {
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530113 if (getReferredSchema() != null) {
114 throw new InvalidNodeForTranslatorException();
115 }
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530116 generateCodeOfNode(this, yangPlugin);
117 } catch (IOException e) {
118 throw new TranslatorException(
Bharat saraswale3175d32016-08-31 17:50:11 +0530119 "Failed to prepare generate code entry for union node " + getName() + " in " +
120 getLineNumber() + " at " +
121 getCharPosition()
122 + " in " + getFileName() + " " + e.getLocalizedMessage());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530123 }
124
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530125 }
126
127 /**
128 * Creates a java file using the YANG union info.
129 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530130 * @throws TranslatorException when fails to translate
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530131 */
132 @Override
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530133 public void generateCodeExit() throws TranslatorException {
134 try {
Gaurav Agrawald30f79b2016-10-06 00:58:57 +0530135 generateJava(GENERATE_UNION_CLASS, this);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530136 } catch (IOException e) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530137 throw new TranslatorException("Failed to generate code for union node " + getName() + " in " +
138 getLineNumber() + " at " +
139 getCharPosition()
140 + " in " + getFileName() + " " + e.getLocalizedMessage());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530141 }
Gaurav Agrawal97a5e1c2016-04-18 18:53:11 +0530142 }
143}