aboutsummaryrefslogtreecommitdiffstats
path: root/models-pdp
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2021-04-26 12:55:35 +0000
committerGerrit Code Review <gerrit@onap.org>2021-04-26 12:55:35 +0000
commit6e589fc31f69482d2cf5bf57c733094394439b0f (patch)
tree60fb300b66012035220f8450f02ae1b74f79e678 /models-pdp
parenta250c0c7a4a680c15cc4cb158f8f3cff53619f1b (diff)
parenta3241bec1cf1ae5fd514923ba5f923abc5427762 (diff)
Merge "Add a new key class which uses @GeneratedValue to base classes"
Diffstat (limited to 'models-pdp')
-rw-r--r--models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/PdpStatistics.java7
-rw-r--r--models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatistics.java44
-rw-r--r--models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java45
-rw-r--r--models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpStatisticsTest.java9
-rw-r--r--models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProviderTest.java6
5 files changed, 77 insertions, 34 deletions
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<PdpStat
@EmbeddedId
@VerifyKey
@NotNull
- private PfTimestampKey key;
+ private PfGeneratedIdKey key;
+
+ @Column(precision = 3)
+ @Temporal(TemporalType.TIMESTAMP)
+ private Date timeStamp;
@Column(length = 120)
private String pdpGroupName;
@@ -99,7 +108,7 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStat
* The Default Constructor creates a {@link JpaPdpStatistics} object with a null key.
*/
public JpaPdpStatistics() {
- this(new PfTimestampKey());
+ this(new PfGeneratedIdKey());
}
/**
@@ -107,11 +116,10 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStat
*
* @param key the key
*/
- public JpaPdpStatistics(@NonNull final PfTimestampKey key) {
- this(key, NULL_NAME, NULL_NAME, 0L, 0L, 0L, 0L, 0L, 0L, null);
+ public JpaPdpStatistics(@NonNull final PfGeneratedIdKey key) {
+ this(key, null, NULL_NAME, NULL_NAME, 0L, 0L, 0L, 0L, 0L, 0L, null);
}
-
/**
* Copy constructor.
*
@@ -119,7 +127,8 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStat
*/
public JpaPdpStatistics(@NonNull final JpaPdpStatistics copyConcept) {
super(copyConcept);
- this.key = new PfTimestampKey(copyConcept.key);
+ this.key = new PfGeneratedIdKey(copyConcept.key);
+ this.timeStamp = copyConcept.timeStamp;
this.pdpGroupName = copyConcept.pdpGroupName;
this.pdpSubGroupName = copyConcept.pdpSubGroupName;
this.policyDeployCount = copyConcept.policyDeployCount;
@@ -153,7 +162,10 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStat
}
final JpaPdpStatistics other = (JpaPdpStatistics) otherConcept;
- return new CompareToBuilder().append(this.key, other.key).append(this.pdpGroupName, other.pdpGroupName)
+ return new CompareToBuilder()
+ .append(this.key, other.key)
+ .append(this.timeStamp, other.timeStamp)
+ .append(this.pdpGroupName, other.pdpGroupName)
.append(this.pdpSubGroupName, other.pdpSubGroupName)
.append(this.policyDeployCount, other.policyDeployCount)
.append(this.policyDeployFailCount, other.policyDeployFailCount)
@@ -167,7 +179,8 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStat
public PdpStatistics toAuthorative() {
PdpStatistics pdpStatistics = new PdpStatistics();
pdpStatistics.setPdpInstanceId(key.getName());
- pdpStatistics.setTimeStamp(key.getInstant());
+ pdpStatistics.setGeneratedId(key.getGeneratedId());
+ pdpStatistics.setTimeStamp(timeStamp.toInstant());
pdpStatistics.setPdpGroupName(pdpGroupName);
pdpStatistics.setPdpSubGroupName(pdpSubGroupName);
pdpStatistics.setPolicyDeployCount(policyDeployCount);
@@ -183,9 +196,16 @@ public class JpaPdpStatistics extends PfConcept implements PfAuthorative<PdpStat
@Override
public void fromAuthorative(@NonNull final PdpStatistics pdpStatistics) {
- if (this.key == null || this.getKey().isNullKey()) {
- this.setKey(new PfTimestampKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION,
- pdpStatistics.getTimeStamp() == null ? Instant.EPOCH : pdpStatistics.getTimeStamp()));
+ if (pdpStatistics.getGeneratedId() == null) {
+ this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(), PfKey.NULL_KEY_VERSION));
+ } else {
+ this.setKey(new PfGeneratedIdKey(pdpStatistics.getPdpInstanceId(),
+ PfKey.NULL_KEY_VERSION, pdpStatistics.getGeneratedId()));
+ }
+ if (pdpStatistics.getTimeStamp() == null) {
+ this.setTimeStamp(Date.from(Instant.EPOCH));
+ } else {
+ this.setTimeStamp(Date.from(pdpStatistics.getTimeStamp()));
}
this.setPdpGroupName(pdpStatistics.getPdpGroupName());
this.setPdpSubGroupName(pdpStatistics.getPdpSubGroupName());
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 e030d089d..ece09b088 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
@@ -32,10 +32,10 @@ import java.util.stream.Collectors;
import javax.ws.rs.core.Response;
import lombok.NonNull;
import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.models.base.PfGeneratedIdKey;
import org.onap.policy.models.base.PfKey;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
-import org.onap.policy.models.base.PfTimestampKey;
import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics;
@@ -58,13 +58,32 @@ public class PdpStatisticsProvider {
* @return the PDP statistics found
* @throws PfModelException on errors getting PDP statistics
*/
- public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final Instant timestamp)
+ public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final Instant timeStamp)
+ throws PfModelException {
+ List<PdpStatistics> 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<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name)
throws PfModelException {
List<PdpStatistics> 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<PdpStatistics> createPdpStatistics(@NonNull final PfDao dao,
@NonNull final List<PdpStatistics> 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<PdpStatistics> deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name,
final Instant timestamp) {
- List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name,
+ 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())));
+ 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