Use new RestClientParameters class in distribution 75/122975/4
authorJim Hahn <jrh3@att.com>
Mon, 19 Jul 2021 20:21:25 +0000 (16:21 -0400)
committerJim Hahn <jrh3@att.com>
Fri, 30 Jul 2021 13:59:53 +0000 (09:59 -0400)
This is a re-do of a previously reverted review.  It's mostly the same
as before, but there are some minor differences.

Replaced LifecycleApiForwarderParameters with RestClientParameters.
Also modified the code to get the HttpClient objects once.
Also removed some unused json test files.

This requires changes to property files in CSITs and OOM:
- add "clientName" to the api & pap structures
- rename "hostName" to "hostname"
- rename "isHttps" to "useHttps" and move it inside the api & pap
  structures

Issue-ID: POLICY-3456
Change-Id: I32b04bc3b6880ec7798814dad268143bc3b06faf
Signed-off-by: Jim Hahn <jrh3@att.com>
15 files changed:
forwarding/src/main/java/org/onap/policy/distribution/forwarding/PolicyForwarder.java
packages/policy-distribution-tarball/src/main/resources/etc/defaultConfig.json
packages/policy-distribution-tarball/src/main/resources/etc/s3pConfig.json
plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParameters.java
plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiParameters.java [deleted file]
plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiPolicyForwarder.java
plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParametersTest.java
plugins/forwarding-plugins/src/test/resources/parameters/ApexPdpPolicyForwarderParameters.json [deleted file]
plugins/forwarding-plugins/src/test/resources/parameters/ApexPdpPolicyForwarderParametersInvalid.json [deleted file]
plugins/forwarding-plugins/src/test/resources/parameters/LifecycleApiPolicyForwarderParameters.json
plugins/forwarding-plugins/src/test/resources/parameters/LifecycleApiPolicyForwarderParametersInvalid.json
plugins/forwarding-plugins/src/test/resources/parameters/XacmlPdpPolicyForwarderParameters.json [deleted file]
plugins/forwarding-plugins/src/test/resources/parameters/XacmlPdpPolicyForwarderParametersInvalid.json [deleted file]
reception/src/main/java/org/onap/policy/distribution/reception/handling/PluginHandler.java
testsuites/stability/src/main/resources/distributionsetup/distribution/etc/defaultConfig.json

index 3059f2f..e4fba0a 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 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 +22,7 @@
 package org.onap.policy.distribution.forwarding;
 
 import java.util.Collection;
+import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
 /**
@@ -40,8 +42,9 @@ public interface PolicyForwarder {
      *
      * @param parameterGroupName the name of the parameter group which contains the configuration
      *        for the policy forwarder
+     * @throws HttpClientConfigException if an exception occurs
      */
-    void configure(String parameterGroupName);
+    void configure(String parameterGroupName) throws HttpClientConfigException;
 
     /**
      * Forward the given policies.
index f5fe30d..02fecfb 100644 (file)
             "parameterClassName": "org.onap.policy.distribution.forwarding.lifecycle.api.LifecycleApiForwarderParameters",
             "parameters": {
                 "apiParameters": {
-                    "hostName": "policy-api",
+                    "clientName": "policy-api",
+                    "hostname": "policy-api",
                     "port": 6969,
+                    "useHttps": true,
                     "userName": "healthcheck",
                     "password": "zb!XztG34"
                 },
                 "papParameters": {
-                    "hostName": "policy-pap",
+                    "clientName": "policy-pap",
+                    "hostname": "policy-pap",
                     "port": 6969,
+                    "useHttps": true,
                     "userName": "healthcheck",
                     "password": "zb!XztG34"
                 },
-                "isHttps": true,
                 "deployPolicies": true
             }
         }
     }
-}
\ No newline at end of file
+}
index 4589fdd..430b81a 100644 (file)
             "parameterClassName": "org.onap.policy.distribution.forwarding.lifecycle.api.LifecycleApiForwarderParameters",
             "parameters": {
                 "apiParameters": {
-                    "hostName": "policy-api",
+                    "clientName": "policy-api",
+                    "hostname": "policy-api",
                     "port": 6969,
+                    "useHttps": true,
                     "userName": "healthcheck",
                     "password": "zb!XztG34"
                 },
                 "papParameters": {
-                    "hostName": "policy-pap",
+                    "clientName": "policy-pap",
+                    "hostname": "policy-pap",
                     "port": 6969,
+                    "useHttps": true,
                     "userName": "healthcheck",
                     "password": "zb!XztG34"
                 },
-                "isHttps": true,
                 "deployPolicies": true
             }
         }
index c266a1b..060be1a 100644 (file)
@@ -22,6 +22,7 @@
 package org.onap.policy.distribution.forwarding.lifecycle.api;
 
 import lombok.Getter;
+import org.onap.policy.common.endpoints.parameters.RestClientParameters;
 import org.onap.policy.common.parameters.annotations.NotBlank;
 import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.common.parameters.annotations.Valid;
@@ -38,10 +39,8 @@ import org.onap.policy.distribution.main.parameters.PolicyForwarderConfiguration
 public class LifecycleApiForwarderParameters extends PolicyForwarderConfigurationParameterGroup {
     public static final String POLICY_FORWARDER_PLUGIN_CLASS = LifecycleApiPolicyForwarder.class.getName();
 
-    private @Valid LifecycleApiParameters apiParameters;
-    private @Valid LifecycleApiParameters papParameters;
-    private boolean isHttps;
-    private boolean allowSelfSignedCerts;
+    private @Valid RestClientParameters apiParameters;
+    private @Valid RestClientParameters papParameters;
     private boolean deployPolicies = true;
 
     public LifecycleApiForwarderParameters() {
diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiParameters.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiParameters.java
deleted file mode 100644 (file)
index 46a7b08..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.distribution.forwarding.lifecycle.api;
-
-import lombok.Getter;
-import org.onap.policy.common.parameters.annotations.Min;
-import org.onap.policy.common.parameters.annotations.NotBlank;
-import org.onap.policy.common.parameters.annotations.NotNull;
-import org.onap.policy.distribution.main.parameters.PolicyForwarderConfigurationParameterGroup;
-
-/**
- * Holds the parameters for the {@link LifecycleApiPolicyForwarder}.
- *
- * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
- */
-@Getter
-@NotNull
-@NotBlank
-public class LifecycleApiParameters extends PolicyForwarderConfigurationParameterGroup {
-
-    private String hostName;
-    @Min(value = 1)
-    private int port;
-    private String userName;
-    private String password;
-
-    public LifecycleApiParameters() {
-        super(LifecycleApiParameters.class.getSimpleName());
-    }
-}
index b6f41fb..8640332 100644 (file)
@@ -31,7 +31,6 @@ import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
-import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
 import org.onap.policy.common.endpoints.http.client.HttpClient;
 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
@@ -57,14 +56,20 @@ public class LifecycleApiPolicyForwarder implements PolicyForwarder {
     private static final String DEPLOY_POLICY_URI = "/policy/pap/v1/pdps/policies";
     private static final String CREATE_POLICY_TYPE_URI = "/policy/api/v1/policytypes/";
     private static final Logger LOGGER = LoggerFactory.getLogger(LifecycleApiPolicyForwarder.class);
+
     private LifecycleApiForwarderParameters forwarderParameters;
+    private HttpClient apiClient;
+    private HttpClient papClient;
 
     /**
      * {@inheritDoc}.
      */
     @Override
-    public void configure(final String parameterGroupName) {
+    public void configure(final String parameterGroupName) throws HttpClientConfigException {
         forwarderParameters = ParameterService.get(parameterGroupName);
+
+        apiClient = HttpClientFactoryInstance.getClientFactory().build(forwarderParameters.getApiParameters());
+        papClient = HttpClientFactoryInstance.getClientFactory().build(forwarderParameters.getPapParameters());
     }
 
     /**
@@ -138,32 +143,19 @@ public class LifecycleApiPolicyForwarder implements PolicyForwarder {
 
     private Response invokeHttpClient(final Entity<?> entity, final String path, final boolean wantApi)
             throws PolicyForwardingException {
-        Response response = null;
-        try {
-            response = getHttpClient(wantApi).post(path, entity, Map.of(HttpHeaders.ACCEPT,
-                    MediaType.APPLICATION_JSON, HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON));
-            if (response.getStatus() / 100 != 2) {
-                LOGGER.error(
-                        "Invocation of path {} failed for entity {}. Response status: {}, Response status info: {}",
-                        path, entity, response.getStatus(), response.getStatusInfo());
-                throw new PolicyForwardingException("Failed creating the entity - " + entity);
-            }
-        } catch (final HttpClientConfigException exception) {
-            throw new PolicyForwardingException("Invocation of path " + path + " failed for entity " + entity,
-                    exception);
+        Response response = getHttpClient(wantApi).post(path, entity, Map.of(HttpHeaders.ACCEPT,
+                        MediaType.APPLICATION_JSON, HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON));
+        if (response.getStatus() / 100 != 2) {
+            LOGGER.error(
+                    "Invocation of path {} failed for entity {}. Response status: {}, Response status info: {}",
+                    path, entity, response.getStatus(), response.getStatusInfo());
+            throw new PolicyForwardingException("Failed creating the entity - " + entity);
         }
         return response;
     }
 
-    private HttpClient getHttpClient(final boolean wantApi) throws HttpClientConfigException {
-        final boolean https = forwarderParameters.isHttps();
-        final LifecycleApiParameters parameters =
-                (wantApi ? forwarderParameters.getApiParameters() : forwarderParameters.getPapParameters());
-        final BusTopicParams params = BusTopicParams.builder().clientName("Policy Distribution").useHttps(https)
-                .hostname(parameters.getHostName()).port(parameters.getPort()).userName(parameters.getUserName())
-                .password(parameters.getPassword()).allowSelfSignedCerts(forwarderParameters.isAllowSelfSignedCerts())
-                .build();
-        return HttpClientFactoryInstance.getClientFactory().build(params);
+    private HttpClient getHttpClient(final boolean wantApi) {
+        return (wantApi ? apiClient : papClient);
     }
 }
 
index 0f010bf..04bcbea 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 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.
@@ -20,6 +21,7 @@
 
 package org.onap.policy.distribution.forwarding.lifecycle.api;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
@@ -54,17 +56,19 @@ public class LifecycleApiForwarderParametersTest {
 
         assertEquals(LifecycleApiForwarderParameters.class.getSimpleName(),
                 configurationParameters.getName());
-        assertFalse(configurationParameters.isHttps());
         assertTrue(configurationParameters.isDeployPolicies());
-        assertEquals(POLICY_API_HOST_NAME, configurationParameters.getApiParameters().getHostName());
+        assertEquals(POLICY_API_HOST_NAME, configurationParameters.getApiParameters().getHostname());
         assertEquals(POLICY_API_PORT, configurationParameters.getApiParameters().getPort());
+        assertFalse(configurationParameters.getApiParameters().isUseHttps());
         assertEquals(POLICY_API_USER, configurationParameters.getApiParameters().getUserName());
         assertEquals(POLICY_API_PASSWORD, configurationParameters.getApiParameters().getPassword());
-        assertEquals(POLICY_PAP_HOST_NAME, configurationParameters.getPapParameters().getHostName());
+        assertEquals(POLICY_PAP_HOST_NAME, configurationParameters.getPapParameters().getHostname());
         assertEquals(POLICY_PAP_PORT, configurationParameters.getPapParameters().getPort());
+        assertFalse(configurationParameters.getPapParameters().isUseHttps());
         assertEquals(POLICY_PAP_USER, configurationParameters.getPapParameters().getUserName());
         assertEquals(POLICY_PAP_PASSWORD, configurationParameters.getPapParameters().getPassword());
 
+        assertThat(configurationParameters.validate().getResult()).isNull();
         assertEquals(ValidationStatus.CLEAN, configurationParameters.validate().getStatus());
     }
 
diff --git a/plugins/forwarding-plugins/src/test/resources/parameters/ApexPdpPolicyForwarderParameters.json b/plugins/forwarding-plugins/src/test/resources/parameters/ApexPdpPolicyForwarderParameters.json
deleted file mode 100644 (file)
index d244e0b..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-{
-    "hostname":"10.10.10.10",
-    "port":"1234",
-    "ignoreConflicts": false,
-    "forceUpdate": true
-}
\ No newline at end of file
diff --git a/plugins/forwarding-plugins/src/test/resources/parameters/ApexPdpPolicyForwarderParametersInvalid.json b/plugins/forwarding-plugins/src/test/resources/parameters/ApexPdpPolicyForwarderParametersInvalid.json
deleted file mode 100644 (file)
index c87b776..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-{
-    "hostname":"",
-    "port":"-1",
-    "ignoreConflicts": false,
-    "forceUpdate": true
-}
index 0791e1a..ba6058b 100644 (file)
@@ -1,16 +1,19 @@
 {
   "apiParameters": {
-    "hostName": "0.0.0.0",
+    "clientName": "policy-api",
+    "hostname": "0.0.0.0",
     "port": 6969,
+    "useHttps": false,
     "userName": "healthcheck",
     "password": "zb!XztG34"
   },
   "papParameters": {
-    "hostName": "0.0.0.0",
+    "clientName": "policy-pap",
+    "hostname": "0.0.0.0",
     "port": 6969,
+    "useHttps": false,
     "userName": "healthcheck",
     "password": "zb!XztG34"
   },
-  "isHttps": false,
   "deployPolicies": true
-}
\ No newline at end of file
+}
index ca0558a..88dd74b 100644 (file)
@@ -1,16 +1,19 @@
 {
   "apiParameters": {
-    "hostName": "",
+    "clientName": "policy-api",
+    "hostname": "",
     "port": -1,
+    "useHttps": false,
     "userName": "healthcheck",
     "password": "zb!XztG34"
   },
   "papParameters": {
-    "hostName": "",
+    "clientName": "policy-pap",
+    "hostname": "",
     "port": -2,
+    "useHttps": false,
     "userName": "healthcheck",
     "password": "zb!XztG34"
   },
-  "isHttps": false,
   "deployPolicies": true
-}
\ No newline at end of file
+}
diff --git a/plugins/forwarding-plugins/src/test/resources/parameters/XacmlPdpPolicyForwarderParameters.json b/plugins/forwarding-plugins/src/test/resources/parameters/XacmlPdpPolicyForwarderParameters.json
deleted file mode 100644 (file)
index 0e115ef..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-{
-    "useHttps": true,
-    "hostname": "10.10.10.10",
-    "port": 1234,
-    "userName": "myUser",
-    "password": "myPassword",
-    "clientAuth": "myClientAuth",
-    "isManaged": false,
-    "pdpGroup": "myPdpGroup"
-}
\ No newline at end of file
diff --git a/plugins/forwarding-plugins/src/test/resources/parameters/XacmlPdpPolicyForwarderParametersInvalid.json b/plugins/forwarding-plugins/src/test/resources/parameters/XacmlPdpPolicyForwarderParametersInvalid.json
deleted file mode 100644 (file)
index 03e342b..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-{
-    "useHttps": true,
-    "hostname": "",
-    "port": -2,
-    "userName": "",
-    "password": "",
-    "clientAuth": "myClientAuth",
-    "isManaged": false,
-    "pdpGroup": "default"
-}
\ No newline at end of file
index c2ea800..ecd40e9 100644 (file)
@@ -26,6 +26,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Map;
+import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
 import org.onap.policy.common.parameters.ParameterService;
 import org.onap.policy.distribution.forwarding.PolicyForwarder;
 import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters;
@@ -120,7 +121,7 @@ public class PluginHandler {
                 policyForwarders.add(policyForwarder);
             } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException
                     | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
-                    | SecurityException exp) {
+                    | SecurityException | HttpClientConfigException exp) {
                 throw new PluginInitializationException(exp.getMessage(), exp.getCause());
             }
         }
index 98a135c..ec36c7a 100644 (file)
             "parameterClassName": "org.onap.policy.distribution.forwarding.lifecycle.api.LifecycleApiForwarderParameters",
             "parameters": {
                 "apiParameters": {
-                    "hostName": "policy-api",
+                    "clientName": "policy-api",
+                    "hostname": "policy-api",
                     "port": 6969,
+                    "useHttps": true,
                     "userName": "healthcheck",
                     "password": "zb!XztG34"
                 },
                 "papParameters": {
-                    "hostName": "policy-pap",
+                    "clientName": "policy-pap",
+                    "hostname": "policy-pap",
                     "port": 7000,
+                    "useHttps": true,
                     "userName": "healthcheck",
                     "password": "zb!XztG34"
                 },
-                "isHttps": true,
                 "deployPolicies": true
             }
         }