2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.eventmanager;
24 import java.io.Serial;
25 import java.util.HashMap;
26 import lombok.AccessLevel;
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;
42 * Manager for a single control loop event. Once this has been created, the event can be
43 * retracted from working memory.
46 public abstract class ClEventManagerWithEvent<T extends Step> extends ClEventManagerWithOutcome<T>
47 implements StepContext {
49 private static final Logger logger = LoggerFactory.getLogger(ClEventManagerWithEvent.class);
51 private static final long serialVersionUID = -1216568161322872641L;
53 public enum NewEventStatus {
54 FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
57 private final VirtualControlLoopEvent event;
59 @Setter(AccessLevel.PROTECTED)
60 private VirtualControlLoopEvent abatement = null;
64 * Constructs the object.
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
73 protected ClEventManagerWithEvent(EventManagerServices services, ControlLoopParams params,
74 VirtualControlLoopEvent event, WorkingMemory workMem) throws ControlLoopException {
76 super(services, params, event.getRequestId(), workMem);
78 checkEventSyntax(event);
84 protected void populateNotification(VirtualControlLoopNotification notif) {
85 super.populateNotification(notif);
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());
93 if (event.getAai() != null) {
94 notif.setAai(new HashMap<>(event.getAai()));
96 notif.setClosedLoopAlarmStart(event.getClosedLoopAlarmStart());
97 notif.setClosedLoopAlarmEnd(event.getClosedLoopAlarmEnd());
101 * Stores an operation outcome in the DB.
103 * @param outcome operation outcome to store
104 * @param targetEntity target entity
106 protected void storeInDataBase(OperationOutcome2 outcome, String targetEntity) {
107 getDataManager().store(getRequestIdStr(), event.getClosedLoopControlName(), event, targetEntity,
108 outcome.getClOperation());
112 public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
113 var clRsp = super.makeControlLoopResponse(outcome);
114 clRsp.setTarget("DCAE");
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());
126 * An event onset/abatement.
128 * @param newEvent the event
131 public NewEventStatus onNewEvent(VirtualControlLoopEvent newEvent) {
133 checkEventSyntax(newEvent);
135 if (newEvent.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
136 if (newEvent.equals(event)) {
137 return NewEventStatus.FIRST_ONSET;
141 return NewEventStatus.SUBSEQUENT_ONSET;
144 if (abatement == null) {
145 abatement = newEvent;
147 return NewEventStatus.FIRST_ABATEMENT;
150 return NewEventStatus.SUBSEQUENT_ABATEMENT;
153 } catch (ControlLoopException e) {
154 logger.error("{}: onNewEvent threw an exception", this, e);
155 return NewEventStatus.SYNTAX_ERROR;
160 * Check an event syntax.
162 * @param event the event syntax
163 * @throws ControlLoopException if an error occurs
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");
170 if (event.getRequestId() == null) {
171 throw new ControlLoopException("No request ID");
173 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
176 validateTarget(event);
180 * Verifies that the event status is valid.
182 * @param event event to check
183 * @throws ControlLoopException if the status is invalid
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");
193 * Verifies that the event target is valid.
195 * @param event event to check
196 * @throws ControlLoopException if the status is invalid
198 protected void validateTarget(VirtualControlLoopEvent event) throws ControlLoopException {
199 if (StringUtils.isBlank(event.getTarget())) {
200 throw new ControlLoopException("No target field");