blob: d37b59f5bd3cdc9be7d9197fae92a2c228fec349 [file] [log] [blame]
Vinod Kumar S9f26ae52016-03-23 15:30:27 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S9f26ae52016-03-23 15:30:27 +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 */
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.YangJavaChoice;
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +053021import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
Bharat saraswal780eca32016-04-05 12:45:45 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +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;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053026import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Bharat saraswale3175d32016-08-31 17:50:11 +053027import org.onosproject.yangutils.utils.io.YangPluginConfig;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053028
Vidyashree Rama13960652016-04-26 15:06:06 +053029import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswalaf413b82016-07-14 15:18:20 +053030import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeAndUpdateInParent;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053031
32/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053033 * Represents choice information extended to support java code generation.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053034 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053035public class YangJavaChoiceTranslator
36 extends YangJavaChoice
Vinod Kumar S79a374b2016-04-30 21:09:15 +053037 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053038
Bharat saraswalc2d3be12016-06-16 00:29:12 +053039 private static final long serialVersionUID = 806201631L;
40
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053041 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053042 * File handle to maintain temporary java code fragments as per the code
43 * snippet types.
44 */
Bharat saraswalc2d3be12016-06-16 00:29:12 +053045 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053046
47 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053048 * Creates YANG java choice object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053049 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053050 public YangJavaChoiceTranslator() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053051 super();
Bharat saraswale50edca2016-08-05 01:58:25 +053052 setJavaFileInfo(new JavaFileInfoTranslator());
Vidyashree Rama13960652016-04-26 15:06:06 +053053 getJavaFileInfo().setGeneratedFileTypes(INTERFACE_MASK);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053054 }
55
56 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053057 * Returns the generated java file information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053058 *
59 * @return generated java file information
60 */
61 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053062 public JavaFileInfoTranslator getJavaFileInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053063 if (javaFileInfo == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +053064 throw new TranslatorException("Missing java info in java datamodel node" +
65 getName() + " in " +
66 getLineNumber() + " at " +
67 getCharPosition()
68 + " in " + getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053069 }
Bharat saraswale50edca2016-08-05 01:58:25 +053070 return (JavaFileInfoTranslator) javaFileInfo;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053071 }
72
73 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053074 * Sets the java file info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053075 *
76 * @param javaInfo java file info object
77 */
78 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053079 public void setJavaFileInfo(JavaFileInfoTranslator javaInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053080 javaFileInfo = javaInfo;
81 }
82
83 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053084 * Returns the temporary file handle.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053085 *
86 * @return temporary file handle
87 */
88 @Override
89 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053090 return tempFileHandle;
91 }
92
93 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053094 * Sets temporary file handle.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095 *
96 * @param fileHandle temporary file handle
97 */
98 @Override
99 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530100 tempFileHandle = fileHandle;
101 }
102
103 /**
104 * Prepare the information for java code generation corresponding to YANG
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530105 * choice info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530106 *
janani b1c6acc42016-04-15 16:18:30 +0530107 * @param yangPlugin YANG plugin config
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530108 * @throws TranslatorException translator operation fail
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530109 */
110 @Override
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530111 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
112 try {
113 generateCodeAndUpdateInParent(this, yangPlugin, false);
Gaurav Agrawal9381ebb2016-09-29 19:00:33 +0530114 if (getReferredSchema() != null) {
115 throw new InvalidNodeForTranslatorException();
116 }
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530117 } catch (IOException e) {
118 throw new TranslatorException(
Bharat saraswale3175d32016-08-31 17:50:11 +0530119 "Failed to prepare generate code entry for choice node " +
120 getName() + " in " +
121 getLineNumber() + " at " +
122 getCharPosition()
123 + " in " + getFileName() + " " + e.getLocalizedMessage());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530124 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530125 }
126
127 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530128 * Creates a java file using the YANG choice info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530129 */
130 @Override
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530131 public void generateCodeExit() throws TranslatorException {
132 try {
133 getTempJavaCodeFragmentFiles().generateJavaFile(INTERFACE_MASK, this);
134 } catch (IOException e) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530135 throw new TranslatorException("Failed to generate code for choice node " +
136 getName() + " in " +
137 getLineNumber() + " at " +
138 getCharPosition()
139 + " in " + getFileName() + " " + e.getLocalizedMessage());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530140 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530141 }
142}