From 2e1da7176952e013276ef29638a313f3a1b49c00 Mon Sep 17 00:00:00 2001 From: ramverma Date: Tue, 12 Jun 2018 14:27:19 +0100 Subject: Adding plugin-persistence module to apex-pdp Change-Id: I68e16b1aad6e7b943439c207816479d151f672fb Issue-ID: POLICY-862 Signed-off-by: ramverma --- .../plugins-persistence-jpa-eclipselink/pom.xml | 32 +++ .../jpa/eclipselink/EclipselinkApexDao.java | 32 +++ .../persistence/jpa/eclipselink/package-info.java | 28 +++ .../plugins-persistence-jpa-hibernate/pom.xml | 45 ++++ .../jpa/hibernate/HibernateApexDao.java | 276 +++++++++++++++++++++ .../persistence/jpa/hibernate/package-info.java | 28 +++ .../plugins-persistence-jpa/pom.xml | 48 ++++ plugins/plugins-persistence/pom.xml | 47 ++++ plugins/pom.xml | 1 + 9 files changed, 537 insertions(+) create mode 100644 plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/pom.xml create mode 100644 plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/eclipselink/EclipselinkApexDao.java create mode 100644 plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/eclipselink/package-info.java create mode 100644 plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/pom.xml create mode 100644 plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/hibernate/HibernateApexDao.java create mode 100644 plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/hibernate/package-info.java create mode 100644 plugins/plugins-persistence/plugins-persistence-jpa/pom.xml create mode 100644 plugins/plugins-persistence/pom.xml (limited to 'plugins') diff --git a/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/pom.xml b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/pom.xml new file mode 100644 index 000000000..c40f0abaa --- /dev/null +++ b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + org.onap.policy.apex-pdp.plugins.plugins-persistence.plugins-persistence-jpa + plugins-persistence-jpa + 2.0.0-SNAPSHOT + + + plugins-persistence-jpa-eclipselink + ${project.artifactId} + [${project.parent.artifactId}] Plugin for persistence using EclipseLink + \ No newline at end of file diff --git a/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/eclipselink/EclipselinkApexDao.java b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/eclipselink/EclipselinkApexDao.java new file mode 100644 index 000000000..e0bce2dd6 --- /dev/null +++ b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/eclipselink/EclipselinkApexDao.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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.apex.plugins.persistence.jpa.eclipselink; + +import org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao; + +/** + * The Class EclipselinkApexDao is the EclipseLink JPA implementation. It just uses the standard Apex JPA + * implementation. + * + * @author Sergey Sachkov (sergey.sachkov@ericsson.com) + */ +public class EclipselinkApexDao extends DefaultApexDao { +} diff --git a/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/eclipselink/package-info.java b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/eclipselink/package-info.java new file mode 100644 index 000000000..f58e5fc76 --- /dev/null +++ b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-eclipselink/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/eclipselink/package-info.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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========================================================= + */ + +/** + * Provides the EclipseLink implementation on the APEX DAO for + * persistence over JDBC. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.plugins.persistence.jpa.eclipselink; diff --git a/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/pom.xml b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/pom.xml new file mode 100644 index 000000000..13ac49b8a --- /dev/null +++ b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + org.onap.policy.apex-pdp.plugins.plugins-persistence.plugins-persistence-jpa + plugins-persistence-jpa + 2.0.0-SNAPSHOT + + + plugins-persistence-jpa-hibernate + ${project.artifactId} + [${project.parent.artifactId}] Plugin for persistence using Hibernate + + + + org.hibernate + hibernate-core + 5.2.10.Final + + + org.hibernate + hibernate-c3p0 + 5.2.10.Final + + + \ No newline at end of file diff --git a/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/hibernate/HibernateApexDao.java b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/hibernate/HibernateApexDao.java new file mode 100644 index 000000000..2d60e3751 --- /dev/null +++ b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/hibernate/HibernateApexDao.java @@ -0,0 +1,276 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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.apex.plugins.persistence.jpa.hibernate; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import javax.persistence.EntityManager; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class HibernateApexDao is the Hibernate JPA implementation of the Apex DAO. + * + * @author Sergey Sachkov (sergey.sachkov@ericsson.com) + */ +public class HibernateApexDao extends DefaultApexDao { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(HibernateApexDao.class); + + private static final String FROM = "FROM "; + private static final String DELETE_FROM = "DELETE FROM "; + private static final String WHERE_KEY_NAME = " WHERE key.name='"; + private static final String AND_KEY_VERSION = "' AND key.version='"; + private static final String WHERE_KEY_PARENT_KEY_NAME = " WHERE key.parentKeyName='"; + private static final String AND_KEY_PARENT_KEY_VERSION = "' AND key.parentKeyVersion='"; + private static final String AND_KEY_LOCAL_NAME = "' AND key.localName='"; + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao#delete(java.lang.Class, + * org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey) + */ + @Override + public void delete(final Class aClass, final AxArtifactKey key) { + if (key == null) { + return; + } + final EntityManager mg = getEntityManager(); + try { + mg.getTransaction().begin(); + mg.createQuery(DELETE_FROM + aClass.getSimpleName() + WHERE_KEY_NAME + key.getName() + AND_KEY_VERSION + + key.getVersion() + "'").executeUpdate(); + mg.getTransaction().commit(); + } finally { + mg.close(); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao#delete(java.lang.Class, + * org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey) + */ + @Override + public void delete(final Class aClass, final AxReferenceKey key) { + if (key == null) { + return; + } + final EntityManager mg = getEntityManager(); + try { + mg.getTransaction().begin(); + mg.createQuery(DELETE_FROM + aClass.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + key.getParentKeyName() + + AND_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() + AND_KEY_LOCAL_NAME + key.getLocalName() + + "'").executeUpdate(); + mg.getTransaction().commit(); + } finally { + mg.close(); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao#deleteByArtifactKey(java.lang.Class, + * java.util.Collection) + */ + @Override + public int deleteByArtifactKey(final Class aClass, final Collection keys) { + if (keys == null || keys.isEmpty()) { + return 0; + } + int deletedCount = 0; + final EntityManager mg = getEntityManager(); + try { + mg.getTransaction().begin(); + for (final AxArtifactKey key : keys) { + deletedCount += mg.createQuery(DELETE_FROM + aClass.getSimpleName() + WHERE_KEY_NAME + key.getName() + + AND_KEY_VERSION + key.getVersion() + "'").executeUpdate(); + } + mg.getTransaction().commit(); + } finally { + mg.close(); + } + return deletedCount; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.model.dao.ApexDao#deleteByContextUsageKey(java.lang.Class, java.util.Collection) + */ + @Override + public int deleteByReferenceKey(final Class aClass, + final Collection keys) { + if (keys == null || keys.isEmpty()) { + return 0; + } + int deletedCount = 0; + final EntityManager mg = getEntityManager(); + try { + mg.getTransaction().begin(); + for (final AxReferenceKey key : keys) { + deletedCount += mg.createQuery(DELETE_FROM + aClass.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + + key.getParentKeyName() + AND_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() + + AND_KEY_LOCAL_NAME + key.getLocalName() + "'").executeUpdate(); + } + mg.getTransaction().commit(); + } finally { + mg.close(); + } + return deletedCount; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.model.dao.ApexDao#deleteAll(java.lang.Class) + */ + @Override + public void deleteAll(final Class aClass) { + final EntityManager mg = getEntityManager(); + try { + mg.getTransaction().begin(); + mg.createQuery(DELETE_FROM + aClass.getSimpleName()).executeUpdate(); + mg.getTransaction().commit(); + } finally { + mg.close(); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.model.dao.ApexDao#getAll(java.lang.Class) + */ + @Override + public List getAll(final Class aClass) { + if (aClass == null) { + return Collections.emptyList(); + } + final EntityManager mg = getEntityManager(); + try { + final List result = mg.createQuery(FROM + aClass.getSimpleName(), aClass).getResultList(); + final List cloneResult = new ArrayList<>(); + for (final T t : result) { + try { + final T clonedT = aClass.newInstance(); + t.copyTo(clonedT); + cloneResult.add(clonedT); + } catch (final Exception e) { + LOGGER.warn("Could not clone object of class \"" + aClass.getCanonicalName() + "\"", e); + return cloneResult; + } + } + return cloneResult; + } finally { + mg.close(); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.model.dao.ApexDao#getAll(java.lang.Class, + * org.onap.policy.apex.core.model.concepts.AxArtifactKey) + */ + @Override + public List getAll(final Class aClass, final AxArtifactKey parentKey) { + if (aClass == null) { + return Collections.emptyList(); + } + final EntityManager mg = getEntityManager(); + try { + return mg.createQuery(FROM + aClass.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + parentKey.getName() + + AND_KEY_PARENT_KEY_VERSION + parentKey.getVersion() + "'", aClass).getResultList(); + } finally { + mg.close(); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.model.dao.ApexDao#getArtifact(java.lang.Class, + * org.onap.policy.apex.core.model.concepts.AxArtifactKey) + */ + @Override + public T getArtifact(final Class aClass, final AxArtifactKey key) { + if (aClass == null || key == null) { + return null; + } + final EntityManager mg = getEntityManager(); + List ret; + try { + ret = mg.createQuery(FROM + aClass.getSimpleName() + WHERE_KEY_NAME + key.getName() + AND_KEY_VERSION + + key.getVersion() + "'", aClass).getResultList(); + } finally { + mg.close(); + } + if (ret == null || ret.isEmpty()) { + return null; + } + if (ret.size() > 1) { + throw new IllegalArgumentException("More than one result was returned for search for " + aClass + + " with key " + key.getID() + ": " + ret); + } + return ret.get(0); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.model.dao.ApexDao#getArtifact(java.lang.Class, + * org.onap.policy.apex.core.model.concepts.AxReferenceKey) + */ + @Override + public T getArtifact(final Class aClass, final AxReferenceKey key) { + if (aClass == null || key == null) { + return null; + } + final EntityManager mg = getEntityManager(); + List ret; + try { + ret = mg.createQuery(FROM + aClass.getSimpleName() + WHERE_KEY_PARENT_KEY_NAME + key.getParentKeyName() + + AND_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() + AND_KEY_LOCAL_NAME + key.getLocalName() + + "'", aClass).getResultList(); + } finally { + mg.close(); + } + if (ret == null || ret.isEmpty()) { + return null; + } + if (ret.size() > 1) { + throw new IllegalArgumentException("More than one result was returned for search for " + aClass + + " with key " + key.getID() + ": " + ret); + } + return ret.get(0); + } +} diff --git a/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/hibernate/package-info.java b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/hibernate/package-info.java new file mode 100644 index 000000000..036d45d5d --- /dev/null +++ b/plugins/plugins-persistence/plugins-persistence-jpa/plugins-persistence-jpa-hibernate/src/main/java/org/onap/policy/apex/plugins/persistence/jpa/hibernate/package-info.java @@ -0,0 +1,28 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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========================================================= + */ + +/** + * Provides the Hibernate implementation on the APEX DAO for persistence over + * JDBC. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.plugins.persistence.jpa.hibernate; diff --git a/plugins/plugins-persistence/plugins-persistence-jpa/pom.xml b/plugins/plugins-persistence/plugins-persistence-jpa/pom.xml new file mode 100644 index 000000000..9a262fc55 --- /dev/null +++ b/plugins/plugins-persistence/plugins-persistence-jpa/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + org.onap.policy.apex-pdp.plugins.plugins-persistence + plugins-persistence + 2.0.0-SNAPSHOT + + + org.onap.policy.apex-pdp.plugins.plugins-persistence.plugins-persistence-jpa + plugins-persistence-jpa + pom + + ${project.artifactId} + Plugins for 3pps that handle persistence over JPA + + + + org.onap.policy.apex-pdp.model + basic-model + ${project.version} + + + + + plugins-persistence-jpa-eclipselink + plugins-persistence-jpa-hibernate + + \ No newline at end of file diff --git a/plugins/plugins-persistence/pom.xml b/plugins/plugins-persistence/pom.xml new file mode 100644 index 000000000..aeae86a8a --- /dev/null +++ b/plugins/plugins-persistence/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + org.onap.policy.apex-pdp.plugins + plugins + 2.0.0-SNAPSHOT + + + org.onap.policy.apex-pdp.plugins.plugins-persistence + plugins-persistence + pom + + ${project.artifactId} + Plugins for 3pps that implement persistence + + + + org.postgresql + postgresql + 42.0.0 + test + + + + plugins-persistence-jpa + + \ No newline at end of file diff --git a/plugins/pom.xml b/plugins/pom.xml index 73bcdfa09..0cf5e8549 100644 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -44,5 +44,6 @@ plugins-context plugins-event plugins-executor + plugins-persistence \ No newline at end of file -- cgit 1.2.3-korg