blob: 5ea5f1ea8c2163401a3088a092d6c2aae022effe [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
2 * Copyright 2016 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
17package org.onosproject.yangutils.translator.tojava.utils;
18
19import java.util.ArrayList;
20
Vinod Kumar S38046502016-03-23 15:30:27 +053021import org.onosproject.yangutils.datamodel.YangNode;
b.janani1fef2732016-03-04 12:29:05 +053022import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053023import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
24import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Bharat saraswale2d51d62016-03-23 19:40:35 +053025
26import static org.onosproject.yangutils.utils.UtilConstants.COLAN;
27import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
28import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
29import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
30import static org.onosproject.yangutils.utils.UtilConstants.JAVA_KEY_WORDS;
31import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
32import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
33import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
34import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SPECIAL_CHAR;
Bharat saraswal2f11f652016-03-25 18:19:46 +053035import static org.onosproject.yangutils.utils.UtilConstants.REVISION_PREFIX;
Bharat saraswale2d51d62016-03-23 19:40:35 +053036import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
37import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE;
Bharat saraswal2f11f652016-03-25 18:19:46 +053038import static org.onosproject.yangutils.utils.UtilConstants.VERSION_PREFIX;
Bharat saraswal870c56f2016-02-20 21:57:16 +053039
40/**
41 * Utility Class for translating the name from YANG to java convention.
42 */
43public final class JavaIdentifierSyntax {
44
b.janani1fef2732016-03-04 12:29:05 +053045 private static final int MAX_MONTHS = 12;
46 private static final int MAX_DAYS = 31;
47 private static final int INDEX_ZERO = 0;
48 private static final int INDEX_ONE = 1;
49 private static final int INDEX_TWO = 2;
b.janani1fef2732016-03-04 12:29:05 +053050
Bharat saraswal870c56f2016-02-20 21:57:16 +053051 /**
b.janani1fef2732016-03-04 12:29:05 +053052 * Default constructor.
Bharat saraswal870c56f2016-02-20 21:57:16 +053053 */
54 private JavaIdentifierSyntax() {
55 }
56
57 /**
58 * Get the root package string.
59 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053060 * @param version YANG version
61 * @param nameSpace name space of the module
Bharat saraswal870c56f2016-02-20 21:57:16 +053062 * @param revision revision of the module defined
Vinod Kumar Sc4216002016-03-03 19:55:30 +053063 * @return returns the root package string
Bharat saraswal870c56f2016-02-20 21:57:16 +053064 */
65 public static String getRootPackage(byte version, String nameSpace, String revision) {
66
67 String pkg;
Bharat saraswale2d51d62016-03-23 19:40:35 +053068 pkg = DEFAULT_BASE_PKG;
69 pkg = pkg + PERIOD;
Bharat saraswal870c56f2016-02-20 21:57:16 +053070 pkg = pkg + getYangVersion(version);
Bharat saraswale2d51d62016-03-23 19:40:35 +053071 pkg = pkg + PERIOD;
Bharat saraswal870c56f2016-02-20 21:57:16 +053072 pkg = pkg + getPkgFromNameSpace(nameSpace);
Bharat saraswale2d51d62016-03-23 19:40:35 +053073 pkg = pkg + PERIOD;
Bharat saraswal870c56f2016-02-20 21:57:16 +053074 pkg = pkg + getYangRevisionStr(revision);
75
Bharat saraswal4bf8b152016-02-25 02:26:43 +053076 return pkg.toLowerCase();
Bharat saraswal870c56f2016-02-20 21:57:16 +053077 }
78
79 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053080 * Get the contained data model parent node.
81 *
82 * @param currentNode current node which parent contained node is required
83 * @return parent node in which the current node is an attribute
84 */
85 public static YangNode getParentNodeInGenCode(YangNode currentNode) {
86
87 /*
88 * TODO: recursive parent lookup to support choice/augment/uses. TODO:
89 * need to check if this needs to be updated for
90 * choice/case/augment/grouping
91 */
92 return currentNode.getParent();
93 }
94
95 /**
96 * Get the node package string.
97 *
98 * @param curNode current java node whose package string needs to be set
99 * @return returns the root package string
100 */
101 public static String getCurNodePackage(YangNode curNode) {
102
103 String pkg;
104 if (!(curNode instanceof HasJavaFileInfo)
105 || curNode.getParent() == null) {
106 throw new RuntimeException("missing parent node to get current node's package");
107 }
108
109 YangNode parentNode = getParentNodeInGenCode(curNode);
110 if (!(parentNode instanceof HasJavaFileInfo)) {
111 throw new RuntimeException("missing parent java node to get current node's package");
112 }
113 JavaFileInfo parentJavaFileHandle = ((HasJavaFileInfo) parentNode).getJavaFileInfo();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530114 pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
Vinod Kumar S38046502016-03-23 15:30:27 +0530115 return pkg.toLowerCase();
116 }
117
118 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530119 * Returns version.
120 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530121 * @param ver YANG version
Bharat saraswal870c56f2016-02-20 21:57:16 +0530122 * @return version
123 */
124 private static String getYangVersion(byte ver) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530125
Bharat saraswal2f11f652016-03-25 18:19:46 +0530126 return VERSION_PREFIX + ver;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530127 }
128
129 /**
130 * Get package name from name space.
131 *
132 * @param nameSpace name space of YANG module
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530133 * @return java package name as per java rules
Bharat saraswal870c56f2016-02-20 21:57:16 +0530134 */
135 public static String getPkgFromNameSpace(String nameSpace) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530136
Bharat saraswal870c56f2016-02-20 21:57:16 +0530137 ArrayList<String> pkgArr = new ArrayList<String>();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530138 nameSpace = nameSpace.replace(QUOTES, EMPTY_STRING);
139 String properNameSpace = nameSpace.replaceAll(REGEX_WITH_SPECIAL_CHAR, COLAN);
140 String[] nameSpaceArr = properNameSpace.split(COLAN);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530141
142 for (String nameSpaceString : nameSpaceArr) {
143 pkgArr.add(nameSpaceString);
144 }
145 return getPkgFrmArr(pkgArr);
146 }
147
148 /**
149 * Returns revision string array.
150 *
151 * @param date YANG module revision
152 * @return revision string
b.janani1fef2732016-03-04 12:29:05 +0530153 * @throws TranslatorException when date is invalid.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530154 */
b.janani1fef2732016-03-04 12:29:05 +0530155 public static String getYangRevisionStr(String date) throws TranslatorException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530156
Bharat saraswale2d51d62016-03-23 19:40:35 +0530157 String[] revisionArr = date.split(HYPHEN);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530158
Bharat saraswal2f11f652016-03-25 18:19:46 +0530159 String rev = REVISION_PREFIX;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530160 rev = rev + revisionArr[INDEX_ZERO];
b.janani1fef2732016-03-04 12:29:05 +0530161
Vinod Kumar S38046502016-03-23 15:30:27 +0530162 if (Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS
b.janani1fef2732016-03-04 12:29:05 +0530163 && Integer.parseInt(revisionArr[INDEX_TWO]) <= MAX_DAYS) {
164 for (int i = INDEX_ONE; i < revisionArr.length; i++) {
165
166 Integer val = Integer.parseInt(revisionArr[i]);
167 if (val < 10) {
168 rev = rev + "0";
169 }
170 rev = rev + val;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530171 }
b.janani1fef2732016-03-04 12:29:05 +0530172
173 return rev;
174 } else {
175 throw new TranslatorException("Date in revision is not proper: " + date);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530176 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530177 }
178
179 /**
180 * Returns the package string.
181 *
182 * @param pkgArr package array
183 * @return package string
184 */
185 public static String getPkgFrmArr(ArrayList<String> pkgArr) {
186
Bharat saraswale2d51d62016-03-23 19:40:35 +0530187 String pkg = EMPTY_STRING;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530188 int size = pkgArr.size();
189 int i = 0;
190 for (String member : pkgArr) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530191 boolean presenceOfKeyword = JAVA_KEY_WORDS.contains(member);
192 if (presenceOfKeyword || member.matches(REGEX_FOR_FIRST_DIGIT)) {
193 member = UNDER_SCORE + member;
b.janani1fef2732016-03-04 12:29:05 +0530194 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530195 pkg = pkg + member;
196 if (i != size - 1) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530197 pkg = pkg + PERIOD;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530198 }
199 i++;
200 }
201 return pkg;
202 }
203
204 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530205 * Get package sub name from YANG identifier name.
206 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530207 * @param name YANG identifier name
208 * @return java package sub name as per java rules
Bharat saraswal870c56f2016-02-20 21:57:16 +0530209 */
210 public static String getSubPkgFromName(String name) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530211
Bharat saraswal870c56f2016-02-20 21:57:16 +0530212 ArrayList<String> pkgArr = new ArrayList<String>();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530213 String[] nameArr = name.split(COLAN);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530214
215 for (String nameString : nameArr) {
216 pkgArr.add(nameString);
217 }
218 return getPkgFrmArr(pkgArr);
219 }
220
221 /**
222 * Translate the YANG identifier name to java identifier.
223 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530224 * @param yangIdentifier identifier in YANG file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530225 * @return corresponding java identifier
226 */
227 public static String getCamelCase(String yangIdentifier) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530228
Bharat saraswale2d51d62016-03-23 19:40:35 +0530229 String[] strArray = yangIdentifier.split(HYPHEN);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530230 String camelCase = strArray[0];
231 for (int i = 1; i < strArray.length; i++) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530232 camelCase = camelCase + strArray[i].substring(0, 1).toUpperCase() + strArray[i].substring(1);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530233 }
234 return camelCase;
235 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530236
237 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530238 * Translate the YANG identifier name to java identifier with first letter
239 * in caps.
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530240 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530241 * @param yangIdentifier identifier in YANG file
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530242 * @return corresponding java identifier
243 */
244 public static String getCaptialCase(String yangIdentifier) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530245
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530246 return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
247 }
b.janani1fef2732016-03-04 12:29:05 +0530248
249 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530250 * Translate the YANG identifier name to java identifier with first letter
251 * in small.
b.janani1fef2732016-03-04 12:29:05 +0530252 *
253 * @param yangIdentifier identifier in YANG file.
254 * @return corresponding java identifier
255 */
Bharat saraswal2f11f652016-03-25 18:19:46 +0530256 public static String getSmallCase(String yangIdentifier) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530257
b.janani1fef2732016-03-04 12:29:05 +0530258 return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
259 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530260
261 /**
262 * Get the java Package from package path.
263 *
264 * @param packagePath package path
265 * @return java package
266 */
267 public static String getJavaPackageFromPackagePath(String packagePath) {
268
Bharat saraswale2d51d62016-03-23 19:40:35 +0530269 return packagePath.replace(SLASH, PERIOD);
Vinod Kumar S38046502016-03-23 15:30:27 +0530270 }
271
272 /**
273 * Get the directory path corresponding to java package.
274 *
275 * @param packagePath package path
276 * @return java package
277 */
278 public static String getPackageDirPathFromJavaJPackage(String packagePath) {
279
Bharat saraswale2d51d62016-03-23 19:40:35 +0530280 return packagePath.replace(PERIOD, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530281 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530282}