blob: f6166a5c54ad4e7cbbd6814444ed4fa8e6a6a18a [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +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 */
16package org.onosproject.yangutils.translator.tojava.javamodel;
17
Bharat saraswalcad0e652016-05-26 23:48:38 +053018import java.io.IOException;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053019import java.util.List;
Bharat saraswalcad0e652016-05-26 23:48:38 +053020
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053021import org.onosproject.yangutils.datamodel.YangGrouping;
22import org.onosproject.yangutils.datamodel.YangLeaf;
23import org.onosproject.yangutils.datamodel.YangLeafList;
24import org.onosproject.yangutils.datamodel.YangNode;
Vinod Kumar S38046502016-03-23 15:30:27 +053025import org.onosproject.yangutils.datamodel.YangUses;
Bharat saraswalcad0e652016-05-26 23:48:38 +053026import org.onosproject.yangutils.translator.exception.TranslatorException;
27import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
28import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
29import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
30import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
31
Bharat saraswal96dfef02016-06-16 00:29:12 +053032import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053033import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeAsAttributeInTargetTempFile;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053034import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.updatePackageInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053035
Vinod Kumar S38046502016-03-23 15:30:27 +053036/**
Bharat saraswald9822e92016-04-05 15:13:44 +053037 * Represents uses information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053038 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053039public class YangJavaUses
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053040 extends YangUses
41 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Bharat saraswalcad0e652016-05-26 23:48:38 +053042
Bharat saraswal96dfef02016-06-16 00:29:12 +053043 private static final long serialVersionUID = 806201618L;
44
Bharat saraswalcad0e652016-05-26 23:48:38 +053045 /**
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 saraswal96dfef02016-06-16 00:29:12 +053054 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053055
56 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053057 * Creates YANG java uses object.
Vinod Kumar S38046502016-03-23 15:30:27 +053058 */
59 public YangJavaUses() {
60 super();
Bharat saraswalcad0e652016-05-26 23:48:38 +053061 setJavaFileInfo(new JavaFileInfo());
Vinod Kumar S38046502016-03-23 15:30:27 +053062 }
Bharat saraswalcad0e652016-05-26 23:48:38 +053063
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 saraswalcad0e652016-05-26 23:48:38 +0530107 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530108 public void generateCodeEntry(YangPluginConfig yangPlugin)
109 throws TranslatorException {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530110 try {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530111 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 saraswal96dfef02016-06-16 00:29:12 +0530125 for (List<YangLeaf> leavesList : getUsesResolvedLeavesList()) {
126 // add the resolved leaves to the parent as an attribute
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530127 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
128 .getBeanTempFiles().addLeavesInfoToTempFiles(leavesList, yangPlugin);
129 }
130
Bharat saraswal96dfef02016-06-16 00:29:12 +0530131 for (List<YangLeafList> listOfLeafLists : getUsesResolvedListOfLeafList()) {
132 // add the resolved leaf-list to the parent as an attribute
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530133 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
134 .getBeanTempFiles().addLeafListInfoToTempFiles(listOfLeafLists, yangPlugin);
135 }
136
Bharat saraswal96dfef02016-06-16 00:29:12 +0530137 for (YangNode usesResolvedNode : getUsesResolvedNodeList()) {
138 // add the resolved nodes to the parent as an attribute
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530139 addCurNodeAsAttributeInTargetTempFile(usesResolvedNode, yangPlugin,
140 getParentNodeInGenCode(this));
141 }
142
Bharat saraswalcad0e652016-05-26 23:48:38 +0530143 } catch (IOException e) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530144 throw new TranslatorException(e.getCause());
Bharat saraswalcad0e652016-05-26 23:48:38 +0530145 }
146 }
147
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530148 @Override
149 public void generateCodeExit()
150 throws TranslatorException {
151 /*
152 * Do nothing.
153 */
154 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530155}