aboutsummaryrefslogtreecommitdiffstats
path: root/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/event/EnEvent.java
blob: 29802e06d71b3f474f7639160d8473125edbd16f (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. 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 lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
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)
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
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\"";

    /*
     * This is not used for encryption/security, thus disabling sonar.
     */
    private static Random rand = new Random(System.nanoTime());     // NOSONAR

    // The definition of this event in the Apex model
    @Setter(AccessLevel.NONE)
    @EqualsAndHashCode.Include
    private final AxEvent axEvent;

    // The event monitor for this event
    @Getter(AccessLevel.NONE)
    private final transient EventMonitor eventMonitor = new EventMonitor();

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

    // 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 = new Properties();

    // 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;
    }

    /**
     * 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();
    }

    /**
     * {@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() + "]";
    }
}