Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangNode; |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo; |
| 24 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 25 | |
| 26 | import static org.onosproject.yangutils.utils.UtilConstants.COLAN; |
| 27 | import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG; |
| 28 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 29 | import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN; |
| 30 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_KEY_WORDS; |
| 31 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.QUOTES; |
| 33 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SPECIAL_CHAR; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 36 | import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Utility Class for translating the name from YANG to java convention. |
| 40 | */ |
| 41 | public final class JavaIdentifierSyntax { |
| 42 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 43 | private static final int MAX_MONTHS = 12; |
| 44 | private static final int MAX_DAYS = 31; |
| 45 | private static final int INDEX_ZERO = 0; |
| 46 | private static final int INDEX_ONE = 1; |
| 47 | private static final int INDEX_TWO = 2; |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 48 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 49 | /** |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 50 | * Default constructor. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 51 | */ |
| 52 | private JavaIdentifierSyntax() { |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the root package string. |
| 57 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 58 | * @param version YANG version |
| 59 | * @param nameSpace name space of the module |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 60 | * @param revision revision of the module defined |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 61 | * @return returns the root package string |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 62 | */ |
| 63 | public static String getRootPackage(byte version, String nameSpace, String revision) { |
| 64 | |
| 65 | String pkg; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 66 | pkg = DEFAULT_BASE_PKG; |
| 67 | pkg = pkg + PERIOD; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 68 | pkg = pkg + getYangVersion(version); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 69 | pkg = pkg + PERIOD; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 70 | pkg = pkg + getPkgFromNameSpace(nameSpace); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 71 | pkg = pkg + PERIOD; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 72 | pkg = pkg + getYangRevisionStr(revision); |
| 73 | |
Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 74 | return pkg.toLowerCase(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 78 | * Get the contained data model parent node. |
| 79 | * |
| 80 | * @param currentNode current node which parent contained node is required |
| 81 | * @return parent node in which the current node is an attribute |
| 82 | */ |
| 83 | public static YangNode getParentNodeInGenCode(YangNode currentNode) { |
| 84 | |
| 85 | /* |
| 86 | * TODO: recursive parent lookup to support choice/augment/uses. TODO: |
| 87 | * need to check if this needs to be updated for |
| 88 | * choice/case/augment/grouping |
| 89 | */ |
| 90 | return currentNode.getParent(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the node package string. |
| 95 | * |
| 96 | * @param curNode current java node whose package string needs to be set |
| 97 | * @return returns the root package string |
| 98 | */ |
| 99 | public static String getCurNodePackage(YangNode curNode) { |
| 100 | |
| 101 | String pkg; |
| 102 | if (!(curNode instanceof HasJavaFileInfo) |
| 103 | || curNode.getParent() == null) { |
| 104 | throw new RuntimeException("missing parent node to get current node's package"); |
| 105 | } |
| 106 | |
| 107 | YangNode parentNode = getParentNodeInGenCode(curNode); |
| 108 | if (!(parentNode instanceof HasJavaFileInfo)) { |
| 109 | throw new RuntimeException("missing parent java node to get current node's package"); |
| 110 | } |
| 111 | JavaFileInfo parentJavaFileHandle = ((HasJavaFileInfo) parentNode).getJavaFileInfo(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 112 | pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 113 | return pkg.toLowerCase(); |
| 114 | } |
| 115 | |
| 116 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 117 | * Returns version. |
| 118 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 119 | * @param ver YANG version |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 120 | * @return version |
| 121 | */ |
| 122 | private static String getYangVersion(byte ver) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 123 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 124 | return "v" + ver; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get package name from name space. |
| 129 | * |
| 130 | * @param nameSpace name space of YANG module |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 131 | * @return java package name as per java rules |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 132 | */ |
| 133 | public static String getPkgFromNameSpace(String nameSpace) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 134 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 135 | ArrayList<String> pkgArr = new ArrayList<String>(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 136 | nameSpace = nameSpace.replace(QUOTES, EMPTY_STRING); |
| 137 | String properNameSpace = nameSpace.replaceAll(REGEX_WITH_SPECIAL_CHAR, COLAN); |
| 138 | String[] nameSpaceArr = properNameSpace.split(COLAN); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 139 | |
| 140 | for (String nameSpaceString : nameSpaceArr) { |
| 141 | pkgArr.add(nameSpaceString); |
| 142 | } |
| 143 | return getPkgFrmArr(pkgArr); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns revision string array. |
| 148 | * |
| 149 | * @param date YANG module revision |
| 150 | * @return revision string |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 151 | * @throws TranslatorException when date is invalid. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 152 | */ |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 153 | public static String getYangRevisionStr(String date) throws TranslatorException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 154 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 155 | String[] revisionArr = date.split(HYPHEN); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 156 | |
| 157 | String rev = "rev"; |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 158 | rev = rev + revisionArr[INDEX_ZERO]; |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 159 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 160 | if (Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 161 | && Integer.parseInt(revisionArr[INDEX_TWO]) <= MAX_DAYS) { |
| 162 | for (int i = INDEX_ONE; i < revisionArr.length; i++) { |
| 163 | |
| 164 | Integer val = Integer.parseInt(revisionArr[i]); |
| 165 | if (val < 10) { |
| 166 | rev = rev + "0"; |
| 167 | } |
| 168 | rev = rev + val; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 169 | } |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 170 | |
| 171 | return rev; |
| 172 | } else { |
| 173 | throw new TranslatorException("Date in revision is not proper: " + date); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 174 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the package string. |
| 179 | * |
| 180 | * @param pkgArr package array |
| 181 | * @return package string |
| 182 | */ |
| 183 | public static String getPkgFrmArr(ArrayList<String> pkgArr) { |
| 184 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 185 | String pkg = EMPTY_STRING; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 186 | int size = pkgArr.size(); |
| 187 | int i = 0; |
| 188 | for (String member : pkgArr) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 189 | boolean presenceOfKeyword = JAVA_KEY_WORDS.contains(member); |
| 190 | if (presenceOfKeyword || member.matches(REGEX_FOR_FIRST_DIGIT)) { |
| 191 | member = UNDER_SCORE + member; |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 192 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 193 | pkg = pkg + member; |
| 194 | if (i != size - 1) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 195 | pkg = pkg + PERIOD; |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 196 | } |
| 197 | i++; |
| 198 | } |
| 199 | return pkg; |
| 200 | } |
| 201 | |
| 202 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 203 | * Get package sub name from YANG identifier name. |
| 204 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 205 | * @param name YANG identifier name |
| 206 | * @return java package sub name as per java rules |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 207 | */ |
| 208 | public static String getSubPkgFromName(String name) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 209 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 210 | ArrayList<String> pkgArr = new ArrayList<String>(); |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 211 | String[] nameArr = name.split(COLAN); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 212 | |
| 213 | for (String nameString : nameArr) { |
| 214 | pkgArr.add(nameString); |
| 215 | } |
| 216 | return getPkgFrmArr(pkgArr); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Translate the YANG identifier name to java identifier. |
| 221 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 222 | * @param yangIdentifier identifier in YANG file |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 223 | * @return corresponding java identifier |
| 224 | */ |
| 225 | public static String getCamelCase(String yangIdentifier) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 226 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 227 | String[] strArray = yangIdentifier.split(HYPHEN); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 228 | String camelCase = strArray[0]; |
| 229 | for (int i = 1; i < strArray.length; i++) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 230 | camelCase = camelCase + strArray[i].substring(0, 1).toUpperCase() + strArray[i].substring(1); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 231 | } |
| 232 | return camelCase; |
| 233 | } |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 234 | |
| 235 | /** |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 236 | * Translate the YANG identifier name to java identifier with first letter |
| 237 | * in caps. |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 238 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 239 | * @param yangIdentifier identifier in YANG file |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 240 | * @return corresponding java identifier |
| 241 | */ |
| 242 | public static String getCaptialCase(String yangIdentifier) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 243 | |
Bharat saraswal | 5e3c45c | 2016-02-22 22:15:21 +0530 | [diff] [blame] | 244 | return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1); |
| 245 | } |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 246 | |
| 247 | /** |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 248 | * Translate the YANG identifier name to java identifier with first letter |
| 249 | * in small. |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 250 | * |
| 251 | * @param yangIdentifier identifier in YANG file. |
| 252 | * @return corresponding java identifier |
| 253 | */ |
| 254 | public static String getLowerCase(String yangIdentifier) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 255 | |
b.janani | e6d43af | 2016-03-04 12:29:05 +0530 | [diff] [blame] | 256 | return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1); |
| 257 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Get the java Package from package path. |
| 261 | * |
| 262 | * @param packagePath package path |
| 263 | * @return java package |
| 264 | */ |
| 265 | public static String getJavaPackageFromPackagePath(String packagePath) { |
| 266 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 267 | return packagePath.replace(SLASH, PERIOD); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Get the directory path corresponding to java package. |
| 272 | * |
| 273 | * @param packagePath package path |
| 274 | * @return java package |
| 275 | */ |
| 276 | public static String getPackageDirPathFromJavaJPackage(String packagePath) { |
| 277 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 278 | return packagePath.replace(PERIOD, SLASH); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 279 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 280 | } |