Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.yangutils.translator.tojava.javamodel; |
| 17 | |
| 18 | import java.io.IOException; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 19 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangModule; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 23 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils; |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 27 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 30 | |
| 31 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 32 | * Represents module information extended to support java code generation. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 33 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 34 | public class YangJavaModule |
| 35 | extends YangModule |
| 36 | implements JavaCodeGeneratorInfo, JavaCodeGenerator { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Contains the information of the java file being generated. |
| 40 | */ |
| 41 | private JavaFileInfo javaFileInfo; |
| 42 | |
| 43 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 44 | * File handle to maintain temporary java code fragments as per the code |
| 45 | * snippet types. |
| 46 | */ |
| 47 | private TempJavaCodeFragmentFiles tempFileHandle; |
| 48 | |
| 49 | /** |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 50 | * Creates a YANG node of module type. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 51 | */ |
| 52 | public YangJavaModule() { |
| 53 | super(); |
| 54 | setJavaFileInfo(new JavaFileInfo()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 55 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 59 | * Returns the generated java file information. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 60 | * |
| 61 | * @return generated java file information |
| 62 | */ |
| 63 | @Override |
| 64 | public JavaFileInfo getJavaFileInfo() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 65 | if (javaFileInfo == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 66 | throw new TranslatorException("Missing java info in java datamodel node"); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 67 | } |
| 68 | return javaFileInfo; |
| 69 | } |
| 70 | |
| 71 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 72 | * Sets the java file info object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 73 | * |
| 74 | * @param javaInfo java file info object |
| 75 | */ |
| 76 | @Override |
| 77 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 78 | javaFileInfo = javaInfo; |
| 79 | } |
| 80 | |
| 81 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 82 | * Returns the temporary file handle. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 83 | * |
| 84 | * @return temporary file handle |
| 85 | */ |
| 86 | @Override |
| 87 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 88 | return tempFileHandle; |
| 89 | } |
| 90 | |
| 91 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 92 | * Sets temporary file handle. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 93 | * |
| 94 | * @param fileHandle temporary file handle |
| 95 | */ |
| 96 | @Override |
| 97 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 98 | tempFileHandle = fileHandle; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Generates java code for module. |
| 103 | * |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 104 | * @param yangPlugin YANG plugin config |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 105 | * @throws IOException when fails to generate the source files |
| 106 | */ |
| 107 | @Override |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 108 | public void generateCodeEntry(YangPluginConfig yangPlugin) |
| 109 | throws IOException { |
Gaurav Agrawal | 2639004 | 2016-04-12 13:30:16 +0530 | [diff] [blame] | 110 | String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate()); |
janani b | 1c6acc4 | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 111 | YangJavaModelUtils.generateCodeOfRootNode(this, yangPlugin, modulePkg); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 112 | } |
| 113 | |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 114 | /** |
| 115 | * Creates a java file using the YANG module info. |
| 116 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 117 | @Override |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 118 | public void generateCodeExit() |
| 119 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 120 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 121 | } |
| 122 | } |