aboutsummaryrefslogtreecommitdiffstats
path: root/model/src/main/java/org/onap/policy/apex/model/eventmodel/concepts/AxEvent.java
blob: a4c0e9db19c4522ac6d448fcaf011ba17826708b (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019-2020,2022 Nordix Foundation.
 *  Modifications Copyright (C) 2022 Bell Canada.
 * ================================================================================
 * 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.model.eventmodel.concepts;

import com.google.common.base.Strings;
import com.google.gson.annotations.SerializedName;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.commons.lang3.EnumUtils;
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.concepts.AxKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxToscaPolicyProcessingStatus;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
import org.onap.policy.common.utils.validation.Assertions;

/**
 * This class defines an Apex event. An {@link AxEvent} is used to kick off execution of policies in Apex and is emitted
 * by policies when they complete execution. In addition, Apex uses {@link AxEvent} instances internally to pass
 * control from one Apex state to the next during execution.
 *
 * <p>The {@link AxArtifactKey} of an event uniquely identifies it in an Apex system and the name field in the key is
 * the name of the event.
 *
 * <p>Each {@link AxEvent} has a name space, which is usually set to identify the domain of application of an event. For
 * example a 4G cell power event might have the name space {@code org.onap.radio.4g} and the name {@code PowerEvent}.
 * The source and target of the event are reserved to hold an identifier that defines the sender and receiver of an
 * event respectively. The definition and structure of these fields is reserved for future use and their use by
 * applications is currently not recommended.
 *
 * <p>The parameters that an event has are defined as a map of {@link AxField} instances.
 *
 * <p>Validation checks that the event key is valid. If name space is a blank string, a warning is issued. Blank source
 * or target fields result in observations being issued. An event may not have any parameters. If it has parameters, the
 * name and value of each parameter entry is checked to ensure they are not null. Then the local name of each parameter
 * is checked to ensure it matches the event parameter key on the event. Finally, the parent key of each parameter is
 * checked to ensure it matches the event key.
 */
public class AxEvent extends AxConcept {
    private static final long serialVersionUID = -1460388382582984269L;

    private static final String WHITESPACE_REGEXP = "\\s+$";

    /** The key of the event, unique in the Apex system. */
    // CHECKSTYLE:OFF: checkstyle:VisibilityMonitor
    protected AxArtifactKey key;
    // CHECKSTYLE:ON: checkstyle:VisibilityMonitor

    private String nameSpace;
    private String source;
    private String target;

    @SerializedName("parameter")
    private Map<String, AxField> parameterMap;
    private String toscaPolicyState;

    /**
     * The default constructor creates an event with a null artifact key. The event name space, source, and target are
     * all defined as empty strings and the parameter map is initialized as an empty map.
     */
    public AxEvent() {
        this(new AxArtifactKey());
    }

    /**
     * Copy constructor.
     *
     * @param copyConcept the concept to copy from
     */
    public AxEvent(final AxEvent copyConcept) {
        super(copyConcept);
    }

    /**
     * The default constructor creates an event with the given artifact key. The event name space, source, and target
     * are all defined as empty strings and the parameter map is initialized as an empty map.
     *
     * @param key the key of the event
     */
    public AxEvent(final AxArtifactKey key) {
        this(key, "", "", "", new TreeMap<>(), "");
    }

    /**
     * This constructor creates an event with the given artifact key and name space. The event source, and target are
     * all defined as empty strings and the parameter map is initialized as an empty map.
     *
     * @param key the key of the event
     * @param nameSpace the name space of the event
     */
    public AxEvent(final AxArtifactKey key, final String nameSpace) {
        this(key, nameSpace, "", "", new TreeMap<>(), "");
    }

    /**
     * This constructor creates an event with the given artifact key, name space, source and target. The parameter map
     * is initialized as an empty map.
     *
     * @param key the key of the event
     * @param nameSpace the name space of the event
     * @param source the source of the event
     * @param target the target of the event
     */
    public AxEvent(final AxArtifactKey key, final String nameSpace, final String source, final String target) {
        this(key, nameSpace, source, target, new TreeMap<>(), "");
    }

    /**
     * This constructor creates an event with all its fields defined.
     *
     * @param key the key of the event
     * @param nameSpace the name space of the event
     * @param source the source of the event
     * @param target the target of the event
     * @param parameterMap the map of parameters that the event has
     * @param toscaPolicyState the TOSCA policy processing status that event is flagged with
     */
    public AxEvent(final AxArtifactKey key, final String nameSpace, final String source, final String target,
                    final SortedMap<String, AxField> parameterMap, final String toscaPolicyState) {
        super();
        Assertions.argumentNotNull(key, "key may not be null");
        Assertions.argumentNotNull(nameSpace, "nameSpace may not be null");
        Assertions.argumentNotNull(source, "source may not be null");
        Assertions.argumentNotNull(target, "target may not be null");
        Assertions.argumentNotNull(parameterMap, "parameterMap may not be null");

        this.key = key;
        this.nameSpace = nameSpace;
        this.source = source;
        this.target = target;
        this.parameterMap = parameterMap;
        this.toscaPolicyState = toscaPolicyState;
    }

    /**
     * This method checks that an event has all the fields in the {@code otherFieldSet} set defined on it.
     *
     * @param otherFieldSet the set of fields to check for existence on this event
     * @return true, if all the {@code otherFieldSet} fields are defined on this event
     */
    public boolean hasFields(final Set<AxField> otherFieldSet) {
        return parameterMap.values().containsAll(otherFieldSet);
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void buildReferences() {
        for (final AxField parameter : parameterMap.values()) {
            parameter.getKey().setParentArtifactKey(key);
        }
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public AxArtifactKey getKey() {
        return key;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public List<AxKey> getKeys() {
        final List<AxKey> keyList = key.getKeys();

        for (final AxField field : parameterMap.values()) {
            keyList.addAll(field.getKeys());
        }
        return keyList;
    }

    /**
     * Sets the key of the event.
     *
     * @param key the key of the event
     */
    public void setKey(final AxArtifactKey key) {
        Assertions.argumentNotNull(key, "key may not be null");
        this.key = key;

        for (final AxField parameter : parameterMap.values()) {
            parameter.getKey().setParentArtifactKey(key);
        }
    }

    /**
     * Gets the name space of the event.
     *
     * @return the name space of the event
     */
    public String getNameSpace() {
        return nameSpace;
    }

    /**
     * Sets the name space of the event.
     *
     * @param nameSpace the name space of the event
     */
    public void setNameSpace(final String nameSpace) {
        Assertions.argumentNotNull(nameSpace, "nameSpace may not be null");
        this.nameSpace = nameSpace.trim();
    }

    /**
     * Gets the source of the event.
     *
     * @return the source of the event
     */
    public String getSource() {
        return source;
    }

    /**
     * Sets the source of the event.
     *
     * @param source the source of the event
     */
    public void setSource(final String source) {
        Assertions.argumentNotNull(source, "source may not be null");
        this.source = source.trim();
    }

    /**
     * Gets the target of the event.
     *
     * @return the target of the event
     */
    public String getTarget() {
        return target;
    }

    /**
     * Sets the target of the event.
     *
     * @param target the target of the event
     */
    public void setTarget(final String target) {
        Assertions.argumentNotNull(target, "target may not be null");
        this.target = target.trim();
    }

    /**
     * Gets the event parameter map.
     *
     * @return the event parameter map
     */
    public Map<String, AxField> getParameterMap() {
        return parameterMap;
    }

    /**
     * Gets the fields defined on the event as a set.
     *
     * @return the fields defined on the event as a set
     */
    public Set<AxField> getFields() {
        return new TreeSet<>(parameterMap.values());
    }

    /**
     * Sets the event parameter map, containing all the fields of the event.
     *
     * @param parameterMap the event parameter map
     */
    public void setParameterMap(final Map<String, AxField> parameterMap) {
        Assertions.argumentNotNull(parameterMap, "parameterMap may not be null");
        this.parameterMap = parameterMap;
    }

    /**
     * Gets the TOSCA policy processing status from the event.
     *
     * @return the TOSCA policy processing status
     */
    public String getToscaPolicyState() {
        return toscaPolicyState;
    }

    /**
     * Sets the TOSCA policy processing status on the event.
     *
     * @param toscaPolicyState the TOSCA policy processing status
     */
    public void setToscaPolicyState(String toscaPolicyState) {
        this.toscaPolicyState = toscaPolicyState;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public AxValidationResult validate(final AxValidationResult resultIn) {
        AxValidationResult result = resultIn;

        if (key.equals(AxArtifactKey.getNullKey())) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                            "key is a null key"));
        }

        result = key.validate(result);

        if (nameSpace.replaceAll(WHITESPACE_REGEXP, "").length() == 0) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.WARNING,
                            "nameSpace on event is blank"));
        }

        if (source.replaceAll(WHITESPACE_REGEXP, "").length() == 0) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
                            "source on event is blank"));
        }

        if (target.replaceAll(WHITESPACE_REGEXP, "").length() == 0) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
                            "target on event is blank"));
        }

        for (final Entry<String, AxField> eventParameterEntry : parameterMap.entrySet()) {
            if (eventParameterEntry.getKey() == null || eventParameterEntry.getKey().equals(AxKey.NULL_KEY_NAME)) {
                result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                                "key on parameter " + eventParameterEntry.getKey() + " may not be the null key"));
            } else if (eventParameterEntry.getValue() == null) {
                result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                                "value on parameter " + eventParameterEntry.getKey() + " may not be null"));
            } else {
                result = validateEventParameters(eventParameterEntry, result);
            }
        }

        if (!Strings.isNullOrEmpty(toscaPolicyState)
                && !EnumUtils.isValidEnum(AxToscaPolicyProcessingStatus.class, toscaPolicyState)) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                    "toscaPolicyState on event is not a valid enum. Valid values are: "
                            + Arrays.asList(AxToscaPolicyProcessingStatus.values())));
        }

        return result;
    }

    /**
     * Validate an event parameter entry.
     *
     * @param eventParameterEntry the event parameter entry
     * @param result the validation result to append to
     * @return The validation result
     */
    private AxValidationResult validateEventParameters(final Entry<String, AxField> eventParameterEntry,
                    final AxValidationResult result) {
        if (!eventParameterEntry.getKey().equals(eventParameterEntry.getValue().getKey().getLocalName())) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                            "key on parameter " + eventParameterEntry.getKey()
                                            + " does not equal parameter field local name "
                                            + eventParameterEntry.getValue().getKey().getLocalName()));
        }

        if (!eventParameterEntry.getValue().getKey().getParentArtifactKey().equals(key)) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                            "parent key on parameter field " + eventParameterEntry.getValue().getKey()
                                            + " does not equal event key"));
        }

        return eventParameterEntry.getValue().validate(result);
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void clean() {
        key.clean();
        nameSpace = nameSpace.trim();
        source = source.trim();
        target = target.trim();
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append(this.getClass().getSimpleName());
        builder.append(":(");
        builder.append("key=");
        builder.append(key);
        builder.append(",nameSpace=");
        builder.append(nameSpace);
        builder.append(",source=");
        builder.append(source);
        builder.append(",target=");
        builder.append(target);
        builder.append(",parameter=");
        builder.append(parameterMap);
        builder.append(",toscaPolicyState=");
        builder.append(toscaPolicyState);
        builder.append(")");
        return builder.toString();
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public AxConcept copyTo(final AxConcept targetObject) {
        Assertions.argumentNotNull(targetObject, "targetObject may not be null");

        final Object copyObject = targetObject;
        Assertions.instanceOf(copyObject, AxEvent.class);

        final AxEvent copy = (AxEvent) copyObject;

        final Map<String, AxField> newParameterMap = new TreeMap<>();
        for (final Entry<String, AxField> eventParameterMapEntry : parameterMap.entrySet()) {
            newParameterMap.put(eventParameterMapEntry.getKey(), new AxField(eventParameterMapEntry.getValue()));
        }
        copy.setParameterMap(newParameterMap);

        copy.setKey(new AxArtifactKey(key));
        copy.setNameSpace(nameSpace);
        copy.setSource(source);
        copy.setTarget(target);
        copy.setToscaPolicyState(toscaPolicyState);

        return copy;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + key.hashCode();
        result = prime * result + nameSpace.hashCode();
        result = prime * result + source.hashCode();
        result = prime * result + target.hashCode();
        result = prime * result + parameterMap.hashCode();
        result = prime * result + toscaPolicyState.hashCode();
        return result;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        final AxEvent other = (AxEvent) obj;
        if (!key.equals(other.key)) {
            return false;
        }
        if (!nameSpace.equals(other.nameSpace)) {
            return false;
        }
        if (!source.equals(other.source)) {
            return false;
        }
        if (!target.equals(other.target)) {
            return false;
        }
        if (!toscaPolicyState.equals(other.toscaPolicyState)) {
            return false;
        }
        return parameterMap.equals(other.parameterMap);
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public int compareTo(final AxConcept otherObj) {
        if (otherObj == null) {
            return -1;
        }
        if (this == otherObj) {
            return 0;
        }
        if (getClass() != otherObj.getClass()) {
            return this.hashCode() - otherObj.hashCode();
        }

        final AxEvent other = (AxEvent) otherObj;
        if (!key.equals(other.key)) {
            return key.compareTo(other.key);
        }
        if (!nameSpace.equals(other.nameSpace)) {
            return nameSpace.compareTo(other.nameSpace);
        }
        if (!source.equals(other.source)) {
            return source.compareTo(other.source);
        }
        if (!target.equals(other.target)) {
            return target.compareTo(other.target);
        }
        if (!parameterMap.equals(other.parameterMap)) {
            return (parameterMap.hashCode() - other.parameterMap.hashCode());
        }
        if (!toscaPolicyState.equals(other.toscaPolicyState)) {
            return toscaPolicyState.compareTo(other.toscaPolicyState);
        }

        return 0;
    }
}