Changes for checkstyle 8.32
[policy/apex-pdp.git] / core / core-engine / src / main / java / org / onap / policy / apex / core / engine / event / EnEvent.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.event;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.Random;
32 import java.util.Set;
33 import lombok.Getter;
34 import lombok.Setter;
35 import org.onap.policy.apex.core.engine.monitoring.EventMonitor;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
38 import org.onap.policy.apex.model.basicmodel.service.ModelService;
39 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
40 import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
41 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
42 import org.slf4j.ext.XLogger;
43 import org.slf4j.ext.XLoggerFactory;
44
45 /**
46  * Instances of the Class EnEvent are events being passed through the Apex system. All events in the
47  * system are instances of this class.
48  *
49  * @author Liam Fallon (liam.fallon@ericsson.com)
50  */
51 public class EnEvent extends HashMap<String, Object> {
52     private static final long serialVersionUID = 6311863111866294637L;
53
54     // Logger for this class
55     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EnEvent.class);
56
57     // Repeasted string constants
58     private static final String NULL_KEYS_ILLEGAL = "null keys are illegal on method parameter \"key\"";
59
60     // The definition of this event in the Apex model
61     private final AxEvent axEvent;
62
63     // The event monitor for this event
64     private final transient EventMonitor eventMonitor = new EventMonitor();
65
66     // The stack of execution of this event, used for monitoring
67     @Getter
68     @Setter
69     private AxConcept[] userArtifactStack;
70
71     private static Random rand = new Random(System.nanoTime());
72
73     // An identifier for the current event execution. The default value here will always be a random
74     // number, and should
75     // be reset
76     @Getter
77     @Setter
78     private long executionId = rand.nextLong();
79
80     // Event related properties used during processing of this event
81     @Getter
82     @Setter
83     private Properties executionProperties = new Properties();
84
85     // A string holding a message that indicates why processing of this event threw an exception
86     @Getter
87     @Setter
88     private String exceptionMessage;
89
90     /**
91      * Instantiates a new EnEvent, an Engine Event.
92      *
93      * @param eventKey the key of the event definition from the Apex model
94      */
95     public EnEvent(final AxArtifactKey eventKey) {
96         this(ModelService.getModel(AxEvents.class).get(eventKey));
97     }
98
99     /**
100      * Instantiates a new EnEvent, an Engine Event.
101      *
102      * @param axEvent the event definition from the Apex model
103      */
104     public EnEvent(final AxEvent axEvent) {
105         super();
106
107         if (axEvent == null) {
108             throw new EnException("event definition is null or was not found in model service");
109         }
110         // Save the event definition from the Apex model
111         this.axEvent = axEvent;
112     }
113
114     /**
115      * Gets the event definition of this event.
116      *
117      * @return the event definition
118      */
119     public AxEvent getAxEvent() {
120         return axEvent;
121     }
122
123     /**
124      * Get the name of the event.
125      *
126      * @return the event name
127      */
128     public String getName() {
129         return axEvent.getKey().getName();
130     }
131
132     /**
133      * Get the key of the event.
134      *
135      * @return the event key
136      */
137     public AxArtifactKey getKey() {
138         return axEvent.getKey();
139     }
140
141     /**
142      * Get the ID of the event.
143      *
144      * @return the event key
145      */
146     public String getId() {
147         return axEvent.getKey().getId();
148     }
149
150     /**
151      * {@inheritDoc}.
152      */
153     @Override
154     public Object get(final Object key) {
155         if (key == null) {
156             LOGGER.warn("null values are illegal on method parameter \"key\"");
157             throw new EnException("null values are illegal on method parameter \"key\"");
158         }
159
160         // Check if this key is a parameter on our event
161         final AxField eventParameter = axEvent.getParameterMap().get(key);
162         if (eventParameter == null) {
163             String message = "parameter with key " + key + " not defined on this event";
164             LOGGER.warn(message);
165             throw new EnException(message);
166         }
167
168         // Get the item
169         final Object item = super.get(key);
170
171         // Get the parameter value and monitor it
172         eventMonitor.monitorGet(eventParameter, item, userArtifactStack);
173         return item;
174     }
175
176     /**
177      * {@inheritDoc}.
178      */
179     @Override
180     public Collection<Object> values() {
181         // Build the key set and return it
182         final ArrayList<Object> valueList = new ArrayList<>();
183
184         // Override the generic "values()" call as we want to monitor the gets
185         for (final String key : super.keySet()) {
186             valueList.add(this.get(key));
187         }
188
189         return valueList;
190     }
191
192     /**
193      * {@inheritDoc}.
194      */
195     @Override
196     public Set<Map.Entry<String, Object>> entrySet() {
197         // Build the entry set and return it
198         final Set<Map.Entry<String, Object>> entrySet = new HashSet<>();
199
200         // Override the generic "entrySet()" call as we want to monitor the gets
201         for (final String key : super.keySet()) {
202             entrySet.add(new SimpleEntry<>(key, this.get(key)));
203         }
204
205         return entrySet;
206     }
207
208     /**
209      * {@inheritDoc}.
210      */
211     @Override
212     public Object put(final String key, final Object incomingValue) {
213         if (key == null) {
214             String message = NULL_KEYS_ILLEGAL;
215             LOGGER.warn(message);
216             throw new EnException(message);
217         }
218
219         // Check if this key is a parameter on our event
220         final AxField eventParameter = axEvent.getParameterMap().get(key);
221         if (eventParameter == null) {
222             String message = "parameter with key \"" + key + "\" not defined on event \"" + getName() + "\"";
223             LOGGER.warn(message);
224             throw new EnException(message);
225         }
226
227         // We allow null values
228         if (incomingValue == null) {
229             eventMonitor.monitorSet(eventParameter, incomingValue, userArtifactStack);
230             return super.put(key, incomingValue);
231         }
232
233         // Holder for the object to assign
234         final Object valueToAssign = new EnField(eventParameter, incomingValue).getAssignableValue();
235
236         // Update the value in the parameter map
237         eventMonitor.monitorSet(eventParameter, valueToAssign, userArtifactStack);
238         return super.put(key, valueToAssign);
239     }
240
241     /**
242      * {@inheritDoc}.
243      */
244     @Override
245     public void putAll(final Map<? extends String, ? extends Object> incomingMap) {
246         // Override the generic "putAll()" call as we want to monitor the puts
247         for (final Map.Entry<? extends String, ? extends Object> incomingEntry : incomingMap.entrySet()) {
248             put(incomingEntry.getKey(), incomingEntry.getValue());
249         }
250     }
251
252     /**
253      * {@inheritDoc}.
254      */
255     @Override
256     public Object remove(final Object key) {
257         if (key == null) {
258             LOGGER.warn(NULL_KEYS_ILLEGAL);
259             throw new EnException(NULL_KEYS_ILLEGAL);
260         }
261
262         // Check if this key is a parameter on our event
263         final AxField eventParameter = axEvent.getParameterMap().get(key);
264         if (eventParameter == null) {
265             String message = "parameter with key " + key + " not defined on this event";
266             LOGGER.warn(message);
267             throw new EnException(message);
268         }
269
270         final Object removedValue = super.remove(key);
271         eventMonitor.monitorRemove(eventParameter, removedValue, userArtifactStack);
272         return removedValue;
273     }
274
275     /**
276      * {@inheritDoc}.
277      */
278     @Override
279     public void clear() {
280         // Override the generic "clear()" call as we want to monitor removals
281         final Set<String> deleteSet = new HashSet<>();
282         deleteSet.addAll(keySet());
283
284         for (final String deleteKey : deleteSet) {
285             this.remove(deleteKey);
286         }
287     }
288
289     /**
290      * {@inheritDoc}.
291      */
292     @Override
293     public String toString() {
294         return "EnEvent [axEvent=" + axEvent + ", userArtifactStack=" + Arrays.toString(userArtifactStack) + ", map="
295                 + super.toString() + "]";
296     }
297
298     /**
299      * {@inheritDoc}.
300      */
301     @Override
302     public int hashCode() {
303         final int prime = 31;
304         int result = super.hashCode();
305         result = prime * result + axEvent.hashCode();
306         return result;
307     }
308
309     /**
310      * {@inheritDoc}.
311      */
312     @Override
313     public boolean equals(Object obj) {
314         if (this == obj) {
315             return true;
316         }
317         if (!super.equals(obj)) {
318             return false;
319         }
320         if (!(obj instanceof EnEvent)) {
321             return false;
322         }
323         EnEvent other = (EnEvent) obj;
324         if (axEvent == null) {
325             if (other.axEvent != null) {
326                 return false;
327             }
328         } else if (!axEvent.equals(other.axEvent)) {
329             return false;
330         }
331         return true;
332     }
333 }