blob: 419f15ee44952112a7170a22a608ac85078b8a0e [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.YangBelongsTo;
21import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053023import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
24import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053025import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053026import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar S38046502016-03-23 15:30:27 +053027
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S38046502016-03-23 15:30:27 +053029import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053030import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfRootNode;
Bharat saraswalc0e04842016-05-12 13:16:57 +053031import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053032
33/**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053034 * Represents sub module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053035 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053036public class YangJavaSubModule
37 extends YangSubModule
38 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053039
40 /**
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 */
49 private TempJavaCodeFragmentFiles tempFileHandle;
50
51 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053052 * Creates YANG java sub module object.
Vinod Kumar S38046502016-03-23 15:30:27 +053053 */
54 public YangJavaSubModule() {
55 super();
56 setJavaFileInfo(new JavaFileInfo());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053057 getJavaFileInfo().setGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
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() {
67 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) {
80 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) {
100 tempFileHandle = fileHandle;
101 }
102
103 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 * Returns the name space of the module to which the sub module belongs to.
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 *
106 * @param belongsToInfo Information of the module to which the sub module
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107 * belongs
Vinod Kumar S38046502016-03-23 15:30:27 +0530108 * @return the name space string of the module.
109 */
110 private String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
111 // TODO Auto-generated method stub
112 return "";
113 }
114
115 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530116 * Prepares the information for java code generation corresponding to YANG
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530117 * submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530118 *
janani bde4ffab2016-04-15 16:18:30 +0530119 * @param yangPlugin YANG plugin config
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530120 * @throws TranslatorException when fails to translate
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 */
122 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530123 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530124 String subModulePkg = getRootPackage(getVersion(), getNameSpaceFromModule(getBelongsTo()),
125 getRevision().getRevDate());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530126 try {
127 generateCodeOfRootNode(this, yangPlugin, subModulePkg);
128 } catch (IOException e) {
129 throw new TranslatorException(
130 "failed to prepare generate code entry for submodule node " + this.getName());
131 }
132
Vinod Kumar S38046502016-03-23 15:30:27 +0530133 }
134
135 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530136 * Creates a java file using the YANG submodule info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530137 */
138 @Override
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530139 public void generateCodeExit() throws TranslatorException {
140 try {
141 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
142 searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
143 getJavaFileInfo().getPackageFilePath());
144 } catch (IOException e) {
145 throw new TranslatorException("Failed to generate code for submodule node " + this.getName());
146 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530147 }
148}