2 * Copyright 2021-2022 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.holmes.rulemgt.dcae;
19 import com.google.gson.JsonArray;
20 import com.google.gson.JsonElement;
21 import com.google.gson.JsonObject;
22 import com.google.gson.JsonParser;
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.holmes.common.ConfigFileScanner;
25 import org.onap.holmes.common.utils.CommonUtils;
26 import org.onap.holmes.common.utils.FileUtils;
27 import org.onap.holmes.common.utils.JerseyClient;
28 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
29 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
30 import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import jakarta.ws.rs.client.Entity;
35 import jakarta.ws.rs.core.MediaType;
37 import java.nio.file.Paths;
39 import java.util.stream.Collectors;
41 public class ConfigFileScanningTask implements Runnable {
42 final public static long POLLING_PERIOD = 30L;
43 final private static Logger LOGGER = LoggerFactory.getLogger(ConfigFileScanningTask.class);
44 final private static long FILE_SIZE_LMT = 1024 * 1024 * 10; // 10MB
45 final private static String DEFAULT_CREATOR = "__SYSTEM__DEFAULT__";
46 private String configFile = "/opt/hrmrules/index.json";
47 private ConfigFileScanner configFileScanner;
50 public ConfigFileScanningTask(ConfigFileScanner configFileScanner) {
51 this.configFileScanner = configFileScanner;
52 this.url = getRequestPref() + "://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
57 List<RuleResult4API> deployedRules = null;
58 boolean isRuleQueryAvailable = true;
61 deployedRules = getExistingRules();
62 } catch (Exception e) {
63 LOGGER.warn("Failed to get existing rules for comparison.", e);
64 isRuleQueryAvailable = false;
67 // If it fails to load rule through API, it means that something must be wrong with the
68 // holmes-rule-mgmt service. Hence, there's no need to go on with remaining steps.
69 if (!isRuleQueryAvailable) {
73 // Contents for configInEffect are <closedControlLoop>:<ruleContents> pairs.
74 Map<String, String> configInEffect = new HashMap();
75 for (RuleResult4API ruleResult4API : deployedRules) {
76 configInEffect.put(ruleResult4API.getLoopControlName(), ruleResult4API.getContent());
79 if (null == configFileScanner) {
80 configFileScanner = new ConfigFileScanner();
84 Map<String, String> newConfig = extractConfigItems(configFileScanner.scan(configFile));
86 // deal with newly added rules
87 final Set<String> existingKeys = new HashSet(configInEffect.keySet());
88 final Set<String> newKeys = new HashSet(newConfig.keySet());
90 .filter(key -> !existingKeys.contains(key))
92 if (deployRule(key, newConfig.get(key))) {
93 LOGGER.info("Rule '{}' has been deployed.", key);
97 // deal with removed rules
98 final List<RuleResult4API> existingRules = deployedRules;
99 existingKeys.stream().filter(key -> !newKeys.contains(key)).forEach(key -> {
100 if (deleteRule(find(existingRules, key))) {
101 LOGGER.info("Rule '{}' has been removed.", key);
105 // deal with changed rules
106 existingKeys.stream().filter(key -> newKeys.contains(key)).forEach(key -> {
107 if (changed(configInEffect.get(key), newConfig.get(key))) {
108 if (deleteRule(find(existingRules, key))) {
109 deployRule(key, newConfig.get(key));
110 LOGGER.info("Rule '{}' has been updated.", key);
114 } catch (Exception e) {
115 LOGGER.warn("Unhandled error: \n" + e.getMessage(), e);
119 private Map<String, String> extractConfigItems(Map<String, String> configFiles) {
120 Map<String, String> ret = new HashMap();
121 for (Map.Entry entry : configFiles.entrySet()) {
122 JsonArray ja = JsonParser.parseString(entry.getValue().toString()).getAsJsonArray();
123 Iterator<JsonElement> iterator = ja.iterator();
124 while (iterator.hasNext()) {
125 JsonObject jo = iterator.next().getAsJsonObject();
126 String contents = readFile(jo.get("file").getAsString());
127 if (StringUtils.isNotBlank(contents)) {
128 ret.put(jo.get("closedControlLoopName").getAsString(), contents);
135 private String normalizePath(String path) {
136 if (!path.startsWith("/")) {
137 return Paths.get(new File(configFile).getParent(), path).toString();
142 private String readFile(String path) {
143 String finalPath = normalizePath(path);
144 File file = new File(finalPath);
145 if (file.exists() && !file.isDirectory() && file.length() <= FILE_SIZE_LMT) {
146 return FileUtils.readTextFile(finalPath);
148 LOGGER.warn("The file {} does not exist or it is a directory or it is too large to load.", finalPath);
153 private RuleResult4API find(final List<RuleResult4API> rules, String clName) {
154 for (RuleResult4API rule : rules) {
155 if (rule.getLoopControlName().equals(clName)) {
162 private boolean changed(String con1, String con2) {
163 // if either of the arguments is null, consider it as invalid and unchanged
164 if (con1 == null || con2 == null) {
168 if (!con1.replaceAll("\\s", StringUtils.EMPTY)
169 .equals(con2.replaceAll("\\s", StringUtils.EMPTY))) {
176 private List<RuleResult4API> getExistingRules() {
177 RuleQueryListResponse ruleQueryListResponse = JerseyClient.newInstance().get(url, RuleQueryListResponse.class);
178 List<RuleResult4API> deployedRules = Collections.EMPTY_LIST;
179 if (null != ruleQueryListResponse) {
180 deployedRules = ruleQueryListResponse.getCorrelationRules()
181 .stream().filter(r -> DEFAULT_CREATOR.equals(r.getCreator())).collect(Collectors.toList());
183 return deployedRules;
186 private boolean deployRule(String clName, String contents) {
187 RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(clName, contents);
188 if (JerseyClient.newInstance().header("Accept", MediaType.APPLICATION_JSON)
189 .put(url, Entity.json(ruleCreateRequest)) == null) {
190 LOGGER.error("Failed to deploy rule: {}.", clName);
196 private RuleCreateRequest getRuleCreateRequest(String clName, String contents) {
197 RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();
198 ruleCreateRequest.setLoopControlName(clName);
199 ruleCreateRequest.setRuleName(clName);
200 ruleCreateRequest.setContent(contents);
201 ruleCreateRequest.setDescription("");
202 ruleCreateRequest.setEnabled(1);
203 ruleCreateRequest.setCreator(DEFAULT_CREATOR);
204 return ruleCreateRequest;
207 private boolean deleteRule(RuleResult4API rule) {
209 LOGGER.info("No rule found, nothing to delete.");
212 if (null == JerseyClient.newInstance().delete(url + "/" + rule.getRuleId())) {
213 LOGGER.warn("Failed to delete rule, the rule id is: {}", rule.getRuleId());
219 private String getRequestPref() {
220 return CommonUtils.isHttpsEnabled() ? JerseyClient.PROTOCOL_HTTPS : JerseyClient.PROTOCOL_HTTP;