blob: 6082963de265e3cd3dff6de735a2739644f28c5e [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.YangJavaList;
Bharat saraswal780eca32016-04-05 12:45:45 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053022import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053023import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswale3175d32016-08-31 17:50:11 +053024import org.onosproject.yangutils.translator.tojava.JavaFileInfoTranslator;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053025import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Bharat saraswale3175d32016-08-31 17:50:11 +053026import org.onosproject.yangutils.utils.io.YangPluginConfig;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053027
28import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswalaf413b82016-07-14 15:18:20 +053029import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeAndUpdateInParent;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053030
31/**
Gaurav Agrawal26390042016-04-12 13:30:16 +053032 * Represents YANG list information extended to support java code generation.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053033 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053034public class YangJavaListTranslator
35 extends YangJavaList
Vinod Kumar S79a374b2016-04-30 21:09:15 +053036 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053037
Bharat saraswalc2d3be12016-06-16 00:29:12 +053038 private static final long serialVersionUID = 806201626L;
39
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053040 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053041 * File handle to maintain temporary java code fragments as per the code
42 * snippet types.
43 */
Bharat saraswalc2d3be12016-06-16 00:29:12 +053044 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053045
46 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053047 * Creates YANG java list object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053048 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053049 public YangJavaListTranslator() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053050 super();
Bharat saraswale50edca2016-08-05 01:58:25 +053051 setJavaFileInfo(new JavaFileInfoTranslator());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053052 getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
53 }
54
55 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053056 * Returns the generated java file information.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053057 *
58 * @return generated java file information
59 */
60 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053061 public JavaFileInfoTranslator getJavaFileInfo() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053062 if (javaFileInfo == null) {
Bharat saraswale3175d32016-08-31 17:50:11 +053063 throw new TranslatorException("Missing java info in java datamodel node " +
64 getName() + " in " +
65 getLineNumber() + " at " +
66 getCharPosition()
67 + " in " + getFileName());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053068 }
Bharat saraswale50edca2016-08-05 01:58:25 +053069 return (JavaFileInfoTranslator) javaFileInfo;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053070 }
71
72 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053073 * Sets the java file info object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053074 *
75 * @param javaInfo java file info object
76 */
77 @Override
Bharat saraswale50edca2016-08-05 01:58:25 +053078 public void setJavaFileInfo(JavaFileInfoTranslator javaInfo) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053079 javaFileInfo = javaInfo;
80 }
81
82 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053083 * Returns the temporary file handle.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053084 *
85 * @return temporary file handle
86 */
87 @Override
88 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053089 return tempFileHandle;
90 }
91
92 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053093 * Sets temporary file handle.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053094 *
95 * @param fileHandle temporary file handle
96 */
97 @Override
98 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053099 tempFileHandle = fileHandle;
100 }
101
102 /**
103 * Prepare the information for java code generation corresponding to YANG
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530104 * list info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530105 *
janani b1c6acc42016-04-15 16:18:30 +0530106 * @param yangPlugin YANG plugin config
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530107 * @throws TranslatorException translator operation fail
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530108 */
109 @Override
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530110 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
111 try {
112 generateCodeAndUpdateInParent(this, yangPlugin, true);
113 } catch (IOException e) {
114 throw new TranslatorException(
Bharat saraswale3175d32016-08-31 17:50:11 +0530115 "Failed to prepare generate code entry for list node " +
116 getName() + " in " +
117 getLineNumber() + " at " +
118 getCharPosition()
119 + " in " + getFileName() + " " + e.getLocalizedMessage());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530120 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530121 }
122
123 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530124 * Creates a java file using the YANG list info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530125 *
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530126 * @throws TranslatorException translator operation fail
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530127 */
128 @Override
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530129 public void generateCodeExit() throws TranslatorException {
130 try {
131 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
132 } catch (IOException e) {
Bharat saraswale3175d32016-08-31 17:50:11 +0530133 throw new TranslatorException("Failed to generate code for list node " +
134 getName() + " in " +
135 getLineNumber() + " at " +
136 getCharPosition()
137 + " in " + getFileName() + " " + e.getLocalizedMessage());
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530138 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530139 }
140}