diff options
author | liamfallon <liam.fallon@est.tech> | 2020-01-27 19:33:25 -0500 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2020-01-28 09:58:17 -0500 |
commit | 252dc4ea8da89739ed18d6509ea72d2321676034 (patch) | |
tree | dc10e683f267261bacf02dd4179bb13d529c2c62 /models-tosca/src/main/java/org | |
parent | f77d0a91298e4e4ec14991366b5e439824919d5d (diff) |
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 <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/main/java/org')
2 files changed, 131 insertions, 7 deletions
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 JpaToscaEntityType<ToscaServiceTemp public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple"; public static final String DEFAULT_VERSION = "1.0.0"; + // @formatter:off @Column @SerializedName("tosca_definitions_version") private String toscaDefinitionsVersion; @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumns( + { + @JoinColumn(name = "dataTypesName", referencedColumnName = "name"), + @JoinColumn(name = "dataTypesVersion", referencedColumnName = "version") + } + ) @SerializedName("data_types") private JpaToscaDataTypes dataTypes; @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumns( + { + @JoinColumn(name = "policyTypesName", referencedColumnName = "name"), + @JoinColumn(name = "policyTypesVersion", referencedColumnName = "version") + } + ) @SerializedName("policy_types") private JpaToscaPolicyTypes policyTypes; @Column @SerializedName("topology_template") private JpaToscaTopologyTemplate topologyTemplate; - + // @formatter:on /** * The Default Constructor creates a {@link JpaToscaServiceTemplate} object with a null key. @@ -125,8 +143,9 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion; this.dataTypes = (copyConcept.dataTypes != null ? new JpaToscaDataTypes(copyConcept.dataTypes) : null); this.policyTypes = (copyConcept.policyTypes != null ? new JpaToscaPolicyTypes(copyConcept.policyTypes) : null); - this.topologyTemplate = (copyConcept.topologyTemplate != null - ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate) : null); + this.topologyTemplate = + (copyConcept.topologyTemplate != null ? new JpaToscaTopologyTemplate(copyConcept.topologyTemplate) + : null); } /** @@ -174,11 +193,11 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) { super.fromAuthorative(toscaServiceTemplate); - if (getKey().getName() == PfKey.NULL_KEY_NAME) { + if (PfKey.NULL_KEY_NAME.equals(getKey().getName())) { getKey().setName(DEFAULT_NAME); } - if (getKey().getVersion() == PfKey.NULL_KEY_VERSION) { + if (PfKey.NULL_KEY_VERSION.equals(getKey().getVersion())) { getKey().setVersion(DEFAULT_VERSION); } @@ -194,7 +213,6 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp policyTypes.fromAuthorative(Collections.singletonList(toscaServiceTemplate.getPolicyTypes())); } - if (toscaServiceTemplate.getToscaTopologyTemplate() != null) { topologyTemplate = new JpaToscaTopologyTemplate(); topologyTemplate.fromAuthorative(toscaServiceTemplate.getToscaTopologyTemplate()); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProvider.java new file mode 100644 index 000000000..a971b9af3 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProvider.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.simple.provider; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class provides CRUD to and from the database en bloc using Service Template reads and writes. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class SimpleToscaServiceTemplateProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaServiceTemplateProvider.class); + + // There is only one service template in the database becasue TOSCA does not specify names and versions on service + // templates. + private static final PfConceptKey DEFAULT_SERVICE_TEMPLATE_KEY = + new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION); + + /** + * Get a service template from the database. + * + * @param dao the DAO to use to access the database + * @return the Service Template read from the database + * @throws PfModelException on errors getting the service template + */ + public JpaToscaServiceTemplate read(@NonNull final PfDao dao) throws PfModelException { + LOGGER.debug("->read"); + + // 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; + } + +} |