Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +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 | package org.onosproject.yangutils.translator.tojava.javamodel; |
| 17 | |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 18 | import java.io.IOException; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 19 | import java.util.List; |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 20 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangGrouping; |
| 22 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 23 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangUses; |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
| 27 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 28 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
| 29 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 30 | import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig; |
| 31 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 32 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeAsAttributeInTargetTempFile; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 35 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 36 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 37 | * Represents uses information extended to support java code generation. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 38 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 39 | public class YangJavaUses |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 40 | extends YangUses |
| 41 | implements JavaCodeGeneratorInfo, JavaCodeGenerator { |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 42 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 43 | private static final long serialVersionUID = 806201618L; |
| 44 | |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 45 | /** |
| 46 | * Contains the information of the java file being generated. |
| 47 | */ |
| 48 | private JavaFileInfo javaFileInfo; |
| 49 | |
| 50 | /** |
| 51 | * File handle to maintain temporary java code fragments as per the code |
| 52 | * snippet types. |
| 53 | */ |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 54 | private transient TempJavaCodeFragmentFiles tempFileHandle; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 55 | |
| 56 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 57 | * Creates YANG java uses object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 58 | */ |
| 59 | public YangJavaUses() { |
| 60 | super(); |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 61 | setJavaFileInfo(new JavaFileInfo()); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 62 | } |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Returns the generated java file information. |
| 66 | * |
| 67 | * @return generated java file information |
| 68 | */ |
| 69 | @Override |
| 70 | public JavaFileInfo getJavaFileInfo() { |
| 71 | if (javaFileInfo == null) { |
| 72 | throw new TranslatorException("Missing java info in java datamodel node"); |
| 73 | } |
| 74 | return javaFileInfo; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Sets the java file info object. |
| 79 | * |
| 80 | * @param javaInfo java file info object |
| 81 | */ |
| 82 | @Override |
| 83 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 84 | javaFileInfo = javaInfo; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the temporary file handle. |
| 89 | * |
| 90 | * @return temporary file handle |
| 91 | */ |
| 92 | @Override |
| 93 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
| 94 | return tempFileHandle; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Sets temporary file handle. |
| 99 | * |
| 100 | * @param fileHandle temporary file handle |
| 101 | */ |
| 102 | @Override |
| 103 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 104 | tempFileHandle = fileHandle; |
| 105 | } |
| 106 | |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 107 | @Override |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 108 | public void generateCodeEntry(YangPluginConfig yangPlugin) |
| 109 | throws TranslatorException { |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 110 | try { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 111 | updatePackageInfo(this, yangPlugin); |
| 112 | |
| 113 | if (!(getParentNodeInGenCode(this) instanceof JavaCodeGeneratorInfo)) { |
| 114 | throw new TranslatorException("invalid container of uses"); |
| 115 | } |
| 116 | JavaCodeGeneratorInfo javaCodeGeneratorInfo = (JavaCodeGeneratorInfo) getParentNodeInGenCode(this); |
| 117 | |
| 118 | if (javaCodeGeneratorInfo instanceof YangGrouping) { |
| 119 | /* |
| 120 | * Do nothing, since it will taken care in the groupings uses. |
| 121 | */ |
| 122 | return; |
| 123 | } |
| 124 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 125 | for (List<YangLeaf> leavesList : getUsesResolvedLeavesList()) { |
| 126 | // add the resolved leaves to the parent as an attribute |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 127 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 128 | .getBeanTempFiles().addLeavesInfoToTempFiles(leavesList, yangPlugin); |
| 129 | } |
| 130 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 131 | for (List<YangLeafList> listOfLeafLists : getUsesResolvedListOfLeafList()) { |
| 132 | // add the resolved leaf-list to the parent as an attribute |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 133 | javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles() |
| 134 | .getBeanTempFiles().addLeafListInfoToTempFiles(listOfLeafLists, yangPlugin); |
| 135 | } |
| 136 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame^] | 137 | for (YangNode usesResolvedNode : getUsesResolvedNodeList()) { |
| 138 | // add the resolved nodes to the parent as an attribute |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 139 | addCurNodeAsAttributeInTargetTempFile(usesResolvedNode, yangPlugin, |
| 140 | getParentNodeInGenCode(this)); |
| 141 | } |
| 142 | |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 143 | } catch (IOException e) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 144 | throw new TranslatorException(e.getCause()); |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 148 | @Override |
| 149 | public void generateCodeExit() |
| 150 | throws TranslatorException { |
| 151 | /* |
| 152 | * Do nothing. |
| 153 | */ |
| 154 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 155 | } |