3d0d9b7f24e2d33cb0828afe89caf1d3fe233151
[policy/apex-pdp.git] / model / event-model / src / main / java / org / onap / policy / apex / model / eventmodel / concepts / AxEvents.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.model.eventmodel.concepts;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.NavigableMap;
28 import java.util.Set;
29 import java.util.TreeMap;
30
31 import javax.persistence.CascadeType;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.JoinColumn;
35 import javax.persistence.JoinTable;
36 import javax.persistence.ManyToMany;
37 import javax.persistence.Table;
38 import javax.xml.bind.Unmarshaller;
39 import javax.xml.bind.annotation.XmlAccessType;
40 import javax.xml.bind.annotation.XmlAccessorType;
41 import javax.xml.bind.annotation.XmlElement;
42 import javax.xml.bind.annotation.XmlType;
43
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
52 import org.onap.policy.common.utils.validation.Assertions;
53
54 /**
55  * This class is an event container and holds a map of the events for an entire Apex model. All Apex models that use
56  * events must have an {@link AxEvents} field. The {@link AxEvents} class implements the helper methods of the
57  * {@link AxConceptGetter} interface to allow {@link AxEvents} instances to be retrieved by calling methods directly on
58  * this class without referencing the contained map.
59  *
60  * <p>Validation checks that the container key is not null. An error is issued if no events are defined in the
61  * container. Each event entry is checked to ensure that its key and value are not null and that the key matches the key
62  * in the map value. Each event entry is then validated individually.
63  */
64 @Entity
65 @Table(name = "AxEvents")
66
67 @XmlAccessorType(XmlAccessType.FIELD)
68 @XmlType(name = "AxEvents", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
69     { "key", "eventMap" })
70
71 public class AxEvents extends AxConcept implements AxConceptGetter<AxEvent> {
72     private static final long serialVersionUID = 4290442590545820316L;
73
74     @EmbeddedId
75     @XmlElement(name = "key", required = true)
76     private AxArtifactKey key;
77
78     // @formatter:off
79     @ManyToMany(cascade = CascadeType.ALL)
80     @JoinTable(
81             joinColumns = { @JoinColumn(name = "eventMapName", referencedColumnName = "name"),
82                     @JoinColumn(name = "eventMapVersion", referencedColumnName = "version") },
83             inverseJoinColumns = { @JoinColumn(name = "eventName", referencedColumnName = "name"),
84                     @JoinColumn(name = "eventVersion", referencedColumnName = "version") })
85     @XmlElement(required = true)
86     private Map<AxArtifactKey, AxEvent> eventMap;
87     // @formatter:on
88
89     /**
90      * The Default Constructor creates a {@link AxEvents} object with a null artifact key and creates an empty event
91      * map.
92      */
93     public AxEvents() {
94         this(new AxArtifactKey());
95     }
96
97     /**
98      * Copy constructor.
99      *
100      * @param copyConcept the concept to copy from
101      */
102     public AxEvents(final AxEvents copyConcept) {
103         super(copyConcept);
104     }
105
106     /**
107      * The Key Constructor creates a {@link AxEvents} object with the given artifact key and creates an empty event map.
108      *
109      * @param key the event container key
110      */
111     public AxEvents(final AxArtifactKey key) {
112         this(key, new TreeMap<AxArtifactKey, AxEvent>());
113     }
114
115     /**
116      * This Constructor creates an event container with all of its fields defined.
117      *
118      * @param key the event container key
119      * @param eventMap the events to be stored in the event container
120      */
121     public AxEvents(final AxArtifactKey key, final Map<AxArtifactKey, AxEvent> eventMap) {
122         super();
123         Assertions.argumentNotNull(key, "key may not be null");
124         Assertions.argumentNotNull(eventMap, "eventMap may not be null");
125
126         this.key = key;
127         this.eventMap = new TreeMap<>();
128         this.eventMap.putAll(eventMap);
129     }
130
131     /**
132      * When a model is unmarshalled from disk or from the database, the event map is returned as a raw hash map. This
133      * method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so that
134      * it will work with the {@link AxConceptGetter} interface.
135      *
136      * @param unmarshaler the unmarshaler that is unmarshaling the model
137      * @param parent the parent object of this object in the unmarshaler
138      */
139     public void afterUnmarshal(final Unmarshaller unmarshaler, final Object parent) {
140         // The map must be navigable to allow name and version searching, unmarshaling returns a hash map
141         final NavigableMap<AxArtifactKey, AxEvent> navigableEventMap = new TreeMap<>();
142         navigableEventMap.putAll(eventMap);
143         eventMap = navigableEventMap;
144     }
145
146     /**
147      * {@inheritDoc}.
148      */
149     @Override
150     public AxArtifactKey getKey() {
151         return key;
152     }
153
154     /**
155      * {@inheritDoc}.
156      */
157     @Override
158     public List<AxKey> getKeys() {
159         final List<AxKey> keyList = key.getKeys();
160
161         for (final AxEvent event : eventMap.values()) {
162             keyList.addAll(event.getKeys());
163         }
164
165         return keyList;
166     }
167
168     /**
169      * Sets the key of the event container.
170      *
171      * @param key the event container key
172      */
173     public void setKey(final AxArtifactKey key) {
174         Assertions.argumentNotNull(key, "key may not be null");
175         this.key = key;
176     }
177
178     /**
179      * Gets the event map containing the events in the event container.
180      *
181      * @return the event map with all the events in the event container
182      */
183     public Map<AxArtifactKey, AxEvent> getEventMap() {
184         return eventMap;
185     }
186
187     /**
188      * Sets the event map containing the events in the event container.
189      *
190      * @param eventMap the event map containing the events in the event container
191      */
192     public void setEventMap(final Map<AxArtifactKey, AxEvent> eventMap) {
193         Assertions.argumentNotNull(eventMap, "eventMap may not be null");
194         this.eventMap = new TreeMap<>();
195         this.eventMap.putAll(eventMap);
196     }
197
198     /**
199      * {@inheritDoc}.
200      */
201     @Override
202     public AxValidationResult validate(final AxValidationResult resultIn) {
203         AxValidationResult result = resultIn;
204
205         if (key.equals(AxArtifactKey.getNullKey())) {
206             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
207                             "key is a null key"));
208         }
209
210         result = key.validate(result);
211
212         if (eventMap.size() == 0) {
213             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
214                             "eventMap may not be empty"));
215         } else {
216             for (final Entry<AxArtifactKey, AxEvent> eventEntry : eventMap.entrySet()) {
217                 if (eventEntry.getKey().equals(AxArtifactKey.getNullKey())) {
218                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
219                                     "key on event entry " + eventEntry.getKey() + " may not be the null key"));
220                 } else if (eventEntry.getValue() == null) {
221                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
222                                     "value on event entry " + eventEntry.getKey() + " may not be null"));
223                 } else {
224                     if (!eventEntry.getKey().equals(eventEntry.getValue().getKey())) {
225                         result.addValidationMessage(
226                                         new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
227                                                         "key on event entry key " + eventEntry.getKey()
228                                                                         + " does not equal event value key "
229                                                                         + eventEntry.getValue().getKey()));
230                     }
231
232                     result = eventEntry.getValue().validate(result);
233                 }
234             }
235         }
236
237         return result;
238     }
239
240     /**
241      * {@inheritDoc}.
242      */
243     @Override
244     public void clean() {
245         key.clean();
246         for (final Entry<AxArtifactKey, AxEvent> eventEntry : eventMap.entrySet()) {
247             eventEntry.getKey().clean();
248             eventEntry.getValue().clean();
249         }
250     }
251
252     /**
253      * {@inheritDoc}.
254      */
255     @Override
256     public String toString() {
257         final StringBuilder builder = new StringBuilder();
258         builder.append(this.getClass().getSimpleName());
259         builder.append(":(");
260         builder.append("key=");
261         builder.append(key);
262         builder.append(",eventMap=");
263         builder.append(eventMap);
264         builder.append(")");
265         return builder.toString();
266     }
267
268     /**
269      * {@inheritDoc}.
270      */
271     @Override
272     public AxConcept copyTo(final AxConcept targetObject) {
273         Assertions.argumentNotNull(targetObject, "target may not be null");
274
275         final Object copyObject = targetObject;
276         Assertions.instanceOf(copyObject, AxEvents.class);
277
278         final AxEvents copy = (AxEvents) copyObject;
279         copy.setKey(new AxArtifactKey(key));
280         final Map<AxArtifactKey, AxEvent> newEventMap = new TreeMap<>();
281         for (final Entry<AxArtifactKey, AxEvent> eventMapEntry : eventMap.entrySet()) {
282             newEventMap.put(new AxArtifactKey(eventMapEntry.getKey()), new AxEvent(eventMapEntry.getValue()));
283         }
284         copy.setEventMap(newEventMap);
285
286         return copy;
287     }
288
289     /**
290      * {@inheritDoc}.
291      */
292     @Override
293     public int hashCode() {
294         final int prime = 31;
295         int result = 1;
296         result = prime * result + key.hashCode();
297         result = prime * result + eventMap.hashCode();
298         return result;
299     }
300
301     /**
302      * {@inheritDoc}.
303      */
304     @Override
305     public boolean equals(final Object obj) {
306         if (obj == null) {
307             return false;
308         }
309         if (this == obj) {
310             return true;
311         }
312
313         if (getClass() != obj.getClass()) {
314             return false;
315         }
316
317         final AxEvents other = (AxEvents) obj;
318         if (!key.equals(other.key)) {
319             return false;
320         }
321         return eventMap.equals(other.eventMap);
322     }
323
324     /**
325      * {@inheritDoc}.
326      */
327     @Override
328     public int compareTo(final AxConcept otherObj) {
329         if (otherObj == null) {
330             return -1;
331         }
332         if (this == otherObj) {
333             return 0;
334         }
335         if (getClass() != otherObj.getClass()) {
336             return this.hashCode() - otherObj.hashCode();
337         }
338
339         final AxEvents other = (AxEvents) otherObj;
340         if (!key.equals(other.key)) {
341             return key.compareTo(other.key);
342         }
343         if (!eventMap.equals(other.eventMap)) {
344             return (eventMap.hashCode() - other.eventMap.hashCode());
345         }
346
347         return 0;
348     }
349
350     /**
351      * {@inheritDoc}.
352      */
353     @Override
354     public AxEvent get(final AxArtifactKey conceptKey) {
355         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxEvent>) eventMap).get(conceptKey);
356     }
357
358     /**
359      * {@inheritDoc}.
360      */
361     @Override
362     public AxEvent get(final String conceptKeyName) {
363         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxEvent>) eventMap).get(conceptKeyName);
364     }
365
366     /**
367      * {@inheritDoc}.
368      */
369     @Override
370     public AxEvent get(final String conceptKeyName, final String conceptKeyVersion) {
371         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxEvent>) eventMap).get(conceptKeyName,
372                         conceptKeyVersion);
373     }
374
375     /**
376      * {@inheritDoc}.
377      */
378     @Override
379     public Set<AxEvent> getAll(final String conceptKeyName) {
380         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxEvent>) eventMap).getAll(conceptKeyName);
381     }
382
383     /**
384      * {@inheritDoc}.
385      */
386     @Override
387     public Set<AxEvent> getAll(final String conceptKeyName, final String conceptKeyVersion) {
388         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxEvent>) eventMap).getAll(conceptKeyName,
389                         conceptKeyVersion);
390     }
391 }