db7ec1d93acdbef352ff6868b5def170a7098443
[policy/drools-applications.git] /
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 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
68      *         be created
69      */
70     public ClEventManagerWithEvent(ControlLoopParams params, VirtualControlLoopEvent event, WorkingMemory workMem)
71                     throws ControlLoopException {
72
73         super(params, event.getRequestId(), workMem);
74
75         checkEventSyntax(event);
76
77         this.event = event;
78     }
79
80     @Override
81     protected void populateNotification(VirtualControlLoopNotification notif) {
82         super.populateNotification(notif);
83
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());
89
90         if (event.getAai() != null) {
91             notif.setAai(new HashMap<>(event.getAai()));
92         }
93         notif.setClosedLoopAlarmStart(event.getClosedLoopAlarmStart());
94         notif.setClosedLoopAlarmEnd(event.getClosedLoopAlarmEnd());
95     }
96
97     /**
98      * Stores an operation outcome in the DB.
99      *
100      * @param outcome operation outcome to store
101      * @param targetEntity target entity
102      */
103     protected void storeInDataBase(OperationOutcome2 outcome, String targetEntity) {
104         getDataManager().store(getRequestIdStr(), event.getClosedLoopControlName(), event, targetEntity,
105                         outcome.getClOperation());
106     }
107
108     @Override
109     public ControlLoopResponse makeControlLoopResponse(OperationOutcome outcome) {
110         ControlLoopResponse clRsp = super.makeControlLoopResponse(outcome);
111         clRsp.setTarget("DCAE");
112
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());
118
119         return clRsp;
120     }
121
122     /**
123      * An event onset/abatement.
124      *
125      * @param newEvent the event
126      * @return the status
127      */
128     public NewEventStatus onNewEvent(VirtualControlLoopEvent newEvent) {
129         try {
130             checkEventSyntax(newEvent);
131
132             if (newEvent.getClosedLoopEventStatus() == ControlLoopEventStatus.ONSET) {
133                 if (newEvent.equals(event)) {
134                     return NewEventStatus.FIRST_ONSET;
135                 }
136
137                 bumpOffsets();
138                 return NewEventStatus.SUBSEQUENT_ONSET;
139
140             } else {
141                 if (abatement == null) {
142                     abatement = newEvent;
143                     bumpAbatements();
144                     return NewEventStatus.FIRST_ABATEMENT;
145                 } else {
146                     bumpAbatements();
147                     return NewEventStatus.SUBSEQUENT_ABATEMENT;
148                 }
149             }
150         } catch (ControlLoopException e) {
151             logger.error("{}: onNewEvent threw an exception", this, e);
152             return NewEventStatus.SYNTAX_ERROR;
153         }
154     }
155
156     /**
157      * Check an event syntax.
158      *
159      * @param event the event syntax
160      * @throws ControlLoopException if an error occurs
161      */
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");
166         }
167         if (event.getRequestId() == null) {
168             throw new ControlLoopException("No request ID");
169         }
170         if (event.getClosedLoopEventStatus() == ControlLoopEventStatus.ABATED) {
171             return;
172         }
173         validateTarget(event);
174     }
175
176     /**
177      * Verifies that the event status is valid.
178      *
179      * @param event event to check
180      * @throws ControlLoopException if the status is invalid
181      */
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");
186         }
187     }
188
189     /**
190      * Verifies that the event target is valid.
191      *
192      * @param event event to check
193      * @throws ControlLoopException if the status is invalid
194      */
195     protected void validateTarget(VirtualControlLoopEvent event) throws ControlLoopException {
196         if (StringUtils.isBlank(event.getTarget())) {
197             throw new ControlLoopException("No target field");
198         }
199     }
200 }