blob: 7a7c96a9dfdd565cda01b3f984be91b1b090761c [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;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053027import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
Gaurav Agrawal56527662016-04-20 15:49:17 +053028import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053029import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal56527662016-04-20 15:49:17 +053030import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053031import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053032import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053033
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053034import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_RPC_INTERFACE;
35import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.getCurNodeAsAttributeInParent;
Gaurav Agrawal56527662016-04-20 15:49:17 +053036import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
37import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo;
38
Vidyashree Rama6a72b792016-03-29 12:00:42 +053039/**
Bharat saraswald9822e92016-04-05 15:13:44 +053040 * Represents rpc information extended to support java code generation.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053041 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053042public class YangJavaRpc
43 extends YangRpc
44 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama6a72b792016-03-29 12:00:42 +053045
46 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053047 * Contains the information of the java file being generated.
48 */
49 private JavaFileInfo javaFileInfo;
50
51 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053052 * Temproary file for code generation.
53 */
54 private TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles;
55
56 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053057 * Creates an instance of YANG java rpc.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053058 */
59 public YangJavaRpc() {
Gaurav Agrawal56527662016-04-20 15:49:17 +053060 super();
61 setJavaFileInfo(new JavaFileInfo());
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053062 getJavaFileInfo().setGeneratedFileTypes(GENERATE_RPC_INTERFACE);
63 try {
64 setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(getJavaFileInfo()));
65 } catch (IOException e) {
66 throw new RuntimeException("Failed to create temporary RPC file handle");
67 }
Vidyashree Rama6a72b792016-03-29 12:00:42 +053068 }
69
70 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053071 * Prepares the information for java code generation corresponding to YANG
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053072 * RPC info.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053073 *
janani bde4ffab2016-04-15 16:18:30 +053074 * @param yangPlugin YANG plugin config
Gaurav Agrawal56527662016-04-20 15:49:17 +053075 * @throws IOException IO operations fails
Vidyashree Rama6a72b792016-03-29 12:00:42 +053076 */
77 @Override
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053078 public void generateCodeEntry(YangPluginConfig yangPlugin)
79 throws IOException {
Gaurav Agrawal56527662016-04-20 15:49:17 +053080
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053081 if (!(this instanceof JavaCodeGeneratorInfo)) {
Gaurav Agrawal56527662016-04-20 15:49:17 +053082 // TODO:throw exception
83 }
84
85 // Add package information for rpc and create corresponding folder.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086 updatePackageInfo((JavaCodeGeneratorInfo) this, yangPlugin);
Gaurav Agrawal56527662016-04-20 15:49:17 +053087
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053088 }
89
90 /**
91 * Creates a java file using the YANG RPC info.
92 *
93 * @throws IOException IO operations fails
94 */
95 @Override
96 public void generateCodeExit()
97 throws IOException {
Gaurav Agrawal56527662016-04-20 15:49:17 +053098 // Get the parent module/sub-module.
99 YangNode parent = getParentNodeInGenCode((YangNode) this);
100
101 // Parent should be holder of rpc or notification.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530102 if (!(parent instanceof RpcNotificationContainer)) {
Gaurav Agrawal56527662016-04-20 15:49:17 +0530103 throw new TranslatorException("parent node of rpc can only be module or sub-module");
104 }
105
106 /*
107 * Create attribute info for input and output of rpc and add it to the
108 * parent import list.
109 */
110
111 JavaAttributeInfo javaAttributeInfoOfInput = null;
112 JavaAttributeInfo javaAttributeInfoOfOutput = null;
113
114 // Get the child input and output node and obtain create java attribute info.
115 YangNode yangNode = this.getChild();
116 while (yangNode != null) {
117 if (yangNode instanceof YangInput) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530118 javaAttributeInfoOfInput = getCurNodeAsAttributeInParent(yangNode, this, false);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530119 } else if (yangNode instanceof YangOutput) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530120 javaAttributeInfoOfOutput = getCurNodeAsAttributeInParent(yangNode, this, false);
Gaurav Agrawal56527662016-04-20 15:49:17 +0530121 } else {
122 // TODO throw exception
123 }
124 yangNode = yangNode.getNextSibling();
125 }
126
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530127 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
Gaurav Agrawal56527662016-04-20 15:49:17 +0530128 throw new TranslatorException("missing parent temp file handle");
129 }
130
131 /*
132 * Add the rpc information to the parent's service temp file.
133 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530134 ((TempJavaCodeFragmentFilesContainer) parent)
135 .getTempJavaCodeFragmentFiles().getServiceTempFiles()
Gaurav Agrawal56527662016-04-20 15:49:17 +0530136 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput,
137 ((YangNode) this).getName());
Gaurav Agrawal56527662016-04-20 15:49:17 +0530138 // No file will be generated during RPC exit.
139 }
140
141 /**
142 * Returns the generated java file information.
143 *
144 * @return generated java file information
145 */
146 @Override
147 public JavaFileInfo getJavaFileInfo() {
148
149 if (javaFileInfo == null) {
150 throw new TranslatorException("missing java info in java datamodel node");
151 }
152 return javaFileInfo;
153 }
154
155 /**
156 * Sets the java file info object.
157 *
158 * @param javaInfo java file info object
159 */
160 @Override
161 public void setJavaFileInfo(JavaFileInfo javaInfo) {
162 javaFileInfo = javaInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530163 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530164
165 @Override
166 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
167 return tempJavaCodeFragmentFiles;
168 }
169
170 @Override
171 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
172 tempJavaCodeFragmentFiles = fileHandle;
173 }
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530174}
Gaurav Agrawal56527662016-04-20 15:49:17 +0530175