blob: 122bb40cc7918c408ee392a17e0735349be6f02e [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-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 Se4b9b0c2016-04-30 21:09:15 +053019
Vinod Kumar S38046502016-03-23 15:30:27 +053020import org.onosproject.yangutils.datamodel.YangChoice;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053021import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053023import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053024import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053025import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053026import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Vinod Kumar S38046502016-03-23 15:30:27 +053027
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053028import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswalb551aae2016-07-14 15:18:20 +053029import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeAndUpdateInParent;
Vinod Kumar S38046502016-03-23 15:30:27 +053030
31/**
Bharat saraswald9822e92016-04-05 15:13:44 +053032 * Represents choice information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053033 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053034public class YangJavaChoice
35 extends YangChoice
36 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053037
Bharat saraswal96dfef02016-06-16 00:29:12 +053038 private static final long serialVersionUID = 806201631L;
39
Vinod Kumar S38046502016-03-23 15:30:27 +053040 /**
41 * Contains the information of the java file being generated.
42 */
43 private JavaFileInfo javaFileInfo;
44
45 /**
Vinod Kumar S38046502016-03-23 15:30:27 +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;
Vinod Kumar S38046502016-03-23 15:30:27 +053050
51 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053052 * Creates YANG java choice object.
Vinod Kumar S38046502016-03-23 15:30:27 +053053 */
54 public YangJavaChoice() {
55 super();
56 setJavaFileInfo(new JavaFileInfo());
Vidyashree Rama7142d9c2016-04-26 15:06:06 +053057 getJavaFileInfo().setGeneratedFileTypes(INTERFACE_MASK);
Vinod Kumar S38046502016-03-23 15:30:27 +053058 }
59
60 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053061 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053062 *
63 * @return generated java file information
64 */
65 @Override
66 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053067 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053068 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053069 }
70 return javaFileInfo;
71 }
72
73 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053074 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053075 *
76 * @param javaInfo java file info object
77 */
78 @Override
79 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053080 javaFileInfo = javaInfo;
81 }
82
83 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053084 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +053085 *
86 * @return temporary file handle
87 */
88 @Override
89 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +053090 return tempFileHandle;
91 }
92
93 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +053095 *
96 * @param fileHandle temporary file handle
97 */
98 @Override
99 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 tempFileHandle = fileHandle;
101 }
102
103 /**
104 * Prepare the information for java code generation corresponding to YANG
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530105 * choice info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 *
janani bde4ffab2016-04-15 16:18:30 +0530107 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530108 * @throws TranslatorException translator operation fail
Vinod Kumar S38046502016-03-23 15:30:27 +0530109 */
110 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530111 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
112 try {
113 generateCodeAndUpdateInParent(this, yangPlugin, false);
114 } catch (IOException e) {
115 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530116 "Failed to prepare generate code entry for choice node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530117 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530118 }
119
120 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530121 * Creates a java file using the YANG choice info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530122 */
123 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530124 public void generateCodeExit() throws TranslatorException {
125 try {
126 getTempJavaCodeFragmentFiles().generateJavaFile(INTERFACE_MASK, this);
127 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530128 throw new TranslatorException("Failed to generate code for choice node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530129 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530130 }
131}