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