diff options
4 files changed, 193 insertions, 75 deletions
diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ConfigurationValidator.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ConfigurationValidator.java new file mode 100644 index 0000000..b645ed1 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/ConfigurationValidator.java @@ -0,0 +1,128 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2020 Nokia. 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. + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.sdc.impl; + +import org.onap.sdc.api.consumer.IConfiguration; +import org.onap.sdc.api.consumer.IStatusCallback; +import org.onap.sdc.utils.DistributionActionResultEnum; +import org.onap.sdc.utils.DistributionClientConstants; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; +import java.util.regex.Matcher; + +public class ConfigurationValidator { + + private Map<Function<IConfiguration, Boolean>, DistributionActionResultEnum> cachedValidators; + + DistributionActionResultEnum validateConfiguration(IConfiguration conf, IStatusCallback statusCallback) { + final Map<Function<IConfiguration, Boolean>, DistributionActionResultEnum> validators = getValidators(statusCallback); + + for (Map.Entry<Function<IConfiguration, Boolean>, DistributionActionResultEnum> validation : validators.entrySet()) { + if (isConfigurationNotValid(conf, validation)) { + return getError(validation); + } + } + return DistributionActionResultEnum.SUCCESS; + } + + private synchronized Map<Function<IConfiguration, Boolean>, DistributionActionResultEnum> getValidators(IStatusCallback statusCallback) { + if(this.cachedValidators == null) { + final Map<Function<IConfiguration, Boolean>, DistributionActionResultEnum> validators = new LinkedHashMap<>(); + validators.put(isConfigurationNotDefined(), DistributionActionResultEnum.CONFIGURATION_IS_MISSING); + validators.put(isCustomerIdNotSet(), DistributionActionResultEnum.CONF_MISSING_CONSUMER_ID); + validators.put(isUserNotSet(), DistributionActionResultEnum.CONF_MISSING_USERNAME); + validators.put(isPasswordNotSet(), DistributionActionResultEnum.CONF_MISSING_PASSWORD); + validators.put(isMsgBusAddressNotSet(), DistributionActionResultEnum.CONF_MISSING_MSG_BUS_ADDRESS); + validators.put(isAsdcAddressNotSet(), DistributionActionResultEnum.CONF_MISSING_ASDC_FQDN); + validators.put(isFqdnValid(), DistributionActionResultEnum.CONF_INVALID_ASDC_FQDN); + validators.put(areFqdnsValid(), DistributionActionResultEnum.CONF_INVALID_MSG_BUS_ADDRESS); + validators.put(isEnvNameNotSet(), DistributionActionResultEnum.CONF_MISSING_ENVIRONMENT_NAME); + validators.put(isRelevantArtifactTypesNotSet(), DistributionActionResultEnum.CONF_MISSING_ARTIFACT_TYPES); + validators.put(isConsumeStatusTopicWithCallbackNotSet(statusCallback), DistributionActionResultEnum.CONF_INVALID_CONSUME_PRODUCE_STATUS_TOPIC_FALG); + this.cachedValidators = validators; + } + return this.cachedValidators; + } + + private DistributionActionResultEnum getError(Map.Entry<Function<IConfiguration, Boolean>, DistributionActionResultEnum> validation) { + return validation.getValue(); + } + + private boolean isConfigurationNotValid(IConfiguration conf, Map.Entry<Function<IConfiguration, Boolean>, DistributionActionResultEnum> validation) { + return validation.getKey().apply(conf); + } + + private Function<IConfiguration, Boolean> isConsumeStatusTopicWithCallbackNotSet(IStatusCallback statusCallback) { + return it -> it.isConsumeProduceStatusTopic() && Objects.isNull(statusCallback); + } + + private Function<IConfiguration, Boolean> isRelevantArtifactTypesNotSet() { + return it -> it.getRelevantArtifactTypes() == null || it.getRelevantArtifactTypes().isEmpty(); + } + + private Function<IConfiguration, Boolean> isEnvNameNotSet() { + return it -> it.getEnvironmentName() == null || it.getEnvironmentName().isEmpty(); + } + + private Function<IConfiguration, Boolean> areFqdnsValid() { + return it -> !isValidFqdns(it.getMsgBusAddress()); + } + + private Function<IConfiguration, Boolean> isFqdnValid() { + return it -> !isValidFqdn(it.getAsdcAddress()); + } + + private Function<IConfiguration, Boolean> isMsgBusAddressNotSet() { + return it -> it.getMsgBusAddress() == null || it.getMsgBusAddress().isEmpty(); + } + + private Function<IConfiguration, Boolean> isPasswordNotSet() { + return it -> it.getPassword() == null || it.getPassword().isEmpty(); + } + + private Function<IConfiguration, Boolean> isConfigurationNotDefined() { + return Objects::isNull; + } + + private Function<IConfiguration, Boolean> isUserNotSet() { + return it -> it.getUser() == null || it.getUser().isEmpty(); + } + + private Function<IConfiguration, Boolean> isCustomerIdNotSet() { + return it -> it.getConsumerID() == null || it.getConsumerID().isEmpty(); + } + + private Function<IConfiguration, Boolean> isAsdcAddressNotSet() { + return it -> it.getAsdcAddress() == null || it.getAsdcAddress().isEmpty(); + } + + static boolean isValidFqdn(String fqdn) { + Matcher matcher = DistributionClientConstants.FQDN_PATTERN.matcher(fqdn); + return matcher.matches(); + } + + static boolean isValidFqdns(List<String> fqdns) { + return fqdns.stream().allMatch(ConfigurationValidator::isValidFqdn); + } +} diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java index 8e3aa9b..6e32b96 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java @@ -31,12 +31,10 @@ import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Objects; import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; import org.onap.sdc.api.IDistributionClient; import org.onap.sdc.api.IDistributionStatusMessageJsonBuilder; @@ -96,6 +94,7 @@ public class DistributionClientImpl implements IDistributionClient { private String statusTopic; private boolean isConsumerGroupGenerated = false; private NotificationSender notificationSender; + private final ConfigurationValidator configurationValidator = new ConfigurationValidator(); private boolean isInitialized; private boolean isStarted; @@ -491,41 +490,17 @@ public class DistributionClientImpl implements IDistributionClient { } protected Pair<DistributionActionResultEnum, Configuration> validateAndInitConfiguration(Wrapper<IDistributionClientResult> errorWrapper, IConfiguration conf) { - DistributionActionResultEnum result = DistributionActionResultEnum.SUCCESS; + DistributionActionResultEnum result = configurationValidator.validateConfiguration(conf, statusCallback); + Configuration configuration = null; - if (conf == null) { - result = DistributionActionResultEnum.CONFIGURATION_IS_MISSING; - } else if (conf.getConsumerID() == null || conf.getConsumerID().isEmpty()) { - result = DistributionActionResultEnum.CONF_MISSING_CONSUMER_ID; - } else if (conf.getUser() == null || conf.getUser().isEmpty()) { - result = DistributionActionResultEnum.CONF_MISSING_USERNAME; - } else if (conf.getPassword() == null || conf.getPassword().isEmpty()) { - result = DistributionActionResultEnum.CONF_MISSING_PASSWORD; - } else if (conf.getMsgBusAddress() == null || conf.getMsgBusAddress().isEmpty()) { - result = DistributionActionResultEnum.CONF_MISSING_MSG_BUS_ADDRESS; - } else if (conf.getAsdcAddress() == null || conf.getAsdcAddress().isEmpty()) { - result = DistributionActionResultEnum.CONF_MISSING_ASDC_FQDN; - } else if (!isValidFqdn(conf.getAsdcAddress())) { - result = DistributionActionResultEnum.CONF_INVALID_ASDC_FQDN; - } else if (!isValidFqdns(conf.getMsgBusAddress())) { - result = DistributionActionResultEnum.CONF_INVALID_MSG_BUS_ADDRESS; - } else if (conf.getEnvironmentName() == null || conf.getEnvironmentName().isEmpty()) { - result = DistributionActionResultEnum.CONF_MISSING_ENVIRONMENT_NAME; - } else if (conf.getRelevantArtifactTypes() == null || conf.getRelevantArtifactTypes().isEmpty()) { - result = DistributionActionResultEnum.CONF_MISSING_ARTIFACT_TYPES; - } else if (conf.isConsumeProduceStatusTopic() && Objects.isNull(statusCallback)) { - result = DistributionActionResultEnum.CONF_INVALID_CONSUME_PRODUCE_STATUS_TOPIC_FALG; - } else { // DistributionActionResultEnum.SUCCESS + if(result == DistributionActionResultEnum.SUCCESS) { configuration = createConfiguration(conf); - } - - if (result != DistributionActionResultEnum.SUCCESS) { - + } else { DistributionClientResultImpl initResult = new DistributionClientResultImpl(result, "configuration is invalid: " + result.name()); - log.error(initResult.toString()); errorWrapper.setInnerElement(initResult); } + return new Pair<>(result, configuration); } @@ -556,29 +531,6 @@ public class DistributionClientImpl implements IDistributionClient { return configuration; } - protected boolean isValidFqdn(String fqdn) { - try { - Matcher matcher = DistributionClientConstants.FQDN_PATTERN.matcher(fqdn); - return matcher.matches(); - } catch (Exception e) { - } - return false; - } - - protected boolean isValidFqdns(List<String> fqdns) { - if (fqdns != null && !fqdns.isEmpty()) { - for (String fqdn : fqdns) { - if (isValidFqdn(fqdn)) { - continue; - } else { - return false; - } - } - return true; - } - return false; - } - private void shutdownExecutor() { if (executorPool == null) { return; diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/ConfigurationValidatorTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/ConfigurationValidatorTest.java new file mode 100644 index 0000000..fca3aee --- /dev/null +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/ConfigurationValidatorTest.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications copyright (C) 2020 Nokia. 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. + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.sdc.impl; + +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ConfigurationValidatorTest { + private static final String[] VALID_FQDNS = {"myHostname", "myHostname:80", "myHostname:8080", "1.1.1.1", "1.1.1.1:8080", "ueb01hydc.it.open.com", "ueb01hydc.it.open.com:8080", "ueb01hydc.it", "my-good.and-simple.fqdn"}; + private static final String[] INVALID_FQDNS = {"myHostname:808080", /* 70 letters */"abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij", "not**good", "very#not#good#"}; + + @Test + public void shouldReportThatFqdnIsValid() { + for (String validFqdn : VALID_FQDNS) { + boolean validationResult = ConfigurationValidator.isValidFqdn(validFqdn); + assertEquals("assertion failed for FQDN " + validFqdn + " expected to be valid, actual invalid", true, validationResult); + } + } + + @Test + public void shouldReportThatFqdnIsInvalid() { + for (String invalidFqdn : INVALID_FQDNS) { + boolean validationResult = ConfigurationValidator.isValidFqdn(invalidFqdn); + assertEquals("assertion failed for FQDN " + invalidFqdn + " expected to be invalid, actual valid", false, validationResult); + } + } + @Test + public void shouldReportThatFqdnsAreValid() { + assertTrue(ConfigurationValidator.isValidFqdns(Arrays.asList(VALID_FQDNS))); + } + + @Test + public void shouldReportThatFqdnsAreInvalid() { + assertFalse(ConfigurationValidator.isValidFqdns(Arrays.asList(INVALID_FQDNS))); + } +} diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java index bc4f78e..d5c8ff6 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java @@ -94,27 +94,6 @@ public class DistributionClientTest { } @Test - public void validateConfigurationFqdnTest() { - - String[] validFqdns = { "myHostname", "myHostname:80", "myHostname:8080", "1.1.1.1", "1.1.1.1:8080", "ueb01hydc.it.open.com", "ueb01hydc.it.open.com:8080", "ueb01hydc.it", "my-good.and-simple.fqdn" }; - - String[] invalidFqdns = { "myHostname:808080", /* 70 letters */"abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij", "not**good", "very#not#good#" }; - - boolean validationResult = true; - - for (int i = 0; i < validFqdns.length; i++) { - validationResult = client.isValidFqdn(validFqdns[i]); - assertEquals("assertion failed for FQDN " + validFqdns[i] + " expected to be valid, actual invalid", true, validationResult); - } - - for (int i = 0; i < invalidFqdns.length; i++) { - validationResult = client.isValidFqdn(invalidFqdns[i]); - assertEquals("assertion failed for FQDN " + invalidFqdns[i] + " expected to be invalid, actual valid", false, validationResult); - } - - } - - @Test public void validateConfigurationPasswordTest() { Wrapper<IDistributionClientResult> errorWrapper = new Wrapper<>(); TestConfiguration testPassword = new TestConfiguration(); |