cd452cae13c6e0499dbe9884544733a3d3eb691c
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.contextmodel.concepts;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.NavigableMap;
27 import java.util.Set;
28 import java.util.TreeMap;
29
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.OneToMany;
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
43 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
51 import org.onap.policy.apex.model.utilities.Assertions;
52
53 /**
54  * This class is a context album container and holds a map of the context albums for an entire Apex model. All Apex
55  * models that use context albums must have an {@link AxContextAlbums} field. The {@link AxContextAlbums} class
56  * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextAlbum} instances to
57  * be retrieved by calling methods directly on this class without referencing the contained map.
58  * 
59  * <p>Validation checks that the container key is not null. An observation is issued if no context albums are defined in
60  * the container. If context albums do exist, they are checked to ensure that keys and values are not null and that the
61  * map key matches the key in the map value for all album entries. Each context album entry is then validated
62  * individually.
63  */
64 @Entity
65 @Table(name = "AxContextAlbums")
66
67 @XmlAccessorType(XmlAccessType.FIELD)
68 @XmlType(name = "AxContextAlbums", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
69     { "key", "albums" })
70
71 public final class AxContextAlbums extends AxConcept implements AxConceptGetter<AxContextAlbum> {
72     private static final long serialVersionUID = -4844259809024470975L;
73
74     @EmbeddedId
75     @XmlElement(name = "key", required = true)
76     private AxArtifactKey key;
77
78     // @formatter:off
79     @OneToMany(cascade = CascadeType.ALL)
80     @JoinTable(joinColumns = {@JoinColumn(name = "contextName", referencedColumnName = "name"),
81             @JoinColumn(name = "contextVersion", referencedColumnName = "version")})
82     @XmlElement(name = "albums", required = true)
83     private Map<AxArtifactKey, AxContextAlbum> albums;
84     // @formatter:on
85
86     /**
87      * The Default Constructor creates a {@link AxContextAlbums} object with a null artifact key and creates an empty
88      * context album map.
89      */
90     public AxContextAlbums() {
91         this(new AxArtifactKey());
92     }
93
94     /**
95      * Copy constructor.
96      *
97      * @param copyConcept the concept to copy from
98      */
99     public AxContextAlbums(final AxContextAlbums copyConcept) {
100         super(copyConcept);
101     }
102
103     /**
104      * The Key Constructor creates a {@link AxContextAlbums} object with the given artifact key and creates an empty
105      * context album map.
106      *
107      * @param key the key of the context album container
108      */
109     public AxContextAlbums(final AxArtifactKey key) {
110         this(key, new TreeMap<AxArtifactKey, AxContextAlbum>());
111     }
112
113     /**
114      * Constructor that creates the context album map with the given albums and key.
115      *
116      * @param key the key of the context album container
117      * @param albums the context albums to place in this context album container
118      */
119     public AxContextAlbums(final AxArtifactKey key, final Map<AxArtifactKey, AxContextAlbum> albums) {
120         super();
121         Assertions.argumentNotNull(key, "key may not be null");
122         Assertions.argumentNotNull(albums, "albums may not be null");
123
124         this.key = key;
125         this.albums = new TreeMap<>();
126         this.albums.putAll(albums);
127     }
128
129     /**
130      * When a model is unmarshalled from disk or from the database, the context album map is returned as a raw hash map.
131      * This method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so
132      * that it will work with the {@link AxConceptGetter} interface.
133      *
134      * @param unmarsaller 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 unmarsaller, final Object parent) {
138         Assertions.argumentNotNull(unmarsaller, "unmarsaller should not be null");
139         Assertions.argumentNotNull(parent, "parent should not be null");
140
141         // The map must be navigable to allow name and version searching, unmarshaling returns a
142         // hash map
143         final NavigableMap<AxArtifactKey, AxContextAlbum> navigableAlbums = new TreeMap<>();
144         navigableAlbums.putAll(albums);
145         albums = navigableAlbums;
146     }
147
148     /*
149      * (non-Javadoc)
150      *
151      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
152      */
153     @Override
154     public AxArtifactKey getKey() {
155         return key;
156     }
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
162      */
163     @Override
164     public List<AxKey> getKeys() {
165         final List<AxKey> keyList = key.getKeys();
166
167         for (final AxContextAlbum contextAlbum : albums.values()) {
168             keyList.addAll(contextAlbum.getKeys());
169         }
170
171         return keyList;
172     }
173
174     /**
175      * Sets the key of the context album container.
176      *
177      * @param key the context album container key
178      */
179     public void setKey(final AxArtifactKey key) {
180         Assertions.argumentNotNull(key, "key may not be null");
181         this.key = key;
182     }
183
184     /**
185      * Gets the map of context albums from the context album container.
186      *
187      * @return the context album map
188      */
189     public Map<AxArtifactKey, AxContextAlbum> getAlbumsMap() {
190         return albums;
191     }
192
193     /**
194      * Sets the map of context albums from the context album container.
195      *
196      * @param albumsMap the map of context albums to place in the container
197      */
198     public void setAlbumsMap(final Map<AxArtifactKey, AxContextAlbum> albumsMap) {
199         Assertions.argumentNotNull(albumsMap, "albums may not be null");
200         this.albums = new TreeMap<>();
201         this.albums.putAll(albumsMap);
202     }
203
204     /*
205      * (non-Javadoc)
206      *
207      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
208      */
209     @Override
210     public void clean() {
211         key.clean();
212         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
213             contextAlbumEntry.getKey().clean();
214             contextAlbumEntry.getValue().clean();
215         }
216     }
217
218     /*
219      * (non-Javadoc)
220      *
221      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
222      */
223     @Override
224     public String toString() {
225         final StringBuilder builder = new StringBuilder();
226         builder.append(this.getClass().getSimpleName());
227         builder.append(":(");
228         builder.append(this.getClass().getSimpleName());
229         builder.append(":(");
230         builder.append("key=");
231         builder.append(key);
232         builder.append(",albums=");
233         builder.append(albums);
234         builder.append(")");
235         return builder.toString();
236     }
237
238     /*
239      * (non-Javadoc)
240      *
241      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
242      * basicmodel.concepts.AxValidationResult)
243      */
244     @Override
245     public AxValidationResult validate(final AxValidationResult resultIn) {
246         AxValidationResult result = resultIn;
247
248         if (key.equals(AxArtifactKey.getNullKey())) {
249             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
250                             "key is a null key"));
251         }
252
253         result = key.validate(result);
254
255         if (albums.size() == 0) {
256             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
257                             "albums are empty"));
258         } else {
259             for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
260                 if (contextAlbumEntry.getKey().equals(AxArtifactKey.getNullKey())) {
261                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
262                                     "key on context album entry " + contextAlbumEntry.getKey()
263                                                     + " may not be the null key"));
264                 } else if (contextAlbumEntry.getValue() == null) {
265                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
266                                     "value on context album entry " + contextAlbumEntry.getKey() + " may not be null"));
267                 } else {
268                     validateContextAlbumKey(result, contextAlbumEntry);
269
270                     result = contextAlbumEntry.getValue().validate(result);
271                 }
272             }
273         }
274
275         return result;
276     }
277
278     private void validateContextAlbumKey(final AxValidationResult result,
279                     final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry) {
280         if (!contextAlbumEntry.getKey().equals(contextAlbumEntry.getValue().getKey())) {
281             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
282                             "key on context album entry key " + contextAlbumEntry.getKey()
283                                             + " does not equal context album value key "
284                                             + contextAlbumEntry.getValue().getKey()));
285         }
286     }
287
288     /*
289      * (non-Javadoc)
290      *
291      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
292      * basicmodel.concepts.AxConcept)
293      */
294     @Override
295     public AxConcept copyTo(final AxConcept target) {
296         Assertions.argumentNotNull(target, "target may not be null");
297
298         final Object copyObject = target;
299         Assertions.instanceOf(copyObject, AxContextAlbums.class);
300
301         final AxContextAlbums copy = ((AxContextAlbums) copyObject);
302         copy.setKey(key);
303         final Map<AxArtifactKey, AxContextAlbum> newContextAlbum = new TreeMap<>();
304         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
305             newContextAlbum.put(new AxArtifactKey(contextAlbumEntry.getKey()),
306                             new AxContextAlbum(contextAlbumEntry.getValue()));
307         }
308         copy.setAlbumsMap(newContextAlbum);
309
310         return copy;
311     }
312
313     /*
314      * (non-Javadoc)
315      *
316      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
317      */
318     @Override
319     public int hashCode() {
320         final int prime = 31;
321         int result = 1;
322         result = prime * result + key.hashCode();
323         result = prime * result + albums.hashCode();
324         return result;
325     }
326
327     /*
328      * (non-Javadoc)
329      *
330      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
331      */
332     @Override
333     public boolean equals(final Object obj) {
334         if (obj == null) {
335             return false;
336         }
337         if (this == obj) {
338             return true;
339         }
340
341         if (getClass() != obj.getClass()) {
342             return false;
343         }
344
345         final AxContextAlbums other = (AxContextAlbums) obj;
346         if (!key.equals(other.key)) {
347             return false;
348         }
349         return albums.equals(other.albums);
350     }
351
352     /*
353      * (non-Javadoc)
354      *
355      * @see java.lang.Comparable#compareTo(java.lang.Object)
356      */
357     @Override
358     public int compareTo(final AxConcept otherObj) {
359         if (otherObj == null) {
360             return -1;
361         }
362         if (this == otherObj) {
363             return 0;
364         }
365         if (getClass() != otherObj.getClass()) {
366             return this.hashCode() - otherObj.hashCode();
367         }
368
369         final AxContextAlbums other = (AxContextAlbums) otherObj;
370         if (!key.equals(other.key)) {
371             return key.compareTo(other.key);
372         }
373         if (!albums.equals(other.albums)) {
374             return (albums.hashCode() - other.albums.hashCode());
375         }
376
377         return 0;
378     }
379
380     /*
381      * (non-Javadoc)
382      *
383      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.
384      * model.basicmodel.concepts.AxArtifactKey)
385      */
386     @Override
387     public AxContextAlbum get(final AxArtifactKey conceptKey) {
388         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
389     }
390
391     /*
392      * (non-Javadoc)
393      *
394      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String)
395      */
396     @Override
397     public AxContextAlbum get(final String conceptKeyName) {
398         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
399     }
400
401     /*
402      * (non-Javadoc)
403      *
404      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String, java.lang.String)
405      */
406     @Override
407     public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
408         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
409                         conceptKeyVersion);
410     }
411
412     /*
413      * (non-Javadoc)
414      *
415      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String)
416      */
417     @Override
418     public Set<AxContextAlbum> getAll(final String conceptKeyName) {
419         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
420     }
421
422     /*
423      * (non-Javadoc)
424      *
425      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String, java.lang.String)
426      */
427     @Override
428     public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
429         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,
430                         conceptKeyVersion);
431     }
432 }