Remove deprecated DMAAP dependency
[policy/drools-applications.git] / controlloop / common / eventmanager / src / main / java / org / onap / policy / controlloop / eventmanager / ClEventManagerWithEvent.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.eventmanager;
23
24 import java.io.Serial;
25 import java.util.HashMap;
26 import lombok.AccessLevel;
27 import lombok.Getter;
28 import lombok.Setter;
29 import org.apache.commons.lang3.StringUtils;
30 import org.drools.core.WorkingMemory;
31 import org.onap.policy.controlloop.ControlLoopEventStatus;
32 import org.onap.policy.controlloop.ControlLoopException;
33 import org.onap.policy.controlloop.ControlLoopResponse;
34 import org.onap.policy.controlloop.VirtualControlLoopEvent;
35 import org.onap.policy.controlloop.VirtualControlLoopNotification;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
37 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Manager for a single control loop event. Once this has been created, the event can be
43  * retracted from working memory.
44  */
45 @Getter
46 public abstract class ClEventManagerWithEvent<T extends Step> extends ClEventManagerWithOutcome<T>
47                 implements StepContext {
48
49     private static final Logger logger = LoggerFactory.getLogger(ClEventManagerWithEvent.class);
50     @Serial
51     private static final long serialVersionUID = -1216568161322872641L;
52
53     public enum NewEventStatus {
54         FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
55     }
56
57     private final VirtualControlLoopEvent event;
58
59     @Setter(AccessLevel.PROTECTED)
60     private VirtualControlLoopEvent abatement = null;
61
62
63     /**
64      * Constructs the object.
65      *
66      * @param services services the manager should use when processing the event
67      * @param params control loop parameters
68      * @param event event to be managed by this object
69      * @param workMem working memory to update if this changes
70      * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
71      *         be created
72      */
73     protected ClEventManagerWithEvent(EventManagerServices services, ControlLoopParams params,
74                     VirtualControlLoopEvent event, WorkingMemory workMem) throws ControlLoopException {
75
76         super(services, params, event.getRequestId(), workMem);
77
78         checkEventSyntax(event);
79
80         this.event = event;
81     }
82
83     @Override
84     protected void populateNotification(VirtualControlLoopNotification notif) {
85         super.populateNotification(notif);
86
87         notif.setClosedLoopControlName(event.getClosedLoopControlName());
88         notif.setRequestId(event.getRequestId());
89         notif.setClosedLoopEventClient(event.getClosedLoopEventClient());
90         notif.setTargetType(event.getTargetType());
91         notif.setTarget(event.getTarget());
92
93         if (event.getAai() != null) {
94             notif.setAai(new HashMap<>(event.getAai()));
95         }
96         notif.setClosedLoopAlarmStart(event.getClosedLoopAlarmStart());
97         notif.setClosedLoopAlarmEnd(event.getClosedLoopAlarmEnd());
98     }
99
100     /**
101      * Stores an operation outcome in the DB.
102      *
103      * @param outcome operation outcome to store
104      * @param targetEntity target entity
105      */
106     protected void storeInDataBase(OperationOutcome2 outcome, String targetEntity) {
107         getDataManager().store(getRequestIdStr(), event.getClosedLoopControlName(), event, targetEntity,
108                         outcome.getClOperation());
109     }
110
111     @Override
112     public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
113         var clRsp = super.makeControlLoopResponse(outcome);
114         clRsp.setTarget("DCAE");
115
116         clRsp.setClosedLoopControlName(event.getClosedLoopControlName());
117         clRsp.setPolicyName(event.getPolicyName());
118         clRsp.setPolicyVersion(event.getPolicyVersion());
119         clRsp.setRequestId(event.getRequestId());
120         clRsp.setVersion(event.getVersion());
121
122         return clRsp;
123     }
124
125     /**
126      * An event onset/abatement.
127      *
128      * @param newEvent the event
129      * @return the status
130      */
131     public NewEventStatus onNewEvent(VirtualControlLoopEvent newEvent) {
132         try {
133             checkEventSyntax(newEvent);
134
135             if (newEvent.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
136                 if (newEvent.equals(event)) {
137                     return NewEventStatus.FIRST_ONSET;
138                 }
139
140                 bumpOffsets();
141                 return NewEventStatus.SUBSEQUENT_ONSET;
142
143             } else {
144                 if (abatement == null) {
145                     abatement = newEvent;
146                     bumpAbatements();
147                     return NewEventStatus.FIRST_ABATEMENT;
148                 } else {
149                     bumpAbatements();
150                     return NewEventStatus.SUBSEQUENT_ABATEMENT;
151                 }
152             }
153         } catch (ControlLoopException e) {
154             logger.error("{}: onNewEvent threw an exception", this, e);
155             return NewEventStatus.SYNTAX_ERROR;
156         }
157     }
158
159     /**
160      * Check an event syntax.
161      *
162      * @param event the event syntax
163      * @throws ControlLoopException if an error occurs
164      */
165     protected void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
166         validateStatus(event);
167         if (StringUtils.isBlank(event.getClosedLoopControlName())) {
168             throw new ControlLoopException("No control loop name");
169         }
170         if (event.getRequestId() == null) {
171             throw new ControlLoopException("No request ID");
172         }
173         if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
174             return;
175         }
176         validateTarget(event);
177     }
178
179     /**
180      * Verifies that the event status is valid.
181      *
182      * @param event event to check
183      * @throws ControlLoopException if the status is invalid
184      */
185     protected void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
186         if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
187                         && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
188             throw new ControlLoopException("Invalid value in closedLoopEventStatus");
189         }
190     }
191
192     /**
193      * Verifies that the event target is valid.
194      *
195      * @param event event to check
196      * @throws ControlLoopException if the status is invalid
197      */
198     protected void validateTarget(VirtualControlLoopEvent event) throws ControlLoopException {
199         if (StringUtils.isBlank(event.getTarget())) {
200             throw new ControlLoopException("No target field");
201         }
202     }
203 }