From c9cdd245b2b575164a11785ab7a4710395f950c7 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Thu, 14 Feb 2019 10:47:49 +0000 Subject: Refactor rule manager key generation Simplify the generateKey() method by using a Java List instead of an Array. This makes the code easier to read. Change-Id: Ida772d33af5a244bafa19b6fff465fd590ac6d1a Issue-ID: AAI-2057 Signed-off-by: mark.j.leonard --- .../validation/ruledriven/RuleDrivenValidator.java | 7 +++-- .../aai/validation/ruledriven/RuleManager.java | 34 +++++++++------------- .../configuration/RulesConfigurationLoader.groovy | 2 +- 3 files changed, 18 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/main/java/org/onap/aai/validation/ruledriven/RuleDrivenValidator.java b/src/main/java/org/onap/aai/validation/ruledriven/RuleDrivenValidator.java index 57ace7d..4b22d77 100644 --- a/src/main/java/org/onap/aai/validation/ruledriven/RuleDrivenValidator.java +++ b/src/main/java/org/onap/aai/validation/ruledriven/RuleDrivenValidator.java @@ -9,7 +9,7 @@ * 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 + * 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, @@ -230,7 +230,8 @@ public class RuleDrivenValidator implements Validator { if (!rulesDefined && ruleIndexingConfig.isPresent()) { final String defaultIndexKey = ruleIndexingConfig.get().getDefaultIndexKey(); if (!StringUtils.isEmpty(defaultIndexKey)) { - return ruleManager.getRulesForEntity(RuleManager.generateKey(new String[] {defaultIndexKey})); + return ruleManager + .getRulesForEntity(RuleManager.generateKey(Collections.singletonList(defaultIndexKey))); } else { applicationLogger.debug("Default index value not configured, unable to get rules"); applicationLogger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, eventType); @@ -252,7 +253,7 @@ public class RuleDrivenValidator implements Validator { Map valuesMap = entity.getAttributeValues(ruleIndexingConfig.get().getIndexAttributes()).generateReport(); applicationLogger.debug("Generating index using attributes: " + valuesMap); - return RuleManager.generateKey(valuesMap.values().stream().toArray(String[]::new)); + return RuleManager.generateKey(valuesMap.values()); } catch (ValidationServiceException e) { applicationLogger.debug("Failed to retrieve index key attributes from event: " + e.getMessage()); applicationLogger.error(ApplicationMsgs.CANNOT_VALIDATE_ERROR, e, eventType); diff --git a/src/main/java/org/onap/aai/validation/ruledriven/RuleManager.java b/src/main/java/org/onap/aai/validation/ruledriven/RuleManager.java index 380c99a..6c67341 100644 --- a/src/main/java/org/onap/aai/validation/ruledriven/RuleManager.java +++ b/src/main/java/org/onap/aai/validation/ruledriven/RuleManager.java @@ -1,32 +1,33 @@ -/* - * ============LICENSE_START=================================================== +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved. * Copyright (c) 2018-2019 European Software Marketing Ltd. - * ============================================================================ + * ================================================================================ * 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 + * 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===================================================== + * ============LICENSE_END========================================================= */ package org.onap.aai.validation.ruledriven; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.SortedSet; -import java.util.TreeSet; +import java.util.stream.Collectors; import org.onap.aai.validation.ruledriven.configuration.EntitySection; import org.onap.aai.validation.ruledriven.configuration.GroovyConfigurationException; import org.onap.aai.validation.ruledriven.configuration.RuleSection; @@ -47,7 +48,7 @@ public class RuleManager { * Create the rules for each type of entity based on the supplied configuration * * @param entities - * configuration (all entities) + * configuration (all entities) * @throws InstantiationException * @throws IllegalAccessException * @throws GroovyConfigurationException @@ -77,16 +78,7 @@ public class RuleManager { return Optional.ofNullable(rulesMap.get(entityType)); } - public static String generateKey(String[] indices) { - SortedSet sortedIndices = new TreeSet<>(); - Collections.addAll(sortedIndices, indices); - StringBuilder sb = new StringBuilder(); - Iterator iterator = sortedIndices.iterator(); - while (iterator.hasNext()) { - sb.append("["); - sb.append(iterator.next()); - sb.append("]"); - } - return sb.toString(); + public static String generateKey(Collection collection) { + return collection.stream().sorted().map(String::valueOf).collect(Collectors.joining("][", "[", "]")); } } diff --git a/src/main/java/org/onap/aai/validation/ruledriven/configuration/RulesConfigurationLoader.groovy b/src/main/java/org/onap/aai/validation/ruledriven/configuration/RulesConfigurationLoader.groovy index 5fabde9..6d8aa6e 100644 --- a/src/main/java/org/onap/aai/validation/ruledriven/configuration/RulesConfigurationLoader.groovy +++ b/src/main/java/org/onap/aai/validation/ruledriven/configuration/RulesConfigurationLoader.groovy @@ -185,7 +185,7 @@ class IndexingDelegate { void indices(String... indices) { this.configuration.setIndices indices - def index = RuleManager.generateKey(indices) + def index = RuleManager.generateKey(indices.toList()) this.configuration.type = index this.configuration.getRules().each { rule -> rule.setObject configuration.type -- cgit 1.2.3-korg