From 252dc4ea8da89739ed18d6509ea72d2321676034 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 27 Jan 2020 19:33:25 -0500 Subject: Add service template persistence provider Persistence needs to be carried out at service template level in order to guarantee consistency of the service template in the database as much as possible. Therefire, service template lelve reading, writing, and delting is required. Issue-ID: POLICY-1402 Change-Id: I5be57c29381bf6ddc87afdbb64ddae66a5ea5e5f Signed-off-by: liamfallon --- .../simple/concepts/JpaToscaServiceTemplate.java | 32 +++++-- .../SimpleToscaServiceTemplateProvider.java | 106 +++++++++++++++++++++ 2 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProvider.java (limited to 'models-tosca/src/main/java/org') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java index e07894cd8..31c7df047 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.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"); @@ -34,11 +34,15 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; import javax.persistence.OneToOne; import javax.persistence.Table; + import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; + import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.common.utils.validation.ParameterValidationUtils; import org.onap.policy.models.base.PfAuthorative; @@ -58,6 +62,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; * * @author Liam Fallon (liam.fallon@est.tech) */ + @Entity @Table(name = "ToscaServiceTemplate") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @@ -71,22 +76,35 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityTyperead"); + + // Get the service template + JpaToscaServiceTemplate serviceTemplate = dao.get(JpaToscaServiceTemplate.class, DEFAULT_SERVICE_TEMPLATE_KEY); + + LOGGER.debug("<-read: serviceTemplate={}", serviceTemplate); + return serviceTemplate; + } + + /** + * Write a service template to the database. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template to be written + * @return the TOSCA service template overwritten by this method + * @throws PfModelException on errors writing the service template + */ + public JpaToscaServiceTemplate write(@NonNull final PfDao dao, + @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException { + LOGGER.debug("->write: serviceTempalate={}", serviceTemplate); + + JpaToscaServiceTemplate overwrittenServiceTemplate = + dao.get(JpaToscaServiceTemplate.class, serviceTemplate.getKey()); + + if (overwrittenServiceTemplate != null) { + dao.delete(overwrittenServiceTemplate); + } + + dao.create(serviceTemplate); + + LOGGER.debug("<-write: overwrittenServiceTemplate={}", overwrittenServiceTemplate); + return overwrittenServiceTemplate; + } + + /** + * Delete a service template from the database. + * + * @param dao the DAO to use to access the database + * @return the Service Template stored in the database + * @throws PfModelException on errors getting the service template + */ + public JpaToscaServiceTemplate delete(@NonNull final PfDao dao) throws PfModelException { + LOGGER.debug("->delete"); + + JpaToscaServiceTemplate serviceTemplateToBeDeleted = + dao.get(JpaToscaServiceTemplate.class, DEFAULT_SERVICE_TEMPLATE_KEY); + + dao.delete(serviceTemplateToBeDeleted); + + LOGGER.debug("<-delete: serviceTemplate={}", serviceTemplateToBeDeleted); + return serviceTemplateToBeDeleted; + } + +} -- cgit 1.2.3-korg