diff options
author | liamfallon <liam.fallon@est.tech> | 2020-01-07 10:24:12 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2020-01-07 14:45:06 +0000 |
commit | f69b6681486e4d1c5859f649a3293488c1859712 (patch) | |
tree | 92664e117820ab4a4cdfdc0b5694388e39752724 /models-tosca/src/main/java | |
parent | 4fab7a327737ebdfaf7b55c39c49f0239ff627c8 (diff) |
Support persistence of data types
Data types must be persisted to the database in order for them to be
retrieved in policy type queries and later for policy validation.
Issue-ID: POLICY-2315
Change-Id: Iacb88501a597aeee8f6d8bcc3d9604c13dc09090
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main/java')
3 files changed, 149 insertions, 12 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java index 8c6e492f7..7999f620b 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,6 +28,7 @@ import java.util.Map; import java.util.TreeMap; import lombok.NonNull; + import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.dao.PfDao; @@ -64,8 +65,9 @@ public class AuthorativeToscaProvider { LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version); - ToscaServiceTemplate serviceTemplate = - new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative(); + JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version); + + ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative(); LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); return serviceTemplate; 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 30ab89491..cce6fd9ee 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,8 @@ import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; @@ -55,6 +57,116 @@ public class SimpleToscaProvider { private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class); /** + * Get data types. + * + * @param dao the DAO to use to access the database + * @param name the name of the data type to get, set to null to get all policy types + * @param version the version of the data type to get, set to null to get all versions + * @return the data types found + * @throws PfModelException on errors getting data types + */ + public JpaToscaServiceTemplate getDataTypes(@NonNull final PfDao dao, final String name, final String version) + throws PfModelException { + LOGGER.debug("->getDataTypes: name={}, version={}", name, version); + + // Create the structure of the TOSCA service template to contain the data type + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + + // Add the data type to the TOSCA service template + List<JpaToscaDataType> jpaDataTypeList = dao.getFiltered(JpaToscaDataType.class, name, version); + serviceTemplate.getDataTypes().getConceptMap().putAll(asConceptMap(jpaDataTypeList)); + + LOGGER.debug("<-getDataTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); + return serviceTemplate; + } + + /** + * Create data types. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definition of the data types to be created + * @return the TOSCA service template containing the created data types + * @throws PfModelException on errors creating data types + */ + public JpaToscaServiceTemplate createDataTypes(@NonNull final PfDao dao, + @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException { + LOGGER.debug("->createDataTypes: serviceTempalate={}", serviceTemplate); + + ToscaUtils.assertDataTypesExist(serviceTemplate); + + for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getAll(null)) { + dao.create(dataType); + } + + // Return the created Data types + JpaToscaDataTypes returnDataTypes = new JpaToscaDataTypes(); + + for (PfConceptKey dataTypeKey : serviceTemplate.getDataTypes().getConceptMap().keySet()) { + returnDataTypes.getConceptMap().put(dataTypeKey, dao.get(JpaToscaDataType.class, dataTypeKey)); + } + + JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate(); + returnServiceTemplate.setDataTypes(returnDataTypes); + + LOGGER.debug("<-createDataTypes: returnServiceTempalate={}", returnServiceTemplate); + return returnServiceTemplate; + } + + /** + * Update Data types. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definition of the data types to be modified + * @return the TOSCA service template containing the modified data types + * @throws PfModelException on errors updating Data types + */ + public JpaToscaServiceTemplate updateDataTypes(@NonNull final PfDao dao, + @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException { + LOGGER.debug("->updateDataTypes: serviceTempalate={}", serviceTemplate); + + ToscaUtils.assertDataTypesExist(serviceTemplate); + + for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getAll(null)) { + dao.update(dataType); + } + + // Return the created data types + JpaToscaDataTypes returnDataTypes = new JpaToscaDataTypes(); + + for (PfConceptKey dataTypeKey : serviceTemplate.getDataTypes().getConceptMap().keySet()) { + returnDataTypes.getConceptMap().put(dataTypeKey, dao.get(JpaToscaDataType.class, dataTypeKey)); + } + + JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate(); + returnServiceTemplate.setDataTypes(returnDataTypes); + + LOGGER.debug("<-updateDataTypes: returnServiceTempalate={}", returnServiceTemplate); + return returnServiceTemplate; + } + + /** + * Delete Data types. + * + * @param dao the DAO to use to access the database + * @param dataTypeKey the data type key for the Data types to be deleted, if the version of the key is null, all + * versions of the data type are deleted. + * @return the TOSCA service template containing the data types that were deleted + * @throws PfModelException on errors deleting data types + */ + public JpaToscaServiceTemplate deleteDataType(@NonNull final PfDao dao, @NonNull final PfConceptKey dataTypeKey) + throws PfModelException { + LOGGER.debug("->deleteDataType: key={}", dataTypeKey); + + JpaToscaServiceTemplate serviceTemplate = getDataTypes(dao, dataTypeKey.getName(), dataTypeKey.getVersion()); + + dao.delete(JpaToscaDataType.class, dataTypeKey); + + LOGGER.debug("<-deleteDataType: key={}, serviceTempalate={}", dataTypeKey, serviceTemplate); + return serviceTemplate; + } + + /** * Get policy types. * * @param dao the DAO to use to access the database @@ -75,7 +187,7 @@ public class SimpleToscaProvider { List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version); serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); - LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate=", name, version, serviceTemplate); + LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); return serviceTemplate; } @@ -112,7 +224,7 @@ public class SimpleToscaProvider { } /** - * Create policy types. + * Update policy types. * * @param dao the DAO to use to access the database * @param serviceTemplate the service template containing the definition of the policy types to be modified @@ -161,7 +273,7 @@ public class SimpleToscaProvider { dao.delete(JpaToscaPolicyType.class, policyTypeKey); - LOGGER.debug("<-deletePolicyType: key={}, serviceTempalate=", policyTypeKey, serviceTemplate); + LOGGER.debug("<-deletePolicyType: key={}, serviceTempalate={}", policyTypeKey, serviceTemplate); return serviceTemplate; } @@ -187,7 +299,7 @@ public class SimpleToscaProvider { List<JpaToscaPolicy> jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version); serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList)); - LOGGER.debug("<-getPolicies: name={}, version={}, serviceTemplate=", name, version, serviceTemplate); + LOGGER.debug("<-getPolicies: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); return serviceTemplate; } @@ -273,7 +385,7 @@ public class SimpleToscaProvider { dao.delete(JpaToscaPolicy.class, policyKey); - LOGGER.debug("<-deletePolicy: key={}, serviceTempalate=", policyKey, serviceTemplate); + LOGGER.debug("<-deletePolicy: key={}, serviceTempalate={}", policyKey, serviceTemplate); return serviceTemplate; } 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 1b509e2a8..23a428b65 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,28 @@ public final class ToscaUtils { } /** - * Check if policy types have been specified is initialized. + * Check if data types have been specified correctly. + * + * @param serviceTemplate the service template containing data types to be checked + */ + public static void assertDataTypesExist(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); + } + + 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); + } + } + + /** + * Check if policy types have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked */ public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getPolicyTypes() == null) { @@ -60,7 +81,9 @@ public final class ToscaUtils { } /** - * Check if policy types have been specified is initialized. + * Check if policies have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked */ public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getTopologyTemplate() == null) { |