Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.javamodel; |
| 18 | |
| 19 | import java.io.IOException; |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame^] | 20 | import java.util.List; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 21 | |
| 22 | import org.onosproject.yangutils.datamodel.RpcNotificationContainer; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangInput; |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame^] | 24 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 25 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 26 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangNode; |
| 28 | import org.onosproject.yangutils.datamodel.YangOutput; |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangRpc; |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame^] | 30 | import org.onosproject.yangutils.datamodel.YangType; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo; |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 36 | import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo; |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
| 39 | import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles; |
janani b | de4ffab | 2016-04-15 16:18:30 +0530 | [diff] [blame] | 40 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 41 | |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame^] | 42 | import static org.onosproject.yangutils.datamodel.YangNodeType.LIST_NODE; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData; |
| 44 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode; |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame^] | 45 | import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.resolveGroupingsQuailifiedInfo; |
| 46 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaDataType; |
| 47 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass; |
| 48 | import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage; |
| 49 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
janani b | 4a6711a | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase; |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode; |
| 52 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo; |
| 53 | |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 54 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 55 | * Represents rpc information extended to support java code generation. |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 56 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 57 | public class YangJavaRpc |
| 58 | extends YangRpc |
| 59 | implements JavaCodeGenerator, JavaCodeGeneratorInfo { |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 60 | |
| 61 | /** |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 62 | * Contains the information of the java file being generated. |
| 63 | */ |
| 64 | private JavaFileInfo javaFileInfo; |
| 65 | |
| 66 | /** |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 67 | * Temproary file for code generation. |
| 68 | */ |
| 69 | private TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles; |
| 70 | |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame^] | 71 | private boolean isInputLeafHolder; |
| 72 | private boolean isOutputLeafHolder; |
| 73 | private boolean isInputSingleChildHolder; |
| 74 | private boolean isOutputSingleChildHolder; |
| 75 | |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 76 | /** |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 77 | * Creates an instance of YANG java rpc. |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 78 | */ |
| 79 | public YangJavaRpc() { |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 80 | super(); |
| 81 | setJavaFileInfo(new JavaFileInfo()); |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 85 | * Returns the generated java file information. |
| 86 | * |
| 87 | * @return generated java file information |
| 88 | */ |
| 89 | @Override |
| 90 | public JavaFileInfo getJavaFileInfo() { |
| 91 | |
| 92 | if (javaFileInfo == null) { |
| 93 | throw new TranslatorException("missing java info in java datamodel node"); |
| 94 | } |
| 95 | return javaFileInfo; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sets the java file info object. |
| 100 | * |
| 101 | * @param javaInfo java file info object |
| 102 | */ |
| 103 | @Override |
| 104 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 105 | javaFileInfo = javaInfo; |
Vidyashree Rama | 6a72b79 | 2016-03-29 12:00:42 +0530 | [diff] [blame] | 106 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
| 110 | return tempJavaCodeFragmentFiles; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 115 | tempJavaCodeFragmentFiles = fileHandle; |
| 116 | } |
Gaurav Agrawal | 5652766 | 2016-04-20 15:49:17 +0530 | [diff] [blame] | 117 | |
Bharat saraswal | cad0e65 | 2016-05-26 23:48:38 +0530 | [diff] [blame^] | 118 | /** |
| 119 | * Prepares the information for java code generation corresponding to YANG |
| 120 | * RPC info. |
| 121 | * |
| 122 | * @param yangPlugin YANG plugin config |
| 123 | * @throws TranslatorException translator operations fails |
| 124 | */ |
| 125 | @Override |
| 126 | public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException { |
| 127 | |
| 128 | if (!(this instanceof JavaCodeGeneratorInfo)) { |
| 129 | // TODO:throw exception |
| 130 | } |
| 131 | |
| 132 | // Add package information for rpc and create corresponding folder. |
| 133 | try { |
| 134 | updatePackageInfo(this, yangPlugin); |
| 135 | if (this.getChild() != null) { |
| 136 | processNodeEntry(this.getChild(), yangPlugin); |
| 137 | if (this.getChild().getNextSibling() != null) { |
| 138 | processNodeEntry(this.getChild().getNextSibling(), yangPlugin); |
| 139 | } |
| 140 | } |
| 141 | } catch (IOException e) { |
| 142 | throw new TranslatorException("Failed to prepare generate code entry for RPC node " + this.getName()); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates a java file using the YANG RPC info. |
| 148 | * |
| 149 | * @throws TranslatorException translator operations fails |
| 150 | */ |
| 151 | @Override |
| 152 | public void generateCodeExit() throws TranslatorException { |
| 153 | // Get the parent module/sub-module. |
| 154 | YangNode parent = getParentNodeInGenCode(this); |
| 155 | |
| 156 | // Parent should be holder of rpc or notification. |
| 157 | if (!(parent instanceof RpcNotificationContainer)) { |
| 158 | throw new TranslatorException("parent node of rpc can only be module or sub-module"); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Create attribute info for input and output of rpc and add it to the |
| 163 | * parent import list. |
| 164 | */ |
| 165 | |
| 166 | JavaAttributeInfo javaAttributeInfoOfInput = null; |
| 167 | JavaAttributeInfo javaAttributeInfoOfOutput = null; |
| 168 | |
| 169 | // Get the child input and output node and obtain create java attribute |
| 170 | // info. |
| 171 | YangNode yangNode = this.getChild(); |
| 172 | while (yangNode != null) { |
| 173 | if (yangNode instanceof YangInput) { |
| 174 | javaAttributeInfoOfInput = processNodeExit(yangNode, getJavaFileInfo().getPluginConfig()); |
| 175 | |
| 176 | } else if (yangNode instanceof YangOutput) { |
| 177 | javaAttributeInfoOfOutput = processNodeExit(yangNode, getJavaFileInfo().getPluginConfig()); |
| 178 | } else { |
| 179 | // TODO throw exception |
| 180 | } |
| 181 | yangNode = yangNode.getNextSibling(); |
| 182 | } |
| 183 | |
| 184 | if (!(parent instanceof TempJavaCodeFragmentFilesContainer)) { |
| 185 | throw new TranslatorException("missing parent temp file handle"); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Add the rpc information to the parent's service temp file. |
| 190 | */ |
| 191 | try { |
| 192 | ((TempJavaCodeFragmentFilesContainer) parent).getTempJavaCodeFragmentFiles().getServiceTempFiles() |
| 193 | .addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, |
| 194 | ((JavaFileInfoContainer) parent).getJavaFileInfo().getPluginConfig(), |
| 195 | ((YangNode) this).getName(), isInputLeafHolder(), isOutputLeafHolder(), |
| 196 | isInputSingleChildHolder(), isOutputSingleChildHolder()); |
| 197 | } catch (IOException e) { |
| 198 | throw new TranslatorException("Failed to generate code for RPC node " + this.getName()); |
| 199 | } |
| 200 | // No file will be generated during RPC exit. |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Creates an attribute info object corresponding to a data model node and |
| 205 | * return it. |
| 206 | * |
| 207 | * @param childNode child data model node(input / output) for which the java code generation |
| 208 | * is being handled |
| 209 | * @param currentNode parent node (module / sub-module) in which the child node is an attribute |
| 210 | * @return AttributeInfo attribute details required to add in temporary |
| 211 | * files |
| 212 | */ |
| 213 | public JavaAttributeInfo getChildNodeAsAttributeInParentService( |
| 214 | YangNode childNode, YangNode currentNode) { |
| 215 | |
| 216 | YangNode parentNode = getParentNodeInGenCode(currentNode); |
| 217 | |
| 218 | String childNodeName = ((JavaFileInfoContainer) childNode).getJavaFileInfo().getJavaName(); |
| 219 | /* |
| 220 | * Get the import info corresponding to the attribute for import in |
| 221 | * generated java files or qualified access |
| 222 | */ |
| 223 | JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(currentNode, |
| 224 | getCapitalCase(childNodeName)); |
| 225 | if (!(parentNode instanceof TempJavaCodeFragmentFilesContainer)) { |
| 226 | throw new TranslatorException("Parent node does not have file info"); |
| 227 | } |
| 228 | |
| 229 | TempJavaFragmentFiles tempJavaFragmentFiles; |
| 230 | tempJavaFragmentFiles = ((TempJavaCodeFragmentFilesContainer) parentNode) |
| 231 | .getTempJavaCodeFragmentFiles() |
| 232 | .getServiceTempFiles(); |
| 233 | |
| 234 | if (tempJavaFragmentFiles == null) { |
| 235 | throw new TranslatorException("Parent node does not have service file info"); |
| 236 | } |
| 237 | boolean isQualified = addImportToService(qualifiedTypeInfo); |
| 238 | return getAttributeInfoForTheData(qualifiedTypeInfo, childNodeName, null, isQualified, false); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Process input/output nodes. |
| 243 | * |
| 244 | * @param node YANG node |
| 245 | * @param yangPluginConfig plugin configurations |
| 246 | */ |
| 247 | private void processNodeEntry(YangNode node, YangPluginConfig yangPluginConfig) { |
| 248 | YangLeavesHolder holder = (YangLeavesHolder) node; |
| 249 | if (node.getChild() == null) { |
| 250 | if (holder.getListOfLeaf() != null && holder.getListOfLeafList().isEmpty() |
| 251 | && holder.getListOfLeaf().size() == 1) { |
| 252 | setCodeGenFlagForNode(node, false); |
| 253 | } else if (holder.getListOfLeaf().isEmpty() && holder.getListOfLeafList() != null |
| 254 | && holder.getListOfLeafList().size() == 1) { |
| 255 | setCodeGenFlagForNode(node, false); |
| 256 | } else { |
| 257 | setCodeGenFlagForNode(node, true); |
| 258 | } |
| 259 | } else if (node.getChild() != null && holder.getListOfLeaf().isEmpty() |
| 260 | && holder.getListOfLeafList().isEmpty()) { |
| 261 | if (getNumberOfChildNodes(node) == 1) { |
| 262 | setCodeGenFlagForNode(node, false); |
| 263 | } else { |
| 264 | setCodeGenFlagForNode(node, true); |
| 265 | } |
| 266 | } else { |
| 267 | setCodeGenFlagForNode(node, true); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Process input/output nodes. |
| 273 | * |
| 274 | * @param node YANG node |
| 275 | * @param yangPluginConfig plugin configurations |
| 276 | * @return java attribute info |
| 277 | */ |
| 278 | private JavaAttributeInfo processNodeExit(YangNode node, YangPluginConfig yangPluginConfig) { |
| 279 | YangLeavesHolder holder = (YangLeavesHolder) node; |
| 280 | if (node.getChild() == null) { |
| 281 | if (holder.getListOfLeaf() != null && holder.getListOfLeafList().isEmpty() |
| 282 | && holder.getListOfLeaf().size() == 1) { |
| 283 | return processNodeWhenOnlyOneLeafIsPresent(node, yangPluginConfig); |
| 284 | |
| 285 | } else if (holder.getListOfLeaf().isEmpty() && holder.getListOfLeafList() != null |
| 286 | && holder.getListOfLeafList().size() == 1) { |
| 287 | return processNodeWhenOnlyOneLeafListIsPresent(node, yangPluginConfig); |
| 288 | } else { |
| 289 | return processNodeWhenMultipleContaintsArePresent(node); |
| 290 | } |
| 291 | } else if (node.getChild() != null && holder.getListOfLeaf().isEmpty() |
| 292 | && holder.getListOfLeafList().isEmpty()) { |
| 293 | if (getNumberOfChildNodes(node) == 1) { |
| 294 | return processNodeWhenOnlyOneChildNodeIsPresent(node, yangPluginConfig); |
| 295 | } else { |
| 296 | return processNodeWhenMultipleContaintsArePresent(node); |
| 297 | } |
| 298 | } else { |
| 299 | return processNodeWhenMultipleContaintsArePresent(node); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Process input/output node when one leaf is present. |
| 305 | * |
| 306 | * @param node input/output node |
| 307 | * @param yangPluginConfig plugin configurations |
| 308 | * @return java attribute for node |
| 309 | */ |
| 310 | private JavaAttributeInfo processNodeWhenOnlyOneLeafIsPresent(YangNode node, |
| 311 | YangPluginConfig yangPluginConfig) { |
| 312 | |
| 313 | YangLeavesHolder holder = (YangLeavesHolder) node; |
| 314 | List<YangLeaf> listOfLeaves = holder.getListOfLeaf(); |
| 315 | |
| 316 | for (YangLeaf leaf : listOfLeaves) { |
| 317 | if (!(leaf instanceof JavaLeafInfoContainer)) { |
| 318 | throw new TranslatorException("Leaf does not have java information"); |
| 319 | } |
| 320 | JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leaf; |
| 321 | javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver()); |
| 322 | javaLeaf.updateJavaQualifiedInfo(); |
| 323 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData( |
| 324 | javaLeaf.getJavaQualifiedInfo(), |
| 325 | javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()), |
| 326 | javaLeaf.getDataType(), |
| 327 | addTypeImport(javaLeaf.getDataType(), false, yangPluginConfig), false); |
| 328 | setLeafHolderFlag(node, true); |
| 329 | return javaAttributeInfo; |
| 330 | } |
| 331 | return null; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Process input/output node when one leaf list is present. |
| 336 | * |
| 337 | * @param node input/output node |
| 338 | * @param yangPluginConfig plugin configurations |
| 339 | * @return java attribute for node |
| 340 | */ |
| 341 | private JavaAttributeInfo processNodeWhenOnlyOneLeafListIsPresent(YangNode node, |
| 342 | YangPluginConfig yangPluginConfig) { |
| 343 | |
| 344 | YangLeavesHolder holder = (YangLeavesHolder) node; |
| 345 | List<YangLeafList> listOfLeafList = holder.getListOfLeafList(); |
| 346 | |
| 347 | for (YangLeafList leafList : listOfLeafList) { |
| 348 | if (!(leafList instanceof JavaLeafInfoContainer)) { |
| 349 | throw new TranslatorException("Leaf-list does not have java information"); |
| 350 | } |
| 351 | JavaLeafInfoContainer javaLeaf = (JavaLeafInfoContainer) leafList; |
| 352 | javaLeaf.setConflictResolveConfig(yangPluginConfig.getConflictResolver()); |
| 353 | javaLeaf.updateJavaQualifiedInfo(); |
| 354 | ((TempJavaCodeFragmentFilesContainer) this.getParent()).getTempJavaCodeFragmentFiles() |
| 355 | .getServiceTempFiles().getJavaImportData().setIfListImported(true); |
| 356 | JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData( |
| 357 | javaLeaf.getJavaQualifiedInfo(), |
| 358 | javaLeaf.getJavaName(yangPluginConfig.getConflictResolver()), |
| 359 | javaLeaf.getDataType(), |
| 360 | addTypeImport(javaLeaf.getDataType(), true, yangPluginConfig), |
| 361 | true); |
| 362 | setLeafHolderFlag(node, true); |
| 363 | return javaAttributeInfo; |
| 364 | } |
| 365 | return null; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Process input/output node when one child node is present. |
| 370 | * |
| 371 | * @param node input/output node |
| 372 | * @param yangPluginConfig plugin configurations |
| 373 | * @return java attribute for node |
| 374 | */ |
| 375 | private JavaAttributeInfo processNodeWhenOnlyOneChildNodeIsPresent(YangNode node, |
| 376 | YangPluginConfig yangPluginConfig) { |
| 377 | JavaFileInfo rpcInfo = getJavaFileInfo(); |
| 378 | String clsInfo = ""; |
| 379 | JavaQualifiedTypeInfo childInfo = new JavaQualifiedTypeInfo(); |
| 380 | if (node.getChild() instanceof YangJavaUses) { |
| 381 | childInfo = resolveGroupingsQuailifiedInfo(((YangJavaUses) node.getChild()).getRefGroup(), |
| 382 | yangPluginConfig); |
| 383 | clsInfo = getCapitalCase(getCamelCase(((YangJavaUses) node.getChild()).getRefGroup().getName(), |
| 384 | yangPluginConfig.getConflictResolver())); |
| 385 | } else { |
| 386 | String pkg = (rpcInfo.getPackage() + "." + rpcInfo.getJavaName() + "." |
| 387 | + getCamelCase(node.getName(), yangPluginConfig.getConflictResolver())).toLowerCase(); |
| 388 | clsInfo = getCapitalCase( |
| 389 | getCamelCase(node.getChild().getName(), yangPluginConfig.getConflictResolver())); |
| 390 | childInfo.setPkgInfo(pkg); |
| 391 | childInfo.setClassInfo(clsInfo); |
| 392 | } |
| 393 | boolean isList = false; |
| 394 | if (node.getChild().getNodeType().equals(LIST_NODE)) { |
| 395 | isList = true; |
| 396 | } |
| 397 | boolean isQualified = addImportToService(childInfo); |
| 398 | |
| 399 | JavaAttributeInfo javaAttributeInfo = |
| 400 | getAttributeInfoForTheData(childInfo, clsInfo, null, isQualified, isList); |
| 401 | |
| 402 | setLeafHolderFlag(node, false); |
| 403 | setSingleChildHolderFlag(node, true); |
| 404 | return javaAttributeInfo; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Process input/output node when multiple leaf and child nodes are present. |
| 409 | * |
| 410 | * @param node input/output node |
| 411 | * @return java attribute for node |
| 412 | */ |
| 413 | private JavaAttributeInfo processNodeWhenMultipleContaintsArePresent(YangNode node) { |
| 414 | |
| 415 | setLeafHolderFlag(node, false); |
| 416 | setSingleChildHolderFlag(node, false); |
| 417 | return getChildNodeAsAttributeInParentService(node, this); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Adds type import to the RPC import list. |
| 422 | * |
| 423 | * @param type YANG type |
| 424 | * @param isList is list attribute |
| 425 | * @param pluginConfig plugin configurations |
| 426 | * @return type import to the RPC import list |
| 427 | */ |
| 428 | private boolean addTypeImport(YangType<?> type, boolean isList, YangPluginConfig pluginConfig) { |
| 429 | |
| 430 | String classInfo = getJavaImportClass(type, isList, pluginConfig.getConflictResolver()); |
| 431 | if (classInfo == null) { |
| 432 | classInfo = getJavaDataType(type); |
| 433 | return false; |
| 434 | } else { |
| 435 | classInfo = getJavaImportClass(type, isList, pluginConfig.getConflictResolver()); |
| 436 | String pkgInfo = getJavaImportPackage(type, isList, pluginConfig.getConflictResolver()); |
| 437 | JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo(); |
| 438 | importInfo.setPkgInfo(pkgInfo); |
| 439 | importInfo.setClassInfo(classInfo); |
| 440 | if (!((JavaFileInfoContainer) this.getParent()).getJavaFileInfo().getJavaName().equals(classInfo)) { |
| 441 | return addImportToService(importInfo); |
| 442 | } else { |
| 443 | return true; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Adds to service class import list. |
| 450 | * |
| 451 | * @param importInfo import info |
| 452 | * @return true or false |
| 453 | */ |
| 454 | private boolean addImportToService(JavaQualifiedTypeInfo importInfo) { |
| 455 | if (((TempJavaCodeFragmentFilesContainer) this.getParent()).getTempJavaCodeFragmentFiles() |
| 456 | .getServiceTempFiles().getJavaImportData().addImportInfo(importInfo)) { |
| 457 | return !((TempJavaCodeFragmentFilesContainer) this.getParent()).getTempJavaCodeFragmentFiles() |
| 458 | .getServiceTempFiles().getJavaImportData().getImportSet().contains(importInfo); |
| 459 | } else { |
| 460 | return true; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Sets leaf holder flag for input/output. |
| 466 | * |
| 467 | * @param node input/output node |
| 468 | * @param flag true or false |
| 469 | */ |
| 470 | private void setLeafHolderFlag(YangNode node, boolean flag) { |
| 471 | if (node instanceof YangJavaInput) { |
| 472 | setInputLeafHolder(flag); |
| 473 | } else { |
| 474 | setOutputLeafHolder(flag); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Sets sing child holder flag for input/output. |
| 480 | * |
| 481 | * @param node input/output node |
| 482 | * @param flag true or false |
| 483 | */ |
| 484 | private void setSingleChildHolderFlag(YangNode node, boolean flag) { |
| 485 | if (node instanceof YangJavaInput) { |
| 486 | setInputSingleChildHolder(flag); |
| 487 | } else { |
| 488 | setOutputSingleChildHolder(flag); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Sets code generator flag for input and output. |
| 494 | * |
| 495 | * @param node YANG node |
| 496 | * @param flag cod generator flag |
| 497 | */ |
| 498 | private void setCodeGenFlagForNode(YangNode node, boolean flag) { |
| 499 | if (node instanceof YangJavaInput) { |
| 500 | ((YangJavaInput) node).setCodeGenFlag(flag); |
| 501 | } else { |
| 502 | ((YangJavaOutput) node).setCodeGenFlag(flag); |
| 503 | } |
| 504 | |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Counts the number of child nodes of a YANG node. |
| 509 | * |
| 510 | * @param node YANG node |
| 511 | * @return count of children |
| 512 | */ |
| 513 | private int getNumberOfChildNodes(YangNode node) { |
| 514 | YangNode tempNode = node.getChild(); |
| 515 | int count = 0; |
| 516 | if (tempNode != null) { |
| 517 | count = 1; |
| 518 | } |
| 519 | while (tempNode != null) { |
| 520 | |
| 521 | tempNode = tempNode.getNextSibling(); |
| 522 | if (tempNode != null) { |
| 523 | count++; |
| 524 | } |
| 525 | } |
| 526 | return count; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Returns true if input is a leaf holder. |
| 531 | * |
| 532 | * @return true if input is a leaf holder |
| 533 | */ |
| 534 | public boolean isInputLeafHolder() { |
| 535 | return isInputLeafHolder; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Sets true if input is a leaf holder. |
| 540 | * |
| 541 | * @param isInputLeafHolder true if input is a leaf holder |
| 542 | */ |
| 543 | public void setInputLeafHolder(boolean isInputLeafHolder) { |
| 544 | this.isInputLeafHolder = isInputLeafHolder; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Returns true if output is a leaf holder. |
| 549 | * |
| 550 | * @return true if output is a leaf holder |
| 551 | */ |
| 552 | public boolean isOutputLeafHolder() { |
| 553 | return isOutputLeafHolder; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Sets true if output is a leaf holder. |
| 558 | * |
| 559 | * @param isOutputLeafHolder true if output is a leaf holder |
| 560 | */ |
| 561 | public void setOutputLeafHolder(boolean isOutputLeafHolder) { |
| 562 | this.isOutputLeafHolder = isOutputLeafHolder; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Returns true if input is single child holder. |
| 567 | * |
| 568 | * @return true if input is single child holder |
| 569 | */ |
| 570 | public boolean isInputSingleChildHolder() { |
| 571 | return isInputSingleChildHolder; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Sets true if input is single child holder. |
| 576 | * |
| 577 | * @param isInputSingleChildHolder true if input is single child holder |
| 578 | */ |
| 579 | public void setInputSingleChildHolder(boolean isInputSingleChildHolder) { |
| 580 | this.isInputSingleChildHolder = isInputSingleChildHolder; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Returns true if output is single child holder. |
| 585 | * |
| 586 | * @return true if output is single child holder |
| 587 | */ |
| 588 | public boolean isOutputSingleChildHolder() { |
| 589 | return isOutputSingleChildHolder; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Sets true if output is single child holder. |
| 594 | * |
| 595 | * @param isOutputSingleChildHolder true if output is single child holder |
| 596 | */ |
| 597 | public void setOutputSingleChildHolder(boolean isOutputSingleChildHolder) { |
| 598 | this.isOutputSingleChildHolder = isOutputSingleChildHolder; |
| 599 | } |
| 600 | |
Bharat saraswal | ab4c6ba | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 601 | } |