Changes to add missing topic parameters 57/91957/6
authora.sreekumar <ajith.sreekumar@est.tech>
Thu, 25 Jul 2019 15:58:58 +0000 (15:58 +0000)
committera.sreekumar <ajith.sreekumar@est.tech>
Thu, 25 Jul 2019 15:58:58 +0000 (15:58 +0000)
Changes to support parameters which would be critical to run in a
secure environment.

Change-Id: Ia7df174261041b35e57b9f1f2be7552e5dcd2cec
Issue-ID: POLICY-1744
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/BusTopicParams.java
policy-endpoints/src/main/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroup.java
policy-endpoints/src/main/java/org/onap/policy/common/endpoints/parameters/TopicParameters.java
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/TopicParameterGroupTest.java
policy-endpoints/src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_all_params.json [new file with mode: 0644]
policy-endpoints/src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_missing_mandatory.json [new file with mode: 0644]
policy-endpoints/src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_valid.json

index b9817ab..9df7221 100644 (file)
@@ -4,6 +4,7 @@
  * ================================================================================
  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  * Modifications Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications 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.
@@ -23,6 +24,8 @@ package org.onap.policy.common.endpoints.event.comm.bus.internal;
 
 import java.util.List;
 import java.util.Map;
+import lombok.Getter;
+import lombok.Setter;
 import org.apache.commons.lang3.StringUtils;
 
 /**
@@ -45,6 +48,8 @@ import org.apache.commons.lang3.StringUtils;
  * useHttps does connection use HTTPS?
  * allowSelfSignedCerts are self-signed certificates allow
  */
+@Getter
+@Setter
 public class BusTopicParams {
 
     private int port;
@@ -79,106 +84,6 @@ public class BusTopicParams {
         return new TopicParamsBuilder();
     }
 
-    public String getPartitionId() {
-        return partitionId;
-    }
-
-    public String getUserName() {
-        return userName;
-    }
-
-    public String getPassword() {
-        return password;
-    }
-
-    public String getEnvironment() {
-        return environment;
-    }
-
-    public String getAftEnvironment() {
-        return aftEnvironment;
-    }
-
-    public String getPartner() {
-        return partner;
-    }
-
-    public String getLatitude() {
-        return latitude;
-    }
-
-    public String getLongitude() {
-        return longitude;
-    }
-
-    public Map<String, String> getAdditionalProps() {
-        return additionalProps;
-    }
-
-    public List<String> getServers() {
-        return servers;
-    }
-
-    public String getTopic() {
-        return topic;
-    }
-
-    public String getEffectiveTopic() {
-        return effectiveTopic;
-    }
-
-    public String getApiKey() {
-        return apiKey;
-    }
-
-    public String getApiSecret() {
-        return apiSecret;
-    }
-
-    public String getConsumerGroup() {
-        return consumerGroup;
-    }
-
-    public String getConsumerInstance() {
-        return consumerInstance;
-    }
-
-    public int getFetchTimeout() {
-        return fetchTimeout;
-    }
-
-    public int getFetchLimit() {
-        return fetchLimit;
-    }
-
-    public String getClientName() {
-        return clientName;
-    }
-
-    public String getHostname() {
-        return hostname;
-    }
-
-    public int getPort() {
-        return port;
-    }
-
-    public String getBasePath() {
-        return basePath;
-    }
-
-    public boolean isManaged() {
-        return managed;
-    }
-
-    public boolean isUseHttps() {
-        return useHttps;
-    }
-
-    public boolean isAllowSelfSignedCerts() {
-        return allowSelfSignedCerts;
-    }
-
     /**
      * Methods to Check if the property is INVALID.
      */
index 3cc2503..70a0603 100644 (file)
@@ -23,7 +23,10 @@ package org.onap.policy.common.endpoints.parameters;
 import java.util.List;
 import lombok.Getter;
 import lombok.Setter;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.policy.common.parameters.GroupValidationResult;
 import org.onap.policy.common.parameters.ParameterGroupImpl;
+import org.onap.policy.common.parameters.ValidationStatus;
 import org.onap.policy.common.parameters.annotations.NotBlank;
 import org.onap.policy.common.parameters.annotations.NotNull;
 
@@ -44,4 +47,29 @@ public class TopicParameterGroup extends ParameterGroupImpl {
     public TopicParameterGroup() {
         super(TopicParameterGroup.class.getSimpleName());
     }
+
+    /**
+     * {@inheritDoc}.
+     */
+    @Override
+    public GroupValidationResult validate() {
+        GroupValidationResult result = super.validate();
+        if (result.isValid() && (checkMissingMandatoryParams(topicSources)
+            || checkMissingMandatoryParams(topicSinks))) {
+            result.setResult(ValidationStatus.INVALID, "Mandatory parameters are missing. topic, servers "
+                + "and topicCommInfrastructure must be specified.");
+        }
+        return result;
+    }
+
+    private boolean checkMissingMandatoryParams(List<TopicParameters> topicParametersList) {
+        for (TopicParameters topicParameters : topicParametersList) {
+            if (StringUtils.isBlank(topicParameters.getTopic())
+                || StringUtils.isBlank(topicParameters.getTopicCommInfrastructure())
+                || topicParameters.getServers().isEmpty()) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
index 82c75d3..d525aaf 100644 (file)
 
 package org.onap.policy.common.endpoints.parameters;
 
-import java.util.List;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.Setter;
+import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
 import org.onap.policy.common.parameters.annotations.NotBlank;
 import org.onap.policy.common.parameters.annotations.NotNull;
 
@@ -36,9 +36,7 @@ import org.onap.policy.common.parameters.annotations.NotNull;
 @NotBlank
 @Getter
 @Setter
-@EqualsAndHashCode
-public class TopicParameters {
-    private String topic;
-    private List<String> servers;
+@EqualsAndHashCode(callSuper = false)
+public class TopicParameters extends BusTopicParams {
     private String topicCommInfrastructure;
 }
index db26a1a..db200c8 100644 (file)
@@ -27,6 +27,9 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.lang.reflect.Method;
+import java.util.List;
+import org.apache.commons.lang3.StringUtils;
 import org.junit.Test;
 import org.onap.policy.common.parameters.GroupValidationResult;
 import org.onap.policy.common.utils.coder.Coder;
@@ -77,6 +80,53 @@ public class TopicParameterGroupTest {
         TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
         final GroupValidationResult result = topicParameterGroup.validate();
         assertFalse(result.isValid());
-        assertTrue(result.getResult().contains("parameter group has status INVALID"));
+        assertTrue(result.getResult().contains("INVALID"));
+    }
+
+    @Test
+    public void test_missing_mandatory_params() throws Exception {
+        String json = testData.getParameterGroupAsString(
+            "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_missing_mandatory.json");
+        TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
+        final GroupValidationResult result = topicParameterGroup.validate();
+        assertTrue(result.getResult().contains("Mandatory parameters are missing"));
+        assertFalse(result.isValid());
+    }
+
+    @Test
+    public void test_allparams() throws Exception {
+        String json = testData.getParameterGroupAsString(
+            "src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_all_params.json");
+        TopicParameterGroup topicParameterGroup = coder.decode(json, TopicParameterGroup.class);
+        final GroupValidationResult result = topicParameterGroup.validate();
+        assertNull(result.getResult());
+        assertTrue(result.isValid());
+        assertTrue(checkIfAllParamsNotEmpty(topicParameterGroup.getTopicSinks()));
+        assertTrue(checkIfAllParamsNotEmpty(topicParameterGroup.getTopicSources()));
+    }
+
+    /**
+     * Method to check if all parameters in TopicParameters are set.
+     * Any parameters added to @link TopicParameters or @link BusTopicParams must be added to
+     * TopicParameters_all_params.json.
+     *
+     * @param topicParameters topic parameters
+     * @return true if all parameters are not empty (if string) or true (if boolean)
+     * @throws Exception the exception
+     */
+    private boolean checkIfAllParamsNotEmpty(List<TopicParameters> topicParametersList) throws Exception {
+        for (TopicParameters topicParameters : topicParametersList) {
+            for (Method m : topicParameters.getClass().getMethods()) {
+                if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
+                    final Object parameter = m.invoke(topicParameters);
+                    if ((parameter instanceof String && StringUtils.isBlank(parameter.toString()))
+                        || (parameter instanceof Boolean && !(Boolean) parameter)
+                        || (parameter instanceof Number && ((Number)parameter).longValue() == 0)) {
+                        return false;
+                    }
+                }
+            }
+        }
+        return true;
     }
 }
diff --git a/policy-endpoints/src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_all_params.json b/policy-endpoints/src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_all_params.json
new file mode 100644 (file)
index 0000000..7d9cce7
--- /dev/null
@@ -0,0 +1,60 @@
+{
+    "topicSources" : [ {
+        "topic" : "POLICY-PDP-PAP1",
+        "servers" : [ "message-router2, message-router3" ],
+        "topicCommInfrastructure" : "dmaap",
+        "effectiveTopic" : "my-effective-topic",
+        "apiKey" : "my-api-key",
+        "apiSecret" : "my-api-secret",
+        "port": 123,
+        "useHttps" : true,
+        "allowSelfSignedCerts" : true,
+        "consumerGroup" : "consumer group",
+        "consumerInstance" : "consumer instance",
+        "fetchTimeout" : 15000,
+        "fetchLimit" : 100,
+        "userName": "username",
+        "password": "password",
+        "managed": true,
+        "environment": "environment1",
+        "aftEnvironment": "aftEnvironment1",
+        "partner": "partner1",
+        "latitude": "1234",
+        "longitude": "1234",
+        "partitionId": "partition_id",
+        "additionalProps": {"xyz":"xyz"},
+        "clientName": "clientName1",
+        "hostname": "hostname1",
+        "basePath": "basePath1",
+        "serializationProvider": "serializationProvider1"
+    }],
+    "topicSinks" : [ {
+        "topic" : "POLICY-PDP-PAP1",
+        "servers" : [ "message-router2, message-router3" ],
+        "topicCommInfrastructure" : "dmaap",
+        "effectiveTopic" : "my-effective-topic",
+        "apiKey" : "my-api-key",
+        "apiSecret" : "my-api-secret",
+        "port": 123,
+        "useHttps" : true,
+        "allowSelfSignedCerts" : true,
+        "consumerGroup" : "consumer group",
+        "consumerInstance" : "consumer instance",
+        "fetchTimeout" : 15000,
+        "fetchLimit" : 100,
+        "userName": "username",
+        "password": "password",
+        "managed": true,
+        "environment": "environment1",
+        "aftEnvironment": "aftEnvironment1",
+        "partner": "partner1",
+        "latitude": "1234",
+        "longitude": "1234",
+        "partitionId": "partition_id",
+        "additionalProps": {"xyz":"xyz"},
+        "clientName": "clientName1",
+        "hostname": "hostname1",
+        "basePath": "basePath1",
+        "serializationProvider": "serializationProvider1"
+    }]
+}
\ No newline at end of file
diff --git a/policy-endpoints/src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_missing_mandatory.json b/policy-endpoints/src/test/resources/org/onap/policy/common/endpoints/parameters/TopicParameters_missing_mandatory.json
new file mode 100644 (file)
index 0000000..157d608
--- /dev/null
@@ -0,0 +1,12 @@
+{
+    "topicSources" : [ {
+        "topic" : "POLICY-PDP-PAP1",
+        "servers" : [],
+        "topicCommInfrastructure" : "dmaap"
+    }],
+    "topicSinks" : [ {
+        "topic" : "POLICY-PDP-PAP2",
+        "servers" : [ "message-router1, message-router2" ],
+        "topicCommInfrastructure" : "dmaap"
+    }]
+}
\ No newline at end of file
index 9222afa..b89f152 100644 (file)
@@ -23,6 +23,8 @@
     },{
         "topic" : "POLICY-PDP-PAP3",
         "servers" : [ "message-router2, message-router3" ],
-        "topicCommInfrastructure" : "dmaap"
+        "topicCommInfrastructure" : "dmaap",
+        "effectiveTopic":"effectiveTopic1",
+        "allowSelfSignedCerts":true
     }]
 }
\ No newline at end of file