summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/toscaparser/api/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/onap/sdc/toscaparser/api/utils')
-rw-r--r--src/main/java/org/onap/sdc/toscaparser/api/utils/CopyUtils.java41
-rw-r--r--src/main/java/org/onap/sdc/toscaparser/api/utils/DumpUtils.java91
-rw-r--r--src/main/java/org/onap/sdc/toscaparser/api/utils/JToscaErrorCodes.java12
-rw-r--r--src/main/java/org/onap/sdc/toscaparser/api/utils/TOSCAVersionProperty.java157
-rw-r--r--src/main/java/org/onap/sdc/toscaparser/api/utils/ThreadLocalsHolder.java17
-rw-r--r--src/main/java/org/onap/sdc/toscaparser/api/utils/UrlUtils.java88
-rw-r--r--src/main/java/org/onap/sdc/toscaparser/api/utils/ValidateUtils.java502
7 files changed, 453 insertions, 455 deletions
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/utils/CopyUtils.java b/src/main/java/org/onap/sdc/toscaparser/api/utils/CopyUtils.java
index a15afe4..237b738 100644
--- a/src/main/java/org/onap/sdc/toscaparser/api/utils/CopyUtils.java
+++ b/src/main/java/org/onap/sdc/toscaparser/api/utils/CopyUtils.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -26,24 +26,25 @@ import java.util.Map;
public class CopyUtils {
+ private CopyUtils() {
+ }
+
@SuppressWarnings("unchecked")
- public static Object copyLhmOrAl(Object src) {
- if(src instanceof LinkedHashMap) {
- LinkedHashMap<String,Object> dst = new LinkedHashMap<String,Object>();
- for(Map.Entry<String,Object> me: ((LinkedHashMap<String,Object>)src).entrySet()) {
- dst.put(me.getKey(),me.getValue());
- }
- return dst;
- }
- else if(src instanceof ArrayList) {
- ArrayList<Object> dst = new ArrayList<Object>();
- for(Object o: (ArrayList<Object>)src) {
- dst.add(o);
- }
- return dst;
- }
- else {
- return null;
- }
+ public static Object copyLhmOrAl(Object src) {
+ if (src instanceof LinkedHashMap) {
+ LinkedHashMap<String, Object> dst = new LinkedHashMap<String, Object>();
+ for (Map.Entry<String, Object> me : ((LinkedHashMap<String, Object>) src).entrySet()) {
+ dst.put(me.getKey(), me.getValue());
+ }
+ return dst;
+ } else if (src instanceof ArrayList) {
+ ArrayList<Object> dst = new ArrayList<Object>();
+ for (Object o : (ArrayList<Object>) src) {
+ dst.add(o);
+ }
+ return dst;
+ } else {
+ return null;
+ }
}
}
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/utils/DumpUtils.java b/src/main/java/org/onap/sdc/toscaparser/api/utils/DumpUtils.java
index d87103b..158a3e1 100644
--- a/src/main/java/org/onap/sdc/toscaparser/api/utils/DumpUtils.java
+++ b/src/main/java/org/onap/sdc/toscaparser/api/utils/DumpUtils.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,51 +25,44 @@ import java.util.LinkedHashMap;
import java.util.Map;
public class DumpUtils {
-
- @SuppressWarnings("unchecked")
- public static void dumpYaml(Object yo,int level) {
- final String indent = " ";
- try {
- if(yo == null) {
- System.out.println("<null>");
- return;
- }
- String cname = yo.getClass().getSimpleName();
- System.out.print(cname);
- if(cname.equals("LinkedHashMap")) {
- LinkedHashMap<String,Object> lhm = (LinkedHashMap<String,Object>)yo;
- System.out.println();
- for(Map.Entry<String,Object> me: lhm.entrySet()) {
- System.out.print(indent.substring(0,level) + me.getKey() + ": ");
- dumpYaml(me.getValue(),level+2);
- }
- }
- else if(cname.equals("ArrayList")) {
- ArrayList<Object> al = (ArrayList<Object>)yo;
- System.out.println();
- for (int i=0; i<al.size(); i++) {
- System.out.format("%s[%d] ",indent.substring(0,level),i);
- dumpYaml(al.get(i),level+2);
- }
- }
- else if(cname.equals("String")) {
- System.out.println(" ==> \"" + (String)yo + "\"");
- }
- else if(cname.equals("Integer")) {
- System.out.println(" ==> " + (int)yo);
- }
- else if(cname.equals("Boolean")) {
- System.out.println(" ==> " + (boolean)yo);
- }
- else if(cname.equals("Double")) {
- System.out.println(" ==> " + (double)yo);
- }
- else {
- System.out.println(" !! unexpected type");
- }
- }
- catch(Exception e) {
- System.out.println("Exception!! " + e.getMessage());
- }
- }
+
+ @SuppressWarnings("unchecked")
+ private static void dumpYaml(Object yo, int level) {
+ final String indent = " ";
+ try {
+ if (yo == null) {
+ System.out.println("<null>");
+ return;
+ }
+ String cname = yo.getClass().getSimpleName();
+ System.out.print(cname);
+ if (cname.equals("LinkedHashMap")) {
+ LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) yo;
+ System.out.println();
+ for (Map.Entry<String, Object> me : lhm.entrySet()) {
+ System.out.print(indent.substring(0, level) + me.getKey() + ": ");
+ dumpYaml(me.getValue(), level + 2);
+ }
+ } else if (cname.equals("ArrayList")) {
+ ArrayList<Object> al = (ArrayList<Object>) yo;
+ System.out.println();
+ for (int i = 0; i < al.size(); i++) {
+ System.out.format("%s[%d] ", indent.substring(0, level), i);
+ dumpYaml(al.get(i), level + 2);
+ }
+ } else if (cname.equals("String")) {
+ System.out.println(" ==> \"" + (String) yo + "\"");
+ } else if (cname.equals("Integer")) {
+ System.out.println(" ==> " + (int) yo);
+ } else if (cname.equals("Boolean")) {
+ System.out.println(" ==> " + (boolean) yo);
+ } else if (cname.equals("Double")) {
+ System.out.println(" ==> " + (double) yo);
+ } else {
+ System.out.println(" !! unexpected type");
+ }
+ } catch (Exception e) {
+ System.out.println("Exception!! " + e.getMessage());
+ }
+ }
}
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/utils/JToscaErrorCodes.java b/src/main/java/org/onap/sdc/toscaparser/api/utils/JToscaErrorCodes.java
index 3515ed0..3849ce0 100644
--- a/src/main/java/org/onap/sdc/toscaparser/api/utils/JToscaErrorCodes.java
+++ b/src/main/java/org/onap/sdc/toscaparser/api/utils/JToscaErrorCodes.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,7 +25,7 @@ public enum JToscaErrorCodes {
MISSING_META_FILE("JE1001"),
INVALID_META_YAML_CONTENT("JE1002"),
ENTRY_DEFINITION_NOT_DEFINED("JE1003"),
- MISSING_ENTRY_DEFINITION_FILE ("JE1004"),
+ MISSING_ENTRY_DEFINITION_FILE("JE1004"),
GENERAL_ERROR("JE1005"),
PATH_NOT_VALID("JE1006"),
CSAR_TOSCA_VALIDATION_ERROR("JE1007"),
@@ -33,7 +33,7 @@ public enum JToscaErrorCodes {
private String value;
- private JToscaErrorCodes(String value) {
+ JToscaErrorCodes(String value) {
this.value = value;
}
@@ -42,8 +42,8 @@ public enum JToscaErrorCodes {
}
public static JToscaErrorCodes getByCode(String code) {
- for(JToscaErrorCodes v : values()){
- if( v.getValue().equals(code)){
+ for (JToscaErrorCodes v : values()) {
+ if (v.getValue().equals(code)) {
return v;
}
}
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/utils/TOSCAVersionProperty.java b/src/main/java/org/onap/sdc/toscaparser/api/utils/TOSCAVersionProperty.java
index 838fb07..a753d62 100644
--- a/src/main/java/org/onap/sdc/toscaparser/api/utils/TOSCAVersionProperty.java
+++ b/src/main/java/org/onap/sdc/toscaparser/api/utils/TOSCAVersionProperty.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,104 +25,111 @@ import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-public class TOSCAVersionProperty {// test with functions/test_concat.yaml
-
- private String version;
-
- private static final String versionRe =
- "^(?<gMajorVersion>([0-9][0-9]*))" +
- "(\\.(?<gMinorVersion>([0-9][0-9]*)))?" +
- "(\\.(?<gFixVersion>([0-9][0-9]*)))?" +
- "(\\.(?<gQualifier>([0-9A-Za-z]+)))?" +
- "(\\-(?<gBuildVersion>[0-9])*)?$";
-
- private String minorVersion = null;
- private String majorVersion = null;
- private String fixVersion = null;
- private String qualifier = null;
- private String buildVersion = null;
-
-
- public TOSCAVersionProperty(Object _version) {
- version = _version.toString();
-
- if(version.equals("0") || version.equals("0.0") || version.equals("0.0.0")) {
- //log.warning(_('Version assumed as not provided'))
- version = "";
+// test with functions/test_concat.yaml
+public class TOSCAVersionProperty {
+
+ private String version;
+
+ private static final String VERSION_RE =
+ "^(?<gMajorVersion>([0-9][0-9]*))"
+ + "(\\.(?<gMinorVersion>([0-9][0-9]*)))?"
+ + "(\\.(?<gFixVersion>([0-9][0-9]*)))?"
+ + "(\\.(?<gQualifier>([0-9A-Za-z]+)))?"
+ + "(\\-(?<gBuildVersion>[0-9])*)?$";
+
+ private String minorVersion = null;
+ private String majorVersion = null;
+ private String fixVersion = null;
+ private String qualifier = null;
+ private String buildVersion = null;
+
+
+ public TOSCAVersionProperty(String version) {
+
+ if (version.equals("0") || version.equals("0.0") || version.equals("0.0.0")) {
return;
}
- Pattern pattern = Pattern.compile(versionRe);
- Matcher matcher = pattern.matcher(version);
- if(!matcher.find()) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE252", String.format(
- "InvalidTOSCAVersionPropertyException: " +
- "Value of TOSCA version property \"%s\" is invalid",
- version)));
+ Pattern pattern = Pattern.compile(VERSION_RE);
+ Matcher matcher = pattern.matcher(version);
+ if (!matcher.find()) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(
+ new JToscaValidationIssue(
+ "JE252",
+ "InvalidTOSCAVersionPropertyException: "
+ + "Value of TOSCA version property \"" + version + "\" is invalid"
+ ));
return;
- }
+ }
minorVersion = matcher.group("gMinorVersion");
majorVersion = matcher.group("gMajorVersion");
fixVersion = matcher.group("gFixVersion");
- qualifier = _validateQualifier(matcher.group("gQualifier"));
- buildVersion = _validateBuild(matcher.group("gBuildVersion"));
- _validateMajorVersion(majorVersion);
-
- }
-
- private String _validateMajorVersion(String value) {
+ qualifier = validateQualifier(matcher.group("gQualifier"));
+ buildVersion = validateBuild(matcher.group("gBuildVersion"));
+ validateMajorVersion(majorVersion);
+
+ this.version = version;
+
+ }
+
+ private String validateMajorVersion(String value) {
// Validate major version
// Checks if only major version is provided and assumes
// minor version as 0.
// Eg: If version = 18, then it returns version = '18.0'
- if(minorVersion == null && buildVersion == null && !value.equals("0")) {
+ if (minorVersion == null && buildVersion == null && !value.equals("0")) {
//log.warning(_('Minor version assumed "0".'))
version = version + "0";
}
return value;
- }
-
- private String _validateQualifier(String value) {
- // Validate qualifier
-
- // TOSCA version is invalid if a qualifier is present without the
- // fix version or with all of major, minor and fix version 0s.
-
- // For example, the following versions are invalid
- // 18.0.abc
- // 0.0.0.abc
-
- if((fixVersion == null && value != null) ||
- (minorVersion.equals("0") && majorVersion.equals("0") &&
- fixVersion.equals("0") && value != null)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE253", String.format(
- "InvalidTOSCAVersionPropertyException: " +
- "Value of TOSCA version property \"%s\" is invalid",
- version)));
- }
- return value;
- }
-
- private String _validateBuild(String value) {
+ }
+
+ private String validateQualifier(String value) {
+ // Validate qualifier
+
+ // TOSCA version is invalid if a qualifier is present without the
+ // fix version or with all of major, minor and fix version 0s.
+
+ // For example, the following versions are invalid
+ // 18.0.abc
+ // 0.0.0.abc
+
+ if ((fixVersion == null && value != null) || (minorVersion.equals("0") && majorVersion.equals("0")
+ && fixVersion.equals("0") && value != null)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(
+ new JToscaValidationIssue(
+ "JE253",
+ "InvalidTOSCAVersionPropertyException: Value of TOSCA version property \""
+ + version
+ + "\" is invalid"
+ ));
+ }
+ return value;
+ }
+
+ private String validateBuild(String value) {
// Validate build version
// TOSCA version is invalid if build version is present without the qualifier.
// Eg: version = 18.0.0-1 is invalid.
- if(qualifier == null && value != null) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE254", String.format(
- "InvalidTOSCAVersionPropertyException: " +
- "Value of TOSCA version property \"%s\" is invalid",
- version)));
- }
+ if (qualifier == null && value != null) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(
+ new JToscaValidationIssue(
+ "JE254",
+ "InvalidTOSCAVersionPropertyException: "
+ + "Value of TOSCA version property \"" + version + "\" is invalid"
+ )
+ );
+ }
return value;
}
- public Object getVersion() {
- return version;
- }
+ public Object getVersion() {
+ return version;
+ }
}
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/utils/ThreadLocalsHolder.java b/src/main/java/org/onap/sdc/toscaparser/api/utils/ThreadLocalsHolder.java
index 2ea8d08..4c4581b 100644
--- a/src/main/java/org/onap/sdc/toscaparser/api/utils/ThreadLocalsHolder.java
+++ b/src/main/java/org/onap/sdc/toscaparser/api/utils/ThreadLocalsHolder.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,21 +24,22 @@ import org.onap.sdc.toscaparser.api.common.ValidationIssueCollector;
public class ThreadLocalsHolder {
- private static final ThreadLocal<ValidationIssueCollector> exceptionCollectorThreadLocal = new ThreadLocal<>();
+ private static final ThreadLocal<ValidationIssueCollector> EXCEPTION_COLLECTOR_THREAD_LOCAL = new ThreadLocal<>();
- private ThreadLocalsHolder(){}
+ private ThreadLocalsHolder() {
+ }
public static ValidationIssueCollector getCollector() {
- return exceptionCollectorThreadLocal.get();
+ return EXCEPTION_COLLECTOR_THREAD_LOCAL.get();
}
public static void setCollector(ValidationIssueCollector validationIssueCollector) {
cleanup();
- exceptionCollectorThreadLocal.set(validationIssueCollector);
+ EXCEPTION_COLLECTOR_THREAD_LOCAL.set(validationIssueCollector);
}
- public static void cleanup(){
- exceptionCollectorThreadLocal.remove();
+ public static void cleanup() {
+ EXCEPTION_COLLECTOR_THREAD_LOCAL.remove();
}
}
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/utils/UrlUtils.java b/src/main/java/org/onap/sdc/toscaparser/api/utils/UrlUtils.java
index 72e5122..d081d91 100644
--- a/src/main/java/org/onap/sdc/toscaparser/api/utils/UrlUtils.java
+++ b/src/main/java/org/onap/sdc/toscaparser/api/utils/UrlUtils.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -28,61 +28,63 @@ import java.net.MalformedURLException;
import java.net.URL;
public class UrlUtils {
-
- public static boolean validateUrl(String sUrl) {
+
+ private static final int HTTP_STATUS_OK = 200;
+
+ private UrlUtils() {
+ }
+
+ public static boolean validateUrl(String sUrl) {
// Validates whether the given path is a URL or not
// If the given path includes a scheme (http, https, ftp, ...) and a net
// location (a domain name such as www.github.com) it is validated as a URL
- try {
- URL url = new URL(sUrl);
- if(url.getProtocol().equals("file")) {
- return true;
- }
- return url.getAuthority() != null;
- }
- catch(MalformedURLException e) {
- return false;
- }
- }
-
- public static String joinUrl(String sUrl,String relativePath) {
+ try {
+ URL url = new URL(sUrl);
+ if (url.getProtocol().equals("file")) {
+ return true;
+ }
+ return url.getAuthority() != null;
+ } catch (MalformedURLException e) {
+ return false;
+ }
+ }
+
+ public static String joinUrl(String sUrl, String relativePath) {
// Builds a new URL from the given URL and the relative path
// Example:
// url: http://www.githib.com/openstack/heat
// relative_path: heat-translator
// - joined: http://www.githib.com/openstack/heat-translator
- if(!validateUrl(sUrl)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE255", String.format(
- "ValueError: The URL \"%s\" is malformed",sUrl)));
- }
- try {
- URL base = new URL(sUrl);
- return (new URL(base,relativePath)).toString();
- }
- catch(MalformedURLException e) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE256", String.format(
- "ValueError: Joining URL \"%s\" and relative path \"%s\" caused an exception",sUrl,relativePath)));
- return sUrl;
- }
- }
-
- public static boolean isUrlAccessible(String sUrl) {
+ if (!validateUrl(sUrl)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE255", String.format(
+ "ValueError: The URL \"%s\" is malformed", sUrl)));
+ }
+ try {
+ URL base = new URL(sUrl);
+ return (new URL(base, relativePath)).toString();
+ } catch (MalformedURLException e) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE256", String.format(
+ "ValueError: Joining URL \"%s\" and relative path \"%s\" caused an exception", sUrl, relativePath)));
+ return sUrl;
+ }
+ }
+
+ public static boolean isUrlAccessible(String sUrl) {
// Validates whether the given URL is accessible
// Returns true if the get call returns a 200 response code.
// Otherwise, returns false.
- try {
- HttpURLConnection connection = (HttpURLConnection) new URL(sUrl).openConnection();
- connection.setRequestMethod("HEAD");
- int responseCode = connection.getResponseCode();
- return responseCode == 200;
- }
- catch(IOException e) {
- return false;
- }
- }
+ try {
+ HttpURLConnection connection = (HttpURLConnection) new URL(sUrl).openConnection();
+ connection.setRequestMethod("HEAD");
+ int responseCode = connection.getResponseCode();
+ return responseCode == HTTP_STATUS_OK;
+ } catch (IOException e) {
+ return false;
+ }
+ }
}
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/utils/ValidateUtils.java b/src/main/java/org/onap/sdc/toscaparser/api/utils/ValidateUtils.java
index a9786ae..b90d882 100644
--- a/src/main/java/org/onap/sdc/toscaparser/api/utils/ValidateUtils.java
+++ b/src/main/java/org/onap/sdc/toscaparser/api/utils/ValidateUtils.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,246 +27,241 @@ import java.util.Date;
import java.util.LinkedHashMap;
public class ValidateUtils {
-
- private static final String RANGE_UNBOUNDED = "UNBOUNDED";
-
- public static Object strToNum(Object value) {
- // Convert a string representation of a number into a numeric type
- // TODO(TBD) we should not allow numeric values in, input should be str
- if(value instanceof Number) {
- return value;
- }
- if(!(value instanceof String)) {
-
- }
- try {
- return Integer.parseInt((String)value);
- }
- catch(NumberFormatException e) {
- }
- try {
- return Float.parseFloat((String)value);
- }
- catch(Exception e) {
- }
- return null;
- }
-
- public static Object validateNumeric(Object value) {
- if(value != null) {
- if (!(value instanceof Number)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE257", String.format(
- "ValueError: \"%s\" is not a numeric",value.toString())));
- }
- }
- return value;
- }
-
- public static Object validateInteger(Object value) {
- if(value != null) {
- if (!(value instanceof Integer)) {
- // allow "true" and "false"
- if (value instanceof Boolean) {
- return (Boolean) value ? 1 : 0;
- }
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE258", String.format(
- "ValueError: \"%s\" is not an integer",value.toString())));
- }
- }
- return value;
- }
-
- public static Object validateFloat(Object value) {
- if(value != null) {
- if (!(value instanceof Float || value instanceof Double)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE259", String.format(
- "ValueError: \"%s\" is not a float",value.toString())));
- }
- }
- return value;
- }
-
- public static Object validateString(Object value) {
- if(value != null) {
- if (!(value instanceof String)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE260", String.format(
- "ValueError: \'%s\' is not a string",value.toString())));
- }
- }
- return value;
- }
-
- public static Object validateList(Object value) {
- if(value != null) {
- if (!(value instanceof ArrayList)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE261", String.format(
- "ValueError: \"%s\" is not a list",value.toString())));
- }
- }
- return value;
- }
-
-
- @SuppressWarnings("unchecked")
- public static Object validateRange(Object range) {
- // list class check
- validateList(range);
- // validate range list has a min and max
- if(range instanceof ArrayList && ((ArrayList<Object>)range).size() != 2) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE262", String.format(
- "ValueError: \"%s\" is not a valid range",range.toString())));
- // too dangerous to continue...
- return range;
- }
- // validate min and max are numerics or the keyword UNBOUNDED
- boolean minTest = false;
- boolean maxTest = false;
- Object r0 = ((ArrayList<Object>)range).get(0);
- Object r1 = ((ArrayList<Object>)range).get(1);
-
- if(!(r0 instanceof Integer) && !(r0 instanceof Float) ||
- !(r1 instanceof Integer) && !(r1 instanceof Float)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE263", String.format(
- "ValueError: \"%s\" is not a valid range",range.toString())));
- // too dangerous to continue...
- return range;
- }
-
- Float min = 0.0F;
- Float max = 0.0F;
- if(r0 instanceof String && ((String)r0).equals(RANGE_UNBOUNDED)) {
- minTest = true;
- }
- else {
- min = r0 instanceof Integer ? ((Integer)r0).floatValue() : (Float)r0;
- }
- if(r1 instanceof String && ((String)r1).equals(RANGE_UNBOUNDED)) {
- maxTest = true;
- }
- else {
- max = r1 instanceof Integer ? ((Integer)r1).floatValue() : (Float)r1;
- }
-
- // validate the max > min (account for UNBOUNDED)
- if(!minTest && !maxTest) {
- // Note: min == max is allowed
- if(min > max) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE264", String.format(
- "ValueError:\"%s\" is not a valid range",range.toString())));
- }
- }
- return range;
- }
-
- @SuppressWarnings("unchecked")
- public static Object validateValueInRange(Object value,Object range,String propName) {
- // verify all 3 are numeric and convert to Floats
- if(!(value instanceof Integer || value instanceof Float)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE265", String.format(
- "ValueError: validateInRange: \"%s\" is not a number",range.toString())));
+
+ private static final String RANGE_UNBOUNDED = "UNBOUNDED";
+
+ private ValidateUtils() {
+ }
+
+ public static Object strToNum(Object value) {
+ // Convert a string representation of a number into a numeric type
+ // TODO(TBD) we should not allow numeric values in, input should be str
+ if (value instanceof Number) {
+ return value;
+ }
+ try {
+ return Integer.parseInt((String) value);
+ } catch (NumberFormatException e) {
+ }
+ try {
+ return Float.parseFloat((String) value);
+ } catch (Exception e) {
+ }
+ return null;
+ }
+
+ public static Object validateNumeric(Object value) {
+ if (value != null) {
+ if (!(value instanceof Number)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE257", String.format(
+ "ValueError: \"%s\" is not a numeric", value.toString())));
+ }
+ }
+ return value;
+ }
+
+ public static Object validateInteger(Object value) {
+ if (value != null) {
+ if (!(value instanceof Integer)) {
+ // allow "true" and "false"
+ if (value instanceof Boolean) {
+ return (Boolean) value ? 1 : 0;
+ }
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE258", String.format(
+ "ValueError: \"%s\" is not an integer", value.toString())));
+ }
+ }
+ return value;
+ }
+
+ public static Object validateFloat(Object value) {
+ if (value != null) {
+ if (!(value instanceof Float || value instanceof Double)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE259", String.format(
+ "ValueError: \"%s\" is not a float", value.toString())));
+ }
+ }
+ return value;
+ }
+
+ public static Object validateString(Object value) {
+ if (value != null) {
+ if (!(value instanceof String)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE260", String.format(
+ "ValueError: \'%s\' is not a string", value.toString())));
+ }
+ }
+ return value;
+ }
+
+ public static Object validateList(Object value) {
+ if (value != null) {
+ if (!(value instanceof ArrayList)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE261", String.format(
+ "ValueError: \"%s\" is not a list", value.toString())));
+ }
+ }
+ return value;
+ }
+
+
+ @SuppressWarnings("unchecked")
+ public static Object validateRange(Object range) {
+ // list class check
+ validateList(range);
+ // validate range list has a min and max
+ if (range instanceof ArrayList && ((ArrayList<Object>) range).size() != 2) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE262", String.format(
+ "ValueError: \"%s\" is not a valid range", range.toString())));
+ // too dangerous to continue...
+ return range;
+ }
+ // validate min and max are numerics or the keyword UNBOUNDED
+ boolean minTest = false;
+ boolean maxTest = false;
+ Object r0 = ((ArrayList<Object>) range).get(0);
+ Object r1 = ((ArrayList<Object>) range).get(1);
+
+ if (!(r0 instanceof Integer) && !(r0 instanceof Float)
+ || !(r1 instanceof Integer) && !(r1 instanceof Float)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE263", String.format(
+ "ValueError: \"%s\" is not a valid range", range.toString())));
+ // too dangerous to continue...
+ return range;
+ }
+
+ Float min = 0.0F;
+ Float max = 0.0F;
+ if (r0 instanceof String && ((String) r0).equals(RANGE_UNBOUNDED)) {
+ minTest = true;
+ } else {
+ min = r0 instanceof Integer ? ((Integer) r0).floatValue() : (Float) r0;
+ }
+ if (r1 instanceof String && ((String) r1).equals(RANGE_UNBOUNDED)) {
+ maxTest = true;
+ } else {
+ max = r1 instanceof Integer ? ((Integer) r1).floatValue() : (Float) r1;
+ }
+
+ // validate the max > min (account for UNBOUNDED)
+ if (!minTest && !maxTest) {
+ // Note: min == max is allowed
+ if (min > max) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE264", String.format(
+ "ValueError:\"%s\" is not a valid range", range.toString())));
+ }
+ }
+ return range;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Object validateValueInRange(Object value, Object range, String propName) {
+ // verify all 3 are numeric and convert to Floats
+ if (!(value instanceof Integer || value instanceof Float)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE265", String.format(
+ "ValueError: validateInRange: \"%s\" is not a number", range.toString())));
return value;
- }
- Float fval = value instanceof Integer ? ((Integer)value).floatValue() : (Float)value;
-
- //////////////////////////
- //"validateRange(range);"
- //////////////////////////
- // better safe than sorry...
- // validate that range list has a min and max
- if(range instanceof ArrayList && ((ArrayList<Object>)range).size() != 2) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE266", String.format(
- "ValueError: \"%s\" is not a valid range",range.toString())));
- // too dangerous to continue...
- return value;
- }
- // validate min and max are numerics or the keyword UNBOUNDED
- boolean minTest = false;
- boolean maxTest = false;
- Object r0 = ((ArrayList<Object>)range).get(0);
- Object r1 = ((ArrayList<Object>)range).get(1);
-
- if(!(r0 instanceof Integer) && !(r0 instanceof Float) ||
- !(r1 instanceof Integer) && !(r1 instanceof Float)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE267", String.format(
- "ValueError: \"%s\" is not a valid range",range.toString())));
- // too dangerous to continue...
- return value;
- }
-
- Float min = 0.0F;
- Float max = 0.0F;
- if(r0 instanceof String && ((String)r0).equals(RANGE_UNBOUNDED)) {
- minTest = true;
- }
- else {
- min = r0 instanceof Integer ? ((Integer)r0).floatValue() : (Float)r0;
- }
- if(r1 instanceof String && ((String)r1).equals(RANGE_UNBOUNDED)) {
- maxTest = true;
- }
- else {
- max = r1 instanceof Integer ? ((Integer)r1).floatValue() : (Float)r1;
- }
-
- // validate the max > min (account for UNBOUNDED)
- if(!minTest && !maxTest) {
- // Note: min == max is allowed
- if(min > max) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE268", String.format(
- "ValueError:\"%s\" is not a valid range",range.toString())));
- }
- }
- // finally...
- boolean bError = false;
- //Note: value is valid if equal to min
- if(!minTest) {
- if(fval < min) {
- bError = true;
- }
- }
- // Note: value is valid if equal to max
- if(!maxTest) {
- if(fval > max) {
- bError = true;
- }
- }
- if(bError) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE269", String.format(
- "RangeValueError: Property \"%s\", \"%s\" not in range [\"%s\" - \"%s\"",
- propName,value.toString(),r0.toString(),r1.toString())));
- }
- return value;
- }
-
- public static Object validateMap(Object ob) {
- if(ob != null) {
- if (!(ob instanceof LinkedHashMap)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE270", String.format(
- "ValueError\"%s\" is not a map.",ob.toString())));
- }
- }
- return ob;
- }
-
- public static Object validateBoolean(Object value) {
- if(value != null) {
- if (value instanceof Boolean) {
- return value;
- }
- if (value instanceof String) {
- String normalized = ((String) value).toLowerCase();
- if (normalized.equals("true") || normalized.equals("false")) {
- return normalized.equals("true");
- }
- }
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE271", String.format(
- "ValueError: \"%s\" is not a boolean",value.toString())));
- }
- return value;
- }
-
- public static Object validateTimestamp(Object value) {
- /*
+ }
+ Float fval = value instanceof Integer ? ((Integer) value).floatValue() : (Float) value;
+
+ //////////////////////////
+ //"validateRange(range);"
+ //////////////////////////
+ // better safe than sorry...
+ // validate that range list has a min and max
+ if (range instanceof ArrayList && ((ArrayList<Object>) range).size() != 2) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE266", String.format(
+ "ValueError: \"%s\" is not a valid range", range.toString())));
+ // too dangerous to continue...
+ return value;
+ }
+ // validate min and max are numerics or the keyword UNBOUNDED
+ boolean minTest = false;
+ boolean maxTest = false;
+ Object r0 = ((ArrayList<Object>) range).get(0);
+ Object r1 = ((ArrayList<Object>) range).get(1);
+
+ if (!(r0 instanceof Integer) && !(r0 instanceof Float)
+ || !(r1 instanceof Integer) && !(r1 instanceof Float)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE267", String.format(
+ "ValueError: \"%s\" is not a valid range", range.toString())));
+ // too dangerous to continue...
+ return value;
+ }
+
+ Float min = 0.0F;
+ Float max = 0.0F;
+ if (r0 instanceof String && ((String) r0).equals(RANGE_UNBOUNDED)) {
+ minTest = true;
+ } else {
+ min = r0 instanceof Integer ? ((Integer) r0).floatValue() : (Float) r0;
+ }
+ if (r1 instanceof String && ((String) r1).equals(RANGE_UNBOUNDED)) {
+ maxTest = true;
+ } else {
+ max = r1 instanceof Integer ? ((Integer) r1).floatValue() : (Float) r1;
+ }
+
+ // validate the max > min (account for UNBOUNDED)
+ if (!minTest && !maxTest) {
+ // Note: min == max is allowed
+ if (min > max) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE268", String.format(
+ "ValueError:\"%s\" is not a valid range", range.toString())));
+ }
+ }
+ // finally...
+ boolean bError = false;
+ //Note: value is valid if equal to min
+ if (!minTest) {
+ if (fval < min) {
+ bError = true;
+ }
+ }
+ // Note: value is valid if equal to max
+ if (!maxTest) {
+ if (fval > max) {
+ bError = true;
+ }
+ }
+ if (bError) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE269", String.format(
+ "RangeValueError: Property \"%s\", \"%s\" not in range [\"%s\" - \"%s\"",
+ propName, value.toString(), r0.toString(), r1.toString())));
+ }
+ return value;
+ }
+
+ public static Object validateMap(Object ob) {
+ if (ob != null) {
+ if (!(ob instanceof LinkedHashMap)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE270", String.format(
+ "ValueError\"%s\" is not a map.", ob.toString())));
+ }
+ }
+ return ob;
+ }
+
+ public static Object validateBoolean(Object value) {
+ if (value != null) {
+ if (value instanceof Boolean) {
+ return value;
+ }
+ if (value instanceof String) {
+ String normalized = ((String) value).toLowerCase();
+ if (normalized.equals("true") || normalized.equals("false")) {
+ return normalized.equals("true");
+ }
+ }
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE271", String.format(
+ "ValueError: \"%s\" is not a boolean", value.toString())));
+ }
+ return value;
+ }
+
+ public static Object validateTimestamp(Object value) {
+
+ /*
try:
# Note: we must return our own exception message
# as dateutil's parser returns different types / values on
@@ -280,19 +275,18 @@ public class ValidateUtils {
ValueError(_('"%(val)s" is not a valid timestamp. "%(msg)s"') %
{'val': value, 'msg': original_err_msg}))
*/
-
- // timestamps are loaded as Date objects by the YAML parser
- if(value != null) {
- if (!(value instanceof Date)) {
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE272", String.format(
- "ValueError: \"%s\" is not a valid timestamp",
- value.toString())));
-
- }
- }
- return value;
- }
-
+ // timestamps are loaded as Date objects by the YAML parser
+ if (value != null) {
+ if (!(value instanceof Date)) {
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE272", String.format(
+ "ValueError: \"%s\" is not a valid timestamp",
+ value.toString())));
+
+ }
+ }
+ return value;
+ }
+
}
/*python