blob: a0834f049e387a37fbd60c6639a9c4e4858d1090 [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;
Vidyashree Rama1db15562016-05-17 16:16:15 +053021import org.onosproject.yangutils.datamodel.YangModule;
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053023import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053024import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
25import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053026import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053027import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar S38046502016-03-23 15:30:27 +053028
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053029import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar S38046502016-03-23 15:30:27 +053030import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053031import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfRootNode;
Bharat saraswalc0e04842016-05-12 13:16:57 +053032import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
Vinod Kumar S38046502016-03-23 15:30:27 +053033
34/**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053035 * Represents sub module information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053036 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053037public class YangJavaSubModule
38 extends YangSubModule
39 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053040
41 /**
42 * Contains the information of the java file being generated.
43 */
44 private JavaFileInfo javaFileInfo;
45
46 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053047 * File handle to maintain temporary java code fragments as per the code
48 * snippet types.
49 */
50 private TempJavaCodeFragmentFiles tempFileHandle;
51
52 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053053 * Creates YANG java sub module object.
Vinod Kumar S38046502016-03-23 15:30:27 +053054 */
55 public YangJavaSubModule() {
56 super();
57 setJavaFileInfo(new JavaFileInfo());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053058 getJavaFileInfo().setGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
Vinod Kumar S38046502016-03-23 15:30:27 +053059 }
60
61 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053062 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053063 *
64 * @return generated java file information
65 */
66 @Override
67 public JavaFileInfo getJavaFileInfo() {
68 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.
Vinod Kumar S38046502016-03-23 15:30:27 +053086 *
87 * @return temporary file handle
88 */
89 @Override
90 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +053091 return tempFileHandle;
92 }
93
94 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053095 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +053096 *
97 * @param fileHandle temporary file handle
98 */
99 @Override
100 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
101 tempFileHandle = fileHandle;
102 }
103
104 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530105 * Returns the name space of the module to which the sub module belongs to.
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 *
107 * @param belongsToInfo Information of the module to which the sub module
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530108 * belongs
Vinod Kumar S38046502016-03-23 15:30:27 +0530109 * @return the name space string of the module.
110 */
111 private String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
Vidyashree Rama1db15562016-05-17 16:16:15 +0530112 return ((YangModule) belongsToInfo.getModuleNode()).getNameSpace().getUri();
Vinod Kumar S38046502016-03-23 15:30:27 +0530113 }
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}