5a0352bfe478ba740e5258c7db54133a1f5ade31
[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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.eventmanager;
22
23 import java.util.HashMap;
24 import lombok.AccessLevel;
25 import lombok.Getter;
26 import lombok.Setter;
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;
38
39 /**
40  * Manager for a single control loop event. Once this has been created, the event can be
41  * retracted from working memory.
42  */
43 public abstract class ClEventManagerWithEvent<T extends Step> extends ClEventManagerWithOutcome<T>
44                 implements StepContext {
45
46     private static final Logger logger = LoggerFactory.getLogger(ClEventManagerWithEvent.class);
47     private static final long serialVersionUID = -1216568161322872641L;
48
49     public enum NewEventStatus {
50         FIRST_ONSET, SUBSEQUENT_ONSET, FIRST_ABATEMENT, SUBSEQUENT_ABATEMENT, SYNTAX_ERROR
51     }
52
53     @Getter
54     private final VirtualControlLoopEvent event;
55
56     @Getter
57     @Setter(AccessLevel.PROTECTED)
58     private VirtualControlLoopEvent abatement = null;
59
60
61     /**
62      * Constructs the object.
63      *
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
69      *         be created
70      */
71     protected ClEventManagerWithEvent(EventManagerServices services, ControlLoopParams params,
72                     VirtualControlLoopEvent event, WorkingMemory workMem) throws ControlLoopException {
73
74         super(services, params, event.getRequestId(), workMem);
75
76         checkEventSyntax(event);
77
78         this.event = event;
79     }
80
81     @Override
82     protected void populateNotification(VirtualControlLoopNotification notif) {
83         super.populateNotification(notif);
84
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());
90
91         if (event.getAai() != null) {
92             notif.setAai(new HashMap<>(event.getAai()));
93         }
94         notif.setClosedLoopAlarmStart(event.getClosedLoopAlarmStart());
95         notif.setClosedLoopAlarmEnd(event.getClosedLoopAlarmEnd());
96     }
97
98     /**
99      * Stores an operation outcome in the DB.
100      *
101      * @param outcome operation outcome to store
102      * @param targetEntity target entity
103      */
104     protected void storeInDataBase(OperationOutcome2 outcome, String targetEntity) {
105         getDataManager().store(getRequestIdStr(), event.getClosedLoopControlName(), event, targetEntity,
106                         outcome.getClOperation());
107     }
108
109     @Override
110     public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
111         var clRsp = super.makeControlLoopResponse(outcome);
112         clRsp.setTarget("DCAE");
113
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());
119
120         return clRsp;
121     }
122
123     /**
124      * An event onset/abatement.
125      *
126      * @param newEvent the event
127      * @return the status
128      */
129     public NewEventStatus onNewEvent(VirtualControlLoopEvent newEvent) {
130         try {
131             checkEventSyntax(newEvent);
132
133             if (newEvent.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
134                 if (newEvent.equals(event)) {
135                     return NewEventStatus.FIRST_ONSET;
136                 }
137
138                 bumpOffsets();
139                 return NewEventStatus.SUBSEQUENT_ONSET;
140
141             } else {
142                 if (abatement == null) {
143                     abatement = newEvent;
144                     bumpAbatements();
145                     return NewEventStatus.FIRST_ABATEMENT;
146                 } else {
147                     bumpAbatements();
148                     return NewEventStatus.SUBSEQUENT_ABATEMENT;
149                 }
150             }
151         } catch (ControlLoopException e) {
152             logger.error("{}: onNewEvent threw an exception", this, e);
153             return NewEventStatus.SYNTAX_ERROR;
154         }
155     }
156
157     /**
158      * Check an event syntax.
159      *
160      * @param event the event syntax
161      * @throws ControlLoopException if an error occurs
162      */
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");
167         }
168         if (event.getRequestId() == null) {
169             throw new ControlLoopException("No request ID");
170         }
171         if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
172             return;
173         }
174         validateTarget(event);
175     }
176
177     /**
178      * Verifies that the event status is valid.
179      *
180      * @param event event to check
181      * @throws ControlLoopException if the status is invalid
182      */
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");
187         }
188     }
189
190     /**
191      * Verifies that the event target is valid.
192      *
193      * @param event event to check
194      * @throws ControlLoopException if the status is invalid
195      */
196     protected void validateTarget(VirtualControlLoopEvent event) throws ControlLoopException {
197         if (StringUtils.isBlank(event.getTarget())) {
198             throw new ControlLoopException("No target field");
199         }
200     }
201 }