blob: c742e7c9224717b52a1ef49d2fe752bdb61988a2 [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
Bharat saraswale2d51d62016-03-23 19:40:35 +053018import java.io.IOException;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053019
janani bf9819ff2016-08-03 16:40:01 +053020import org.onosproject.yangutils.datamodel.YangDerivedInfo;
21import org.onosproject.yangutils.datamodel.YangType;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053022import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
23import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaTypeDef;
24import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
janani bf9819ff2016-08-03 16:40:01 +053025import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053026import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053027import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053028import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswale2d51d62016-03-23 19:40:35 +053029import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Vinod Kumar S38046502016-03-23 15:30:27 +053030
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053031import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.DERIVED;
32import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.LEAFREF;
Bharat saraswale2d51d62016-03-23 19:40:35 +053033import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
Bharat saraswalb551aae2016-07-14 15:18:20 +053034import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.generateCodeOfNode;
Vinod Kumar S38046502016-03-23 15:30:27 +053035
36/**
Bharat saraswald9822e92016-04-05 15:13:44 +053037 * Represents type define information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053038 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053039public class YangJavaTypeDefTranslator
40 extends YangJavaTypeDef
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053041 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053042
Bharat saraswal96dfef02016-06-16 00:29:12 +053043 private static final long serialVersionUID = 806201620L;
44
Vinod Kumar S38046502016-03-23 15:30:27 +053045 /**
Bharat saraswale2d51d62016-03-23 19:40:35 +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;
Bharat saraswale2d51d62016-03-23 19:40:35 +053050
51 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053052 * Creates a YANG java typedef object.
Vinod Kumar S38046502016-03-23 15:30:27 +053053 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053054 public YangJavaTypeDefTranslator() {
Vinod Kumar S38046502016-03-23 15:30:27 +053055 super();
56 setJavaFileInfo(new JavaFileInfo());
Bharat saraswale2d51d62016-03-23 19:40:35 +053057 getJavaFileInfo().setGeneratedFileTypes(GENERATE_TYPEDEF_CLASS);
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() {
Bharat saraswale2d51d62016-03-23 19:40:35 +053067
Vinod Kumar S38046502016-03-23 15:30:27 +053068 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053069 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053070 }
71 return javaFileInfo;
72 }
73
74 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053075 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053076 *
77 * @param javaInfo java file info object
78 */
79 @Override
80 public void setJavaFileInfo(JavaFileInfo javaInfo) {
81 javaFileInfo = javaInfo;
82 }
83
84 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053085 * Returns the temporary file handle.
Bharat saraswale2d51d62016-03-23 19:40:35 +053086 *
87 * @return temporary file handle
88 */
89 @Override
90 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Bharat saraswale2d51d62016-03-23 19:40:35 +053091 return tempFileHandle;
92 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Sets temporary file handle.
Bharat saraswale2d51d62016-03-23 19:40:35 +053096 *
97 * @param fileHandle temporary file handle
98 */
99 @Override
100 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530101 tempFileHandle = fileHandle;
102 }
103
104 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 * Prepare the information for java code generation corresponding to YANG
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530106 * typedef info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530107 *
janani bde4ffab2016-04-15 16:18:30 +0530108 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530109 * @throws TranslatorException when fails to translate
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 */
111 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530112 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
janani bf9819ff2016-08-03 16:40:01 +0530113 YangType typeInTypeDef = this.getTypeDefBaseType();
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530114 if (typeInTypeDef.getDataType() == DERIVED) {
janani bf9819ff2016-08-03 16:40:01 +0530115 YangDerivedInfo derivedInfo = (YangDerivedInfo) typeInTypeDef.getDataTypeExtendedInfo();
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530116 if (derivedInfo.getEffectiveBuiltInType() == LEAFREF) {
janani bf9819ff2016-08-03 16:40:01 +0530117 throw new InvalidNodeForTranslatorException();
118 }
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530119 } else if (typeInTypeDef.getDataType() == LEAFREF) {
janani bf9819ff2016-08-03 16:40:01 +0530120 throw new InvalidNodeForTranslatorException();
121 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530122 try {
123 generateCodeOfNode(this, yangPlugin);
124 } catch (IOException e) {
125 throw new TranslatorException(
Bharat saraswal96dfef02016-06-16 00:29:12 +0530126 "Failed to prepare generate code entry for typedef node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530127 }
128
Vinod Kumar S38046502016-03-23 15:30:27 +0530129 }
130
131 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530132 * Create a java file using the YANG typedef info.
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530133 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530134 * @throws TranslatorException when fails to translate
Vinod Kumar S38046502016-03-23 15:30:27 +0530135 */
136 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530137 public void generateCodeExit() throws TranslatorException {
138 try {
139 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_TYPEDEF_CLASS, this);
140 } catch (IOException e) {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530141 throw new TranslatorException("Failed to generate code for typedef node " + getName());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530142 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530143 }
144
145}