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