blob: b7437731b5e77cc8592cf46c844830e406e6e732 [file] [log] [blame]
Vidyashree Rama09e1ca52016-03-29 12:00:42 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama09e1ca52016-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 S79a374b2016-04-30 21:09:15 +053020
21import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053022import org.onosproject.yangutils.datamodel.YangInput;
23import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053025import org.onosproject.yangutils.datamodel.YangRpc;
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053026import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053027import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053028import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053029import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
VinodKumarS-Huawei6266db32016-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 S79a374b2016-04-30 21:09:15 +053033import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053034import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
35import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles;
janani b1c6acc42016-04-15 16:18:30 +053036import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053037
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053038import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
39import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053040import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
41import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo;
42
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053043/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053044 * Represents rpc information extended to support java code generation.
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053045 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +053046public class YangJavaRpc
47 extends YangRpc
48 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053049
50 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053051 * Contains the information of the java file being generated.
52 */
53 private JavaFileInfo javaFileInfo;
54
55 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +053056 * Temproary file for code generation.
57 */
58 private TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles;
59
60 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053061 * Creates an instance of YANG java rpc.
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053062 */
63 public YangJavaRpc() {
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053064 super();
65 setJavaFileInfo(new JavaFileInfo());
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053066 }
67
68 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053069 * Prepares the information for java code generation corresponding to YANG
Vinod Kumar S79a374b2016-04-30 21:09:15 +053070 * RPC info.
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053071 *
janani b1c6acc42016-04-15 16:18:30 +053072 * @param yangPlugin YANG plugin config
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053073 * @throws IOException IO operations fails
Vidyashree Rama09e1ca52016-03-29 12:00:42 +053074 */
75 @Override
Vinod Kumar S79a374b2016-04-30 21:09:15 +053076 public void generateCodeEntry(YangPluginConfig yangPlugin)
77 throws IOException {
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053078
Vinod Kumar S79a374b2016-04-30 21:09:15 +053079 if (!(this instanceof JavaCodeGeneratorInfo)) {
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053080 // TODO:throw exception
81 }
82
83 // Add package information for rpc and create corresponding folder.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053084 updatePackageInfo((JavaCodeGeneratorInfo) this, yangPlugin);
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053085
Vinod Kumar S79a374b2016-04-30 21:09:15 +053086 }
87
88 /**
89 * Creates a java file using the YANG RPC info.
90 *
91 * @throws IOException IO operations fails
92 */
93 @Override
94 public void generateCodeExit()
95 throws IOException {
Gaurav Agrawal02a60de2016-04-20 15:49:17 +053096 // Get the parent module/sub-module.
97 YangNode parent = getParentNodeInGenCode((YangNode) this);
98
99 // Parent should be holder of rpc or notification.
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530100 if (!(parent instanceof RpcNotificationContainer)) {
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530101 throw new TranslatorException("parent node of rpc can only be module or sub-module");
102 }
103
104 /*
105 * Create attribute info for input and output of rpc and add it to the
106 * parent import list.
107 */
108
109 JavaAttributeInfo javaAttributeInfoOfInput = null;
110 JavaAttributeInfo javaAttributeInfoOfOutput = null;
111
112 // Get the child input and output node and obtain create java attribute info.
113 YangNode yangNode = this.getChild();
114 while (yangNode != null) {
115 if (yangNode instanceof YangInput) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530116 javaAttributeInfoOfInput = getChildNodeAsAttributeInParentService(yangNode, this);
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530117 } else if (yangNode instanceof YangOutput) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530118 javaAttributeInfoOfOutput = getChildNodeAsAttributeInParentService(yangNode, this);
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530119 } else {
120 // TODO throw exception
121 }
122 yangNode = yangNode.getNextSibling();
123 }
124
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530125 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530126 throw new TranslatorException("missing parent temp file handle");
127 }
128
129 /*
130 * Add the rpc information to the parent's service temp file.
131 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530132 ((TempJavaCodeFragmentFilesContainer) parent)
133 .getTempJavaCodeFragmentFiles().getServiceTempFiles()
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530134 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput,
135 ((YangNode) this).getName());
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530136 // No file will be generated during RPC exit.
137 }
138
139 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530140 * Creates an attribute info object corresponding to a data model node and
141 * return it.
142 *
143 * @param childNode child data model node(input / output) for which the java code generation
144 * is being handled
145 * @param currentNode parent node (module / sub-module) in which the child node is an attribute
146 * @return AttributeInfo attribute details required to add in temporary
147 * files
148 */
149 public static JavaAttributeInfo getChildNodeAsAttributeInParentService(
150 YangNode childNode, YangNode currentNode) {
151
152 YangNode parentNode = getParentNodeInGenCode(currentNode);
153
154 String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName();
155 /*
156 * Get the import info corresponding to the attribute for import in
157 * generated java files or qualified access
158 */
159 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(currentNode,
160 childNodeName);
161 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
162 throw new TranslatorException("Parent node does not have file info");
163 }
164
165 TempJavaFragmentFiles tempJavaFragmentFiles;
166 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode)
167 .getTempJavaCodeFragmentFiles()
168 .getServiceTempFiles();
169
170 if (tempJavaFragmentFiles == null) {
171 throw new TranslatorException("Parent node does not have service file info");
172 }
173
174 JavaImportData parentImportData = tempJavaFragmentFiles.getJavaImportData();
175 boolean isQualified = parentImportData.addImportInfo(qualifiedTypeInfo);
176 return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false);
177 }
178
179 /**
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530180 * Returns the generated java file information.
181 *
182 * @return generated java file information
183 */
184 @Override
185 public JavaFileInfo getJavaFileInfo() {
186
187 if (javaFileInfo == null) {
188 throw new TranslatorException("missing java info in java datamodel node");
189 }
190 return javaFileInfo;
191 }
192
193 /**
194 * Sets the java file info object.
195 *
196 * @param javaInfo java file info object
197 */
198 @Override
199 public void setJavaFileInfo(JavaFileInfo javaInfo) {
200 javaFileInfo = javaInfo;
Vidyashree Rama09e1ca52016-03-29 12:00:42 +0530201 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530202
203 @Override
204 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
205 return tempJavaCodeFragmentFiles;
206 }
207
208 @Override
209 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
210 tempJavaCodeFragmentFiles = fileHandle;
211 }
Vidyashree Rama09e1ca52016-03-29 12:00:42 +0530212}
Gaurav Agrawal02a60de2016-04-20 15:49:17 +0530213