From a3241bec1cf1ae5fd514923ba5f923abc5427762 Mon Sep 17 00:00:00 2001 From: ktimoney Date: Tue, 13 Apr 2021 14:40:06 +0100 Subject: Add a new key class which uses @GeneratedValue to base classes This will provide an alternative unique key. It will be used in place of PfTimestampKey in the PdpStatistics table. We make use of the @GeneratedValue annotation to provide this feature. Added DummyGeneratedIdEntity and unit tests Added PfFilterFactory class Added new filter classes Issue-ID: POLICY-3189 Change-Id: I097eab318d2cb958e1157e35825fd6d35d429a25 Signed-off-by: ktimoney --- .../policy/models/pdp/concepts/PdpStatistics.java | 7 ++-- .../pdp/persistence/concepts/JpaPdpStatistics.java | 44 +++++++++++++++------ .../provider/PdpStatisticsProvider.java | 45 +++++++++++++++------- .../persistence/concepts/JpaPdpStatisticsTest.java | 9 +++-- .../provider/PdpStatisticsProviderTest.java | 6 ++- 5 files changed, 77 insertions(+), 34 deletions(-) (limited to 'models-pdp/src') diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpStatistics.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpStatistics.java index f7a0e7d91..c9c4cda0e 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpStatistics.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpStatistics.java @@ -1,8 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. - * Modifications Copyright (C) 2020-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +21,7 @@ package org.onap.policy.models.pdp.concepts; + import java.time.Instant; import java.util.List; import lombok.Data; @@ -40,6 +40,7 @@ public class PdpStatistics { private String pdpInstanceId; private Instant timeStamp; + private Long generatedId; private String pdpGroupName; private String pdpSubGroupName; private long policyDeployCount; @@ -58,6 +59,7 @@ public class PdpStatistics { public PdpStatistics(@NonNull PdpStatistics source) { this.pdpInstanceId = source.pdpInstanceId; this.timeStamp = source.timeStamp; + this.generatedId = source.generatedId; this.pdpGroupName = source.pdpGroupName; this.pdpSubGroupName = source.pdpSubGroupName; this.policyDeployCount = source.policyDeployCount; @@ -68,5 +70,4 @@ public class PdpStatistics { this.policyExecutedSuccessCount = source.policyExecutedSuccessCount; this.engineStats = PfUtils.mapList(source.engineStats, PdpEngineWorkerStatistics::new, null); } - } diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java index c3329cfa4..ae859325d 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java @@ -25,6 +25,7 @@ package org.onap.policy.models.pdp.persistence.concepts; import java.io.Serializable; import java.time.Instant; +import java.util.Date; import java.util.List; import javax.persistence.Column; import javax.persistence.ElementCollection; @@ -33,16 +34,19 @@ import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; import org.apache.commons.lang3.builder.CompareToBuilder; +import org.eclipse.persistence.annotations.Index; import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfGeneratedIdKey; import org.onap.policy.models.base.PfKey; -import org.onap.policy.models.base.PfTimestampKey; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.base.validation.annotations.VerifyKey; import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics; @@ -55,6 +59,7 @@ import org.onap.policy.models.pdp.concepts.PdpStatistics; */ @Entity @Table(name = "PdpStatistics") +@Index(name = "IDX_TSIDX1", columnNames = {"timeStamp", "name", "version"}) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Data @AllArgsConstructor @@ -66,7 +71,11 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative getPdpStatistics(@NonNull final PfDao dao, final String name, final Instant timestamp) + public List getPdpStatistics(@NonNull final PfDao dao, final String name, final Instant timeStamp) + throws PfModelException { + List pdpStatistics = new ArrayList<>(); + if (name != null && timeStamp != null) { + return asPdpStatisticsList(dao.getByTimestamp(JpaPdpStatistics.class, + new PfGeneratedIdKey(name, PfKey.NULL_KEY_VERSION), timeStamp)); + } else { + return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class)); + } + } + + /** + * Get PDP statistics. + * + * @param dao the DAO to use to access the database + * @param name the name of the PDP statistics to get, null to get all PDPs + * @return the PDP statistics found + * @throws PfModelException on errors getting PDP statistics + */ + public List getPdpStatistics(@NonNull final PfDao dao, final String name) throws PfModelException { List pdpStatistics = new ArrayList<>(); if (name != null) { pdpStatistics - .add(dao.get(JpaPdpStatistics.class, new PfTimestampKey(name, PfKey.NULL_KEY_VERSION, timestamp)) + .add(dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(name, PfKey.NULL_KEY_VERSION)) .toAuthorative()); } else { return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class)); @@ -95,7 +114,8 @@ public class PdpStatisticsProvider { filterMap.put("pdpSubGroupName", pdpSubGroup); } - return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name, PfKey.NULL_KEY_VERSION, startTimeStamp, + return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name, + PfKey.NULL_KEY_VERSION, startTimeStamp, endTimeStamp, filterMap, sortOrder, getRecordNum)); } @@ -109,17 +129,16 @@ public class PdpStatisticsProvider { */ public List createPdpStatistics(@NonNull final PfDao dao, @NonNull final List pdpStatisticsList) throws PfModelException { - for (PdpStatistics pdpStatistics : pdpStatisticsList) { JpaPdpStatistics jpaPdpStatistics = new JpaPdpStatistics(); jpaPdpStatistics.fromAuthorative(pdpStatistics); - BeanValidationResult validationResult = jpaPdpStatistics.validate("pdp statistics"); if (!validationResult.isValid()) { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult()); } dao.create(jpaPdpStatistics); + pdpStatistics.setGeneratedId(jpaPdpStatistics.getKey().getGeneratedId()); } // Return the created PDP statistics @@ -127,11 +146,10 @@ public class PdpStatisticsProvider { for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) { JpaPdpStatistics jpaPdpStatistics = - dao.get(JpaPdpStatistics.class, new PfTimestampKey(pdpStatisticsItem.getPdpInstanceId(), - PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getTimeStamp())); + dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(), + PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId())); pdpStatistics.add(jpaPdpStatistics.toAuthorative()); } - return pdpStatistics; } @@ -163,8 +181,8 @@ public class PdpStatisticsProvider { for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) { JpaPdpStatistics jpaPdpStatistics = - dao.get(JpaPdpStatistics.class, new PfTimestampKey(pdpStatisticsItem.getPdpInstanceId(), - PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getTimeStamp())); + dao.get(JpaPdpStatistics.class, new PfGeneratedIdKey(pdpStatisticsItem.getPdpInstanceId(), + PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getGeneratedId())); pdpStatistics.add(jpaPdpStatistics.toAuthorative()); } @@ -182,11 +200,12 @@ public class PdpStatisticsProvider { */ public List deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name, final Instant timestamp) { - List pdpStatisticsListToDel = asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name, + List 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()))); + new PfGeneratedIdKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getGeneratedId()))); return pdpStatisticsListToDel; } diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java index 405425e2c..819c3b6e7 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java @@ -31,7 +31,7 @@ import static org.junit.Assert.assertTrue; import java.time.Instant; import java.util.ArrayList; import org.junit.Test; -import org.onap.policy.models.base.PfTimestampKey; +import org.onap.policy.models.base.PfGeneratedIdKey; import org.onap.policy.models.pdp.concepts.PdpStatistics; /** @@ -41,14 +41,14 @@ public class JpaPdpStatisticsTest { @Test public void testConstructor() { - assertThatThrownBy(() -> new JpaPdpStatistics((PfTimestampKey) null)).hasMessageContaining("key"); + assertThatThrownBy(() -> new JpaPdpStatistics((PfGeneratedIdKey) null)).hasMessageContaining("key"); assertThatThrownBy(() -> new JpaPdpStatistics((JpaPdpStatistics) null)).hasMessageContaining("copyConcept"); assertThatThrownBy(() -> new JpaPdpStatistics((PdpStatistics) null)).hasMessageContaining("authorativeConcept"); assertNotNull(new JpaPdpStatistics()); - assertNotNull(new JpaPdpStatistics(new PfTimestampKey())); + assertNotNull(new JpaPdpStatistics(new PfGeneratedIdKey())); PdpStatistics pdpStat = createPdpStatistics(); JpaPdpStatistics jpaPdpStat = new JpaPdpStatistics(createPdpStatistics()); @@ -114,7 +114,7 @@ public class JpaPdpStatisticsTest { assertEquals(pdpStat.getPdpInstanceId(), jpaPdpStat.getKey().getName()); assertEquals(pdpStat.getPdpGroupName(), jpaPdpStat.getPdpGroupName()); assertEquals(pdpStat.getPdpSubGroupName(), jpaPdpStat.getPdpSubGroupName()); - assertEquals(pdpStat.getTimeStamp(), jpaPdpStat.getKey().getInstant()); + assertEquals(pdpStat.getTimeStamp(), jpaPdpStat.getTimeStamp().toInstant()); assertEquals(pdpStat.getPolicyDeployCount(), jpaPdpStat.getPolicyDeployCount()); assertEquals(pdpStat.getPolicyDeploySuccessCount(), jpaPdpStat.getPolicyDeploySuccessCount()); assertEquals(pdpStat.getPolicyDeployFailCount(), jpaPdpStat.getPolicyDeployFailCount()); @@ -128,6 +128,7 @@ public class JpaPdpStatisticsTest { pdpStat.setPdpInstanceId("PDP0"); pdpStat.setPdpGroupName("PDPGroup0"); pdpStat.setPdpSubGroupName("PDPSubGroup0"); + pdpStat.setGeneratedId(10001L); pdpStat.setTimeStamp(Instant.EPOCH); pdpStat.setPolicyDeployCount(3); pdpStat.setPolicyDeploySuccessCount(1); diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java index b8d33150d..8259dc0c6 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java @@ -50,6 +50,8 @@ public class PdpStatisticsProviderTest { private static final String SUBGROUP = "subgroup"; private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1078884319); private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1078884350); + private static final Long GENERATEDID1 = 1L; + private static final Long GENERATEDID2 = 2L; private static final String ORDER = "DESC"; private PfDao pfDao; @@ -90,6 +92,7 @@ public class PdpStatisticsProviderTest { PdpStatistics pdpStatistics = new PdpStatistics(); pdpStatistics.setPdpInstanceId(NAME); pdpStatistics.setTimeStamp(TIMESTAMP1); + pdpStatistics.setGeneratedId(GENERATEDID1); pdpStatistics.setPdpGroupName(GROUP); pdpStatistics.setPdpSubGroupName(SUBGROUP); pdpStatistics.setPolicyDeployCount(2); @@ -105,6 +108,7 @@ public class PdpStatisticsProviderTest { PdpStatistics pdpStatistics2 = new PdpStatistics(); pdpStatistics2.setPdpInstanceId("name2"); pdpStatistics2.setTimeStamp(TIMESTAMP2); + pdpStatistics2.setGeneratedId(GENERATEDID2); pdpStatistics2.setPdpGroupName(GROUP); pdpStatistics2.setPdpSubGroupName(SUBGROUP); pdpStatistics2.setPolicyDeployCount(2); @@ -121,7 +125,6 @@ public class PdpStatisticsProviderTest { createdPdpStatisticsList = new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsTestList); createdListStr = createdPdpStatisticsList.toString(); assertEquals(createdListStr.replaceAll("\\s+", ""), testListStr.replaceAll("\\s+", "")); - } @After @@ -219,7 +222,6 @@ public class PdpStatisticsProviderTest { new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsTestList); String gotListStr = updatePdpStatisticsList.toString(); assertEquals(testListStr.replaceAll("\\s+", ""), gotListStr.replaceAll("\\s+", "")); - } @Test -- cgit 1.2.3-korg