Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 72b2df2 | 2017-08-03 18:48:28 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 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.yang.compiler.translator.tojava.utils; |
| 18 | |
| 19 | import org.onosproject.yang.compiler.datamodel.YangNode; |
| 20 | import org.onosproject.yang.compiler.datamodel.YangRevision; |
| 21 | import org.onosproject.yang.compiler.translator.exception.TranslatorException; |
| 22 | import org.onosproject.yang.compiler.translator.tojava.JavaFileInfoContainer; |
| 23 | import org.onosproject.yang.compiler.translator.tojava.JavaFileInfoTranslator; |
| 24 | import org.onosproject.yang.compiler.utils.io.YangToJavaNamingConflictUtil; |
| 25 | |
| 26 | import java.io.File; |
| 27 | import java.io.IOException; |
Vidyashree Rama | 7b9726e | 2019-08-26 13:48:33 +0530 | [diff] [blame] | 28 | import java.time.LocalDate; |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.Arrays; |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 31 | import java.util.List; |
| 32 | |
| 33 | import static org.onosproject.yang.compiler.datamodel.utils.DataModelUtils.getParentNodeInGenCode; |
sonugupta-huawei | 35540a3 | 2017-08-22 16:18:50 +0530 | [diff] [blame] | 34 | import static org.onosproject.yang.compiler.utils.UtilConstants.ASTERISK; |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 35 | import static org.onosproject.yang.compiler.utils.UtilConstants.COLON; |
| 36 | import static org.onosproject.yang.compiler.utils.UtilConstants.DEFAULT_BASE_PKG; |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 37 | import static org.onosproject.yang.compiler.utils.UtilConstants.HYPHEN; |
sonugupta-huawei | 711d1e3 | 2017-09-14 10:59:08 +0530 | [diff] [blame] | 38 | import static org.onosproject.yang.compiler.utils.UtilConstants.ONE_DOT_ONE; |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 39 | import static org.onosproject.yang.compiler.utils.UtilConstants.PERIOD; |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 40 | import static org.onosproject.yang.compiler.utils.UtilConstants.REGEX_WITH_ALL_SPECIAL_CHAR; |
| 41 | import static org.onosproject.yang.compiler.utils.UtilConstants.REVISION_PREFIX; |
| 42 | import static org.onosproject.yang.compiler.utils.UtilConstants.SLASH; |
| 43 | import static org.onosproject.yang.compiler.utils.UtilConstants.UNDER_SCORE; |
| 44 | import static org.onosproject.yang.compiler.utils.UtilConstants.VERSION_PREFIX; |
| 45 | import static org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.addPackageInfo; |
| 46 | import static org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.createDirectories; |
| 47 | import static org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
Gaurav Agrawal | fda09be | 2017-05-15 15:48:25 +0530 | [diff] [blame] | 48 | import static org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.getCamelCase; |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 49 | import static org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.getJavaPackageFromPackagePath; |
| 50 | import static org.onosproject.yang.compiler.utils.io.impl.YangIoUtils.getPackageDirPathFromJavaJPackage; |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Represents an utility Class for translating the name from YANG to java convention. |
| 54 | */ |
| 55 | public final class JavaIdentifierSyntax { |
| 56 | |
| 57 | private static final int INDEX_ZERO = 0; |
| 58 | private static final int INDEX_ONE = 1; |
| 59 | private static final int VALUE_CHECK = 10; |
| 60 | private static final String ZERO = "0"; |
| 61 | private static final String DATE_FORMAT = "yyyy-MM-dd"; |
| 62 | |
| 63 | /** |
| 64 | * Create instance of java identifier syntax. |
| 65 | */ |
| 66 | private JavaIdentifierSyntax() { |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the root package string. |
| 71 | * |
Gaurav Agrawal | fda09be | 2017-05-15 15:48:25 +0530 | [diff] [blame] | 72 | * @param version YANG version |
| 73 | * @param name name of the module |
| 74 | * @param revision revision of the module defined |
| 75 | * @param resolver object of YANG to java naming conflict util |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 76 | * @return the root package string |
| 77 | */ |
sonugupta-huawei | 711d1e3 | 2017-09-14 10:59:08 +0530 | [diff] [blame] | 78 | public static String getRootPackage(String version, String name, |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 79 | YangRevision revision, |
| 80 | YangToJavaNamingConflictUtil resolver) { |
| 81 | |
| 82 | StringBuilder pkg = new StringBuilder(DEFAULT_BASE_PKG) |
| 83 | .append(PERIOD) |
| 84 | .append(getYangVersion(version)) |
| 85 | .append(PERIOD) |
Gaurav Agrawal | fda09be | 2017-05-15 15:48:25 +0530 | [diff] [blame] | 86 | .append(getCamelCase(name, resolver)); |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 87 | if (revision != null) { |
| 88 | pkg.append(PERIOD) |
| 89 | .append(getYangRevisionStr(revision.getRevDate())); |
| 90 | } |
| 91 | return pkg.toString().toLowerCase(); |
| 92 | } |
| 93 | |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 94 | /** |
| 95 | * Returns version. |
| 96 | * |
| 97 | * @param ver YANG version |
| 98 | * @return version |
| 99 | */ |
sonugupta-huawei | 711d1e3 | 2017-09-14 10:59:08 +0530 | [diff] [blame] | 100 | private static String getYangVersion(String ver) { |
| 101 | if (ver.equals(ONE_DOT_ONE)) { |
| 102 | ver = "11"; |
| 103 | } |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 104 | return VERSION_PREFIX + ver; |
| 105 | } |
| 106 | |
| 107 | /** |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 108 | * Returns revision string array. |
| 109 | * |
| 110 | * @param date YANG module revision |
| 111 | * @return revision string |
| 112 | */ |
Vidyashree Rama | 7b9726e | 2019-08-26 13:48:33 +0530 | [diff] [blame] | 113 | private static String getYangRevisionStr(LocalDate date) { |
| 114 | String[] revisionArr = date.toString().split(HYPHEN); |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 115 | |
| 116 | StringBuilder rev = new StringBuilder(REVISION_PREFIX) |
| 117 | .append(revisionArr[INDEX_ZERO]); |
| 118 | |
| 119 | for (int i = INDEX_ONE; i < revisionArr.length; i++) { |
| 120 | Integer val = Integer.parseInt(revisionArr[i]); |
| 121 | if (val < VALUE_CHECK) { |
| 122 | rev.append(ZERO); |
| 123 | } |
| 124 | rev.append(val); |
| 125 | } |
| 126 | return rev.toString(); |
| 127 | } |
| 128 | |
| 129 | /** |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 130 | * Returns enum's java name. |
| 131 | * |
| 132 | * @param name enum's name |
| 133 | * @return enum's java name |
| 134 | */ |
| 135 | public static String getEnumJavaAttribute(String name) { |
| 136 | |
sonugupta-huawei | 35540a3 | 2017-08-22 16:18:50 +0530 | [diff] [blame] | 137 | if (name.equals("*")) { |
| 138 | name = ASTERISK; |
| 139 | } else { |
| 140 | name = name.replaceAll(REGEX_WITH_ALL_SPECIAL_CHAR, COLON); |
| 141 | } |
| 142 | |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 143 | String[] strArray = name.split(COLON); |
| 144 | StringBuilder output = new StringBuilder(); |
| 145 | if (strArray[0].isEmpty()) { |
| 146 | List<String> stringArrangement = new ArrayList<>(); |
| 147 | stringArrangement.addAll(Arrays.asList(strArray).subList(1, strArray.length)); |
| 148 | strArray = stringArrangement.toArray(new String[stringArrangement.size()]); |
| 149 | } |
| 150 | for (int i = 0; i < strArray.length; i++) { |
| 151 | if (i > 0 && i < strArray.length) { |
| 152 | output.append(UNDER_SCORE); |
| 153 | } |
| 154 | output.append(strArray[i]); |
| 155 | } |
| 156 | return output.toString(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Creates a package structure with package info java file if not present. |
| 161 | * |
| 162 | * @param yangNode YANG node for which code is being generated |
| 163 | * @throws IOException any IO exception |
| 164 | */ |
| 165 | public static void createPackage(YangNode yangNode) |
| 166 | throws IOException { |
| 167 | if (!(yangNode instanceof JavaFileInfoContainer)) { |
| 168 | throw new TranslatorException("current node must have java file info " + |
| 169 | yangNode.getName() + " in " + |
| 170 | yangNode.getLineNumber() + " at " + |
| 171 | yangNode.getCharPosition() + |
| 172 | " in " + yangNode.getFileName()); |
| 173 | } |
| 174 | String pkgInfo; |
| 175 | JavaFileInfoTranslator javaFileInfo = ((JavaFileInfoContainer) yangNode) |
| 176 | .getJavaFileInfo(); |
| 177 | String pkg = getAbsolutePackagePath(javaFileInfo.getBaseCodeGenPath(), |
| 178 | javaFileInfo.getPackageFilePath()); |
| 179 | JavaFileInfoTranslator parentInfo; |
| 180 | if (!doesPackageExist(pkg)) { |
| 181 | try { |
| 182 | File pack = createDirectories(pkg); |
| 183 | YangNode parent = getParentNodeInGenCode(yangNode); |
| 184 | if (parent != null) { |
| 185 | parentInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo(); |
| 186 | pkgInfo = parentInfo.getJavaName(); |
| 187 | addPackageInfo(pack, pkgInfo, getJavaPackageFromPackagePath(pkg), |
| 188 | true); |
| 189 | } else { |
| 190 | pkgInfo = javaFileInfo.getJavaName(); |
| 191 | addPackageInfo(pack, pkgInfo, getJavaPackageFromPackagePath(pkg), |
| 192 | false); |
| 193 | } |
| 194 | } catch (IOException e) { |
Yuta HIGUCHI | 601dfd8 | 2017-08-11 11:26:39 -0700 | [diff] [blame] | 195 | throw new IOException("failed to create package-info file for " + pkg, e); |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Checks if the package directory structure created. |
| 202 | * |
| 203 | * @param pkg Package to check if it is created |
| 204 | * @return existence status of package |
| 205 | */ |
Gaurav Agrawal | fda09be | 2017-05-15 15:48:25 +0530 | [diff] [blame] | 206 | static boolean doesPackageExist(String pkg) { |
Gaurav Agrawal | a599a8f | 2017-01-10 20:45:27 +0530 | [diff] [blame] | 207 | File pkgDir = new File(getPackageDirPathFromJavaJPackage(pkg)); |
| 208 | File pkgWithFile = new File(pkgDir + SLASH + "package-info.java"); |
| 209 | return pkgDir.exists() && pkgWithFile.isFile(); |
| 210 | } |
| 211 | } |