blob: d2132f0233ae10f4697c03db4df9858877c2325e [file] [log] [blame]
Vidyashree Rama6a72b792016-03-29 12:00:42 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama6a72b792016-03-29 12:00:42 +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 */
16
17package org.onosproject.yangutils.translator.tojava.javamodel;
18
19import java.io.IOException;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020
21import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Gaurav Agrawal56527662016-04-20 15:49:17 +053022import org.onosproject.yangutils.datamodel.YangInput;
23import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053025import org.onosproject.yangutils.datamodel.YangRpc;
Gaurav Agrawal56527662016-04-20 15:49:17 +053026import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal56527662016-04-20 15:49:17 +053027import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053028import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal56527662016-04-20 15:49:17 +053029import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
31import org.onosproject.yangutils.translator.tojava.JavaImportData;
32import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053033import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
35import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053036import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053037
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053038import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
39import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
janani b4a6711a2016-05-17 13:12:22 +053040import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Gaurav Agrawal56527662016-04-20 15:49:17 +053041import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
42import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo;
43
Vidyashree Rama6a72b792016-03-29 12:00:42 +053044/**
Bharat saraswald9822e92016-04-05 15:13:44 +053045 * Represents rpc information extended to support java code generation.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053046 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053047public class YangJavaRpc
48 extends YangRpc
49 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama6a72b792016-03-29 12:00:42 +053050
51 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053052 * Contains the information of the java file being generated.
53 */
54 private JavaFileInfo javaFileInfo;
55
56 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053057 * Temproary file for code generation.
58 */
59 private TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles;
60
61 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053062 * Creates an instance of YANG java rpc.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053063 */
64 public YangJavaRpc() {
Gaurav Agrawal56527662016-04-20 15:49:17 +053065 super();
66 setJavaFileInfo(new JavaFileInfo());
Vidyashree Rama6a72b792016-03-29 12:00:42 +053067 }
68
69 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Prepares the information for java code generation corresponding to YANG
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053071 * RPC info.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053072 *
janani bde4ffab2016-04-15 16:18:30 +053073 * @param yangPlugin YANG plugin config
Gaurav Agrawal56527662016-04-20 15:49:17 +053074 * @throws IOException IO operations fails
Vidyashree Rama6a72b792016-03-29 12:00:42 +053075 */
76 @Override
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053077 public void generateCodeEntry(YangPluginConfig yangPlugin)
78 throws IOException {
Gaurav Agrawal56527662016-04-20 15:49:17 +053079
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080 if (!(this instanceof JavaCodeGeneratorInfo)) {
Gaurav Agrawal56527662016-04-20 15:49:17 +053081 // TODO:throw exception
82 }
83
84 // Add package information for rpc and create corresponding folder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085 updatePackageInfo((JavaCodeGeneratorInfo) this, yangPlugin);
Gaurav Agrawal56527662016-04-20 15:49:17 +053086
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087 }
88
89 /**
90 * Creates a java file using the YANG RPC info.
91 *
92 * @throws IOException IO operations fails
93 */
94 @Override
95 public void generateCodeExit()
96 throws IOException {
Gaurav Agrawal56527662016-04-20 15:49:17 +053097 // Get the parent module/sub-module.
98 YangNode parent = getParentNodeInGenCode((YangNode) this);
99
100 // Parent should be holder of rpc or notification.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530101 if (!(parent instanceof RpcNotificationContainer)) {
Gaurav Agrawal56527662016-04-20 15:49:17 +0530102 throw new TranslatorException("parent node of rpc can only be module or sub-module");
103 }
104
105 /*
106 * Create attribute info for input and output of rpc and add it to the
107 * parent import list.
108 */
109
110 JavaAttributeInfo javaAttributeInfoOfInput = null;
111 JavaAttributeInfo javaAttributeInfoOfOutput = null;
112
113 // Get the child input and output node and obtain create java attribute info.
114 YangNode yangNode = this.getChild();
115 while (yangNode != null) {
116 if (yangNode instanceof YangInput) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530117 javaAttributeInfoOfInput = getChildNodeAsAttributeInParentService(yangNode, this);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530118 } else if (yangNode instanceof YangOutput) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530119 javaAttributeInfoOfOutput = getChildNodeAsAttributeInParentService(yangNode, this);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530120 } else {
121 // TODO throw exception
122 }
123 yangNode = yangNode.getNextSibling();
124 }
125
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530126 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
Gaurav Agrawal56527662016-04-20 15:49:17 +0530127 throw new TranslatorException("missing parent temp file handle");
128 }
129
130 /*
131 * Add the rpc information to the parent's service temp file.
132 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530133 ((TempJavaCodeFragmentFilesContainer) parent)
134 .getTempJavaCodeFragmentFiles().getServiceTempFiles()
Gaurav Agrawal56527662016-04-20 15:49:17 +0530135 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput,
136 ((YangNode) this).getName());
Gaurav Agrawal56527662016-04-20 15:49:17 +0530137 // No file will be generated during RPC exit.
138 }
139
140 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530141 * Creates an attribute info object corresponding to a data model node and
142 * return it.
143 *
144 * @param childNode child data model node(input / output) for which the java code generation
145 * is being handled
146 * @param currentNode parent node (module / sub-module) in which the child node is an attribute
147 * @return AttributeInfo attribute details required to add in temporary
148 * files
149 */
150 public static JavaAttributeInfo getChildNodeAsAttributeInParentService(
151 YangNode childNode, YangNode currentNode) {
152
153 YangNode parentNode = getParentNodeInGenCode(currentNode);
154
155 String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName();
156 /*
157 * Get the import info corresponding to the attribute for import in
158 * generated java files or qualified access
159 */
160 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(currentNode,
janani b4a6711a2016-05-17 13:12:22 +0530161 getCapitalCase(childNodeName));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530162 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
163 throw new TranslatorException("Parent node does not have file info");
164 }
165
166 TempJavaFragmentFiles tempJavaFragmentFiles;
167 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode)
168 .getTempJavaCodeFragmentFiles()
169 .getServiceTempFiles();
170
171 if (tempJavaFragmentFiles == null) {
172 throw new TranslatorException("Parent node does not have service file info");
173 }
174
175 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
176 boolean isQualified = parentImportData.addImportInfo(qualifiedTypeInfo);
177 return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false);
178 }
179
180 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530181 * Returns the generated java file information.
182 *
183 * @return generated java file information
184 */
185 @Override
186 public JavaFileInfo getJavaFileInfo() {
187
188 if (javaFileInfo == null) {
189 throw new TranslatorException("missing java info in java datamodel node");
190 }
191 return javaFileInfo;
192 }
193
194 /**
195 * Sets the java file info object.
196 *
197 * @param javaInfo java file info object
198 */
199 @Override
200 public void setJavaFileInfo(JavaFileInfo javaInfo) {
201 javaFileInfo = javaInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530202 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530203
204 @Override
205 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
206 return tempJavaCodeFragmentFiles;
207 }
208
209 @Override
210 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
211 tempJavaCodeFragmentFiles = fileHandle;
212 }
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530213}
Gaurav Agrawal56527662016-04-20 15:49:17 +0530214