From 6652164b384221018bf605b1d5fb809425b26a8e Mon Sep 17 00:00:00 2001 From: jhh Date: Tue, 18 Oct 2022 12:08:45 -0500 Subject: Support optional cluster name in policy engine This allows also for correlation with running pod hostnames in the case of multiple drools pdp flavors coexist. Issue-ID: POLICY-4403 Signed-off-by: jhh Change-Id: I81affeeec622e6c0e9627651bf34c0775a796827 Signed-off-by: jhh --- .../onap/policy/drools/system/PolicyEngine.java | 21 +++++++++++++- .../drools/system/PolicyEngineConstants.java | 7 +---- .../policy/drools/system/PolicyEngineManager.java | 23 ++++++++++++++- .../src/main/server/config/engine.properties | 4 ++- .../drools/system/PolicyEngineManagerTest.java | 33 ++++++++++++++++++++++ .../policy/drools/system/PolicyEngineTest.java | 13 +++++---- .../drools/system/PolicyEngineManagerTest.json | 3 ++ .../policy/drools/system/PolicyEngineTestAdd.json | 3 ++ .../drools/system/PolicyEngineTestConfig.json | 3 ++ 9 files changed, 95 insertions(+), 15 deletions(-) (limited to 'policy-management') diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java index 10623c50..75e7c74a 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngine.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2022 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. @@ -121,6 +121,25 @@ public interface PolicyEngine extends Startable, Lockable, TopicListener { */ String setEnvironmentProperty(String key, String value); + /** + * Gets the hostname used by this PDP-D. + */ + String getHostName(); + + /** + * Gets the cluster name as configured in $CLUSTER_NAME, + * otherwise it will assume an UUID as the cluster name. + */ + String getClusterName(); + + /** + * Gets the PDP Name from hostname and $CLUSTER_NAME, + * otherwise if CLUSTER_NAME is not set, the PdpName + * will be the concatenation of the hostname and a + * UUID. + */ + String getPdpName(); + /** * registers a new Policy Controller with the Policy Engine initialized per properties. * diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineConstants.java b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineConstants.java index 23fc247e..ffd0c858 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineConstants.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineConstants.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * policy-management * ================================================================================ - * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021-2022 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. @@ -43,11 +43,6 @@ public final 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. */ diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineManager.java b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineManager.java index ea4094fc..19aec79a 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineManager.java +++ b/policy-management/src/main/java/org/onap/policy/drools/system/PolicyEngineManager.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Properties; +import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.atomic.AtomicReference; @@ -41,6 +42,8 @@ import java.util.stream.Stream; import lombok.AccessLevel; import lombok.Getter; import lombok.NonNull; +import lombok.Setter; +import lombok.Synchronized; import lombok.ToString; import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.endpoints.event.comm.Topic; @@ -59,6 +62,7 @@ import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.common.gson.annotation.GsonJsonProperty; import org.onap.policy.common.utils.logging.LoggerUtils; +import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.common.utils.resources.PrometheusUtils; import org.onap.policy.common.utils.services.FeatureApiUtils; import org.onap.policy.drools.controller.DroolsControllerConstants; @@ -107,6 +111,8 @@ class PolicyEngineManager implements PolicyEngine { public static final String EXECUTOR_THREAD_PROP = "executor.threads"; protected static final int DEFAULT_EXECUTOR_THREADS = 5; + public static final String CLUSTER_NAME_PROP = "engine.cluster"; + /** * logger. */ @@ -170,6 +176,18 @@ class PolicyEngineManager implements PolicyEngine { @Getter private final PolicyStatsManager stats = new PolicyStatsManager(); + @Getter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + @Setter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + private String clusterName = UUID.randomUUID().toString(); + + @Getter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + @Setter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + private String hostName = NetworkUtil.getHostname(); + + @Getter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + @Setter(onMethod_ = {@Synchronized}, value = AccessLevel.PUBLIC) + private String pdpName; + /** * gson parser to decode configuration requests. */ @@ -368,6 +386,10 @@ class PolicyEngineManager implements PolicyEngine { } this.properties = properties; + if (!StringUtils.isBlank(this.properties.getProperty(CLUSTER_NAME_PROP))) { + this.clusterName = this.properties.getProperty(CLUSTER_NAME_PROP, this.clusterName); + } + this.pdpName = hostName + "." + this.clusterName; try { this.sources = getTopicEndpointManager().addTopicSources(properties); @@ -438,7 +460,6 @@ class PolicyEngineManager implements PolicyEngine { @Override public synchronized PolicyController createPolicyController(String name, Properties properties) { - String tempName = name; // check if a PROPERTY_CONTROLLER_NAME property is present // if so, override the given name diff --git a/policy-management/src/main/server/config/engine.properties b/policy-management/src/main/server/config/engine.properties index 89a749c9..e30682f4 100644 --- a/policy-management/src/main/server/config/engine.properties +++ b/policy-management/src/main/server/config/engine.properties @@ -2,7 +2,7 @@ # ============LICENSE_START======================================================= # ONAP # ================================================================================ -# Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. +# Copyright (C) 2019, 2021-2022 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. @@ -36,3 +36,5 @@ http.server.services.CONFIG.serialization.provider=org.onap.policy.common.gson.J aaf.namespace=${envd:AAF_NAMESPACE:false} aaf.root.permission=${envd:AAF_NAMESPACE:org.onap.policy}.pdpd + +engine.cluster=${envd:CLUSTER_NAME} diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java index 82c28695..fe307f28 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineManagerTest.java @@ -23,6 +23,7 @@ package org.onap.policy.drools.system; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -46,6 +47,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Properties; +import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; import java.util.function.BiConsumer; import java.util.function.Consumer; @@ -330,6 +332,8 @@ public class PolicyEngineManagerTest { @Test public void testSerialize() { + mgr.setHostName("foo"); + mgr.setClusterName("bar"); mgr.configure(properties); assertThatCode(() -> gson.compareGson(mgr, PolicyEngineManagerTest.class)).doesNotThrowAnyException(); } @@ -443,6 +447,35 @@ public class PolicyEngineManagerTest { assertFalse(config.isEmpty()); } + @Test + public void testGetPdpName() { + properties.setProperty(PolicyEngineManager.CLUSTER_NAME_PROP, "east1"); + mgr.configure(properties); + + var pdpName = mgr.getPdpName(); + assertEquals("east1", extractCluster(pdpName)); + assertEquals(mgr.getClusterName(), extractCluster(pdpName)); + assertEquals(mgr.getHostName(), extractHostname(pdpName)); + + mgr.setHostName("foo"); + mgr.setClusterName("bar"); + mgr.setPdpName("foo.bar"); + + pdpName = mgr.getPdpName(); + assertEquals("bar", extractCluster(pdpName)); + assertEquals(mgr.getClusterName(), extractCluster(pdpName)); + assertEquals("foo", extractHostname(pdpName)); + assertEquals(mgr.getHostName(), extractHostname(pdpName)); + } + + private String extractCluster(String name) { + return name.substring(name.lastIndexOf(".") + 1); + } + + private String extractHostname(String name) { + return name.substring(0, name.lastIndexOf(".")); + } + /** * Tests that makeExecutorService() uses the value from the thread * property. diff --git a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java index 0fdc00a6..2054d91d 100644 --- a/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java +++ b/policy-management/src/test/java/org/onap/policy/drools/system/PolicyEngineTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2022 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. @@ -155,17 +155,18 @@ public class PolicyEngineTest { @Test public void test100Configure() { - logger.info("enter"); - - final Properties engineProps = PolicyEngineConstants.getManager().defaultTelemetryConfig(); + var manager = (PolicyEngineManager) PolicyEngineConstants.getManager(); + var engineProps = manager.defaultTelemetryConfig(); /* override default port */ engineProps.put(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + PolicyEngineConstants.TELEMETRY_SERVER_DEFAULT_NAME + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "" + DEFAULT_TELEMETRY_PORT); - assertFalse(PolicyEngineConstants.getManager().isAlive()); - PolicyEngineConstants.getManager().configure(engineProps); + assertFalse(manager.isAlive()); + manager.setHostName("foo"); + manager.setClusterName("0"); + manager.configure(engineProps); assertFalse(PolicyEngineConstants.getManager().isAlive()); logger.info("engine {} has configuration {}", PolicyEngineConstants.getManager(), engineProps); diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json index d2d2075e..4c7e1f03 100644 --- a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json +++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineManagerTest.json @@ -13,6 +13,9 @@ { "port": 1002 } ], "locked": false, + "hostName": "foo", + "clusterName": "bar", + "pdpName": "foo.bar", "sinks": [ { "name": "sink1-topic" }, { "name": "sink2-topic" } diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json index 555f361f..35afaff8 100644 --- a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json +++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestAdd.json @@ -1,6 +1,9 @@ { "alive": true, "locked": false, + "hostName": "foo", + "clusterName": "0", + "pdpName": "foo.0", "sources": [], "sinks": [], "httpServers": [ diff --git a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json index 0e010323..9053c90a 100644 --- a/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json +++ b/policy-management/src/test/resources/org/onap/policy/drools/system/PolicyEngineTestConfig.json @@ -14,6 +14,9 @@ "prometheus": false } ], + "clusterName": "0", + "hostName": "foo", + "pdpName": "foo.0", "features": [], "controllers": [], "stats": { -- cgit 1.2.3-korg