diff options
author | liamfallon <liam.fallon@est.tech> | 2020-01-07 18:26:53 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2020-01-08 09:23:18 +0000 |
commit | 2ed313e69cfd0a587f072079e23ab8f6499518db (patch) | |
tree | 9e461ea953297a02d07337a46a021dcd86f03f07 /models-tosca/src/main | |
parent | f69b6681486e4d1c5859f649a3293488c1859712 (diff) |
Store and return data types for policies
In this change, the data types of policy types are persisted to the
database. All policy types currently in the database are returned on all
policy type get requests. This will be made more intelligent in the next
review.
Issue-ID: POLICY-2315
Change-Id: I10065ab9b90e72114a453494fc706d8aabc50c0a
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main')
-rw-r--r-- | models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java | 21 | ||||
-rw-r--r-- | models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java | 106 |
2 files changed, 102 insertions, 25 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java index cce6fd9ee..0a7983c47 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -29,6 +29,7 @@ import javax.ws.rs.core.Response; import lombok.NonNull; +import org.apache.commons.collections4.CollectionUtils; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptFilter; import org.onap.policy.models.base.PfConceptKey; @@ -187,6 +188,14 @@ public class SimpleToscaProvider { List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version); serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); + // Return all data types + // TODO: In the next review, return just the data types used by the policy types on the policy type list + List<JpaToscaDataType> jpaDataTypeList = dao.getFiltered(JpaToscaDataType.class, null, null); + if (!CollectionUtils.isEmpty(jpaDataTypeList)) { + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + serviceTemplate.getDataTypes().getConceptMap().putAll(asConceptMap(jpaDataTypeList)); + } + LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); return serviceTemplate; } @@ -205,6 +214,11 @@ public class SimpleToscaProvider { ToscaUtils.assertPolicyTypesExist(serviceTemplate); + // Create the data types on the policy type + if (ToscaUtils.doDataTypesExist(serviceTemplate)) { + createDataTypes(dao, serviceTemplate); + } + for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { dao.create(policyType); } @@ -237,6 +251,11 @@ public class SimpleToscaProvider { ToscaUtils.assertPolicyTypesExist(serviceTemplate); + // Update the data types on the policy type + if (ToscaUtils.doDataTypesExist(serviceTemplate)) { + updateDataTypes(dao, serviceTemplate); + } + for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { dao.update(policyType); } @@ -444,7 +463,7 @@ public class SimpleToscaProvider { // Policy type version is not specified, get the latest version from the database List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, policyTypeName, null); - if (jpaPolicyTypeList.isEmpty()) { + if (CollectionUtils.isEmpty(jpaPolicyTypeList)) { return null; } 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 23a428b65..71158f0d1 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 @@ -43,22 +43,86 @@ public final class ToscaUtils { } /** - * Check if data types have been specified correctly. + * Assert that data types have been specified correctly. * * @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); + } + } + + /** + * Assert that policy types have been specified correctly. + * + * @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); + } + } + + /** + * Assert that policies have been specified correctly. + * + * @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); + } + } + + /** + * Check that data types have been specified correctly. + * + * @param serviceTemplate the service template containing data types to be checked + */ + public static boolean doDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) { + return checkDataTypesExist(serviceTemplate) == null; + } + + /** + * Check that policy types have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked + */ + public static boolean doPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { + return checkPolicyTypesExist(serviceTemplate) == null; + } + + /** + * Check that policies have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked + */ + public static boolean doPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { + return checkPoliciesExist(serviceTemplate) == null; + } + + /** + * Check if data types have been specified correctly. + * + * @param serviceTemplate the service template containing data types to be checked + */ + public static String checkDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getDataTypes() == null) { - String errorMessage = "no data types specified on service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "no data types specified on service template"; } if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) { - String errorMessage = "list of data types specified on service template is empty"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "list of data types specified on service template is empty"; } + + return null; } /** @@ -66,18 +130,16 @@ public final class ToscaUtils { * * @param serviceTemplate the service template containing policy types to be checked */ - public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { + public static String checkPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getPolicyTypes() == null) { - String errorMessage = "no policy types specified on service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "no policy types specified on service template"; } if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) { - String errorMessage = "list of policy types specified on service template is empty"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "list of policy types specified on service template is empty"; } + + return null; } /** @@ -85,23 +147,19 @@ public final class ToscaUtils { * * @param serviceTemplate the service template containing policy types to be checked */ - public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { + public static String checkPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getTopologyTemplate() == null) { - String errorMessage = "topology template not specified on service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "topology template not specified on service template"; } if (serviceTemplate.getTopologyTemplate().getPolicies() == null) { - String errorMessage = "no policies specified on topology template of service template"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "no policies specified on topology template of service template"; } if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) { - String errorMessage = "list of policies specified on topology template of service template is empty"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + return "list of policies specified on topology template of service template is empty"; } + + return null; } } |