summaryrefslogtreecommitdiff
path: root/src/main/java/com/geedge/scheduler/FqdnScheduler.java
diff options
context:
space:
mode:
authorwangwei <[email protected]>2024-01-18 10:14:21 +0800
committerwangwei <[email protected]>2024-01-18 10:14:21 +0800
commit01e97642bc8191dfc80dd13c22511279c272ad52 (patch)
tree59f7e00081e1af619fb935a7bfaf5c1284596a23 /src/main/java/com/geedge/scheduler/FqdnScheduler.java
parentb1fd5dc8152c2bd1d3a5ecb74ea53f183e1d7235 (diff)
适配 CMv23.10版本
Diffstat (limited to 'src/main/java/com/geedge/scheduler/FqdnScheduler.java')
-rw-r--r--src/main/java/com/geedge/scheduler/FqdnScheduler.java100
1 files changed, 95 insertions, 5 deletions
diff --git a/src/main/java/com/geedge/scheduler/FqdnScheduler.java b/src/main/java/com/geedge/scheduler/FqdnScheduler.java
index ec34a6e..b92c6a1 100644
--- a/src/main/java/com/geedge/scheduler/FqdnScheduler.java
+++ b/src/main/java/com/geedge/scheduler/FqdnScheduler.java
@@ -4,6 +4,8 @@ import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
import com.geedge.common.constant.TsgObject;
import com.geedge.common.util.TsgUtil;
import com.google.common.base.Stopwatch;
@@ -18,10 +20,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.TimeZone;
+import java.util.*;
import java.util.concurrent.TimeUnit;
/**
@@ -157,6 +156,22 @@ public class FqdnScheduler {
}
private static void executeUpdate(Integer id, String name, Boolean enable, String sql, Counter counter) {
+ if (TsgUtil.isLatestVersion) {
+ executeUpdateLatest(id, name, enable, sql, counter);
+ } else {
+ executeUpdateOld(id, name, enable, sql, counter);
+ }
+ }
+
+ private static void executeDelete(Integer id, Boolean enable, Integer offsetSecond) {
+ if (TsgUtil.isLatestVersion) {
+ executeDeleteLatest(id, enable, offsetSecond);
+ } else {
+ executeDeleteOld(id, enable, offsetSecond);
+ }
+ }
+
+ private static void executeUpdateLatest(Integer id, String name, Boolean enable, String sql, Counter counter) {
log.info("{}: started update task.", id);
if (BooleanUtil.isFalse(enable)) {
log.warn("{}: interrupted update task. enable: {}", id, enable);
@@ -212,7 +227,58 @@ public class FqdnScheduler {
}
}
- private static void executeDelete(Integer id, Boolean enable, Integer offsetSecond) {
+
+ private static void executeUpdateOld(Integer id, String name, Boolean enable, String sql, Counter counter) {
+ log.info("{}: started update task.", id);
+ if (BooleanUtil.isFalse(enable)) {
+ log.warn("{}: interrupted update task. enable: {}", id, enable);
+ return;
+ }
+ try {
+ Stopwatch watch = Stopwatch.createStarted();
+ List<Record> data = Db.find(sql);
+ log.info("{}: query knowledge base content, cost {} seconds", id, watch.elapsed(TimeUnit.SECONDS));
+ watch.reset().start();
+
+ List<Map<String, Object>> items = Lists.newArrayList();
+ for (Record record : data) {
+ String domain = record.get("domain");
+ List<String> keywordArray = new ArrayList<>();
+ keywordArray.add(domain);
+ JSONObject obj = new JSONObject();
+ obj.set("isHexbin", 0);
+ obj.set(TsgObject.TSG_IS_BUILTIN_V2310, 0);
+ obj.set(TsgObject.TSG_KEYWORDARRAY_V2310, keywordArray);
+ items.add(obj);
+ }
+
+ JSONArray array = new JSONArray();
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.set(TsgObject.TSG_OBJECT_ID_V2310, id);
+ jsonObject.set(TsgObject.TSG_OBJECT_TYPE_V2310, TsgObject.TSG_FQDN_OBJECT_V2310);
+ jsonObject.set(TsgObject.TSG_OBJECT_NAME_V2310, name);
+ jsonObject.set(TsgObject.TSG_IS_BUILTIN_V2310, 0);
+ jsonObject.set(TsgObject.TSG_IS_EXCLUSION_V2310, 0);
+ jsonObject.set(TsgObject.TSG_IS_VALID_V2310, 1);
+ jsonObject.set("addItemList", items);
+ log.info("{}: build api params, items size: {}, cost {} seconds", id, items.size(), watch.elapsed(TimeUnit.SECONDS));
+ if (items.isEmpty()) {
+ return;
+ }
+ array.add(jsonObject);
+ JSONObject body = new JSONObject();
+ body.set(TsgObject.TSG_OP_ACTION_V2310, TsgObject.TSG_UPDATE_V2310);
+ body.set(TsgObject.TSG_OBJECT_LIST_V2310, array);
+ TsgUtil.updateObjectOld(body);
+ counter.inc(items.size());
+ } catch (Exception e) {
+ log.error("{}: failed to execute update task. message: {}", id, e.getMessage());
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ private static void executeDeleteLatest(Integer id, Boolean enable, Integer offsetSecond) {
log.info("{}: started delete task.", id);
if (BooleanUtil.isFalse(enable)) {
log.warn("{}: interrupted delete task. enable: {}", id, enable);
@@ -231,4 +297,28 @@ public class FqdnScheduler {
throw new RuntimeException(e);
}
}
+
+ private static void executeDeleteOld(Integer id, Boolean enable, Integer offsetSecond) {
+ log.info("{}: started delete task.", id);
+ if (BooleanUtil.isFalse(enable)) {
+ log.warn("{}: interrupted delete task. enable: {}", id, enable);
+ return;
+ }
+ try {
+ DateTime dateTime = DateUtil.offsetSecond(new Date(), offsetSecond).setTimeZone(TimeZone.getTimeZone("UTC"));
+ String datetimeZ = DateUtil.format(dateTime, DatePattern.UTC_PATTERN);
+ Map<String, Object> body = new HashMap<>(16);
+ body.put(TsgObject.TSG_DELETE_ITEMS_BYL_TTIME_V2310, datetimeZ);
+ body.put(TsgObject.TSG_ITEM_TYPE_V2310, TsgObject.TSG_FQDN_OBJECT_V2310);
+ body.put(TsgObject.TSG_VSYS_ID_V2310, 1);
+ List<Object> objectIds = new ArrayList<>();
+ objectIds.add(id);
+ body.put(TsgObject.TSG_TSG_OBJECT_IDS_V2310, objectIds);
+ TsgUtil.deleteItemOfObjectOld(body);
+ } catch (Exception e) {
+ log.error("{}: failed to execute delete task. message: {}", id, e.getMessage());
+ throw new RuntimeException(e);
+ }
+ }
+
}