blob: cef6202d54d98247c1e1d6bc29ab2348aefc29a6 [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;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053028import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
Bharat saraswalcad0e652016-05-26 23:48:38 +053029import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
30import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053031import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Bharat saraswalcad0e652016-05-26 23:48:38 +053032
Bharat saraswal96dfef02016-06-16 00:29:12 +053033import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053034import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeAsAttributeInTargetTempFile;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053035import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.updatePackageInfo;
Vinod Kumar S38046502016-03-23 15:30:27 +053036
Vinod Kumar S38046502016-03-23 15:30:27 +053037/**
Bharat saraswald9822e92016-04-05 15:13:44 +053038 * Represents uses information extended to support java code generation.
Vinod Kumar S38046502016-03-23 15:30:27 +053039 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053040public class YangJavaUses
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053041 extends YangUses
42 implements JavaCodeGeneratorInfo, JavaCodeGenerator {
Bharat saraswalcad0e652016-05-26 23:48:38 +053043
Bharat saraswal96dfef02016-06-16 00:29:12 +053044 private static final long serialVersionUID = 806201618L;
45
Bharat saraswalcad0e652016-05-26 23:48:38 +053046 /**
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 saraswal96dfef02016-06-16 00:29:12 +053055 private transient TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053056
57 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053058 * Creates YANG java uses object.
Vinod Kumar S38046502016-03-23 15:30:27 +053059 */
60 public YangJavaUses() {
61 super();
Bharat saraswalcad0e652016-05-26 23:48:38 +053062 setJavaFileInfo(new JavaFileInfo());
Vinod Kumar S38046502016-03-23 15:30:27 +053063 }
Bharat saraswalcad0e652016-05-26 23:48:38 +053064
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 saraswalcad0e652016-05-26 23:48:38 +0530108 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530109 public void generateCodeEntry(YangPluginConfig yangPlugin)
110 throws TranslatorException {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530111 try {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530112 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 saraswal96dfef02016-06-16 00:29:12 +0530126 for (List<YangLeaf> leavesList : getUsesResolvedLeavesList()) {
127 // add the resolved leaves to the parent as an attribute
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530128 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
129 .getBeanTempFiles().addLeavesInfoToTempFiles(leavesList, yangPlugin);
130 }
131
Bharat saraswal96dfef02016-06-16 00:29:12 +0530132 for (List<YangLeafList> listOfLeafLists : getUsesResolvedListOfLeafList()) {
133 // add the resolved leaf-list to the parent as an attribute
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530134 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
135 .getBeanTempFiles().addLeafListInfoToTempFiles(listOfLeafLists, yangPlugin);
136 }
137
Bharat saraswal96dfef02016-06-16 00:29:12 +0530138 for (YangNode usesResolvedNode : getUsesResolvedNodeList()) {
139 // add the resolved nodes to the parent as an attribute
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530140 addCurNodeAsAttributeInTargetTempFile(usesResolvedNode, yangPlugin,
141 getParentNodeInGenCode(this));
142 }
143
Bharat saraswalcad0e652016-05-26 23:48:38 +0530144 } catch (IOException e) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530145 throw new TranslatorException(e.getCause());
Bharat saraswalcad0e652016-05-26 23:48:38 +0530146 }
147 }
148
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530149 @Override
150 public void generateCodeExit()
151 throws TranslatorException {
152 /*
153 * Do nothing.
154 */
155 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530156}