aboutsummaryrefslogtreecommitdiffstats
path: root/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/event/EnEvent.java
blob: d53fdf8ff33a866eee505d8a253b1bafab376844 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.core.engine.event;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.Set;

import org.onap.policy.apex.core.engine.monitoring.EventMonitor;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
import org.onap.policy.apex.model.basicmodel.service.ModelService;
import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
import org.onap.policy.apex.model.eventmodel.concepts.AxEvents;
import org.onap.policy.apex.model.eventmodel.concepts.AxField;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * Instances of the Class EnEvent are events being passed through the Apex system. All events in the
 * system are instances of this class.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class EnEvent extends HashMap<String, Object> {
    private static final long serialVersionUID = 6311863111866294637L;

    // Logger for this class
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(EnEvent.class);

    // Repeasted string constants
    private static final String NULL_KEYS_ILLEGAL = "null keys are illegal on method parameter \"key\"";

    // The definition of this event in the Apex model
    private final AxEvent axEvent;

    // The event monitor for this event
    private final transient EventMonitor eventMonitor = new EventMonitor();

    // The stack of execution of this event, used for monitoring
    private AxConcept[] userArtifactStack;

    private static Random rand = new Random(System.nanoTime());

    // An identifier for the current event execution. The default value here will always be a random
    // number, and should
    // be reset
    private long executionId = rand.nextLong();

    // Event related properties used during processing of this event
    private Properties executionProperties;

    // A string holding a message that indicates why processing of this event threw an exception
    private String exceptionMessage;

    /**
     * Instantiates a new EnEvent, an Engine Event.
     *
     * @param eventKey the key of the event definition from the Apex model
     */
    public EnEvent(final AxArtifactKey eventKey) {
        this(ModelService.getModel(AxEvents.class).get(eventKey));
    }

    /**
     * Instantiates a new EnEvent, an Engine Event.
     *
     * @param axEvent the event definition from the Apex model
     */
    public EnEvent(final AxEvent axEvent) {
        super();

        if (axEvent == null) {
            throw new EnException("event definition is null or was not found in model service");
        }
        // Save the event definition from the Apex model
        this.axEvent = axEvent;
    }

    /**
     * Gets the event definition of this event.
     *
     * @return the event definition
     */
    public AxEvent getAxEvent() {
        return axEvent;
    }

    /**
     * Get the name of the event.
     *
     * @return the event name
     */
    public String getName() {
        return axEvent.getKey().getName();
    }

    /**
     * Get the key of the event.
     *
     * @return the event key
     */
    public AxArtifactKey getKey() {
        return axEvent.getKey();
    }

    /**
     * Get the ID of the event.
     *
     * @return the event key
     */
    public String getId() {
        return axEvent.getKey().getId();
    }

    /**
     * Get the currently set value for the ExecutionID for this event. A ExecutionID in an EnEvent
     * is used identify all EnEvents (input, internal and output events) used in a single Engine
     * invocation. Therefore, a ExecutionID can be used to match which output event is the result of
     * a particular input event. The default initialized value for the ExecutionID is always unique
     * in a single JVM.
     *
     * @return the currently set value for the ExecutionID for this event.
     */
    public long getExecutionId() {
        return executionId;
    }

    /**
     * Set the value for the ExecutionID for this event. A ExecutionID in an EnEvent is used
     * identify all EnEvents (input, internal and output events) used in a single Engine invocation.
     * Therefore, a ExecutionID can be used to match which output event is the result of a
     * particular input event. The default initialised value for the ExecutionID is always unique in
     * a single JVM.
     *
     * @param executionId the new value for the ExecutionID for this event.
     */
    public void setExecutionId(final long executionId) {
        this.executionId = executionId;
    }

    /**
     * Get the event execution properties.
     *
     * @return the event execution properties
     */
    public Properties getExecutionProperties() {
        return executionProperties;
    }

    /**
     * Set the event execution properties.
     *
     * @param executionProperties the execution properties to set
     */
    public void setExecutionProperties(Properties executionProperties) {
        this.executionProperties = executionProperties;
    }

    /**
     * Gets the exception message explaining why processing of this event to fail.
     *
     * @return the exception message
     */
    public String getExceptionMessage() {
        return exceptionMessage;
    }

    /**
     * Sets the exception message explaining why processing of this event to fail.
     *
     * @param exceptionMessage the exception message
     */
    public void setExceptionMessage(final String exceptionMessage) {
        this.exceptionMessage = exceptionMessage;
    }

    /**
     * Get the user artifact stack of the event.
     *
     * @return the event user artifact stack
     */
    public AxConcept[] getUserArtifactStack() {
        return userArtifactStack;
    }

    /**
     * Store the user artifact stack of the event.
     *
     * @param usedArtifactStackArray the event user artifact stack
     */
    public void setUserArtifactStack(final AxConcept[] usedArtifactStackArray) {
        userArtifactStack = usedArtifactStackArray;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public Object get(final Object key) {
        if (key == null) {
            LOGGER.warn("null values are illegal on method parameter \"key\"");
            throw new EnException("null values are illegal on method parameter \"key\"");
        }

        // Check if this key is a parameter on our event
        final AxField eventParameter = axEvent.getParameterMap().get(key);
        if (eventParameter == null) {
            String message = "parameter with key " + key + " not defined on this event";
            LOGGER.warn(message);
            throw new EnException(message);
        }

        // Get the item
        final Object item = super.get(key);

        // Get the parameter value and monitor it
        eventMonitor.monitorGet(eventParameter, item, userArtifactStack);
        return item;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public Collection<Object> values() {
        // Build the key set and return it
        final ArrayList<Object> valueList = new ArrayList<>();

        // Override the generic "values()" call as we want to monitor the gets
        for (final String key : super.keySet()) {
            valueList.add(this.get(key));
        }

        return valueList;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public Set<Map.Entry<String, Object>> entrySet() {
        // Build the entry set and return it
        final Set<Map.Entry<String, Object>> entrySet = new HashSet<>();

        // Override the generic "entrySet()" call as we want to monitor the gets
        for (final String key : super.keySet()) {
            entrySet.add(new SimpleEntry<>(key, this.get(key)));
        }

        return entrySet;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public Object put(final String key, final Object incomingValue) {
        if (key == null) {
            String message = NULL_KEYS_ILLEGAL;
            LOGGER.warn(message);
            throw new EnException(message);
        }

        // Check if this key is a parameter on our event
        final AxField eventParameter = axEvent.getParameterMap().get(key);
        if (eventParameter == null) {
            String message = "parameter with key \"" + key + "\" not defined on event \"" + getName() + "\"";
            LOGGER.warn(message);
            throw new EnException(message);
        }

        // We allow null values
        if (incomingValue == null) {
            eventMonitor.monitorSet(eventParameter, incomingValue, userArtifactStack);
            return super.put(key, incomingValue);
        }

        // Holder for the object to assign
        final Object valueToAssign = new EnField(eventParameter, incomingValue).getAssignableValue();

        // Update the value in the parameter map
        eventMonitor.monitorSet(eventParameter, valueToAssign, userArtifactStack);
        return super.put(key, valueToAssign);
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void putAll(final Map<? extends String, ? extends Object> incomingMap) {
        // Override the generic "putAll()" call as we want to monitor the puts
        for (final Map.Entry<? extends String, ? extends Object> incomingEntry : incomingMap.entrySet()) {
            put(incomingEntry.getKey(), incomingEntry.getValue());
        }
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public Object remove(final Object key) {
        if (key == null) {
            LOGGER.warn(NULL_KEYS_ILLEGAL);
            throw new EnException(NULL_KEYS_ILLEGAL);
        }

        // Check if this key is a parameter on our event
        final AxField eventParameter = axEvent.getParameterMap().get(key);
        if (eventParameter == null) {
            String message = "parameter with key " + key + " not defined on this event";
            LOGGER.warn(message);
            throw new EnException(message);
        }

        final Object removedValue = super.remove(key);
        eventMonitor.monitorRemove(eventParameter, removedValue, userArtifactStack);
        return removedValue;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void clear() {
        // Override the generic "clear()" call as we want to monitor removals
        final Set<String> deleteSet = new HashSet<>();
        deleteSet.addAll(keySet());

        for (final String deleteKey : deleteSet) {
            this.remove(deleteKey);
        }
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public String toString() {
        return "EnEvent [axEvent=" + axEvent + ", userArtifactStack=" + Arrays.toString(userArtifactStack) + ", map="
                + super.toString() + "]";
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + axEvent.hashCode();
        return result;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!super.equals(obj)) {
            return false;
        }
        if (!(obj instanceof EnEvent)) {
            return false;
        }
        EnEvent other = (EnEvent) obj;
        if (axEvent == null) {
            if (other.axEvent != null) {
                return false;
            }
        } else if (!axEvent.equals(other.axEvent)) {
            return false;
        }
        return true;
    }
}