/**
- * Copyright 2021 ZTE Corporation.
+ * Copyright 2021-2022 ZTE Corporation.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
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;
import org.onap.holmes.common.utils.JerseyClient;
import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
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<String, String> configInEffect = new HashMap(); // Contents for configInEffect are <closedControlLoop>:<ruleContents> pairs.
+ final private static String DEFAULT_CREATOR = "__SYSTEM__DEFAULT__";
private String configFile = "/opt/hrmrules/index.json";
private ConfigFileScanner configFileScanner;
- private String url = "https://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
+ private String url;
public ConfigFileScanningTask(ConfigFileScanner configFileScanner) {
this.configFileScanner = configFileScanner;
+ this.url = getRequestPref() + "://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
}
@Override
public void run() {
+ List<RuleResult4API> 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 <closedControlLoop>:<ruleContents> pairs.
+ Map<String, String> configInEffect = new HashMap();
+ for (RuleResult4API ruleResult4API : deployedRules) {
+ configInEffect.put(ruleResult4API.getLoopControlName(), ruleResult4API.getContent());
+ }
+
if (null == configFileScanner) {
configFileScanner = new ConfigFileScanner();
}
- Map<String, String> newConfig = extractConfigItems(configFileScanner.scan(configFile));
-
- List<RuleResult4API> deployedRules = getExistingRules();
-
- // deal with newly added rules
- final Set<String> existingKeys = new HashSet(configInEffect.keySet());
- final Set<String> newKeys = new HashSet(newConfig.keySet());
- newKeys.stream()
- .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
- existingKeys.stream().filter(key -> !newKeys.contains(key)).forEach(key -> {
- if (deleteRule(find(deployedRules, key))) {
- configInEffect.remove(key);
- LOGGER.info("Rule '{}' has been removed.", key);
- }
- });
-
- // 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);
- deployRule(key, newConfig.get(key));
- configInEffect.put(key, newConfig.get(key));
- LOGGER.info("Rule '{}' has been updated.", key);
+ try {
+ Map<String, String> newConfig = extractConfigItems(configFileScanner.scan(configFile));
+
+ // deal with newly added rules
+ final Set<String> existingKeys = new HashSet(configInEffect.keySet());
+ final Set<String> newKeys = new HashSet(newConfig.keySet());
+ newKeys.stream()
+ .filter(key -> !existingKeys.contains(key))
+ .forEach(key -> {
+ if (deployRule(key, newConfig.get(key))) {
+ LOGGER.info("Rule '{}' has been deployed.", key);
+ }
+ });
+
+ // deal with removed rules
+ final List<RuleResult4API> existingRules = deployedRules;
+ existingKeys.stream().filter(key -> !newKeys.contains(key)).forEach(key -> {
+ if (deleteRule(find(existingRules, key))) {
+ LOGGER.info("Rule '{}' has been removed.", key);
}
- }
- });
+ });
+
+ // deal with changed rules
+ existingKeys.stream().filter(key -> newKeys.contains(key)).forEach(key -> {
+ if (changed(configInEffect.get(key), newConfig.get(key))) {
+ if (deleteRule(find(existingRules, key))) {
+ deployRule(key, newConfig.get(key));
+ LOGGER.info("Rule '{}' has been updated.", key);
+ }
+ }
+ });
+ } catch (Exception e) {
+ LOGGER.warn("Unhandled error: \n" + e.getMessage(), e);
+ }
}
private Map<String, String> extractConfigItems(Map<String, String> configFiles) {
}
return path;
}
+
private String readFile(String path) {
String finalPath = normalizePath(path);
File file = new File(finalPath);
RuleQueryListResponse ruleQueryListResponse = JerseyClient.newInstance().get(url, RuleQueryListResponse.class);
List<RuleResult4API> 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;
}
ruleCreateRequest.setContent(contents);
ruleCreateRequest.setDescription("");
ruleCreateRequest.setEnabled(1);
+ ruleCreateRequest.setCreator(DEFAULT_CREATOR);
return ruleCreateRequest;
}
}
return true;
}
+
+ private String getRequestPref() {
+ return CommonUtils.isHttpsEnabled() ? JerseyClient.PROTOCOL_HTTPS : JerseyClient.PROTOCOL_HTTP;
+ }
}