blob: f000b99b3f7019444dcf4aa749774021434bfdf9 [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
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053032import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeAsAttributeInTargetTempFile;
33import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
34import 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
43 /**
44 * Contains the information of the java file being generated.
45 */
46 private JavaFileInfo javaFileInfo;
47
48 /**
49 * File handle to maintain temporary java code fragments as per the code
50 * snippet types.
51 */
52 private TempJavaCodeFragmentFiles tempFileHandle;
Vinod Kumar S38046502016-03-23 15:30:27 +053053
54 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053055 * Creates YANG java uses object.
Vinod Kumar S38046502016-03-23 15:30:27 +053056 */
57 public YangJavaUses() {
58 super();
Bharat saraswalcad0e652016-05-26 23:48:38 +053059 setJavaFileInfo(new JavaFileInfo());
Vinod Kumar S38046502016-03-23 15:30:27 +053060 }
Bharat saraswalcad0e652016-05-26 23:48:38 +053061
62 /**
63 * Returns the generated java file information.
64 *
65 * @return generated java file information
66 */
67 @Override
68 public JavaFileInfo getJavaFileInfo() {
69 if (javaFileInfo == null) {
70 throw new TranslatorException("Missing java info in java datamodel node");
71 }
72 return javaFileInfo;
73 }
74
75 /**
76 * Sets the java file info object.
77 *
78 * @param javaInfo java file info object
79 */
80 @Override
81 public void setJavaFileInfo(JavaFileInfo javaInfo) {
82 javaFileInfo = javaInfo;
83 }
84
85 /**
86 * Returns the temporary file handle.
87 *
88 * @return temporary file handle
89 */
90 @Override
91 public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
92 return tempFileHandle;
93 }
94
95 /**
96 * Sets temporary file handle.
97 *
98 * @param fileHandle temporary file handle
99 */
100 @Override
101 public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
102 tempFileHandle = fileHandle;
103 }
104
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530105
Bharat saraswalcad0e652016-05-26 23:48:38 +0530106 @Override
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530107 public void generateCodeEntry(YangPluginConfig yangPlugin)
108 throws TranslatorException {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530109 try {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530110 updatePackageInfo(this, yangPlugin);
111
112 if (!(getParentNodeInGenCode(this) instanceof JavaCodeGeneratorInfo)) {
113 throw new TranslatorException("invalid container of uses");
114 }
115 JavaCodeGeneratorInfo javaCodeGeneratorInfo = (JavaCodeGeneratorInfo) getParentNodeInGenCode(this);
116
117 if (javaCodeGeneratorInfo instanceof YangGrouping) {
118 /*
119 * Do nothing, since it will taken care in the groupings uses.
120 */
121 return;
122 }
123
124 for (List<YangLeaf> leavesList :
125 getUsesResolvedLeavesList()) {
126 //add the resolved leaves to the parent as an attribute
127 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
128 .getBeanTempFiles().addLeavesInfoToTempFiles(leavesList, yangPlugin);
129 }
130
131 for (List<YangLeafList> listOfLeafLists :
132 getUsesResolvedListOfLeafList()) {
133 //add the resolved leaf-list to the parent as an attribute
134 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
135 .getBeanTempFiles().addLeafListInfoToTempFiles(listOfLeafLists, yangPlugin);
136 }
137
138 for (YangNode usesResolvedNode :
139 getUsesResolvedNodeList()) {
140 //add the resolved nodes to the parent as an attribute
141 addCurNodeAsAttributeInTargetTempFile(usesResolvedNode, yangPlugin,
142 getParentNodeInGenCode(this));
143 }
144
Bharat saraswalcad0e652016-05-26 23:48:38 +0530145 } catch (IOException e) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530146 throw new TranslatorException(e.getCause());
Bharat saraswalcad0e652016-05-26 23:48:38 +0530147 }
148 }
149
Bharat saraswalcad0e652016-05-26 23:48:38 +0530150
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530151 @Override
152 public void generateCodeExit()
153 throws TranslatorException {
154 /*
155 * Do nothing.
156 */
157 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530158}