aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca/src
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2020-01-27 19:33:25 -0500
committerliamfallon <liam.fallon@est.tech>2020-01-28 09:58:17 -0500
commit252dc4ea8da89739ed18d6509ea72d2321676034 (patch)
treedc10e683f267261bacf02dd4179bb13d529c2c62 /models-tosca/src
parentf77d0a91298e4e4ec14991366b5e439824919d5d (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')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java32
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProvider.java106
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java1
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProviderTest.java173
-rw-r--r--models-tosca/src/test/resources/META-INF/persistence.xml5
5 files changed, 309 insertions, 8 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;
+ }
+
+}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java
index 63ebcbd24..03c7f9b1b 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java
@@ -61,6 +61,7 @@ public class SimpleToscaProviderTest {
private static final String TEMPLATE_IS_NULL = "^serviceTemplate is marked .*on.*ull but is null$";
private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json";
private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
+
private PfDao pfDao;
private StandardCoder standardCoder;
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProviderTest.java
new file mode 100644
index 000000000..791928a0e
--- /dev/null
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaServiceTemplateProviderTest.java
@@ -0,0 +1,173 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Nordix Foundation.
+ * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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 static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.util.Properties;
+
+import org.eclipse.persistence.config.PersistenceUnitProperties;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.dao.DaoParameters;
+import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.dao.PfDaoFactory;
+import org.onap.policy.models.dao.impl.DefaultPfDao;
+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.JpaToscaPolicyType;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+
+/**
+ * Test the {@link SimpleToscaProvider} class.
+ *
+ * @author Liam Fallon (liam.fallon@est.tech)
+ */
+public class SimpleToscaServiceTemplateProviderTest {
+ private static final String TEMPLATE_IS_NULL = "^serviceTemplate is marked .*on.*ull but is null$";
+ private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
+
+ private PfDao pfDao;
+
+ /**
+ * Set up the DAO towards the database.
+ *
+ * @throws Exception on database errors
+ */
+ @Before
+ public void setupDao() throws Exception {
+ final DaoParameters daoParameters = new DaoParameters();
+ daoParameters.setPluginClass(DefaultPfDao.class.getName());
+
+ daoParameters.setPersistenceUnit("ToscaConceptTest");
+
+ Properties jdbcProperties = new Properties();
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
+
+ // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
+
+ daoParameters.setJdbcProperties(jdbcProperties);
+
+ pfDao = new PfDaoFactory().createPfDao(daoParameters);
+ pfDao.init(daoParameters);
+ }
+
+ @After
+ public void teardown() {
+ pfDao.close();
+ }
+
+ @Test
+ public void testCreateUpdateGetDeleteDataType() throws PfModelException {
+ JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
+
+ JpaToscaServiceTemplate overwrittenServiceTemplate =
+ new SimpleToscaServiceTemplateProvider().write(pfDao, serviceTemplate);
+
+ assertNull(overwrittenServiceTemplate);
+
+ JpaToscaServiceTemplate readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao);
+ assertEquals(serviceTemplate, readServiceTemplate);
+
+ assertNull(readServiceTemplate.getDataTypes());
+
+ PfConceptKey dataType0Key = new PfConceptKey("DataType0", "0.0.1");
+ JpaToscaDataType dataType0 = new JpaToscaDataType();
+ dataType0.setKey(dataType0Key);
+ serviceTemplate.setDataTypes(new JpaToscaDataTypes());
+ serviceTemplate.getDataTypes().getConceptMap().put(dataType0Key, dataType0);
+
+ overwrittenServiceTemplate = new SimpleToscaServiceTemplateProvider().write(pfDao, serviceTemplate);
+ assertEquals(readServiceTemplate, overwrittenServiceTemplate);
+
+ readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao);
+ assertEquals(serviceTemplate, readServiceTemplate);
+
+ assertEquals(1, readServiceTemplate.getDataTypes().getConceptMap().size());
+ assertEquals(dataType0, readServiceTemplate.getDataTypes().get(dataType0Key));
+ assertNull(readServiceTemplate.getDataTypes().get(dataType0Key).getDescription());
+
+ dataType0.setDescription("Updated Description");
+
+ overwrittenServiceTemplate = new SimpleToscaServiceTemplateProvider().write(pfDao, serviceTemplate);
+ assertEquals(readServiceTemplate, overwrittenServiceTemplate);
+
+ readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao);
+ assertEquals(serviceTemplate, readServiceTemplate);
+
+ assertEquals(dataType0, readServiceTemplate.getDataTypes().get(dataType0Key));
+ assertEquals("Updated Description", readServiceTemplate.getDataTypes().get(dataType0Key).getDescription());
+
+ PfConceptKey policyType0Key = new PfConceptKey("PolicyType0", "0.0.1");
+
+ JpaToscaPolicyType policyType0 = new JpaToscaPolicyType();
+
+ policyType0.setKey(policyType0Key);
+ serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
+
+ serviceTemplate.getPolicyTypes().getConceptMap().put(policyType0Key, policyType0);
+
+ overwrittenServiceTemplate = new SimpleToscaServiceTemplateProvider().write(pfDao, serviceTemplate);
+ assertEquals(readServiceTemplate, overwrittenServiceTemplate);
+
+ readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao);
+ assertEquals(serviceTemplate, readServiceTemplate);
+
+ JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaServiceTemplateProvider().delete(pfDao);
+ assertEquals(serviceTemplate, deletedServiceTemplate);
+
+ readServiceTemplate = new SimpleToscaServiceTemplateProvider().read(pfDao);
+ assertNull(readServiceTemplate);
+ }
+
+ @Test
+ public void testNonNulls() {
+ assertThatThrownBy(() -> {
+ new SimpleToscaServiceTemplateProvider().write(null, null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaServiceTemplateProvider().write(pfDao, null);
+ }).hasMessageMatching(TEMPLATE_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaServiceTemplateProvider().write(null, new JpaToscaServiceTemplate());
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaServiceTemplateProvider().read(null);
+ }).hasMessageMatching(DAO_IS_NULL);
+
+ assertThatThrownBy(() -> {
+ new SimpleToscaServiceTemplateProvider().delete(null);
+ }).hasMessageMatching(DAO_IS_NULL);
+ }
+}
diff --git a/models-tosca/src/test/resources/META-INF/persistence.xml b/models-tosca/src/test/resources/META-INF/persistence.xml
index 1cca1ee62..62e0b6046 100644
--- a/models-tosca/src/test/resources/META-INF/persistence.xml
+++ b/models-tosca/src/test/resources/META-INF/persistence.xml
@@ -26,9 +26,12 @@
<class>org.onap.policy.models.dao.converters.CDataConditioner</class>
<class>org.onap.policy.models.dao.converters.Uuid2String</class>
<class>org.onap.policy.models.base.PfConceptKey</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class>
<class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class>
- <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes</class>
+ <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate</class>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />