Changed the name of a constant
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / dcae / ConfigFileScanningTask.java
index 88ab637..d24d567 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * 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.
@@ -20,8 +20,9 @@ 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;
 import org.onap.holmes.common.utils.JerseyClient;
 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
@@ -30,27 +31,51 @@ 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<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();
         }
@@ -58,8 +83,6 @@ public class ConfigFileScanningTask implements Runnable {
         try {
             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());
@@ -67,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<RuleResult4API> 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);
                 }
             });
@@ -83,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);
                     }
                 }
@@ -157,7 +177,8 @@ public class ConfigFileScanningTask implements Runnable {
         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;
     }
@@ -179,6 +200,7 @@ public class ConfigFileScanningTask implements Runnable {
         ruleCreateRequest.setContent(contents);
         ruleCreateRequest.setDescription("");
         ruleCreateRequest.setEnabled(1);
+        ruleCreateRequest.setCreator(DEFAULT_CREATOR);
         return ruleCreateRequest;
     }
 
@@ -193,4 +215,8 @@ public class ConfigFileScanningTask implements Runnable {
         }
         return true;
     }
+
+    private String getRequestPref() {
+        return CommonUtils.isHttpsEnabled() ? JerseyClient.PROTOCOL_HTTPS : JerseyClient.PROTOCOL_HTTP;
+    }
 }