aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca/src/main/java/org/onap
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2020-01-09 14:33:29 +0000
committerGerrit Code Review <gerrit@onap.org>2020-01-09 14:33:29 +0000
commita3771d2efbf906cc4bf58683d4b2f40637d63ced (patch)
treecbf069f7ea93df9c7615cf1a4a6496dc50465334 /models-tosca/src/main/java/org/onap
parentca0e6bd8fcc79dd3a681fbb158017e6a5f5397f5 (diff)
parent1902963bbc9d35fc16562b815b8a3f1950b048c9 (diff)
Merge "Update tosca utils to remove code duplication"
Diffstat (limited to 'models-tosca/src/main/java/org/onap')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java123
1 files changed, 70 insertions, 53 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
index 71158f0d1..5ba42fef5 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
@@ -48,11 +48,7 @@ public final class ToscaUtils {
* @param serviceTemplate the service template containing data types to be checked
*/
public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
- String message = checkDataTypesExist(serviceTemplate);
- if (message != null) {
- LOGGER.warn(message);
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
- }
+ assertExist(serviceTemplate, checkDataTypesExist());
}
/**
@@ -61,11 +57,7 @@ public final class ToscaUtils {
* @param serviceTemplate the service template containing policy types to be checked
*/
public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
- String message = checkPolicyTypesExist(serviceTemplate);
- if (message != null) {
- LOGGER.warn(message);
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
- }
+ assertExist(serviceTemplate, checkPolicyTypesExist());
}
/**
@@ -74,11 +66,7 @@ public final class ToscaUtils {
* @param serviceTemplate the service template containing policy types to be checked
*/
public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
- String message = checkPoliciesExist(serviceTemplate);
- if (message != null) {
- LOGGER.warn(message);
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
- }
+ assertExist(serviceTemplate, checkPoliciesExist());
}
/**
@@ -87,7 +75,7 @@ public final class ToscaUtils {
* @param serviceTemplate the service template containing data types to be checked
*/
public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
- return checkDataTypesExist(serviceTemplate) == null;
+ return doExist(serviceTemplate, checkDataTypesExist());
}
/**
@@ -96,7 +84,7 @@ public final class ToscaUtils {
* @param serviceTemplate the service template containing policy types to be checked
*/
public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
- return checkPolicyTypesExist(serviceTemplate) == null;
+ return doExist(serviceTemplate, checkPolicyTypesExist());
}
/**
@@ -105,61 +93,90 @@ public final class ToscaUtils {
* @param serviceTemplate the service template containing policy types to be checked
*/
public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
- return checkPoliciesExist(serviceTemplate) == null;
+ return doExist(serviceTemplate, checkPoliciesExist());
}
/**
- * Check if data types have been specified correctly.
+ * Assert that something have been specified correctly.
*
- * @param serviceTemplate the service template containing data types to be checked
+ * @param serviceTemplate the service template containing policy types to be checked
*/
- public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
- if (serviceTemplate.getDataTypes() == null) {
- return "no data types specified on service template";
- }
-
- if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
- return "list of data types specified on service template is empty";
+ public static void assertExist(final JpaToscaServiceTemplate serviceTemplate,
+ final ToscaChecker<JpaToscaServiceTemplate> checkerFunction) {
+ String message = checkerFunction.check(serviceTemplate);
+ if (message != null) {
+ LOGGER.warn(message);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message);
}
-
- return null;
}
/**
- * Check if policy types have been specified correctly.
+ * Check that something have been specified correctly.
*
* @param serviceTemplate the service template containing policy types to be checked
*/
- public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) {
- if (serviceTemplate.getPolicyTypes() == null) {
- return "no policy types specified on service template";
- }
+ public static boolean doExist(final JpaToscaServiceTemplate serviceTemplate,
+ final ToscaChecker<JpaToscaServiceTemplate> checkerFunction) {
+ return checkerFunction.check(serviceTemplate) == null;
+ }
- if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
- return "list of policy types specified on service template is empty";
- }
+ /**
+ * Check if data types have been specified correctly.
+ */
+ public static ToscaChecker<JpaToscaServiceTemplate> checkDataTypesExist() {
+ return serviceTemplate -> {
+ if (serviceTemplate.getDataTypes() == null) {
+ return "no data types specified on service template";
+ }
+
+ if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) {
+ return "list of data types specified on service template is empty";
+ }
+
+ return null;
+ };
+ }
- return null;
+ /**
+ * Check if policy types have been specified correctly.
+ */
+ public static ToscaChecker<JpaToscaServiceTemplate> checkPolicyTypesExist() {
+ return serviceTemplate -> {
+ if (serviceTemplate.getPolicyTypes() == null) {
+ return "no policy types specified on service template";
+ }
+
+ if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
+ return "list of policy types specified on service template is empty";
+ }
+
+ return null;
+ };
}
/**
* Check if policies have been specified correctly.
- *
- * @param serviceTemplate the service template containing policy types to be checked
*/
- public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) {
- if (serviceTemplate.getTopologyTemplate() == null) {
- return "topology template not specified on service template";
- }
-
- if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
- return "no policies specified on topology template of service template";
- }
-
- if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
- return "list of policies specified on topology template of service template is empty";
- }
+ public static ToscaChecker<JpaToscaServiceTemplate> checkPoliciesExist() {
+ return serviceTemplate -> {
+ if (serviceTemplate.getTopologyTemplate() == null) {
+ return "topology template not specified on service template";
+ }
+
+ if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
+ return "no policies specified on topology template of service template";
+ }
+
+ if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
+ return "list of policies specified on topology template of service template is empty";
+ }
+
+ return null;
+ };
+ }
- return null;
+ @FunctionalInterface
+ interface ToscaChecker<T> {
+ String check(final T serviceTemplate);
}
}