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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.engine.event;
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;
30 import java.util.Properties;
31 import java.util.Random;
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;
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.
49 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public class EnEvent extends HashMap<String, Object> {
52 private static final long serialVersionUID = 6311863111866294637L;
54 // Logger for this class
55 private static final XLogger LOGGER = XLoggerFactory.getXLogger(EnEvent.class);
57 // Repeasted string constants
58 private static final String NULL_KEYS_ILLEGAL = "null keys are illegal on method parameter \"key\"";
60 // The definition of this event in the Apex model
61 private final AxEvent axEvent;
63 // The event monitor for this event
64 private final transient EventMonitor eventMonitor = new EventMonitor();
66 // The stack of execution of this event, used for monitoring
69 private AxConcept[] userArtifactStack;
71 private static Random rand = new Random(System.nanoTime());
73 // An identifier for the current event execution. The default value here will always be a random
78 private long executionId = rand.nextLong();
80 // Event related properties used during processing of this event
83 private Properties executionProperties = new Properties();
85 // A string holding a message that indicates why processing of this event threw an exception
88 private String exceptionMessage;
91 * Instantiates a new EnEvent, an Engine Event.
93 * @param eventKey the key of the event definition from the Apex model
95 public EnEvent(final AxArtifactKey eventKey) {
96 this(ModelService.getModel(AxEvents.class).get(eventKey));
100 * Instantiates a new EnEvent, an Engine Event.
102 * @param axEvent the event definition from the Apex model
104 public EnEvent(final AxEvent axEvent) {
107 if (axEvent == null) {
108 throw new EnException("event definition is null or was not found in model service");
110 // Save the event definition from the Apex model
111 this.axEvent = axEvent;
115 * Gets the event definition of this event.
117 * @return the event definition
119 public AxEvent getAxEvent() {
124 * Get the name of the event.
126 * @return the event name
128 public String getName() {
129 return axEvent.getKey().getName();
133 * Get the key of the event.
135 * @return the event key
137 public AxArtifactKey getKey() {
138 return axEvent.getKey();
142 * Get the ID of the event.
144 * @return the event key
146 public String getId() {
147 return axEvent.getKey().getId();
154 public Object get(final Object key) {
156 LOGGER.warn("null values are illegal on method parameter \"key\"");
157 throw new EnException("null values are illegal on method parameter \"key\"");
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);
169 final Object item = super.get(key);
171 // Get the parameter value and monitor it
172 eventMonitor.monitorGet(eventParameter, item, userArtifactStack);
180 public Collection<Object> values() {
181 // Build the key set and return it
182 final ArrayList<Object> valueList = new ArrayList<>();
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));
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<>();
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)));
212 public Object put(final String key, final Object incomingValue) {
214 String message = NULL_KEYS_ILLEGAL;
215 LOGGER.warn(message);
216 throw new EnException(message);
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);
227 // We allow null values
228 if (incomingValue == null) {
229 eventMonitor.monitorSet(eventParameter, incomingValue, userArtifactStack);
230 return super.put(key, incomingValue);
233 // Holder for the object to assign
234 final Object valueToAssign = new EnField(eventParameter, incomingValue).getAssignableValue();
236 // Update the value in the parameter map
237 eventMonitor.monitorSet(eventParameter, valueToAssign, userArtifactStack);
238 return super.put(key, valueToAssign);
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());
256 public Object remove(final Object key) {
258 LOGGER.warn(NULL_KEYS_ILLEGAL);
259 throw new EnException(NULL_KEYS_ILLEGAL);
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);
270 final Object removedValue = super.remove(key);
271 eventMonitor.monitorRemove(eventParameter, removedValue, userArtifactStack);
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());
284 for (final String deleteKey : deleteSet) {
285 this.remove(deleteKey);
293 public String toString() {
294 return "EnEvent [axEvent=" + axEvent + ", userArtifactStack=" + Arrays.toString(userArtifactStack) + ", map="
295 + super.toString() + "]";
302 public int hashCode() {
303 final int prime = 31;
304 int result = super.hashCode();
305 result = prime * result + axEvent.hashCode();
313 public boolean equals(Object obj) {
317 if (!super.equals(obj)) {
320 if (!(obj instanceof EnEvent)) {
323 EnEvent other = (EnEvent) obj;
324 if (axEvent == null) {
325 if (other.axEvent != null) {
328 } else if (!axEvent.equals(other.axEvent)) {