aboutsummaryrefslogtreecommitdiffstats
path: root/models-dao/src/main
diff options
context:
space:
mode:
authorning.xi <ning.xi@est.tech>2019-12-11 15:55:59 +0800
committerning.xi <ning.xi@est.tech>2019-12-20 10:50:23 +0800
commit1c9d2d26b1f34567dc6d3e9c51a4fb78c9fc13d4 (patch)
tree862013dc0095b33bfd28daeac9ac54eae8899a5f /models-dao/src/main
parente279f0ec53147f53d7a48041c7b433b7738ae37d (diff)
pdp statistics database provider implementation
Issue-ID: POLICY-1629 Signed-off-by: ning.xi <ning.xi@est.tech> Change-Id: Ife65d50c862ed90c3c9a3cea91a2b9a8d874fa14
Diffstat (limited to 'models-dao/src/main')
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java46
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java177
2 files changed, 192 insertions, 31 deletions
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java
index e635085ff..692aa08bf 100644
--- a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java
@@ -21,12 +21,14 @@
package org.onap.policy.models.dao;
import java.util.Collection;
+import java.util.Date;
import java.util.List;
-
+import java.util.Map;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfReferenceKey;
+import org.onap.policy.models.base.PfTimestampKey;
/**
* The Interface PfDao describes the DAO interface for reading and writing Policy Framework {@link PfConcept} concepts
@@ -82,6 +84,15 @@ public interface PfDao {
<T extends PfConcept> void delete(Class<T> someClass, PfReferenceKey key);
/**
+ * Delete an Policy Framework concept on the database.
+ *
+ * @param <T> the type of the object to delete, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to delete, a subclass of {@link PfConcept}
+ * @param timeStampKey the PfTimestampKey of the object to delete
+ */
+ <T extends PfConcept> void delete(Class<T> someClass, PfTimestampKey timeStampKey);
+
+ /**
* Create a collection of objects in the database.
*
* @param <T> the type of the object to create, a subclass of {@link PfConcept}
@@ -142,8 +153,27 @@ public interface PfDao {
* Get an object from the database, referred to by concept key.
*
* @param <T> the type of the object to get, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}, if name is null, all concepts
+ * of type T are returned, if name is not null and version is null, all versions of that concept matching the
+ * name are returned.
+ * @param name the name of the object to get, null returns all objects
+ * @param version the version the object to get, null returns all objects for a specified name
+ * @param startTime the start timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp
+ * <= endTime. null for ignore start time.
+ * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <=
+ * endTime. null for ignore end time
+ * @filterMap Map store extra key/value used to filter from database, can be null.
+ * @return the objects that was retrieved from the database
+ */
+ <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version, Date startTime,
+ Date endTime, Map<String, Object> filterMap);
+
+ /**
+ * Get an object from the database, referred to by concept key.
+ *
+ * @param <T> the type of the object to get, a subclass of {@link PfConcept}
* @param someClass the class of the object to get, a subclass of {@link PfConcept}
- * @param key the key of the object to get
+ * @param key the PfConceptKey of the object to get
* @return the object that was retrieved from the database
*/
<T extends PfConcept> T get(Class<T> someClass, PfConceptKey key);
@@ -153,12 +183,22 @@ public interface PfDao {
*
* @param <T> the type of the object to get, a subclass of {@link PfConcept}
* @param someClass the class of the object to get, a subclass of {@link PfConcept}
- * @param key the key of the object to get
+ * @param key the PfReferenceKey of the object to get
* @return the object that was retrieved from the database or null if the object was not retrieved
*/
<T extends PfConcept> T get(Class<T> someClass, PfReferenceKey key);
/**
+ * Get an object from the database, referred to by reference key.
+ *
+ * @param <T> the type of the object to get, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}
+ * @param timestampKey the PfTimestampKey of the object to get
+ * @return the object that was retrieved from the database or null if the object was not retrieved
+ */
+ <T extends PfConcept> T get(Class<T> someClass, PfTimestampKey timestampKey);
+
+ /**
* Get all the objects in the database of a given type.
*
* @param <T> the type of the objects to get, a subclass of {@link PfConcept}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java
index 62b9c41a3..e98266706 100644
--- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java
@@ -23,18 +23,20 @@ package org.onap.policy.models.dao.impl;
import java.util.Collection;
import java.util.Collections;
+import java.util.Date;
import java.util.List;
-
+import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
+import javax.persistence.TypedQuery;
import javax.ws.rs.core.Response;
-
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.base.PfReferenceKey;
+import org.onap.policy.models.base.PfTimestampKey;
import org.onap.policy.models.base.PfUtils;
import org.onap.policy.models.dao.DaoParameters;
import org.onap.policy.models.dao.PfDao;
@@ -51,6 +53,7 @@ public class DefaultPfDao implements PfDao {
// @formatter:off
private static final String NAME = "name";
private static final String VERSION = "version";
+ private static final String TIMESTAMP = "timeStamp";
private static final String PARENT_NAME = "parentname";
private static final String PARENT_VERSION = "parentversion";
private static final String LOCAL_NAME = "localname";
@@ -64,11 +67,15 @@ public class DefaultPfDao implements PfDao {
private static final String WHERE = " WHERE ";
private static final String AND = " AND ";
- private static final String NAME_FILTER = "c.key.name = :name";
- private static final String VERSION_FILTER = "c.key.version = :version";
- private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname";
- private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion";
- private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname";
+ private static final String NAME_FILTER = "c.key.name = :name";
+ private static final String VERSION_FILTER = "c.key.version = :version";
+ private static final String TIMESTAMP_START_FILTER = "c.key.timeStamp >= :startTime";
+ private static final String TIMESTAMP_END_FILTER = "c.key.timeStamp <= :endTime";
+ private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname";
+ private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion";
+ private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname";
+
+ private static final String CLONE_ERR_MSG = "Could not clone object of class \"{}\"";
private static final String DELETE_BY_CONCEPT_KEY =
DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER;
@@ -206,6 +213,27 @@ public class DefaultPfDao implements PfDao {
}
@Override
+ public <T extends PfConcept> void delete(final Class<T> someClass, final PfTimestampKey key) {
+ if (key == null) {
+ return;
+ }
+ final EntityManager mg = getEntityManager();
+ try {
+ // @formatter:off
+ mg.getTransaction().begin();
+ mg.createQuery(setQueryTable(DELETE_BY_CONCEPT_KEY, someClass), someClass)
+ .setParameter(NAME, key.getName())
+ .setParameter(VERSION, key.getVersion())
+ .setParameter(TIMESTAMP, key.getTimeStamp())
+ .executeUpdate();
+ mg.getTransaction().commit();
+ // @formatter:on
+ } finally {
+ mg.close();
+ }
+ }
+
+ @Override
public <T extends PfConcept> void createCollection(final Collection<T> objs) {
if (objs == null || objs.isEmpty()) {
return;
@@ -318,6 +346,52 @@ public class DefaultPfDao implements PfDao {
}
@Override
+ public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, final String name, final String version,
+ final Date startTime, final Date endTime, final Map<String, Object> filterMap) {
+ final EntityManager mg = getEntityManager();
+
+ String filterQueryString = SELECT_FROM_TABLE + WHERE;
+
+ try {
+ if (filterMap != null) {
+ StringBuilder bld = new StringBuilder(filterQueryString);
+ for (String key : filterMap.keySet()) {
+ bld.append("c." + key + "= :" + key + AND);
+ }
+ filterQueryString = bld.toString();
+ }
+ filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime);
+ TypedQuery<T> query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass);
+
+ if (filterMap != null) {
+ for (Map.Entry<String, Object> entry : filterMap.entrySet()) {
+ query.setParameter(entry.getKey(), entry.getValue());
+ }
+ }
+ if (name != null) {
+ query.setParameter("name", name);
+ }
+ if (startTime != null) {
+ if (endTime != null) {
+ query.setParameter("startTime", startTime);
+ query.setParameter("endTime", endTime);
+ } else {
+ query.setParameter("startTime", startTime);
+ }
+ } else {
+ if (endTime != null) {
+ query.setParameter("endTime", endTime);
+ }
+ }
+
+ LOGGER.error("filterQueryString is \"{}\"", filterQueryString);
+ return query.getResultList();
+ } finally {
+ mg.close();
+ }
+ }
+
+ @Override
public <T extends PfConcept> T get(final Class<T> someClass, final PfConceptKey key) {
if (someClass == null) {
return null;
@@ -325,17 +399,7 @@ public class DefaultPfDao implements PfDao {
final EntityManager mg = getEntityManager();
try {
final T t = mg.find(someClass, key);
- if (t != null) {
- // This clone is created to force the JPA DAO to recurse down through the object
- try {
- return PfUtils.makeCopy(t);
- } catch (final Exception e) {
- LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e);
- return null;
- }
- } else {
- return null;
- }
+ return checkAndReturn(someClass, t);
} finally {
mg.close();
}
@@ -349,16 +413,21 @@ public class DefaultPfDao implements PfDao {
final EntityManager mg = getEntityManager();
try {
final T t = mg.find(someClass, key);
- if (t != null) {
- try {
- return PfUtils.makeCopy(t);
- } catch (final Exception e) {
- LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e);
- return null;
- }
- } else {
- return null;
- }
+ return checkAndReturn(someClass, t);
+ } finally {
+ mg.close();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> T get(final Class<T> someClass, final PfTimestampKey key) {
+ if (someClass == null) {
+ return null;
+ }
+ final EntityManager mg = getEntityManager();
+ try {
+ final T t = mg.find(someClass, key);
+ return checkAndReturn(someClass, t);
} finally {
mg.close();
}
@@ -515,4 +584,56 @@ public class DefaultPfDao implements PfDao {
}
return ret.get(0);
}
+
+ /**
+ * generate filter string with the filter value in TimestampKey.
+ *
+ * @param inputFilterString current filterString generated from FilterMap
+ * @param name the pdp name the start timeStamp to filter from database, filter rule: startTime <= filteredRecord
+ * timeStamp <= endTime. null for ignore start time.
+ * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <=
+ * endTime. null for ignore end time
+ * @return the filter string to query database
+ */
+ private String addKeyFilterString(String inputFilterString, final String name, final Date startTime,
+ final Date endTime) {
+ String filterQueryString;
+ if (name != null) {
+ inputFilterString += NAME_FILTER + AND;
+ }
+ if (startTime != null) {
+ if (endTime != null) {
+ filterQueryString = inputFilterString + TIMESTAMP_START_FILTER + AND + TIMESTAMP_END_FILTER;
+ } else {
+ filterQueryString = inputFilterString + TIMESTAMP_START_FILTER;
+ }
+ } else {
+ if (endTime != null) {
+ filterQueryString = inputFilterString + TIMESTAMP_END_FILTER;
+ } else {
+ filterQueryString = inputFilterString.substring(0, inputFilterString.length() - AND.length());
+ }
+ }
+
+ return filterQueryString;
+ }
+
+ /**
+ * check the result get from database and return the object.
+ *
+ * @param <T> the type of the object to get, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}
+ * @param t the object that was retrieved from the database
+ * @return the checked object or null
+ */
+ private <T extends PfConcept> T checkAndReturn(final Class<T> someClass, final T objToCheck) {
+ if (objToCheck != null) {
+ try {
+ return PfUtils.makeCopy(objToCheck);
+ } catch (final Exception e) {
+ LOGGER.warn(CLONE_ERR_MSG, someClass.getName(), e);
+ }
+ }
+ return null;
+ }
}