X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=rulemgt%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Frulemgt%2Fdcae%2FConfigFileScanningTask.java;h=d24d567a49bb7df1e3813f5bd65e3157a7b979ff;hb=1915e1c1948fd4252ad6caab9468ea88d24b869e;hp=884ea311bc8a731a100ffaf2e53100e023464ade;hpb=bf9883c5347a94e61c79b5f9d9bc329a1b0e7c4e;p=holmes%2Frule-management.git diff --git a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTask.java b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTask.java index 884ea31..d24d567 100644 --- a/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTask.java +++ b/rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/ConfigFileScanningTask.java @@ -1,5 +1,5 @@ /** - * Copyright 2021 ZTE Corporation. + * Copyright 2021-2022 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.onap.holmes.common.ConfigFileScanner; import org.onap.holmes.common.utils.CommonUtils; import org.onap.holmes.common.utils.FileUtils; @@ -31,17 +31,18 @@ import org.onap.holmes.rulemgt.bean.response.RuleResult4API; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.MediaType; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.core.MediaType; import java.io.File; import java.nio.file.Paths; import java.util.*; +import java.util.stream.Collectors; public class ConfigFileScanningTask implements Runnable { final public static long POLLING_PERIOD = 30L; final private static Logger LOGGER = LoggerFactory.getLogger(ConfigFileScanningTask.class); final private static long FILE_SIZE_LMT = 1024 * 1024 * 10; // 10MB - final private Map configInEffect = new HashMap(); // Contents for configInEffect are : pairs. + final private static String DEFAULT_CREATOR = "__SYSTEM__DEFAULT__"; private String configFile = "/opt/hrmrules/index.json"; private ConfigFileScanner configFileScanner; private String url; @@ -53,6 +54,28 @@ public class ConfigFileScanningTask implements Runnable { @Override public void run() { + List deployedRules = null; + boolean isRuleQueryAvailable = true; + + try { + deployedRules = getExistingRules(); + } catch (Exception e) { + LOGGER.warn("Failed to get existing rules for comparison.", e); + isRuleQueryAvailable = false; + } + + // If it fails to load rule through API, it means that something must be wrong with the + // holmes-rule-mgmt service. Hence, there's no need to go on with remaining steps. + if (!isRuleQueryAvailable) { + return; + } + + // Contents for configInEffect are : pairs. + Map configInEffect = new HashMap(); + for (RuleResult4API ruleResult4API : deployedRules) { + configInEffect.put(ruleResult4API.getLoopControlName(), ruleResult4API.getContent()); + } + if (null == configFileScanner) { configFileScanner = new ConfigFileScanner(); } @@ -60,8 +83,6 @@ public class ConfigFileScanningTask implements Runnable { try { Map newConfig = extractConfigItems(configFileScanner.scan(configFile)); - List deployedRules = getExistingRules(); - // deal with newly added rules final Set existingKeys = new HashSet(configInEffect.keySet()); final Set newKeys = new HashSet(newConfig.keySet()); @@ -69,15 +90,14 @@ public class ConfigFileScanningTask implements Runnable { .filter(key -> !existingKeys.contains(key)) .forEach(key -> { if (deployRule(key, newConfig.get(key))) { - configInEffect.put(key, newConfig.get(key)); LOGGER.info("Rule '{}' has been deployed.", key); } }); // deal with removed rules + final List existingRules = deployedRules; existingKeys.stream().filter(key -> !newKeys.contains(key)).forEach(key -> { - if (deleteRule(find(deployedRules, key))) { - configInEffect.remove(key); + if (deleteRule(find(existingRules, key))) { LOGGER.info("Rule '{}' has been removed.", key); } }); @@ -85,10 +105,8 @@ public class ConfigFileScanningTask implements Runnable { // deal with changed rules existingKeys.stream().filter(key -> newKeys.contains(key)).forEach(key -> { if (changed(configInEffect.get(key), newConfig.get(key))) { - if (deleteRule(find(deployedRules, key))) { - configInEffect.remove(key); + if (deleteRule(find(existingRules, key))) { deployRule(key, newConfig.get(key)); - configInEffect.put(key, newConfig.get(key)); LOGGER.info("Rule '{}' has been updated.", key); } } @@ -159,7 +177,8 @@ public class ConfigFileScanningTask implements Runnable { RuleQueryListResponse ruleQueryListResponse = JerseyClient.newInstance().get(url, RuleQueryListResponse.class); List deployedRules = Collections.EMPTY_LIST; if (null != ruleQueryListResponse) { - deployedRules = ruleQueryListResponse.getCorrelationRules(); + deployedRules = ruleQueryListResponse.getCorrelationRules() + .stream().filter(r -> DEFAULT_CREATOR.equals(r.getCreator())).collect(Collectors.toList()); } return deployedRules; } @@ -181,6 +200,7 @@ public class ConfigFileScanningTask implements Runnable { ruleCreateRequest.setContent(contents); ruleCreateRequest.setDescription(""); ruleCreateRequest.setEnabled(1); + ruleCreateRequest.setCreator(DEFAULT_CREATOR); return ruleCreateRequest; }