Use unique name in drools PdpMessages 39/122339/2
authorJim Hahn <jrh3@att.com>
Mon, 28 Jun 2021 19:04:36 +0000 (15:04 -0400)
committerJim Hahn <jrh3@att.com>
Tue, 29 Jun 2021 14:21:49 +0000 (10:21 -0400)
Also modified distributed locking to use the unique name, in case the DB
is shared.

Issue-ID: POLICY-3410
Change-Id: I046a9ffdcb62f3d84d7d388a1e64bcf025fbbf75
Signed-off-by: Jim Hahn <jrh3@att.com>
feature-distributed-locking/src/main/java/org/onap/policy/distributed/locking/DistributedLockManager.java
feature-distributed-locking/src/test/java/org/onap/policy/distributed/locking/DistributedLockManagerTest.java
feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFsm.java
feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmTest.java
feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmUpdateTest.java
feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActivePoliciesTest.java
feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java
feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java
feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTerminatedTest.java
policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineConstants.java

index c114d26..93a6375 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * 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.
@@ -41,7 +41,6 @@ import lombok.Getter;
 import lombok.Setter;
 import org.apache.commons.dbcp2.BasicDataSource;
 import org.apache.commons.dbcp2.BasicDataSourceFactory;
-import org.onap.policy.common.utils.network.NetworkUtil;
 import org.onap.policy.drools.core.lock.LockCallback;
 import org.onap.policy.drools.core.lock.LockState;
 import org.onap.policy.drools.core.lock.PolicyResourceLockManager;
@@ -94,7 +93,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
      * Name of the host on which this JVM is running.
      */
     @Getter
-    private final String hostName;
+    private final String pdpName;
 
     /**
      * UUID of this object.
@@ -135,7 +134,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
      * Constructs the object.
      */
     public DistributedLockManager() {
-        this.hostName = NetworkUtil.getHostname();
+        this.pdpName = PolicyEngineConstants.PDP_NAME;
         this.resource2lock = getResource2lock();
     }
 
@@ -305,7 +304,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
                         "SELECT resourceId FROM pooling.locks WHERE host=? AND owner=? AND expirationTime > now()")) {
             // @formatter:on
 
-            stmt.setString(1, hostName);
+            stmt.setString(1, pdpName);
             stmt.setString(2, uuidString);
 
             try (ResultSet resultSet = stmt.executeQuery()) {
@@ -415,7 +414,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
             super(state, resourceId, ownerKey, holdSec, callback);
 
             this.feature = feature;
-            this.hostName = feature.hostName;
+            this.hostName = feature.pdpName;
             this.uuidString = feature.uuidString;
         }
 
@@ -713,13 +712,13 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
                                             + "values (?, ?, ?, timestampadd(second, ?, now()))")) {
 
                 stmt.setString(1, getResourceId());
-                stmt.setString(2, feature.hostName);
+                stmt.setString(2, feature.pdpName);
                 stmt.setString(3, feature.uuidString);
                 stmt.setInt(4, getHoldSec());
 
                 stmt.executeUpdate();
 
-                this.hostName = feature.hostName;
+                this.hostName = feature.pdpName;
                 this.uuidString = feature.uuidString;
 
                 return true;
@@ -742,7 +741,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
                                             + " AND ((host=? AND owner=?) OR expirationTime < now())")) {
 
                 stmt.setString(1, getResourceId());
-                stmt.setString(2, feature.hostName);
+                stmt.setString(2, feature.pdpName);
                 stmt.setString(3, feature.uuidString);
                 stmt.setInt(4, getHoldSec());
 
@@ -754,7 +753,7 @@ public class DistributedLockManager extends LockManager<DistributedLockManager.D
                     return false;
                 }
 
-                this.hostName = feature.hostName;
+                this.hostName = feature.pdpName;
                 this.uuidString = feature.uuidString;
 
                 return true;
index d36fbe1..2fbdb26 100644 (file)
@@ -441,13 +441,13 @@ public class DistributedLockManagerTest {
         assertEquals(5, getRecordCount());
 
         // expire one record
-        updateRecord(RESOURCE, feature.getHostName(), feature.getUuidString(), -1);
+        updateRecord(RESOURCE, feature.getPdpName(), feature.getUuidString(), -1);
 
         // change host of another record
         updateRecord(RESOURCE3, OTHER_HOST, feature.getUuidString(), HOLD_SEC);
 
         // change uuid of another record
-        updateRecord(RESOURCE5, feature.getHostName(), OTHER_OWNER, HOLD_SEC);
+        updateRecord(RESOURCE5, feature.getPdpName(), OTHER_OWNER, HOLD_SEC);
 
 
         // run the checker
@@ -569,7 +569,7 @@ public class DistributedLockManagerTest {
         assertEquals(3, getRecordCount());
 
         // expire one record
-        updateRecord(RESOURCE, feature.getHostName(), feature.getUuidString(), -1);
+        updateRecord(RESOURCE, feature.getPdpName(), feature.getUuidString(), -1);
 
         // arrange to free lock4 while the checker is running
         freeLock.set(lock4);
@@ -1578,7 +1578,7 @@ public class DistributedLockManagerTest {
                                         + " WHERE resourceId=? AND host=? AND owner=?")) {
 
             stmt.setString(1, resourceId);
-            stmt.setString(2, feature.getHostName());
+            stmt.setString(2, feature.getPdpName());
             stmt.setString(3, uuidString);
 
             try (ResultSet result = stmt.executeQuery()) {
@@ -1603,7 +1603,7 @@ public class DistributedLockManagerTest {
      * @throws SQLException if an error occurs accessing the DB
      */
     private void insertRecord(String resourceId, String uuidString, int expireOffset) throws SQLException {
-        this.insertRecord(resourceId, feature.getHostName(), uuidString, expireOffset);
+        this.insertRecord(resourceId, feature.getPdpName(), uuidString, expireOffset);
     }
 
     private void insertRecord(String resourceId, String hostName, String uuidString, int expireOffset)
index 0e602ec..3cec745 100644 (file)
@@ -53,7 +53,6 @@ import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
 import org.onap.policy.common.endpoints.listeners.ScoListener;
 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
 import org.onap.policy.common.utils.coder.StandardCoderObject;
-import org.onap.policy.common.utils.network.NetworkUtil;
 import org.onap.policy.drools.metrics.Metric;
 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
 import org.onap.policy.drools.policies.DomainMaker;
@@ -112,7 +111,7 @@ public class LifecycleFsm implements Startable {
     protected TopicSinkClient client;
 
     @Getter
-    protected final String name = NetworkUtil.getHostname();
+    protected final String name = PolicyEngineConstants.PDP_NAME;
 
     protected LifecycleState state = new LifecycleStateTerminated(this);
 
@@ -179,7 +178,7 @@ public class LifecycleFsm implements Startable {
         logger.info("The mandatory Policy Types are {}. Compliance is {}",
                 mandatoryPolicyTypes, isMandatoryPolicyTypesCompliant());
 
-        stats.setPdpInstanceId(Metric.HOSTNAME);
+        stats.setPdpInstanceId(PolicyEngineConstants.PDP_NAME);
     }
 
     @GsonJsonIgnore
index 73d265c..4479aa1 100644 (file)
@@ -232,7 +232,7 @@ public class LifecycleFsmTest {
 
         assertEquals(fsm.getGroup(), status.getStatistics().getPdpGroupName());
         assertEquals(fsm.getSubGroup(), status.getStatistics().getPdpSubGroupName());
-        assertEquals(NetworkUtil.getHostname(), status.getStatistics().getPdpInstanceId());
+        assertEquals(PolicyEngineConstants.PDP_NAME, status.getStatistics().getPdpInstanceId());
         assertEquals(6, status.getStatistics().getPolicyDeploySuccessCount());
         assertEquals(2, status.getStatistics().getPolicyDeployFailCount());
         assertEquals(8, status.getStatistics().getPolicyDeployCount());
@@ -291,4 +291,4 @@ public class LifecycleFsmTest {
         return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
     }
 
-}
\ No newline at end of file
+}
index 1927c51..253ca5d 100644 (file)
@@ -46,7 +46,6 @@ import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
-import org.onap.policy.common.utils.network.NetworkUtil;
 import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.common.utils.time.PseudoScheduledExecutorService;
 import org.onap.policy.common.utils.time.TestTimeMulti;
@@ -54,6 +53,7 @@ import org.onap.policy.drools.domain.models.artifact.NativeArtifactPolicy;
 import org.onap.policy.drools.domain.models.controller.ControllerPolicy;
 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
 import org.onap.policy.drools.system.PolicyControllerConstants;
+import org.onap.policy.drools.system.PolicyEngineConstants;
 import org.onap.policy.drools.utils.logging.LoggerUtil;
 import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
@@ -363,7 +363,7 @@ public class LifecycleFsmUpdateTest {
 
     protected PdpUpdate getPdpUpdate(List<ToscaPolicy> policiesToDeploy, List<ToscaPolicy> policiesToUndeploy) {
         PdpUpdate update = new PdpUpdate();
-        update.setName(NetworkUtil.getHostname());
+        update.setName(PolicyEngineConstants.PDP_NAME);
         update.setPdpGroup("A");
         update.setPdpSubgroup("a");
         update.setPoliciesToBeDeployed(policiesToDeploy);
index b56d21f..f37e8ef 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -40,7 +40,7 @@ import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
-import org.onap.policy.common.utils.network.NetworkUtil;
+import org.onap.policy.drools.system.PolicyEngineConstants;
 import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
 import org.onap.policy.models.pdp.enums.PdpState;
@@ -116,7 +116,7 @@ public class LifecycleStateActivePoliciesTest extends LifecycleStateRunningTest
             getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
 
         PdpUpdate update = new PdpUpdate();
-        update.setName(NetworkUtil.getHostname());
+        update.setName(PolicyEngineConstants.PDP_NAME);
         update.setPdpGroup("W");
         update.setPdpSubgroup("w");
         update.setPoliciesToBeDeployed(List.of(policyNativeController));
index 2efaa49..f4c3314 100644 (file)
@@ -40,7 +40,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
-import org.onap.policy.common.utils.network.NetworkUtil;
+import org.onap.policy.drools.system.PolicyEngineConstants;
 import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpStatus;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
@@ -189,7 +189,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         // TODO: extract repeated similar assertion blocks into their own helper methods
 
         PdpUpdate update = new PdpUpdate();
-        update.setName(NetworkUtil.getHostname());
+        update.setName(PolicyEngineConstants.PDP_NAME);
         update.setPdpGroup("W");
         update.setPdpSubgroup("w");
 
index 9c4c8ab..d99f011 100644 (file)
@@ -35,7 +35,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
-import org.onap.policy.common.utils.network.NetworkUtil;
+import org.onap.policy.drools.system.PolicyEngineConstants;
 import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpStatus;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
@@ -143,7 +143,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
         assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
 
         PdpUpdate update = new PdpUpdate();
-        update.setName(NetworkUtil.getHostname());
+        update.setName(PolicyEngineConstants.PDP_NAME);
         update.setPdpGroup("Z");
         update.setPdpSubgroup("z");
 
@@ -269,7 +269,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
         assertNull(fsm.getSubGroup());
 
         PdpUpdate update = new PdpUpdate();
-        update.setName(NetworkUtil.getHostname());
+        update.setName(PolicyEngineConstants.PDP_NAME);
         update.setPdpGroup("A");
         update.setPdpSubgroup("a");
 
@@ -330,7 +330,7 @@ public class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
         assertEquals("foo", status.getPdpType());
         assertEquals(PdpState.TERMINATED, status.getState());
         assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
-        assertEquals(NetworkUtil.getHostname(), status.getName());
+        assertEquals(PolicyEngineConstants.PDP_NAME, status.getName());
         assertEquals(fsm.getName(), status.getName());
         assertEquals(PdpMessageType.PDP_STATUS, status.getMessageName());
     }
index e17f43a..cf6f451 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -33,8 +33,8 @@ import java.util.Collections;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.onap.policy.common.utils.network.NetworkUtil;
 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
+import org.onap.policy.drools.system.PolicyEngineConstants;
 import org.onap.policy.drools.utils.logging.LoggerUtil;
 import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
@@ -139,7 +139,7 @@ public class LifecycleStateTerminatedTest {
     @Test
     public void testUpdate() {
         PdpUpdate update = new PdpUpdate();
-        update.setName(NetworkUtil.getHostname());
+        update.setName(PolicyEngineConstants.PDP_NAME);
         update.setPdpGroup("A");
         update.setPdpSubgroup("a");
         update.setPoliciesToBeDeployed(Collections.emptyList());
@@ -190,4 +190,4 @@ public class LifecycleStateTerminatedTest {
         assertFalse(fsm.statusTask.isCancelled());
         assertFalse(fsm.statusTask.isDone());
     }
-}
\ No newline at end of file
+}
index 4d18d6b..2173687 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * policy-management
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * 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,8 +21,9 @@
 package org.onap.policy.drools.system;
 
 import lombok.Getter;
+import org.onap.policy.common.utils.network.NetworkUtil;
 
-public class PolicyEngineConstants {
+public final class PolicyEngineConstants {
 
     /**
      * Default Telemetry Server Port.
@@ -39,6 +40,11 @@ public class PolicyEngineConstants {
      */
     public static final String TELEMETRY_SERVER_DEFAULT_NAME = "TELEMETRY";
 
+    /**
+     * Unique name of this drools-pdp JVM.
+     */
+    public static final String PDP_NAME = NetworkUtil.genUniqueName("drools");
+
     /**
      * Policy Engine Manager.
      */