blob: 1f138ea955bddd18c4c98eb30bc035bf3731f8b5 [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;
Gaurav Agrawal56527662016-04-20 15:49:17 +053020import org.onosproject.yangutils.datamodel.HasRpcNotification;
21import org.onosproject.yangutils.datamodel.YangInput;
22import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053024import org.onosproject.yangutils.datamodel.YangRpc;
Gaurav Agrawal56527662016-04-20 15:49:17 +053025import org.onosproject.yangutils.translator.exception.TranslatorException;
26import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
27import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
28import 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;
janani bde4ffab2016-04-15 16:18:30 +053031import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053032
Gaurav Agrawal56527662016-04-20 15:49:17 +053033import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent;
34import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
35import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo;
36
Vidyashree Rama6a72b792016-03-29 12:00:42 +053037/**
Bharat saraswald9822e92016-04-05 15:13:44 +053038 * Represents rpc information extended to support java code generation.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053039 */
Gaurav Agrawal56527662016-04-20 15:49:17 +053040public class YangJavaRpc extends YangRpc implements JavaCodeGenerator, HasJavaFileInfo {
Vidyashree Rama6a72b792016-03-29 12:00:42 +053041
42 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053043 * Contains the information of the java file being generated.
44 */
45 private JavaFileInfo javaFileInfo;
46
47 /**
48 * Creates an instance of YANG java rpc.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053049 */
50 public YangJavaRpc() {
Gaurav Agrawal56527662016-04-20 15:49:17 +053051 super();
52 setJavaFileInfo(new JavaFileInfo());
Vidyashree Rama6a72b792016-03-29 12:00:42 +053053 }
54
55 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053056 * Prepares the information for java code generation corresponding to YANG
Gaurav Agrawal56527662016-04-20 15:49:17 +053057 * rpc info.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053058 *
janani bde4ffab2016-04-15 16:18:30 +053059 * @param yangPlugin YANG plugin config
Gaurav Agrawal56527662016-04-20 15:49:17 +053060 * @throws IOException IO operations fails
Vidyashree Rama6a72b792016-03-29 12:00:42 +053061 */
62 @Override
janani bde4ffab2016-04-15 16:18:30 +053063 public void generateCodeEntry(YangPluginConfig yangPlugin) throws IOException {
Gaurav Agrawal56527662016-04-20 15:49:17 +053064
65 if (!(this instanceof YangNode)) {
66 // TODO:throw exception
67 }
68
69 // Add package information for rpc and create corresponding folder.
70 updatePackageInfo(this, yangPlugin);
71
72 // Get the parent module/sub-module.
73 YangNode parent = getParentNodeInGenCode((YangNode) this);
74
75 // Parent should be holder of rpc or notification.
76 if (!(parent instanceof HasRpcNotification)) {
77 throw new TranslatorException("parent node of rpc can only be module or sub-module");
78 }
79
80 /*
81 * Create attribute info for input and output of rpc and add it to the
82 * parent import list.
83 */
84
85 JavaAttributeInfo javaAttributeInfoOfInput = null;
86 JavaAttributeInfo javaAttributeInfoOfOutput = null;
87
88 // Get the child input and output node and obtain create java attribute info.
89 YangNode yangNode = this.getChild();
90 while (yangNode != null) {
91 if (yangNode instanceof YangInput) {
92 javaAttributeInfoOfInput = getCurNodeAsAttributeInParent(parent, false, yangNode.getName());
93 } else if (yangNode instanceof YangOutput) {
94 javaAttributeInfoOfOutput = getCurNodeAsAttributeInParent(parent, false, yangNode.getName());
95 } else {
96 // TODO throw exception
97 }
98 yangNode = yangNode.getNextSibling();
99 }
100
101 if (!(parent instanceof HasTempJavaCodeFragmentFiles)) {
102 throw new TranslatorException("missing parent temp file handle");
103 }
104
105 /*
106 * Add the rpc information to the parent's service temp file.
107 */
108 ((HasTempJavaCodeFragmentFiles) parent)
109 .getTempJavaCodeFragmentFiles()
110 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput,
111 ((YangNode) this).getName());
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530112 }
113
114 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +0530115 * Creates a java file using the YANG rpc info.
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530116 *
Gaurav Agrawal56527662016-04-20 15:49:17 +0530117 * @throws IOException IO operations fails
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530118 */
119 @Override
120 public void generateCodeExit() throws IOException {
Gaurav Agrawal56527662016-04-20 15:49:17 +0530121 // No file will be generated during RPC exit.
122 }
123
124 /**
125 * Returns the generated java file information.
126 *
127 * @return generated java file information
128 */
129 @Override
130 public JavaFileInfo getJavaFileInfo() {
131
132 if (javaFileInfo == null) {
133 throw new TranslatorException("missing java info in java datamodel node");
134 }
135 return javaFileInfo;
136 }
137
138 /**
139 * Sets the java file info object.
140 *
141 * @param javaInfo java file info object
142 */
143 @Override
144 public void setJavaFileInfo(JavaFileInfo javaInfo) {
145 javaFileInfo = javaInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530146 }
147}
Gaurav Agrawal56527662016-04-20 15:49:17 +0530148