blob: 2639b8b064d656855fad03fb178f2faca633b236 [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
Vinod Kumar S38046502016-03-23 15:30:27 +053019import java.io.File;
Bharat saraswal870c56f2016-02-20 21:57:16 +053020import java.util.ArrayList;
21
Vinod Kumar S38046502016-03-23 15:30:27 +053022import org.onosproject.yangutils.datamodel.YangNode;
b.janani1fef2732016-03-04 12:29:05 +053023import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053024import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
25import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053026import org.onosproject.yangutils.utils.UtilConstants;
27
28/**
29 * Utility Class for translating the name from YANG to java convention.
30 */
31public final class JavaIdentifierSyntax {
32
b.janani1fef2732016-03-04 12:29:05 +053033 private static final int MAX_MONTHS = 12;
34 private static final int MAX_DAYS = 31;
35 private static final int INDEX_ZERO = 0;
36 private static final int INDEX_ONE = 1;
37 private static final int INDEX_TWO = 2;
b.janani1fef2732016-03-04 12:29:05 +053038
Bharat saraswal870c56f2016-02-20 21:57:16 +053039 /**
b.janani1fef2732016-03-04 12:29:05 +053040 * Default constructor.
Bharat saraswal870c56f2016-02-20 21:57:16 +053041 */
42 private JavaIdentifierSyntax() {
43 }
44
45 /**
46 * Get the root package string.
47 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053048 * @param version YANG version
49 * @param nameSpace name space of the module
Bharat saraswal870c56f2016-02-20 21:57:16 +053050 * @param revision revision of the module defined
Vinod Kumar Sc4216002016-03-03 19:55:30 +053051 * @return returns the root package string
Bharat saraswal870c56f2016-02-20 21:57:16 +053052 */
53 public static String getRootPackage(byte version, String nameSpace, String revision) {
54
55 String pkg;
56 pkg = UtilConstants.DEFAULT_BASE_PKG;
57 pkg = pkg + UtilConstants.PERIOD;
58 pkg = pkg + getYangVersion(version);
59 pkg = pkg + UtilConstants.PERIOD;
60 pkg = pkg + getPkgFromNameSpace(nameSpace);
61 pkg = pkg + UtilConstants.PERIOD;
62 pkg = pkg + getYangRevisionStr(revision);
63
Bharat saraswal4bf8b152016-02-25 02:26:43 +053064 return pkg.toLowerCase();
Bharat saraswal870c56f2016-02-20 21:57:16 +053065 }
66
67 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053068 * Get the contained data model parent node.
69 *
70 * @param currentNode current node which parent contained node is required
71 * @return parent node in which the current node is an attribute
72 */
73 public static YangNode getParentNodeInGenCode(YangNode currentNode) {
74
75 /*
76 * TODO: recursive parent lookup to support choice/augment/uses. TODO:
77 * need to check if this needs to be updated for
78 * choice/case/augment/grouping
79 */
80 return currentNode.getParent();
81 }
82
83 /**
84 * Get the node package string.
85 *
86 * @param curNode current java node whose package string needs to be set
87 * @return returns the root package string
88 */
89 public static String getCurNodePackage(YangNode curNode) {
90
91 String pkg;
92 if (!(curNode instanceof HasJavaFileInfo)
93 || curNode.getParent() == null) {
94 throw new RuntimeException("missing parent node to get current node's package");
95 }
96
97 YangNode parentNode = getParentNodeInGenCode(curNode);
98 if (!(parentNode instanceof HasJavaFileInfo)) {
99 throw new RuntimeException("missing parent java node to get current node's package");
100 }
101 JavaFileInfo parentJavaFileHandle = ((HasJavaFileInfo) parentNode).getJavaFileInfo();
102 pkg = parentJavaFileHandle.getPackage() + UtilConstants.PERIOD + parentJavaFileHandle.getJavaName();
103 return pkg.toLowerCase();
104 }
105
106 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530107 * Returns version.
108 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530109 * @param ver YANG version
Bharat saraswal870c56f2016-02-20 21:57:16 +0530110 * @return version
111 */
112 private static String getYangVersion(byte ver) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530113
Bharat saraswal870c56f2016-02-20 21:57:16 +0530114 return "v" + ver;
115 }
116
117 /**
118 * Get package name from name space.
119 *
120 * @param nameSpace name space of YANG module
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530121 * @return java package name as per java rules
Bharat saraswal870c56f2016-02-20 21:57:16 +0530122 */
123 public static String getPkgFromNameSpace(String nameSpace) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530124
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 ArrayList<String> pkgArr = new ArrayList<String>();
b.janani1fef2732016-03-04 12:29:05 +0530126 nameSpace = nameSpace.replace(UtilConstants.QUOTES, UtilConstants.EMPTY_STRING);
127 String properNameSpace = nameSpace.replaceAll(UtilConstants.REGEX_WITH_SPECIAL_CHAR, UtilConstants.COLAN);
128 String[] nameSpaceArr = properNameSpace.split(UtilConstants.COLAN);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530129
130 for (String nameSpaceString : nameSpaceArr) {
131 pkgArr.add(nameSpaceString);
132 }
133 return getPkgFrmArr(pkgArr);
134 }
135
136 /**
137 * Returns revision string array.
138 *
139 * @param date YANG module revision
140 * @return revision string
b.janani1fef2732016-03-04 12:29:05 +0530141 * @throws TranslatorException when date is invalid.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530142 */
b.janani1fef2732016-03-04 12:29:05 +0530143 public static String getYangRevisionStr(String date) throws TranslatorException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530144
Bharat saraswal870c56f2016-02-20 21:57:16 +0530145 String[] revisionArr = date.split(UtilConstants.HYPHEN);
146
147 String rev = "rev";
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530148 rev = rev + revisionArr[INDEX_ZERO];
b.janani1fef2732016-03-04 12:29:05 +0530149
Vinod Kumar S38046502016-03-23 15:30:27 +0530150 if (Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS
b.janani1fef2732016-03-04 12:29:05 +0530151 && Integer.parseInt(revisionArr[INDEX_TWO]) <= MAX_DAYS) {
152 for (int i = INDEX_ONE; i < revisionArr.length; i++) {
153
154 Integer val = Integer.parseInt(revisionArr[i]);
155 if (val < 10) {
156 rev = rev + "0";
157 }
158 rev = rev + val;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530159 }
b.janani1fef2732016-03-04 12:29:05 +0530160
161 return rev;
162 } else {
163 throw new TranslatorException("Date in revision is not proper: " + date);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530164 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530165 }
166
167 /**
168 * Returns the package string.
169 *
170 * @param pkgArr package array
171 * @return package string
172 */
173 public static String getPkgFrmArr(ArrayList<String> pkgArr) {
174
b.janani1fef2732016-03-04 12:29:05 +0530175 String pkg = UtilConstants.EMPTY_STRING;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530176 int size = pkgArr.size();
177 int i = 0;
178 for (String member : pkgArr) {
b.janani1fef2732016-03-04 12:29:05 +0530179 boolean presenceOfKeyword = UtilConstants.JAVA_KEY_WORDS.contains(member);
Vinod Kumar S38046502016-03-23 15:30:27 +0530180 if (presenceOfKeyword || member.matches(UtilConstants.REGEX_FOR_FIRST_DIGIT)) {
b.janani1fef2732016-03-04 12:29:05 +0530181 member = UtilConstants.UNDER_SCORE + member;
182 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530183 pkg = pkg + member;
184 if (i != size - 1) {
185 pkg = pkg + UtilConstants.PERIOD;
186 }
187 i++;
188 }
189 return pkg;
190 }
191
192 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530193 * Get package sub name from YANG identifier name.
194 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530195 * @param name YANG identifier name
196 * @return java package sub name as per java rules
Bharat saraswal870c56f2016-02-20 21:57:16 +0530197 */
198 public static String getSubPkgFromName(String name) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530199
Bharat saraswal870c56f2016-02-20 21:57:16 +0530200 ArrayList<String> pkgArr = new ArrayList<String>();
201 String[] nameArr = name.split(UtilConstants.COLAN);
202
203 for (String nameString : nameArr) {
204 pkgArr.add(nameString);
205 }
206 return getPkgFrmArr(pkgArr);
207 }
208
209 /**
210 * Translate the YANG identifier name to java identifier.
211 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530212 * @param yangIdentifier identifier in YANG file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530213 * @return corresponding java identifier
214 */
215 public static String getCamelCase(String yangIdentifier) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530216
Bharat saraswal870c56f2016-02-20 21:57:16 +0530217 String[] strArray = yangIdentifier.split(UtilConstants.HYPHEN);
218 String camelCase = strArray[0];
219 for (int i = 1; i < strArray.length; i++) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530220 camelCase = camelCase + strArray[i].substring(0, 1).toUpperCase() + strArray[i].substring(1);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530221 }
222 return camelCase;
223 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530224
225 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530226 * Translate the YANG identifier name to java identifier with first letter
227 * in caps.
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530228 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530229 * @param yangIdentifier identifier in YANG file
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530230 * @return corresponding java identifier
231 */
232 public static String getCaptialCase(String yangIdentifier) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530233
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530234 return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
235 }
b.janani1fef2732016-03-04 12:29:05 +0530236
237 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530238 * Translate the YANG identifier name to java identifier with first letter
239 * in small.
b.janani1fef2732016-03-04 12:29:05 +0530240 *
241 * @param yangIdentifier identifier in YANG file.
242 * @return corresponding java identifier
243 */
244 public static String getLowerCase(String yangIdentifier) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530245
b.janani1fef2732016-03-04 12:29:05 +0530246 return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
247 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530248
249 /**
250 * Get the java Package from package path.
251 *
252 * @param packagePath package path
253 * @return java package
254 */
255 public static String getJavaPackageFromPackagePath(String packagePath) {
256
257 return packagePath.replace(File.separator, UtilConstants.PERIOD);
258 }
259
260 /**
261 * Get the directory path corresponding to java package.
262 *
263 * @param packagePath package path
264 * @return java package
265 */
266 public static String getPackageDirPathFromJavaJPackage(String packagePath) {
267
268 return packagePath.replace(UtilConstants.PERIOD, File.separator);
269 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530270}