diff options
8 files changed, 76 insertions, 38 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 692aa08bf..062ec4662 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-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. @@ -162,11 +162,13 @@ public interface PfDao { * <= 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. + * @param filterMap Map store extra key/value used to filter from database, can be null. + * @param sortOrder sortOrder to query database + * @param getRecordNum Total query count from database * @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); + Date endTime, Map<String, Object> filterMap, String sortOrder, int getRecordNum); /** * Get an object from the database, referred to by concept key. 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 e98266706..b3d72a3fd 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 @@ -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"); @@ -66,9 +66,11 @@ public class DefaultPfDao implements PfDao { private static final String WHERE = " WHERE "; private static final String AND = " AND "; + private static final String ORDER = " ORDER BY "; 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_FILTER = "c.key.timeStamp = :timeStamp"; 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"; @@ -80,6 +82,9 @@ public class DefaultPfDao implements PfDao { private static final String DELETE_BY_CONCEPT_KEY = DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER; + private static final String DELETE_BY_TIMESTAMP_KEY = + DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER + AND + TIMESTAMP_FILTER; + private static final String DELETE_BY_REFERENCE_KEY = DELETE_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER + AND + LOCAL_NAME_FILTER; @@ -221,7 +226,7 @@ public class DefaultPfDao implements PfDao { try { // @formatter:off mg.getTransaction().begin(); - mg.createQuery(setQueryTable(DELETE_BY_CONCEPT_KEY, someClass), someClass) + mg.createQuery(setQueryTable(DELETE_BY_TIMESTAMP_KEY, someClass), someClass) .setParameter(NAME, key.getName()) .setParameter(VERSION, key.getVersion()) .setParameter(TIMESTAMP, key.getTimeStamp()) @@ -347,7 +352,8 @@ 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 Date startTime, final Date endTime, final Map<String, Object> filterMap, final String sortOrder, + final int getRecordNum) { final EntityManager mg = getEntityManager(); String filterQueryString = SELECT_FROM_TABLE + WHERE; @@ -361,6 +367,9 @@ public class DefaultPfDao implements PfDao { filterQueryString = bld.toString(); } filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime); + if (getRecordNum > 0) { + filterQueryString += ORDER + " c.key.timeStamp " + sortOrder; + } TypedQuery<T> query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass); if (filterMap != null) { @@ -383,6 +392,9 @@ public class DefaultPfDao implements PfDao { query.setParameter("endTime", endTime); } } + if (getRecordNum > 0) { + query.setMaxResults(getRecordNum); + } LOGGER.error("filterQueryString is \"{}\"", filterQueryString); return query.getResultList(); diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java index f149a3602..3b1718090 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-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. @@ -52,6 +52,7 @@ public class PdpStatisticsProvider { // Recurring string constants private static final String NOT_VALID = "\" is not valid \n"; + private static final String DESC_ORDER = "DESC"; /** * Get PDP statistics. @@ -84,12 +85,14 @@ public class PdpStatisticsProvider { * @param pdpSubGroup pdpSubGroupType name to filter statistics * @param startTimeStamp startTimeStamp to filter statistics * @param endTimeStamp endTimeStamp to filter statistics + * @param sortOrder sortOrder to query database + * @param getRecordNum Total query count from database * @return the PDP statistics found * @throws PfModelException on errors getting policies */ public List<PdpStatistics> getFilteredPdpStatistics(@NonNull final PfDao dao, final String name, @NonNull final String pdpGroupName, final String pdpSubGroup, final Date startTimeStamp, - final Date endTimeStamp) { + final Date endTimeStamp, final String sortOrder, final int getRecordNum) { Map<String, Object> filterMap = new HashMap<>(); filterMap.put("pdpGroupName", pdpGroupName); if (pdpSubGroup != null) { @@ -97,7 +100,7 @@ public class PdpStatisticsProvider { } return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name, PfKey.NULL_KEY_VERSION, startTimeStamp, - endTimeStamp, filterMap)); + endTimeStamp, filterMap, sortOrder, getRecordNum)); } /** @@ -187,8 +190,8 @@ public class PdpStatisticsProvider { */ public List<PdpStatistics> deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name, final Date timestamp) { - List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList( - dao.getFiltered(JpaPdpStatistics.class, name, PfKey.NULL_KEY_VERSION, timestamp, timestamp, null)); + List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name, + PfKey.NULL_KEY_VERSION, timestamp, timestamp, null, DESC_ORDER, 0)); pdpStatisticsListToDel.stream().forEach(s -> dao.delete(JpaPdpStatistics.class, new PfTimestampKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getTimeStamp()))); diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java index 2e1c71426..767a0fb6f 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-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. @@ -364,11 +364,13 @@ public interface PolicyModelsProvider extends AutoCloseable { * @param pdpSubGroup pdpSubGroupType name to filter statistics * @param startTimeStamp startTimeStamp to filter statistics * @param endTimeStamp endTimeStamp to filter statistics + * @param sortOrder sortOrder to query database + * @param getRecordNum Total query count from database * @return the PDP statistics found * @throws PfModelException on errors getting policies */ public List<PdpStatistics> getFilteredPdpStatistics(String name, @NonNull String pdpGroupName, String pdpSubGroup, - Date startTimeStamp, Date endTimeStamp) throws PfModelException; + Date startTimeStamp, Date endTimeStamp, String sortOrder, int getRecordNum) throws PfModelException; /** * Creates PDP statistics. diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java index d154910bb..08c01b68f 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java @@ -337,10 +337,11 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { @Override public List<PdpStatistics> getFilteredPdpStatistics(final String name, @NonNull final String pdpGroupName, - final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp) throws PfModelException { + final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp, final String sortOrder, + final int getRecordNum) throws PfModelException { assertInitialized(); return new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, name, pdpGroupName, pdpSubGroup, - startTimeStamp, endTimeStamp); + startTimeStamp, endTimeStamp, sortOrder, getRecordNum); } @Override diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java index c0a6e2c2a..3d1c9f61c 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java @@ -236,7 +236,7 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { @Override public List<PdpStatistics> getFilteredPdpStatistics(String name, String pdpGroupName, String pdpSubGroup, - Date startTimeStamp, Date endTimeStamp) { + Date startTimeStamp, Date endTimeStamp, String sortOrder, int getRecordNum) { // Not implemented return new ArrayList<>(); } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java index 02481ca12..fd566a52f 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java @@ -31,7 +31,6 @@ import java.util.ArrayList; import java.util.Base64; import java.util.Date; import java.util.List; - import org.junit.Before; import org.junit.Test; import org.onap.policy.models.pdp.concepts.Pdp; @@ -81,6 +80,8 @@ public class DatabasePolicyModelsProviderTest { private static final Date TIMESTAMP = new Date(); + private static final String ORDER = "DESC"; + private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class); PolicyModelsProviderParameters parameters; @@ -326,7 +327,7 @@ public class DatabasePolicyModelsProviderTest { }).hasMessageMatching(NAME_IS_NULL); assertThatThrownBy(() -> { - databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP); + databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP, ORDER, 0); }).hasMessageMatching(GROUP_IS_NULL); assertThatThrownBy(() -> { @@ -467,9 +468,10 @@ public class DatabasePolicyModelsProviderTest { pdpSubGroup.getPdpInstances().add(pdp); PdpStatistics pdpStatistics = new PdpStatistics(); - pdpStatistics.setPdpInstanceId("Pdp1"); + pdpStatistics.setPdpInstanceId(NAME); pdpStatistics.setTimeStamp(new Date()); - pdpStatistics.setPdpGroupName("DefaultGroup"); + pdpStatistics.setPdpGroupName(GROUP); + pdpStatistics.setPdpSubGroupName("type"); ArrayList<PdpStatistics> statisticsArrayList = new ArrayList<>(); statisticsArrayList.add(pdpStatistics); @@ -496,22 +498,38 @@ public class DatabasePolicyModelsProviderTest { assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName()); assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); - - databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null); - databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null); - databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date()); - databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date()); - - databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null); - databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date()); - - databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null); - databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date()); - - databaseProvider.createPdpStatistics(statisticsArrayList); - databaseProvider.updatePdpStatistics(statisticsArrayList); - - databaseProvider.deletePdpStatistics("pdp1", null); + assertEquals(1, databaseProvider.createPdpStatistics(statisticsArrayList).size()); + assertEquals(1, databaseProvider.updatePdpStatistics(statisticsArrayList).size()); + + assertEquals(NAME, databaseProvider.getPdpStatistics(null, null).get(0).getPdpInstanceId()); + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null, ORDER, 0).get(0) + .getPdpInstanceId()); + assertEquals(0, + databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null, ORDER, 0).size()); + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date(), ORDER, 0) + .get(0).getPdpInstanceId()); + assertEquals(0, databaseProvider + .getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date(), ORDER, 0).size()); + + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null, ORDER, 0).get(0) + .getPdpInstanceId()); + assertEquals(0, databaseProvider + .getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date(), ORDER, 0).size()); + + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 0) + .get(0).getPdpInstanceId()); + assertEquals(0, databaseProvider + .getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 0).size()); + + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 1) + .get(0).getPdpInstanceId()); + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 5) + .get(0).getPdpInstanceId()); + assertEquals(0, databaseProvider + .getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 5).size()); + + assertEquals(NAME, databaseProvider.deletePdpStatistics(NAME, null).get(0).getPdpInstanceId()); + assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); } catch (Exception exc) { LOGGER.warn("test should not throw an exception", exc); fail("test should not throw an exception"); diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java index d809f66ba..73940a6b3 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.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"); @@ -230,7 +230,7 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { @Override public List<PdpStatistics> getFilteredPdpStatistics(String name, String pdpGroupName, String pdpSubGroup, - Date startTimeStamp, Date endTimeStamp) { + Date startTimeStamp, Date endTimeStamp, String sortOrder, int getRecordNum) { // Not implemented return new ArrayList<>(); } |