Add timestamp to Pdp records 74/122074/3
authorJim Hahn <jrh3@att.com>
Fri, 18 Jun 2021 20:03:04 +0000 (16:03 -0400)
committerAjith Sreekumar <ajith.sreekumar@bell.ca>
Tue, 22 Jun 2021 08:37:55 +0000 (08:37 +0000)
To support the case where multiple PAPs are running, added a timestamp
to the PDP records so the PAPs can all be kept in sync as far as when a
PDP was last responsive.

Issue-ID: POLICY-2898
Change-Id: Ia7e0fd17363ff5827564cbc1537e516156b7f53a
Signed-off-by: Jim Hahn <jrh3@att.com>
models-pdp/src/main/java/org/onap/policy/models/pdp/concepts/Pdp.java
models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java
models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpTest.java
models-pdp/src/test/resources/testdata/PdpGroups0.json
models-pdp/src/test/resources/testdata/PdpGroups0Update.json
models-pdp/src/test/resources/testdata/PdpGroupsForFiltering.json
models-pdp/src/test/resources/testdata/PdpGroupsNoPDPs.json

index 034374c..978dd7c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
- *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. 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.
@@ -21,6 +21,7 @@
 
 package org.onap.policy.models.pdp.concepts;
 
+import java.time.Instant;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.NonNull;
@@ -47,6 +48,13 @@ public class Pdp {
     private PdpHealthStatus healthy;
     private String message;
 
+    /**
+     * Time when the record was last updated as a result of receiving a message from the
+     * PDP.
+     */
+    private Instant lastUpdate;
+
+
     /**
      * Constructs the object, creating a deep copy of the fields from the source.
      *
@@ -57,5 +65,6 @@ public class Pdp {
         this.pdpState = source.pdpState;
         this.healthy = source.healthy;
         this.message = source.message;
+        this.lastUpdate = source.lastUpdate;
     }
 }
index daa327d..7c584f3 100644 (file)
@@ -24,6 +24,7 @@
 package org.onap.policy.models.pdp.persistence.concepts;
 
 import java.io.Serializable;
+import java.util.Date;
 import java.util.List;
 import javax.persistence.Column;
 import javax.persistence.EmbeddedId;
@@ -31,10 +32,12 @@ 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.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
-import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.builder.CompareToBuilder;
 import org.onap.policy.common.parameters.BeanValidationResult;
 import org.onap.policy.common.parameters.annotations.NotBlank;
 import org.onap.policy.common.parameters.annotations.NotNull;
@@ -77,6 +80,10 @@ public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializabl
     @NotBlank
     private String message;
 
+    @Column(precision = 0)
+    @Temporal(TemporalType.TIMESTAMP)
+    private Date lastUpdate;
+
     /**
      * The Default Constructor creates a {@link JpaPdp} object with a null key.
      */
@@ -101,10 +108,11 @@ public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializabl
      * @param healthy the health state of the PDP
      */
     public JpaPdp(@NonNull final PfReferenceKey key, @NonNull final PdpState pdpState,
-            @NonNull PdpHealthStatus healthy) {
+                    @NonNull PdpHealthStatus healthy) {
         this.key = key;
         this.pdpState = pdpState;
         this.healthy = healthy;
+        this.lastUpdate = new Date();
     }
 
     /**
@@ -118,6 +126,7 @@ public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializabl
         this.pdpState = copyConcept.pdpState;
         this.healthy = copyConcept.healthy;
         this.message = copyConcept.message;
+        this.lastUpdate = copyConcept.lastUpdate;
     }
 
     /**
@@ -137,6 +146,7 @@ public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializabl
         pdp.setPdpState(pdpState);
         pdp.setHealthy(healthy);
         pdp.setMessage(message);
+        pdp.setLastUpdate(lastUpdate.toInstant());
 
         return pdp;
     }
@@ -151,6 +161,12 @@ public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializabl
         this.setPdpState(pdp.getPdpState());
         this.setHealthy(pdp.getHealthy());
         this.setMessage(pdp.getMessage());
+
+        if (pdp.getLastUpdate() == null) {
+            this.setLastUpdate(new Date());
+        } else {
+            this.setLastUpdate(Date.from(pdp.getLastUpdate()));
+        }
     }
 
     @Override
@@ -193,20 +209,15 @@ public class JpaPdp extends PfConcept implements PfAuthorative<Pdp>, Serializabl
         }
 
         final JpaPdp other = (JpaPdp) otherConcept;
-        if (!key.equals(other.key)) {
-            return key.compareTo(other.key);
-        }
-
-        int result = ObjectUtils.compare(pdpState, other.pdpState);
-        if (result != 0) {
-            return result;
-        }
-
-        result = ObjectUtils.compare(healthy, other.healthy);
-        if (result != 0) {
-            return result;
-        }
 
-        return ObjectUtils.compare(message, other.message);
+        // @formatter:off
+        return new CompareToBuilder()
+                        .append(this.key, other.key)
+                        .append(this.pdpState, other.pdpState)
+                        .append(this.healthy, other.healthy)
+                        .append(this.message, other.message)
+                        .append(this.lastUpdate, other.lastUpdate)
+                        .toComparison();
+        // @formatter:on
     }
 }
index b5b29d3..ab592f5 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2021 Nordix Foundation.
- *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. 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.
@@ -29,6 +29,7 @@ import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Date;
 import org.junit.Test;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfReferenceKey;
@@ -47,6 +48,7 @@ public class JpaPdpTest {
 
     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
     private static final String PDP1 = "ThePDP";
+    private static final Date CURRENT_DATE = new Date();
 
     @Test
     public void testJpaPdp() {
@@ -78,10 +80,6 @@ public class JpaPdpTest {
             new JpaPdp(null, PdpState.ACTIVE, PdpHealthStatus.UNKNOWN);
         }).hasMessageMatching(NULL_KEY_ERROR);
 
-        assertThatThrownBy(() -> {
-            new JpaPdp(null, null, PdpHealthStatus.UNKNOWN);
-        }).hasMessageMatching(NULL_KEY_ERROR);
-
         assertThatThrownBy(() -> {
             new JpaPdp((Pdp) null);
         }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
@@ -175,7 +173,7 @@ public class JpaPdpTest {
     }
 
     @Test
-    public void testJpaPdpCompare() {
+    public void testJpaPdpCompare_testToAuthorative() {
         JpaPdp testJpaPdp = setUpJpaPdp();
 
         JpaPdp otherJpaPdp = new JpaPdp(testJpaPdp);
@@ -204,7 +202,15 @@ public class JpaPdpTest {
         testJpaPdp.setMessage("Valid Message");
         assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
 
+        testJpaPdp.setLastUpdate(new Date(0));
+        assertEquals(-1, testJpaPdp.compareTo(otherJpaPdp));
+        testJpaPdp.setLastUpdate(CURRENT_DATE);
+        assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
+
         assertEquals(testJpaPdp, new JpaPdp(testJpaPdp));
+
+        otherJpaPdp.fromAuthorative(testJpaPdp.toAuthorative());
+        assertEquals(0, testJpaPdp.compareTo(otherJpaPdp));
     }
 
     private JpaPdp setUpJpaPdp() {
@@ -218,6 +224,7 @@ public class JpaPdpTest {
         testJpaPdp.setPdpState(PdpState.ACTIVE);
         testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY);
         testJpaPdp.setMessage("Valid Message");
+        testJpaPdp.setLastUpdate(CURRENT_DATE);
         return testJpaPdp;
     }
 }
index 01e140c..be3d800 100644 (file)
                             "instanceId": "apex-0",
                             "pdpState": "ACTIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 }
             ]
         }
     ]
-}
\ No newline at end of file
+}
index 05594fa..06d0aef 100644 (file)
                             "instanceId": "apex-0",
                             "pdpState": "ACTIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         },
                         {
                             "instanceId": "apex-1",
                             "pdpState": "ACTIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         },
                         {
                             "instanceId": "apex-2",
                             "pdpState": "ACTIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 },
                             "instanceId": "drools-0",
                             "pdpState": "ACTIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 }
             ]
         }
     ]
-}
\ No newline at end of file
+}
index f1d4378..dfc8308 100644 (file)
                             "instanceId": "apex-0",
                             "pdpState": "ACTIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         },
                         {
                             "instanceId": "apex-1",
                             "pdpState": "PASSIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         },
                         {
                             "instanceId": "apex-2",
                             "pdpState": "SAFE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         },
                         {
                             "instanceId": "apex-3",
                             "pdpState": "TEST",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 }
                             "instanceId": "apex-0",
                             "pdpState": "ACTIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 }
                             "instanceId": "apex-0",
                             "pdpState": "SAFE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 },
                             "instanceId": "apex-0",
                             "pdpState": "SAFE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 }
                             "instanceId": "apex-0",
                             "pdpState": "PASSIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 }
                             "instanceId": "apex-0",
                             "pdpState": "TEST",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 },
                             "instanceId": "apex-0",
                             "pdpState": "PASSIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 },
                             "instanceId": "apex-0",
                             "pdpState": "ACTIVE",
                             "healthy": "NOT_HEALTHY",
-                            "message": "message from PDP"
+                            "message": "message from PDP",
+                            "lastUpdate": "1970-01-01T00:00:00Z"
                         }
                     ]
                 }