blob: 0826c80ab7d90806889a9aa5147b10f58eb3c061 [file] [log] [blame]
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
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
Bharat saraswalb551aae2016-07-14 15:18:20 +053017package org.onosproject.yangutils.translator.tojava;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053018
19import java.io.IOException;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053020import java.util.ArrayList;
21import java.util.List;
22
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053023import org.onosproject.yangutils.datamodel.RpcNotificationContainer;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053024import org.onosproject.yangutils.datamodel.YangAtomicPath;
Bharat saraswald50c6382016-07-14 21:57:13 +053025import org.onosproject.yangutils.datamodel.YangAugment;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053026import org.onosproject.yangutils.datamodel.YangCase;
27import org.onosproject.yangutils.datamodel.YangChoice;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053028import org.onosproject.yangutils.datamodel.YangLeavesHolder;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053029import org.onosproject.yangutils.datamodel.YangModule;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053030import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswald50c6382016-07-14 21:57:13 +053031import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053032import org.onosproject.yangutils.datamodel.YangSubModule;
Bharat saraswald50c6382016-07-14 21:57:13 +053033import org.onosproject.yangutils.datamodel.YangTranslatorOperatorNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053034import org.onosproject.yangutils.datamodel.YangTypeHolder;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053035import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
36import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053037import org.onosproject.yangutils.datamodel.utils.DataModelUtils;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053038import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053039import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugmentTranslator;
40import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumerationTranslator;
41import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
42import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053043
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053044import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.isRpcChildNodePresent;
45import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeInfoInParentTempFile;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053047import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
Bharat saraswald50c6382016-07-14 21:57:13 +053048import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053049import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
50import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
51import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
52import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053053import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053054
55/**
56 * Represents utility class for YANG java model.
57 */
58public final class YangJavaModelUtils {
59
60 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053061 * Creates an instance of YANG java model utility.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053062 */
63 private YangJavaModelUtils() {
64 }
65
66 /**
67 * Updates YANG java file package information.
68 *
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053069 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053070 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053071 * @throws IOException IO operations fails
72 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053073 public static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +053074 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +053075 throws IOException {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053076 if (javaCodeGeneratorInfo instanceof YangJavaAugmentTranslator) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053077 updatePackageForAugmentInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswalb1170bd2016-07-14 13:26:18 +053078 } else {
79 javaCodeGeneratorInfo.getJavaFileInfo()
80 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
81 yangPluginConfig.getConflictResolver()));
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053082 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(getCurNodePackage((YangNode) javaCodeGeneratorInfo));
Bharat saraswalb1170bd2016-07-14 13:26:18 +053083 }
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053084 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
85 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
86
87 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
88 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
89
90 }
91
92 /**
93 * Updates YANG java file package information.
94 *
95 * @param javaCodeGeneratorInfo YANG java file info node
96 * @param yangPluginConfig YANG plugin config
97 * @throws IOException IO operations fails
98 */
99 private static void updatePackageForAugmentInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
100 YangPluginConfig yangPluginConfig)
101 throws IOException {
102 javaCodeGeneratorInfo.getJavaFileInfo()
103 .setJavaName(getAugmentClassName((YangJavaAugmentTranslator) javaCodeGeneratorInfo,
104 yangPluginConfig));
105 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(
106 getAugmentsNodePackage((YangNode) javaCodeGeneratorInfo, yangPluginConfig));
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530107 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
108 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530109 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPluginConfig.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530110 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530111 }
112
113 /**
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530114 * Returns package for augment node.
115 *
116 * @param yangNode augment node
117 * @param yangPluginConfig plugin configurations
118 * @return package for augment node
119 */
120 private static String getAugmentsNodePackage(YangNode yangNode, YangPluginConfig yangPluginConfig) {
121 YangAugment augment = (YangAugment) yangNode;
122 StringBuilder augmentPkg = new StringBuilder();
123 augmentPkg.append(getCurNodePackage(augment));
124
125 String pkg = PERIOD;
126 for (YangAtomicPath atomicPath : augment.getTargetNode()) {
127 pkg = pkg + getCamelCase(atomicPath.getNodeIdentifier().getName(), yangPluginConfig.getConflictResolver())
128 + PERIOD;
129 }
130 pkg = trimAtLast(pkg, PERIOD);
131 augmentPkg.append(pkg.toLowerCase());
132 return augmentPkg.toString();
133 }
134
135 /**
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530136 * Updates YANG java file package information for specified package.
137 *
138 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530139 * @param yangPlugin YANG plugin config
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530140 * @throws IOException IO operations fails
141 */
janani bde4ffab2016-04-15 16:18:30 +0530142 private static void updatePackageInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo, YangPluginConfig yangPlugin,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530143 String pkg) throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530144 javaCodeGeneratorInfo.getJavaFileInfo()
janani b4a6711a2016-05-17 13:12:22 +0530145 .setJavaName(getCamelCase(((YangNode) javaCodeGeneratorInfo).getName(),
146 yangPlugin.getConflictResolver()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530147 javaCodeGeneratorInfo.getJavaFileInfo().setPackage(pkg);
148 javaCodeGeneratorInfo.getJavaFileInfo().setPackageFilePath(
149 getPackageDirPathFromJavaJPackage(javaCodeGeneratorInfo.getJavaFileInfo().getPackage()));
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530150 javaCodeGeneratorInfo.getJavaFileInfo().setBaseCodeGenPath(yangPlugin.getCodeGenDir());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530151 javaCodeGeneratorInfo.getJavaFileInfo().setPluginConfig(yangPlugin);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530152 }
153
154 /**
155 * Updates temporary java code fragment files.
156 *
157 * @param javaCodeGeneratorInfo YANG java file info node
158 * @throws IOException IO operations fails
159 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530160 private static void createTempFragmentFile(JavaCodeGeneratorInfo javaCodeGeneratorInfo)
161 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530162 javaCodeGeneratorInfo.setTempJavaCodeFragmentFiles(
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530163 new TempJavaCodeFragmentFiles(javaCodeGeneratorInfo.getJavaFileInfo()));
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530164 }
165
166 /**
167 * Updates leaf information in temporary java code fragment files.
168 *
169 * @param javaCodeGeneratorInfo YANG java file info node
170 * @throws IOException IO operations fails
171 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530172 private static void updateTempFragmentFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530173 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530174 throws IOException {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530175
176 if (javaCodeGeneratorInfo instanceof YangModule
177 || javaCodeGeneratorInfo instanceof YangSubModule) {
178 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().setRooNode(true);
179 }
180
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530181 if (javaCodeGeneratorInfo instanceof RpcNotificationContainer) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530182 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530183 * Module / sub module node code generation.
184 */
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530185 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530186 .getServiceTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530187 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530188 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
189 if (!((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
190 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530191 }
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530192 } else if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
193 if (!((YangJavaSubModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes().isEmpty()) {
194 updateNotificationNodeInfo(javaCodeGeneratorInfo, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530195 }
196 }
197
Bharat saraswale707f032016-07-14 23:33:55 +0530198 }
199 if (javaCodeGeneratorInfo instanceof YangLeavesHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530200 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530201 * Container
202 * Case
203 * Grouping
204 * Input
205 * List
206 * Notification
207 * Output
208 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530209 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530210 .getBeanTempFiles().addCurNodeLeavesInfoToTempFiles(
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530211 (YangNode) javaCodeGeneratorInfo, yangPluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530212 } else if (javaCodeGeneratorInfo instanceof YangTypeHolder) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530213 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530214 * Typedef
215 * Union
216 */
217 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530218 .addTypeInfoToTempFiles((YangTypeHolder) javaCodeGeneratorInfo, yangPluginConfig);
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530219 } else if (javaCodeGeneratorInfo instanceof YangJavaEnumerationTranslator) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530220 /*
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530221 * Enumeration
222 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530223 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530224 .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo, yangPluginConfig);
225
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530226 } else if (javaCodeGeneratorInfo instanceof YangChoice) {
227 /*Do nothing, only the interface needs to be generated*/
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530228 } else {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530229 throw new TranslatorException("Unsupported Node Translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530230 }
231 }
232
233 /**
234 * Process generate code entry of YANG node.
235 *
236 * @param javaCodeGeneratorInfo YANG java file info node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530237 * @param yangPluginConfig plugin configurations
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530238 * @throws IOException IO operations fails
239 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530240 private static void generateTempFiles(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswale707f032016-07-14 23:33:55 +0530241 YangPluginConfig yangPluginConfig)
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530242 throws IOException {
243 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530244 throw new TranslatorException("translation is not supported for the node");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530245 }
246 createTempFragmentFile(javaCodeGeneratorInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530247 updateTempFragmentFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530248
249 }
250
251 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530252 * Updates notification node info in service temporary file.
253 *
254 * @param javaCodeGeneratorInfo java code generator info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530255 * @param yangPluginConfig plugin configurations
Bharat saraswal33dfa012016-05-17 19:59:16 +0530256 * @throws IOException when fails to do IO operations
257 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530258 private static void updateNotificationNodeInfo(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530259 YangPluginConfig yangPluginConfig)
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530260 throws IOException {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530261 if (javaCodeGeneratorInfo instanceof YangJavaModuleTranslator) {
262 for (YangNode notification : ((YangJavaModuleTranslator) javaCodeGeneratorInfo).getNotificationNodes()) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530263 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswald50c6382016-07-14 21:57:13 +0530264 .getEventFragmentFiles()
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530265 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530266 }
267 }
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530268 if (javaCodeGeneratorInfo instanceof YangJavaSubModuleTranslator) {
269 for (YangNode notification : ((YangJavaSubModuleTranslator) javaCodeGeneratorInfo)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530270 .getNotificationNodes()) {
271 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
Bharat saraswald50c6382016-07-14 21:57:13 +0530272 .getEventFragmentFiles()
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530273 .addJavaSnippetOfEvent(notification, yangPluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530274 }
275 }
276 }
277
278 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530279 * Generates code for the current ata model node and adds itself as an attribute in the parent.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530280 *
281 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530282 * @param yangPlugin YANG plugin config
283 * @param isMultiInstance flag to indicate whether it's a list
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530284 * @throws IOException IO operations fails
285 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530286 public static void generateCodeAndUpdateInParent(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530287 YangPluginConfig yangPlugin, boolean isMultiInstance)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530288 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530289 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530290 throw new TranslatorException("Invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530291 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530292
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530293 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530294 * Generate the Java files corresponding to the current node.
295 */
296 generateCodeOfAugmentableNode(javaCodeGeneratorInfo, yangPlugin);
297
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530298 /*
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530299 * Update the current nodes info in its parent nodes generated files.
300 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530301 addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance, yangPlugin);
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530302 }
303
304 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530305 * Generates code for the current data model node and adds support for it to be augmented.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530306 *
307 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530308 * @param yangPlugin YANG plugin config
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530309 * @throws IOException IO operations fails
310 */
311 public static void generateCodeOfAugmentableNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530312 YangPluginConfig yangPlugin)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530313 throws IOException {
314 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
315 throw new TranslatorException("invalid node for translation");
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530316 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530317
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530318 generateCodeOfNode(javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530319 TempJavaCodeFragmentFiles tempJavaCodeFragmentFiles = javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530320
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530321 if (javaCodeGeneratorInfo instanceof YangCase) {
322 YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530323 if (parent instanceof YangAugment) {
324 parent = ((YangAugment) parent).getAugmentedNode();
325 }
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530326 JavaQualifiedTypeInfoTranslator parentsInfo = new JavaQualifiedTypeInfoTranslator();
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530327 JavaFileInfo parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo();
328 String parentName;
329 String parentPkg;
330 if (parentInfo.getPackage() != null) {
331 parentName = getCapitalCase(((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName());
332 parentPkg = ((JavaFileInfoContainer) parent).getJavaFileInfo().getPackage();
333 } else {
334 parentName = getCapitalCase(getCamelCase(parent.getName(), yangPlugin.getConflictResolver()));
335 parentPkg = getNodesPackage(parent, yangPlugin);
336 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530337 parentsInfo.setClassInfo(parentName);
338 parentsInfo.setPkgInfo(parentPkg);
339 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles().getJavaExtendsListHolder()
Bharat saraswalb551aae2016-07-14 15:18:20 +0530340 .addToExtendsList(parentsInfo, (YangNode) javaCodeGeneratorInfo,
341 tempJavaCodeFragmentFiles.getBeanTempFiles());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530342
343 javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getBeanTempFiles()
Bharat saraswal33dfa012016-05-17 19:59:16 +0530344 .addParentInfoInCurNodeTempFile((YangNode) javaCodeGeneratorInfo, yangPlugin);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530345
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530346 }
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530347 }
348
349 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530350 * Generates code for the current data model node.
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530351 *
352 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530353 * @param yangPluginConfig YANG plugin config
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530354 * @throws IOException IO operations fails
355 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530356 public static void generateCodeOfNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530357 YangPluginConfig yangPluginConfig)
Gaurav Agrawal56527662016-04-20 15:49:17 +0530358 throws IOException {
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530359 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900360 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530361 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530362 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig);
363 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530364 }
365
366 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530367 * Generates code for the root module/sub-module node.
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530368 *
369 * @param javaCodeGeneratorInfo YANG java file info node
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530370 * @param yangPluginConfig YANG plugin config
371 * @param rootPkg package of the root node
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530372 * @throws IOException IO operations fails
373 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530374 public static void generateCodeOfRootNode(JavaCodeGeneratorInfo javaCodeGeneratorInfo,
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530375 YangPluginConfig yangPluginConfig, String rootPkg)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530376 throws IOException {
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530377 if (!(javaCodeGeneratorInfo instanceof YangNode)) {
sangyun-hand45d42c2016-05-30 20:13:13 +0900378 throw new TranslatorException("invalid node for translation");
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530379 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530380 updatePackageInfo(javaCodeGeneratorInfo, yangPluginConfig, rootPkg);
381
382 if (isRpcChildNodePresent((YangNode) javaCodeGeneratorInfo)) {
383 javaCodeGeneratorInfo.getJavaFileInfo().addGeneratedFileTypes(GENERATE_SERVICE_AND_MANAGER);
384 }
385
386 generateTempFiles(javaCodeGeneratorInfo, yangPluginConfig);
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530387 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530388
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530389 /**
390 * Returns the node package string.
391 *
392 * @param curNode current java node whose package string needs to be set
393 * @return returns the root package string
394 */
395 public static String getCurNodePackage(YangNode curNode) {
396
397 String pkg;
398 if (!(curNode instanceof JavaFileInfoContainer)
399 || curNode.getParent() == null) {
400 throw new TranslatorException("missing parent node to get current node's package");
401 }
402
403 YangNode parentNode = DataModelUtils.getParentNodeInGenCode(curNode);
404 if (!(parentNode instanceof JavaFileInfoContainer)) {
405 throw new TranslatorException("missing parent java node to get current node's package");
406 }
407 JavaFileInfo parentJavaFileHandle = ((JavaFileInfoContainer) parentNode).getJavaFileInfo();
408 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
409 return pkg.toLowerCase();
410 }
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530411
412 /**
413 * Returns true if root node contains any data node.
414 *
415 * @param node root YANG node
416 * @return true if root node contains any data node
417 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530418 public static boolean isRootNodesCodeGenRequired(YangNode node) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530419
420 List<YangNode> childNodes = new ArrayList<>();
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530421 YangNode tempNode = node.getChild();
422 while (tempNode != null) {
423 childNodes.add(tempNode);
424 tempNode = tempNode.getNextSibling();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530425 }
426
427 if (childNodes.size() == 0) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530428 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
429 return !leavesHolder.getListOfLeaf().isEmpty() || !leavesHolder.getListOfLeafList().isEmpty();
Bharat saraswalb551aae2016-07-14 15:18:20 +0530430 } else if (childNodes.size() == 1) {
Bharat saraswald50c6382016-07-14 21:57:13 +0530431 return !(childNodes.get(0) instanceof YangTranslatorOperatorNode);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530432 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530433 List<Boolean> booleanData = new ArrayList<>();
434 for (YangNode child : childNodes) {
435 if (child instanceof YangTranslatorOperatorNode) {
436 booleanData.add(false);
437 } else {
438 booleanData.add(true);
439 }
440 }
441 return booleanData.contains(true);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530442 }
443
444 /**
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530445 * Returns nodes package.
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530446 *
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530447 * @param node YANG node
Bharat saraswalb551aae2016-07-14 15:18:20 +0530448 * @param yangPluginConfig plugin config
449 * @return java package
450 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530451 public static String getNodesPackage(YangNode node, YangPluginConfig yangPluginConfig) {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530452
453 List<String> clsInfo = new ArrayList<>();
Bharat saraswald50c6382016-07-14 21:57:13 +0530454 while (node.getParent() != null) {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530455 if (node instanceof YangJavaAugmentTranslator) {
456 clsInfo.add(getAugmentClassName((YangAugment) node, yangPluginConfig));
457 } else {
458 clsInfo.add(getCamelCase(node.getName(), yangPluginConfig.getConflictResolver()));
Bharat saraswalb551aae2016-07-14 15:18:20 +0530459 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530460 node = node.getParent();
461 }
462
463 StringBuilder pkg = new StringBuilder();
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530464 if (node instanceof YangJavaModuleTranslator) {
465 YangJavaModuleTranslator module = (YangJavaModuleTranslator) node;
Bharat saraswalb551aae2016-07-14 15:18:20 +0530466 pkg.append(getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
467 .getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530468 } else if (node instanceof YangJavaSubModuleTranslator) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530469 YangJavaSubModuleTranslator subModule = (YangJavaSubModuleTranslator) node;
470 pkg.append(getRootPackage(subModule.getVersion(),
471 subModule.getNameSpaceFromModule(subModule.getBelongsTo()),
472 subModule.getRevision().getRevDate(), yangPluginConfig.getConflictResolver()));
Bharat saraswalb551aae2016-07-14 15:18:20 +0530473 }
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530474 String concat = "";
Bharat saraswalb551aae2016-07-14 15:18:20 +0530475 for (int i = 1; i <= clsInfo.size(); i++) {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530476 concat = concat + "." + clsInfo.get(clsInfo.size() - i);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530477 }
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530478 pkg.append(concat);
Bharat saraswalb551aae2016-07-14 15:18:20 +0530479 return pkg.toString().toLowerCase();
480
481 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530482
483 /**
484 * Returns augment class name.
485 *
Bharat saraswale707f032016-07-14 23:33:55 +0530486 * @param augment YANG augment
Bharat saraswald50c6382016-07-14 21:57:13 +0530487 * @param yangPluginConfig plugin configurations
488 * @return augment class name
489 */
490 public static String getAugmentClassName(YangAugment augment, YangPluginConfig yangPluginConfig) {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530491 YangNodeIdentifier yangNodeIdentifier = augment.getTargetNode().get(augment.getTargetNode().size() - 1)
492 .getNodeIdentifier();
493 String name = getCapitalCase(getCamelCase(yangNodeIdentifier.getName(), yangPluginConfig
494 .getConflictResolver()));
495 if (yangNodeIdentifier.getPrefix() != null) {
496 return AUGMENTED + getCapitalCase(yangNodeIdentifier.getPrefix()) + name;
Bharat saraswald50c6382016-07-14 21:57:13 +0530497 } else {
498 return AUGMENTED + name;
499 }
500 }
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530501
Gaurav Agrawal1c8f80c2016-04-12 13:30:16 +0530502}