3232import com .park .utmstack .util .UtilJson ;
3333import com .park .utmstack .util .exceptions .UtmNotImplementedException ;
3434import io .grpc .stub .StreamObserver ;
35+ import lombok .RequiredArgsConstructor ;
3536import org .slf4j .Logger ;
3637import org .slf4j .LoggerFactory ;
3738import org .springframework .scheduling .annotation .Async ;
4950import java .util .stream .Collectors ;
5051
5152@ Service
53+ @ RequiredArgsConstructor
5254@ Transactional
5355public class UtmAlertResponseRuleService {
5456
@@ -64,30 +66,9 @@ public class UtmAlertResponseRuleService {
6466 private final UtmAlertResponseRuleExecutionRepository alertResponseRuleExecutionRepository ;
6567 private final UtmIncidentVariableService utmIncidentVariableService ;
6668 private final UtmAlertResponseActionTemplateRepository utmAlertResponseActionTemplateRepository ;
67-
6869 private final UtmAlertResponseActionTemplateService utmAlertResponseActionTemplateService ;
6970
70- public UtmAlertResponseRuleService (UtmAlertResponseRuleRepository alertResponseRuleRepository ,
71- UtmAlertResponseRuleHistoryRepository alertResponseRuleHistoryRepository ,
72- UtmNetworkScanRepository networkScanRepository ,
73- ApplicationEventService eventService ,
74- AgentService agentService ,
75- IncidentResponseCommandService incidentResponseCommandService ,
76- UtmAlertResponseRuleExecutionRepository alertResponseRuleExecutionRepository ,
77- UtmIncidentVariableService utmIncidentVariableService ,
78- UtmAlertResponseActionTemplateRepository utmAlertResponseActionTemplateRepository ,
79- UtmAlertResponseActionTemplateService utmAlertResponseActionTemplateService ) {
80- this .alertResponseRuleRepository = alertResponseRuleRepository ;
81- this .alertResponseRuleHistoryRepository = alertResponseRuleHistoryRepository ;
82- this .networkScanRepository = networkScanRepository ;
83- this .eventService = eventService ;
84- this .agentService = agentService ;
85- this .incidentResponseCommandService = incidentResponseCommandService ;
86- this .alertResponseRuleExecutionRepository = alertResponseRuleExecutionRepository ;
87- this .utmIncidentVariableService = utmIncidentVariableService ;
88- this .utmAlertResponseActionTemplateRepository = utmAlertResponseActionTemplateRepository ;
89- this .utmAlertResponseActionTemplateService = utmAlertResponseActionTemplateService ;
90- }
71+
9172
9273 public UtmAlertResponseRule save (UtmAlertResponseRule alertResponseRule ) {
9374 final String ctx = CLASSNAME + ".save" ;
@@ -96,19 +77,11 @@ public UtmAlertResponseRule save(UtmAlertResponseRule alertResponseRule) {
9677 String alertRuleId = String .valueOf (alertResponseRule .getId ());
9778 UtmAlertResponseRule current = alertResponseRuleRepository .findById (alertResponseRule .getId ())
9879 .orElseThrow (() -> new RuntimeException (String .format ("Incident response rule with ID: %1$s not found" , alertRuleId )));
99- alertResponseRule .mergeInto (current );
80+ this .mergeInto (current , alertResponseRule );
10081 alertResponseRuleHistoryRepository .save (new UtmAlertResponseRuleHistory (new UtmAlertResponseRuleDTO (alertResponseRule )));
10182 alertResponseRule = current ;
10283 }
10384
104- if (alertResponseRule .getUtmAlertResponseActionTemplates () != null ) {
105- for (UtmAlertResponseActionTemplate action : alertResponseRule .getUtmAlertResponseActionTemplates ()) {
106- if (action .getId () == null || !utmAlertResponseActionTemplateRepository .existsById (action .getId ())) {
107- utmAlertResponseActionTemplateService .save (action );
108- }
109- }
110- }
111-
11285 return alertResponseRuleRepository .save (alertResponseRule );
11386 } catch (Exception e ) {
11487 throw new RuntimeException (ctx + ": " + e .getLocalizedMessage ());
@@ -347,4 +320,41 @@ public void onCompleted() {
347320 log .error (msg );
348321 }
349322 }
323+
324+ public void mergeInto (UtmAlertResponseRule target , UtmAlertResponseRule source ) {
325+ target .setRuleName (source .getRuleName ());
326+ target .setRuleDescription (source .getRuleDescription ());
327+ target .setRuleCmd (source .getRuleCmd ());
328+ target .setRuleActive (source .getRuleActive ());
329+ target .setAgentPlatform (source .getAgentPlatform ());
330+ target .setDefaultAgent (source .getDefaultAgent ());
331+ target .setExcludedAgents (source .getExcludedAgents ());
332+ target .setRuleConditions (source .getRuleConditions ());
333+
334+ target .getUtmAlertResponseActionTemplates ().clear ();
335+
336+ if (!source .getUtmAlertResponseActionTemplates ().isEmpty ()) {
337+ List <UtmAlertResponseActionTemplate > managedTemplates = source .getUtmAlertResponseActionTemplates ()
338+ .stream ()
339+ .map (t -> {
340+ if (t .getId () != null ) {
341+ UtmAlertResponseActionTemplate existing = utmAlertResponseActionTemplateRepository .findById (t .getId ())
342+ .orElseThrow (() -> new RuntimeException ("Template not found: " + t .getId ()));
343+ existing .setTitle (t .getTitle ());
344+ existing .setDescription (t .getDescription ());
345+ existing .setCommand (t .getCommand ());
346+ return existing ;
347+ } else {
348+ UtmAlertResponseActionTemplate newT = new UtmAlertResponseActionTemplate ();
349+ newT .setTitle (t .getTitle ());
350+ newT .setDescription (t .getDescription ());
351+ newT .setCommand (t .getCommand ());
352+ return utmAlertResponseActionTemplateService .save (newT );
353+ }
354+ })
355+ .collect (Collectors .toList ());
356+
357+ target .getUtmAlertResponseActionTemplates ().addAll (managedTemplates );
358+ }
359+ }
350360}
0 commit comments