blob: 2bf1989438bd859656b90332814cd71e19fbb4d3 [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;
Bharat saraswalcad0e652016-05-26 23:48:38 +053020import java.util.List;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053021
22import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Gaurav Agrawal56527662016-04-20 15:49:17 +053023import org.onosproject.yangutils.datamodel.YangInput;
Bharat saraswalcad0e652016-05-26 23:48:38 +053024import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangLeafList;
26import org.onosproject.yangutils.datamodel.YangLeavesHolder;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053027import org.onosproject.yangutils.datamodel.YangList;
Gaurav Agrawal56527662016-04-20 15:49:17 +053028import org.onosproject.yangutils.datamodel.YangNode;
29import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053030import org.onosproject.yangutils.datamodel.YangRpc;
Bharat saraswalcad0e652016-05-26 23:48:38 +053031import org.onosproject.yangutils.datamodel.YangType;
Bharat saraswal4ca63712016-05-28 17:50:57 +053032import org.onosproject.yangutils.datamodel.YangUses;
Gaurav Agrawal56527662016-04-20 15:49:17 +053033import org.onosproject.yangutils.translator.exception.TranslatorException;
Gaurav Agrawal56527662016-04-20 15:49:17 +053034import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053035import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
Gaurav Agrawal56527662016-04-20 15:49:17 +053036import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053038import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053039import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053040import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
41import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles;
janani bde4ffab2016-04-15 16:18:30 +053042import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vidyashree Rama6a72b792016-03-29 12:00:42 +053043
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053044import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
45import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Bharat saraswalcad0e652016-05-26 23:48:38 +053046import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaDataType;
47import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass;
48import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage;
49import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
janani b4a6711a2016-05-17 13:12:22 +053050import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Bharat saraswal4ca63712016-05-28 17:50:57 +053051import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Gaurav Agrawal56527662016-04-20 15:49:17 +053052import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
53import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053054import static org.onosproject.yangutils.utils.UtilConstants.ACTIVATE;
55import static org.onosproject.yangutils.utils.UtilConstants.COMPONENT;
56import static org.onosproject.yangutils.utils.UtilConstants.DEACTIVATE;
57import static org.onosproject.yangutils.utils.UtilConstants.MANAGER;
58import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE;
59import static org.onosproject.yangutils.utils.UtilConstants.REFERENCE_CARDINALITY;
60import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
Bharat saraswal4ca63712016-05-28 17:50:57 +053061import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
Gaurav Agrawal56527662016-04-20 15:49:17 +053062
Vidyashree Rama6a72b792016-03-29 12:00:42 +053063/**
Bharat saraswald9822e92016-04-05 15:13:44 +053064 * Represents rpc information extended to support java code generation.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053065 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053066public class YangJavaRpc
67 extends YangRpc
68 implements JavaCodeGenerator, JavaCodeGeneratorInfo {
Vidyashree Rama6a72b792016-03-29 12:00:42 +053069
70 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053071 * Contains the information of the java file being generated.
72 */
73 private JavaFileInfo javaFileInfo;
74
75 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076 * Temproary file for code generation.
77 */
78 private TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles;
79
Bharat saraswalcad0e652016-05-26 23:48:38 +053080 private boolean isInputLeafHolder;
81 private boolean isOutputLeafHolder;
82 private boolean isInputSingleChildHolder;
83 private boolean isOutputSingleChildHolder;
84
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053085 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053086 * Creates an instance of YANG java rpc.
Vidyashree Rama6a72b792016-03-29 12:00:42 +053087 */
88 public YangJavaRpc() {
Gaurav Agrawal56527662016-04-20 15:49:17 +053089 super();
90 setJavaFileInfo(new JavaFileInfo());
Vidyashree Rama6a72b792016-03-29 12:00:42 +053091 }
92
93 /**
Gaurav Agrawal56527662016-04-20 15:49:17 +053094 * Returns the generated java file information.
95 *
96 * @return generated java file information
97 */
98 @Override
99 public JavaFileInfo getJavaFileInfo() {
100
101 if (javaFileInfo == null) {
102 throw new TranslatorException("missing java info in java datamodel node");
103 }
104 return javaFileInfo;
105 }
106
107 /**
108 * Sets the java file info object.
109 *
110 * @param javaInfo java file info object
111 */
112 @Override
113 public void setJavaFileInfo(JavaFileInfo javaInfo) {
114 javaFileInfo = javaInfo;
Vidyashree Rama6a72b792016-03-29 12:00:42 +0530115 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530116
117 @Override
118 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
119 return tempJavaCodeFragmentFiles;
120 }
121
122 @Override
123 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
124 tempJavaCodeFragmentFiles = fileHandle;
125 }
Gaurav Agrawal56527662016-04-20 15:49:17 +0530126
Bharat saraswalcad0e652016-05-26 23:48:38 +0530127 /**
128 * Prepares the information for java code generation corresponding to YANG
129 * RPC info.
130 *
131 * @param yangPlugin YANG plugin config
132 * @throws TranslatorException translator operations fails
133 */
134 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530135 public void generateCodeEntry(YangPluginConfig yangPlugin)
136 throws TranslatorException {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530137
138 if (!(this instanceof JavaCodeGeneratorInfo)) {
139 // TODO:throw exception
140 }
141
142 // Add package information for rpc and create corresponding folder.
143 try {
144 updatePackageInfo(this, yangPlugin);
145 if (this.getChild() != null) {
146 processNodeEntry(this.getChild(), yangPlugin);
147 if (this.getChild().getNextSibling() != null) {
148 processNodeEntry(this.getChild().getNextSibling(), yangPlugin);
149 }
150 }
151 } catch (IOException e) {
152 throw new TranslatorException("Failed to prepare generate code entry for RPC node " + this.getName());
153 }
154 }
155
156 /**
157 * Creates a java file using the YANG RPC info.
158 *
159 * @throws TranslatorException translator operations fails
160 */
161 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530162 public void generateCodeExit()
163 throws TranslatorException {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530164 // Get the parent module/sub-module.
165 YangNode parent = getParentNodeInGenCode(this);
166
167 // Parent should be holder of rpc or notification.
168 if (!(parent instanceof RpcNotificationContainer)) {
169 throw new TranslatorException("parent node of rpc can only be module or sub-module");
170 }
171
172 /*
173 * Create attribute info for input and output of rpc and add it to the
174 * parent import list.
175 */
176
177 JavaAttributeInfo javaAttributeInfoOfInput = null;
178 JavaAttributeInfo javaAttributeInfoOfOutput = null;
179
180 // Get the child input and output node and obtain create java attribute
181 // info.
182 YangNode yangNode = this.getChild();
183 while (yangNode != null) {
184 if (yangNode instanceof YangInput) {
185 javaAttributeInfoOfInput = processNodeExit(yangNode, getJavaFileInfo().getPluginConfig());
186
187 } else if (yangNode instanceof YangOutput) {
188 javaAttributeInfoOfOutput = processNodeExit(yangNode, getJavaFileInfo().getPluginConfig());
189 } else {
190 // TODO throw exception
191 }
192 yangNode = yangNode.getNextSibling();
193 }
194
195 if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) {
196 throw new TranslatorException("missing parent temp file handle");
197 }
198
199 /*
200 * Add the rpc information to the parent's service temp file.
201 */
202 try {
Bharat saraswal4ca63712016-05-28 17:50:57 +0530203 String rpcsChildNodePkg = getPackageDirPathFromJavaJPackage(getJavaFileInfo().getBaseCodeGenPath() +
204 (getJavaFileInfo().getPackage() + "." + getJavaFileInfo().getJavaName()).toLowerCase());
205
Bharat saraswalcad0e652016-05-26 23:48:38 +0530206 ((TempJavaCodeFragmentFilesContainer) parent).getTempJavaCodeFragmentFiles().getServiceTempFiles()
207 .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput,
208 ((JavaFileInfoContainer) parent).getJavaFileInfo().getPluginConfig(),
209 ((YangNode) this).getName(), isInputLeafHolder(), isOutputLeafHolder(),
210 isInputSingleChildHolder(), isOutputSingleChildHolder());
Bharat saraswal4ca63712016-05-28 17:50:57 +0530211
212 if (javaAttributeInfoOfInput != null && javaAttributeInfoOfOutput != null
213 && isInputLeafHolder() && isOutputLeafHolder()) {
214 deleteDirectoryWhenNoFileIsGeneratedForInputOutput(rpcsChildNodePkg);
215 } else if (javaAttributeInfoOfInput != null && javaAttributeInfoOfOutput == null
216 && isInputLeafHolder()) {
217 deleteDirectoryWhenNoFileIsGeneratedForInputOutput(rpcsChildNodePkg);
218 } else if (javaAttributeInfoOfInput == null && javaAttributeInfoOfOutput != null
219 && isOutputLeafHolder()) {
220 deleteDirectoryWhenNoFileIsGeneratedForInputOutput(rpcsChildNodePkg);
221 } else {
222 YangNode node = this.getChild();
223 while (node != null) {
224 YangNode tempNode = node.getChild();
225 while (tempNode != null) {
226 if (tempNode instanceof YangUses) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530227 boolean isCodeGenerated = true;
228 if (node instanceof YangJavaInput) {
229 isCodeGenerated = ((YangJavaInput) node).isCodeGenFlag();
230 } else if (node instanceof YangJavaOutput) {
231 isCodeGenerated = ((YangJavaOutput) node).isCodeGenFlag();
232 }
233 if (!isCodeGenerated) {
234 deleteDirectoryWhenNoFileIsGeneratedForInputOutput(rpcsChildNodePkg);
235 }
Bharat saraswal4ca63712016-05-28 17:50:57 +0530236 }
237 tempNode = tempNode.getNextSibling();
238 }
239 node = node.getNextSibling();
240 }
241 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530242 } catch (IOException e) {
243 throw new TranslatorException("Failed to generate code for RPC node " + this.getName());
244 }
245 // No file will be generated during RPC exit.
246 }
247
248 /**
Bharat saraswal4ca63712016-05-28 17:50:57 +0530249 * When there is no file generation for input output node we should delete the directory generated
250 * for RPC.
251 *
252 * @param emptyPkg empty package
253 * @throws IOException when fails to do IO operations
254 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530255 private void deleteDirectoryWhenNoFileIsGeneratedForInputOutput(String emptyPkg)
256 throws IOException {
Bharat saraswal4ca63712016-05-28 17:50:57 +0530257 deleteDirectory(emptyPkg);
258 }
259
260 /**
Bharat saraswalcad0e652016-05-26 23:48:38 +0530261 * Creates an attribute info object corresponding to a data model node and
262 * return it.
263 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530264 * @param childNode child data model node(input / output) for which the java code generation
265 * is being handled
Bharat saraswalcad0e652016-05-26 23:48:38 +0530266 * @param currentNode parent node (module / sub-module) in which the child node is an attribute
267 * @return AttributeInfo attribute details required to add in temporary
268 * files
269 */
270 public JavaAttributeInfo getChildNodeAsAttributeInParentService(
271 YangNode childNode, YangNode currentNode) {
272
273 YangNode parentNode = getParentNodeInGenCode(currentNode);
274
275 String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName();
276 /*
277 * Get the import info corresponding to the attribute for import in
278 * generated java files or qualified access
279 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530280 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(childNode,
Bharat saraswalcad0e652016-05-26 23:48:38 +0530281 getCapitalCase(childNodeName));
282 if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) {
283 throw new TranslatorException("Parent node does not have file info");
284 }
285
286 TempJavaFragmentFiles tempJavaFragmentFiles;
287 tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode)
288 .getTempJavaCodeFragmentFiles()
289 .getServiceTempFiles();
290
291 if (tempJavaFragmentFiles == null) {
292 throw new TranslatorException("Parent node does not have service file info");
293 }
294 boolean isQualified = addImportToService(qualifiedTypeInfo);
295 return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false);
296 }
297
298 /**
299 * Process input/output nodes.
300 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530301 * @param node YANG node
Bharat saraswalcad0e652016-05-26 23:48:38 +0530302 * @param yangPluginConfig plugin configurations
303 */
304 private void processNodeEntry(YangNode node, YangPluginConfig yangPluginConfig) {
305 YangLeavesHolder holder = (YangLeavesHolder) node;
306 if (node.getChild() == null) {
307 if (holder.getListOfLeaf() != null && holder.getListOfLeafList().isEmpty()
308 && holder.getListOfLeaf().size() == 1) {
309 setCodeGenFlagForNode(node, false);
310 } else if (holder.getListOfLeaf().isEmpty() && holder.getListOfLeafList() != null
311 && holder.getListOfLeafList().size() == 1) {
312 setCodeGenFlagForNode(node, false);
313 } else {
314 setCodeGenFlagForNode(node, true);
315 }
316 } else if (node.getChild() != null && holder.getListOfLeaf().isEmpty()
317 && holder.getListOfLeafList().isEmpty()) {
318 if (getNumberOfChildNodes(node) == 1) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530319 if (node.getChild() instanceof YangUses) {
320 setCodeGenFlagForNode(node, getCodeGenFlagWhenUsesIsAChildNode(node));
321 } else {
322 setCodeGenFlagForNode(node, false);
323 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530324 } else {
325 setCodeGenFlagForNode(node, true);
326 }
327 } else {
328 setCodeGenFlagForNode(node, true);
329 }
330 }
331
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530332 /*Returns code gen flag when uses is a child node.*/
333 private boolean getCodeGenFlagWhenUsesIsAChildNode(YangNode node) {
334 YangUses uses = (YangUses) node.getChild();
335
336 List<YangNode> groupingChildResolvedNodes = uses.getUsesResolvedNodeList();
337 List<List<YangLeaf>> groupingChildResolvedLeaves = uses.getUsesResolvedLeavesList();
338 List<List<YangLeafList>> groupingChildResolvedLeafList = uses.getUsesResolvedListOfLeafList();
339
340 if (!groupingChildResolvedNodes.isEmpty()
341 && groupingChildResolvedNodes.size() == 1
342 && groupingChildResolvedLeaves.isEmpty()
343 && groupingChildResolvedLeafList.isEmpty()) {
344 return false;
345 } else if (groupingChildResolvedNodes.isEmpty()
346 && isOnlyOneLeafPresentInGrouping(groupingChildResolvedLeaves)
347 && groupingChildResolvedLeafList.isEmpty()) {
348 return false;
349 } else if (groupingChildResolvedNodes.isEmpty()
350 && groupingChildResolvedLeaves.isEmpty()
351 && isOnlyOneLeafListPresentInGrouping(groupingChildResolvedLeafList)) {
352 return false;
353 }
354 return (groupingChildResolvedNodes.isEmpty()
355 && groupingChildResolvedLeaves.isEmpty()
356 && groupingChildResolvedLeafList.isEmpty());
357 }
358
Bharat saraswalcad0e652016-05-26 23:48:38 +0530359 /**
360 * Process input/output nodes.
361 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530362 * @param node YANG node
Bharat saraswalcad0e652016-05-26 23:48:38 +0530363 * @param yangPluginConfig plugin configurations
364 * @return java attribute info
365 */
366 private JavaAttributeInfo processNodeExit(YangNode node, YangPluginConfig yangPluginConfig) {
367 YangLeavesHolder holder = (YangLeavesHolder) node;
368 if (node.getChild() == null) {
369 if (holder.getListOfLeaf() != null && holder.getListOfLeafList().isEmpty()
370 && holder.getListOfLeaf().size() == 1) {
371 return processNodeWhenOnlyOneLeafIsPresent(node, yangPluginConfig);
372
373 } else if (holder.getListOfLeaf().isEmpty() && holder.getListOfLeafList() != null
374 && holder.getListOfLeafList().size() == 1) {
375 return processNodeWhenOnlyOneLeafListIsPresent(node, yangPluginConfig);
376 } else {
377 return processNodeWhenMultipleContaintsArePresent(node);
378 }
379 } else if (node.getChild() != null && holder.getListOfLeaf().isEmpty()
380 && holder.getListOfLeafList().isEmpty()) {
381 if (getNumberOfChildNodes(node) == 1) {
382 return processNodeWhenOnlyOneChildNodeIsPresent(node, yangPluginConfig);
383 } else {
384 return processNodeWhenMultipleContaintsArePresent(node);
385 }
386 } else {
387 return processNodeWhenMultipleContaintsArePresent(node);
388 }
389 }
390
391 /**
392 * Process input/output node when one leaf is present.
393 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530394 * @param node input/output node
Bharat saraswalcad0e652016-05-26 23:48:38 +0530395 * @param yangPluginConfig plugin configurations
396 * @return java attribute for node
397 */
398 private JavaAttributeInfo processNodeWhenOnlyOneLeafIsPresent(YangNode node,
399 YangPluginConfig yangPluginConfig) {
400
401 YangLeavesHolder holder = (YangLeavesHolder) node;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530402 YangLeaf leaf = holder.getListOfLeaf().get(0);
403 setLeafHolderFlag(node, true);
404 return getAttributeInfoForLeaf(leaf, yangPluginConfig);
Bharat saraswalcad0e652016-05-26 23:48:38 +0530405 }
406
407 /**
408 * Process input/output node when one leaf list is present.
409 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530410 * @param node input/output node
Bharat saraswalcad0e652016-05-26 23:48:38 +0530411 * @param yangPluginConfig plugin configurations
412 * @return java attribute for node
413 */
414 private JavaAttributeInfo processNodeWhenOnlyOneLeafListIsPresent(YangNode node,
415 YangPluginConfig yangPluginConfig) {
416
417 YangLeavesHolder holder = (YangLeavesHolder) node;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530418 YangLeafList leafList = holder.getListOfLeafList().get(0);
419 setLeafHolderFlag(node, true);
420 return getAttributeInfoForLeafList(leafList, yangPluginConfig);
421 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530422
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530423 /**
424 * Returns java attribute info for leaf.
425 *
426 * @param leaf YANG leaf
427 * @param yangPluginConfig plugin configurations
428 * @return java attribute info for leaf
429 */
430 private JavaAttributeInfo getAttributeInfoForLeaf(YangLeaf leaf, YangPluginConfig yangPluginConfig) {
431 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf;
432 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
433 javaLeaf.updateJavaQualifiedInfo();
434 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
435 javaLeaf.getJavaQualifiedInfo(),
436 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
437 javaLeaf.getDataType(),
438 addTypeImport(javaLeaf.getDataType(), false, yangPluginConfig), false);
439 return javaAttributeInfo;
440 }
441
442 /**
443 * Returns java attribute info for leaf list.
444 *
445 * @param leafList YANG leaf list
446 * @param yangPluginConfig plugin configurations
447 * @return java attribute info for leaf list
448 */
449 private JavaAttributeInfo getAttributeInfoForLeafList(YangLeafList leafList,
450 YangPluginConfig yangPluginConfig) {
451 JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList;
452 javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver());
453 javaLeaf.updateJavaQualifiedInfo();
454 ((TempJavaCodeFragmentFilesContainer) this.getParent()).getTempJavaCodeFragmentFiles()
455 .getServiceTempFiles().getJavaImportData().setIfListImported(true);
456 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
457 javaLeaf.getJavaQualifiedInfo(),
458 javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()),
459 javaLeaf.getDataType(),
460 addTypeImport(javaLeaf.getDataType(), true, yangPluginConfig),
461 true);
462 return javaAttributeInfo;
Bharat saraswalcad0e652016-05-26 23:48:38 +0530463 }
464
465 /**
466 * Process input/output node when one child node is present.
467 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530468 * @param node input/output node
Bharat saraswalcad0e652016-05-26 23:48:38 +0530469 * @param yangPluginConfig plugin configurations
470 * @return java attribute for node
471 */
472 private JavaAttributeInfo processNodeWhenOnlyOneChildNodeIsPresent(YangNode node,
473 YangPluginConfig yangPluginConfig) {
474 JavaFileInfo rpcInfo = getJavaFileInfo();
475 String clsInfo = "";
476 JavaQualifiedTypeInfo childInfo = new JavaQualifiedTypeInfo();
477 if (node.getChild() instanceof YangJavaUses) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530478 YangJavaUses uses = (YangJavaUses) node.getChild();
479 return processNodeWhenUsesIsChild(uses, node, yangPluginConfig);
Bharat saraswalcad0e652016-05-26 23:48:38 +0530480 } else {
481 String pkg = (rpcInfo.getPackage() + "." + rpcInfo.getJavaName() + "."
482 + getCamelCase(node.getName(), yangPluginConfig.getConflictResolver())).toLowerCase();
483 clsInfo = getCapitalCase(
484 getCamelCase(node.getChild().getName(), yangPluginConfig.getConflictResolver()));
485 childInfo.setPkgInfo(pkg);
486 childInfo.setClassInfo(clsInfo);
487 }
488 boolean isList = false;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530489 if (node.getChild() instanceof YangList) {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530490 isList = true;
491 }
492 boolean isQualified = addImportToService(childInfo);
493
494 JavaAttributeInfo javaAttributeInfo =
495 getAttributeInfoForTheData(childInfo, clsInfo, null, isQualified, isList);
496
497 setLeafHolderFlag(node, false);
498 setSingleChildHolderFlag(node, true);
499 return javaAttributeInfo;
500 }
501
502 /**
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530503 * Returns java attribute info when child node is uses.
504 *
505 * @param uses YANG uses node
506 * @param node YANG node
507 * @param yangPluginConfig plugin configurations
508 * @return java attribute info when child node is uses
509 */
510 private JavaAttributeInfo processNodeWhenUsesIsChild(YangUses uses, YangNode node,
511 YangPluginConfig yangPluginConfig) {
512 String clsInfo = "";
513 JavaQualifiedTypeInfo childInfo = new JavaQualifiedTypeInfo();
514 List<YangNode> groupingChildResolvedNodes = uses.getUsesResolvedNodeList();
515 List<List<YangLeaf>> groupingChildResolvedLeaves = uses.getUsesResolvedLeavesList();
516 List<List<YangLeafList>> groupingChildResolvedLeafList = uses.getUsesResolvedListOfLeafList();
517
518 if (!groupingChildResolvedNodes.isEmpty()
519 && groupingChildResolvedNodes.size() == 1
520 && groupingChildResolvedLeaves.isEmpty()
521 && groupingChildResolvedLeafList.isEmpty()) {
522 YangNode childNodeOfGrouping = groupingChildResolvedNodes.get(0);
523 boolean isList = false;
524 if (childNodeOfGrouping instanceof YangList) {
525 isList = true;
526 }
527 JavaFileInfo childNodeOfGroupingInfo =
528 ((JavaFileInfoContainer) childNodeOfGrouping).getJavaFileInfo();
529 childInfo.setClassInfo(getCapitalCase(childNodeOfGroupingInfo.getJavaName()));
530 childInfo.setPkgInfo(childNodeOfGroupingInfo.getPackage());
531 clsInfo = childInfo.getClassInfo();
532 boolean isQualified = addImportToService(childInfo);
533 setLeafHolderFlag(node, false);
534 setSingleChildHolderFlag(node, true);
535 return getAttributeInfoForTheData(childInfo, clsInfo, null, isQualified, isList);
536
537 } else if (groupingChildResolvedNodes.isEmpty()
538 && isOnlyOneLeafPresentInGrouping(groupingChildResolvedLeaves)
539 && groupingChildResolvedLeafList.isEmpty()) {
540
541 YangLeaf leaf = groupingChildResolvedLeaves.get(0).get(0);
542 setLeafHolderFlag(node, true);
543 return getAttributeInfoForLeaf(leaf, yangPluginConfig);
544
545 } else if (groupingChildResolvedNodes.isEmpty()
546 && groupingChildResolvedLeaves.isEmpty()
547 && isOnlyOneLeafListPresentInGrouping(groupingChildResolvedLeafList)) {
548 YangLeafList leafList = groupingChildResolvedLeafList.get(0).get(0);
549 setLeafHolderFlag(node, true);
550 return getAttributeInfoForLeafList(leafList, yangPluginConfig);
551
552 } else if (groupingChildResolvedNodes.isEmpty()
553 && groupingChildResolvedLeaves.isEmpty()
554 && groupingChildResolvedLeafList.isEmpty()) {
555 return null;
556 } else {
557 return processNodeWhenMultipleContaintsArePresent(node);
558 }
559 }
560
561 /*Return true if only one leaf is present in grouping node.*/
562 private boolean isOnlyOneLeafPresentInGrouping(List<List<YangLeaf>> groupingChildResolvedLeaves) {
563 if (!groupingChildResolvedLeaves.isEmpty() &&
564 groupingChildResolvedLeaves.size() == 1) {
565 List<YangLeaf> leaves = groupingChildResolvedLeaves.get(0);
566 return leaves.size() == 1;
567 } else {
568 return false;
569 }
570 }
571
572 /*Returns true is only one leaf list is present in grouping node.*/
573 private boolean isOnlyOneLeafListPresentInGrouping(List<List<YangLeafList>> groupingChildResolvedLeafList) {
574
575 if (!groupingChildResolvedLeafList.isEmpty() &&
576 groupingChildResolvedLeafList.size() == 1) {
577 List<YangLeafList> leaves = groupingChildResolvedLeafList.get(0);
578 return leaves.size() == 1;
579 } else {
580 return false;
581 }
582 }
583
584 /**
Bharat saraswalcad0e652016-05-26 23:48:38 +0530585 * Process input/output node when multiple leaf and child nodes are present.
586 *
587 * @param node input/output node
588 * @return java attribute for node
589 */
590 private JavaAttributeInfo processNodeWhenMultipleContaintsArePresent(YangNode node) {
591
592 setLeafHolderFlag(node, false);
593 setSingleChildHolderFlag(node, false);
594 return getChildNodeAsAttributeInParentService(node, this);
595 }
596
597 /**
598 * Adds type import to the RPC import list.
599 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530600 * @param type YANG type
601 * @param isList is list attribute
Bharat saraswalcad0e652016-05-26 23:48:38 +0530602 * @param pluginConfig plugin configurations
603 * @return type import to the RPC import list
604 */
605 private boolean addTypeImport(YangType<?> type, boolean isList, YangPluginConfig pluginConfig) {
606
607 String classInfo = getJavaImportClass(type, isList, pluginConfig.getConflictResolver());
608 if (classInfo == null) {
609 classInfo = getJavaDataType(type);
610 return false;
611 } else {
612 classInfo = getJavaImportClass(type, isList, pluginConfig.getConflictResolver());
613 String pkgInfo = getJavaImportPackage(type, isList, pluginConfig.getConflictResolver());
614 JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
615 importInfo.setPkgInfo(pkgInfo);
616 importInfo.setClassInfo(classInfo);
617 if (!((JavaFileInfoContainer) this.getParent()).getJavaFileInfo().getJavaName().equals(classInfo)) {
618 return addImportToService(importInfo);
619 } else {
620 return true;
621 }
622 }
623 }
624
625 /**
626 * Adds to service class import list.
627 *
628 * @param importInfo import info
629 * @return true or false
630 */
631 private boolean addImportToService(JavaQualifiedTypeInfo importInfo) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530632 JavaFileInfo fileInfo = ((JavaFileInfoContainer) this.getParent()).getJavaFileInfo();
633
634 if (importInfo.getClassInfo().contentEquals(SERVICE)
635 || importInfo.getClassInfo().contentEquals(COMPONENT)
636 || importInfo.getClassInfo().contentEquals(getCapitalCase(ACTIVATE))
637 || importInfo.getClassInfo().contentEquals(getCapitalCase(DEACTIVATE))
638 || importInfo.getClassInfo().contentEquals(REFERENCE_CARDINALITY)
639 || importInfo.getClassInfo().contentEquals(REFERENCE)
640 || importInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + SERVICE))
641 || importInfo.getClassInfo().contentEquals(getCapitalCase(fileInfo.getJavaName() + MANAGER))) {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530642 return true;
643 }
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530644
645 String className;
646 className = getCapitalCase(fileInfo.getJavaName()) + "Service";
647
648 return ((TempJavaCodeFragmentFilesContainer) this.getParent()).getTempJavaCodeFragmentFiles()
649 .getServiceTempFiles().getJavaImportData().addImportInfo(importInfo,
650 className, fileInfo.getPackage());
651
Bharat saraswalcad0e652016-05-26 23:48:38 +0530652 }
653
654 /**
655 * Sets leaf holder flag for input/output.
656 *
657 * @param node input/output node
658 * @param flag true or false
659 */
660 private void setLeafHolderFlag(YangNode node, boolean flag) {
661 if (node instanceof YangJavaInput) {
662 setInputLeafHolder(flag);
663 } else {
664 setOutputLeafHolder(flag);
665 }
666 }
667
668 /**
669 * Sets sing child holder flag for input/output.
670 *
671 * @param node input/output node
672 * @param flag true or false
673 */
674 private void setSingleChildHolderFlag(YangNode node, boolean flag) {
675 if (node instanceof YangJavaInput) {
676 setInputSingleChildHolder(flag);
677 } else {
678 setOutputSingleChildHolder(flag);
679 }
680 }
681
682 /**
683 * Sets code generator flag for input and output.
684 *
685 * @param node YANG node
686 * @param flag cod generator flag
687 */
688 private void setCodeGenFlagForNode(YangNode node, boolean flag) {
689 if (node instanceof YangJavaInput) {
690 ((YangJavaInput) node).setCodeGenFlag(flag);
691 } else {
692 ((YangJavaOutput) node).setCodeGenFlag(flag);
693 }
694
695 }
696
697 /**
698 * Counts the number of child nodes of a YANG node.
699 *
700 * @param node YANG node
701 * @return count of children
702 */
703 private int getNumberOfChildNodes(YangNode node) {
704 YangNode tempNode = node.getChild();
705 int count = 0;
706 if (tempNode != null) {
707 count = 1;
708 }
709 while (tempNode != null) {
710
711 tempNode = tempNode.getNextSibling();
712 if (tempNode != null) {
713 count++;
714 }
715 }
716 return count;
717 }
718
719 /**
720 * Returns true if input is a leaf holder.
721 *
722 * @return true if input is a leaf holder
723 */
724 public boolean isInputLeafHolder() {
725 return isInputLeafHolder;
726 }
727
728 /**
729 * Sets true if input is a leaf holder.
730 *
731 * @param isInputLeafHolder true if input is a leaf holder
732 */
733 public void setInputLeafHolder(boolean isInputLeafHolder) {
734 this.isInputLeafHolder = isInputLeafHolder;
735 }
736
737 /**
738 * Returns true if output is a leaf holder.
739 *
740 * @return true if output is a leaf holder
741 */
742 public boolean isOutputLeafHolder() {
743 return isOutputLeafHolder;
744 }
745
746 /**
747 * Sets true if output is a leaf holder.
748 *
749 * @param isOutputLeafHolder true if output is a leaf holder
750 */
751 public void setOutputLeafHolder(boolean isOutputLeafHolder) {
752 this.isOutputLeafHolder = isOutputLeafHolder;
753 }
754
755 /**
756 * Returns true if input is single child holder.
757 *
758 * @return true if input is single child holder
759 */
760 public boolean isInputSingleChildHolder() {
761 return isInputSingleChildHolder;
762 }
763
764 /**
765 * Sets true if input is single child holder.
766 *
767 * @param isInputSingleChildHolder true if input is single child holder
768 */
769 public void setInputSingleChildHolder(boolean isInputSingleChildHolder) {
770 this.isInputSingleChildHolder = isInputSingleChildHolder;
771 }
772
773 /**
774 * Returns true if output is single child holder.
775 *
776 * @return true if output is single child holder
777 */
778 public boolean isOutputSingleChildHolder() {
779 return isOutputSingleChildHolder;
780 }
781
782 /**
783 * Sets true if output is single child holder.
784 *
785 * @param isOutputSingleChildHolder true if output is single child holder
786 */
787 public void setOutputSingleChildHolder(boolean isOutputSingleChildHolder) {
788 this.isOutputSingleChildHolder = isOutputSingleChildHolder;
789 }
790
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530791}