2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.eventmanager;
23 import java.util.HashMap;
24 import lombok.AccessLevel;
27 import org.apache.commons.lang3.StringUtils;
28 import org.drools.core.WorkingMemory;
29 import org.onap.policy.controlloop.ControlLoopEventStatus;
30 import org.onap.policy.controlloop.ControlLoopException;
31 import org.onap.policy.controlloop.ControlLoopResponse;
32 import org.onap.policy.controlloop.VirtualControlLoopEvent;
33 import org.onap.policy.controlloop.VirtualControlLoopNotification;
34 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
35 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Manager for a single control loop event. Once this has been created, the event can be
41 * retracted from working memory.
43 public abstract class ClEventManagerWithEvent<T extends Step> extends ClEventManagerWithOutcome<T>
44 implements StepContext {
46 private static final Logger logger = LoggerFactory.getLogger(ClEventManagerWithEvent.class);
47 private static final long serialVersionUID = -1216568161322872641L;
49 public enum NewEventStatus {
50 FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
54 private final VirtualControlLoopEvent event;
57 @Setter(AccessLevel.PROTECTED)
58 private VirtualControlLoopEvent abatement = null;
62 * Constructs the object.
64 * @param params control loop parameters
65 * @param event event to be managed by this object
66 * @param workMem working memory to update if this changes
67 * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
70 public ClEventManagerWithEvent(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
71 throws ControlLoopException {
73 super(params, event.getRequestId(), workMem);
75 checkEventSyntax(event);
81 protected void populateNotification(VirtualControlLoopNotification notif) {
82 super.populateNotification(notif);
84 notif.setClosedLoopControlName(event.getClosedLoopControlName());
85 notif.setRequestId(event.getRequestId());
86 notif.setClosedLoopEventClient(event.getClosedLoopEventClient());
87 notif.setTargetType(event.getTargetType());
88 notif.setTarget(event.getTarget());
90 if (event.getAai() != null) {
91 notif.setAai(new HashMap<>(event.getAai()));
93 notif.setClosedLoopAlarmStart(event.getClosedLoopAlarmStart());
94 notif.setClosedLoopAlarmEnd(event.getClosedLoopAlarmEnd());
98 * Stores an operation outcome in the DB.
100 * @param outcome operation outcome to store
101 * @param targetEntity target entity
103 protected void storeInDataBase(OperationOutcome2 outcome, String targetEntity) {
104 getDataManager().store(getRequestIdStr(), event.getClosedLoopControlName(), event, targetEntity,
105 outcome.getClOperation());
109 public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
110 ControlLoopResponse clRsp = super.makeControlLoopResponse(outcome);
111 clRsp.setTarget("DCAE");
113 clRsp.setClosedLoopControlName(event.getClosedLoopControlName());
114 clRsp.setPolicyName(event.getPolicyName());
115 clRsp.setPolicyVersion(event.getPolicyVersion());
116 clRsp.setRequestId(event.getRequestId());
117 clRsp.setVersion(event.getVersion());
123 * An event onset/abatement.
125 * @param newEvent the event
128 public NewEventStatus onNewEvent(VirtualControlLoopEvent newEvent) {
130 checkEventSyntax(newEvent);
132 if (newEvent.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
133 if (newEvent.equals(event)) {
134 return NewEventStatus.FIRST_ONSET;
138 return NewEventStatus.SUBSEQUENT_ONSET;
141 if (abatement == null) {
142 abatement = newEvent;
144 return NewEventStatus.FIRST_ABATEMENT;
147 return NewEventStatus.SUBSEQUENT_ABATEMENT;
150 } catch (ControlLoopException e) {
151 logger.error("{}: onNewEvent threw an exception", this, e);
152 return NewEventStatus.SYNTAX_ERROR;
157 * Check an event syntax.
159 * @param event the event syntax
160 * @throws ControlLoopException if an error occurs
162 protected void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
163 validateStatus(event);
164 if (StringUtils.isBlank(event.getClosedLoopControlName())) {
165 throw new ControlLoopException("No control loop name");
167 if (event.getRequestId() == null) {
168 throw new ControlLoopException("No request ID");
170 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
173 validateTarget(event);
177 * Verifies that the event status is valid.
179 * @param event event to check
180 * @throws ControlLoopException if the status is invalid
182 protected void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
183 if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
184 && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
185 throw new ControlLoopException("Invalid value in closedLoopEventStatus");
190 * Verifies that the event target is valid.
192 * @param event event to check
193 * @throws ControlLoopException if the status is invalid
195 protected void validateTarget(VirtualControlLoopEvent event) throws ControlLoopException {
196 if (StringUtils.isBlank(event.getTarget())) {
197 throw new ControlLoopException("No target field");