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 services services the manager should use when processing the event
65 * @param params control loop parameters
66 * @param event event to be managed by this object
67 * @param workMem working memory to update if this changes
68 * @throws ControlLoopException if the event is invalid or if a YAML processor cannot
71 protected ClEventManagerWithEvent(EventManagerServices services, ControlLoopParams params,
72 VirtualControlLoopEvent event, WorkingMemory workMem) throws ControlLoopException {
74 super(services, params, event.getRequestId(), workMem);
76 checkEventSyntax(event);
82 protected void populateNotification(VirtualControlLoopNotification notif) {
83 super.populateNotification(notif);
85 notif.setClosedLoopControlName(event.getClosedLoopControlName());
86 notif.setRequestId(event.getRequestId());
87 notif.setClosedLoopEventClient(event.getClosedLoopEventClient());
88 notif.setTargetType(event.getTargetType());
89 notif.setTarget(event.getTarget());
91 if (event.getAai() != null) {
92 notif.setAai(new HashMap<>(event.getAai()));
94 notif.setClosedLoopAlarmStart(event.getClosedLoopAlarmStart());
95 notif.setClosedLoopAlarmEnd(event.getClosedLoopAlarmEnd());
99 * Stores an operation outcome in the DB.
101 * @param outcome operation outcome to store
102 * @param targetEntity target entity
104 protected void storeInDataBase(OperationOutcome2 outcome, String targetEntity) {
105 getDataManager().store(getRequestIdStr(), event.getClosedLoopControlName(), event, targetEntity,
106 outcome.getClOperation());
110 public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
111 var clRsp = super.makeControlLoopResponse(outcome);
112 clRsp.setTarget("DCAE");
114 clRsp.setClosedLoopControlName(event.getClosedLoopControlName());
115 clRsp.setPolicyName(event.getPolicyName());
116 clRsp.setPolicyVersion(event.getPolicyVersion());
117 clRsp.setRequestId(event.getRequestId());
118 clRsp.setVersion(event.getVersion());
124 * An event onset/abatement.
126 * @param newEvent the event
129 public NewEventStatus onNewEvent(VirtualControlLoopEvent newEvent) {
131 checkEventSyntax(newEvent);
133 if (newEvent.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
134 if (newEvent.equals(event)) {
135 return NewEventStatus.FIRST_ONSET;
139 return NewEventStatus.SUBSEQUENT_ONSET;
142 if (abatement == null) {
143 abatement = newEvent;
145 return NewEventStatus.FIRST_ABATEMENT;
148 return NewEventStatus.SUBSEQUENT_ABATEMENT;
151 } catch (ControlLoopException e) {
152 logger.error("{}: onNewEvent threw an exception", this, e);
153 return NewEventStatus.SYNTAX_ERROR;
158 * Check an event syntax.
160 * @param event the event syntax
161 * @throws ControlLoopException if an error occurs
163 protected void checkEventSyntax(VirtualControlLoopEvent event) throws ControlLoopException {
164 validateStatus(event);
165 if (StringUtils.isBlank(event.getClosedLoopControlName())) {
166 throw new ControlLoopException("No control loop name");
168 if (event.getRequestId() == null) {
169 throw new ControlLoopException("No request ID");
171 if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
174 validateTarget(event);
178 * Verifies that the event status is valid.
180 * @param event event to check
181 * @throws ControlLoopException if the status is invalid
183 protected void validateStatus(VirtualControlLoopEvent event) throws ControlLoopException {
184 if (event.getClosedLoopEventStatus() != ControlLoopEventStatus.ONSET
185 && event.getClosedLoopEventStatus() != ControlLoopEventStatus.ABATED) {
186 throw new ControlLoopException("Invalid value in closedLoopEventStatus");
191 * Verifies that the event target is valid.
193 * @param event event to check
194 * @throws ControlLoopException if the status is invalid
196 protected void validateTarget(VirtualControlLoopEvent event) throws ControlLoopException {
197 if (StringUtils.isBlank(event.getTarget())) {
198 throw new ControlLoopException("No target field");