blob: cdda4d3f61e6fb3cf5cdc945a9e96bcc466f09a9 [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.YangModule;
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;
23import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053024import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053025import org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils;
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;
Vinod Kumar S38046502016-03-23 15:30:27 +053030
31/**
Bharat saraswald9822e92016-04-05 15:13:44 +053032 * Represents module 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 YangJavaModule
35 extends YangModule
36 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Vinod Kumar S38046502016-03-23 15:30:27 +053037
38 /**
39 * Contains the information of the java file being generated.
40 */
41 private JavaFileInfo javaFileInfo;
42
43 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053044 * File handle to maintain temporary java code fragments as per the code
45 * snippet types.
46 */
47 private TempJavaCodeFragmentFiles tempFileHandle;
48
49 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053050 * Creates a YANG node of module type.
Vinod Kumar S38046502016-03-23 15:30:27 +053051 */
52 public YangJavaModule() {
53 super();
54 setJavaFileInfo(new JavaFileInfo());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053055 getJavaFileInfo().setGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
Vinod Kumar S38046502016-03-23 15:30:27 +053056 }
57
58 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053059 * Returns the generated java file information.
Vinod Kumar S38046502016-03-23 15:30:27 +053060 *
61 * @return generated java file information
62 */
63 @Override
64 public JavaFileInfo getJavaFileInfo() {
Vinod Kumar S38046502016-03-23 15:30:27 +053065 if (javaFileInfo == null) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +053066 throw new TranslatorException("Missing java info in java datamodel node");
Vinod Kumar S38046502016-03-23 15:30:27 +053067 }
68 return javaFileInfo;
69 }
70
71 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053072 * Sets the java file info object.
Vinod Kumar S38046502016-03-23 15:30:27 +053073 *
74 * @param javaInfo java file info object
75 */
76 @Override
77 public void setJavaFileInfo(JavaFileInfo javaInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +053078 javaFileInfo = javaInfo;
79 }
80
81 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053082 * Returns the temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +053083 *
84 * @return temporary file handle
85 */
86 @Override
87 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
Vinod Kumar S38046502016-03-23 15:30:27 +053088 return tempFileHandle;
89 }
90
91 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053092 * Sets temporary file handle.
Vinod Kumar S38046502016-03-23 15:30:27 +053093 *
94 * @param fileHandle temporary file handle
95 */
96 @Override
97 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
Vinod Kumar S38046502016-03-23 15:30:27 +053098 tempFileHandle = fileHandle;
99 }
100
101 /**
102 * Generates java code for module.
103 *
janani bde4ffab2016-04-15 16:18:30 +0530104 * @param yangPlugin YANG plugin config
Vinod Kumar S38046502016-03-23 15:30:27 +0530105 * @throws IOException when fails to generate the source files
106 */
107 @Override
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530108 public void generateCodeEntry(YangPluginConfig yangPlugin)
109 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530110 String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate());
janani bde4ffab2016-04-15 16:18:30 +0530111 YangJavaModelUtils.generateCodeOfRootNode(this, yangPlugin, modulePkg);
Vinod Kumar S38046502016-03-23 15:30:27 +0530112 }
113
Vidyashree Rama74453712016-04-18 12:29:39 +0530114 /**
115 * Creates a java file using the YANG module info.
116 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530117 @Override
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118 public void generateCodeExit()
119 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530120 getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 }
122}