9df11ae25bd58246311e7a69dfb1f1bf244cc272
[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         // The map must be navigable to allow name and version searching, unmarshaling returns a
139         // hash map
140         final NavigableMap<AxArtifactKey, AxContextAlbum> navigableAlbums = new TreeMap<>();
141         navigableAlbums.putAll(albums);
142         albums = navigableAlbums;
143     }
144
145     /*
146      * (non-Javadoc)
147      *
148      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
149      */
150     @Override
151     public AxArtifactKey getKey() {
152         return key;
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
159      */
160     @Override
161     public List<AxKey> getKeys() {
162         final List<AxKey> keyList = key.getKeys();
163
164         for (final AxContextAlbum contextAlbum : albums.values()) {
165             keyList.addAll(contextAlbum.getKeys());
166         }
167
168         return keyList;
169     }
170
171     /**
172      * Sets the key of the context album container.
173      *
174      * @param key the context album container key
175      */
176     public void setKey(final AxArtifactKey key) {
177         Assertions.argumentNotNull(key, "key may not be null");
178         this.key = key;
179     }
180
181     /**
182      * Gets the map of context albums from the context album container.
183      *
184      * @return the context album map
185      */
186     public Map<AxArtifactKey, AxContextAlbum> getAlbumsMap() {
187         return albums;
188     }
189
190     /**
191      * Sets the map of context albums from the context album container.
192      *
193      * @param albumsMap the map of context albums to place in the container
194      */
195     public void setAlbumsMap(final Map<AxArtifactKey, AxContextAlbum> albumsMap) {
196         Assertions.argumentNotNull(albumsMap, "albums may not be null");
197         this.albums = new TreeMap<>();
198         this.albums.putAll(albumsMap);
199     }
200
201     /*
202      * (non-Javadoc)
203      *
204      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
205      */
206     @Override
207     public void clean() {
208         key.clean();
209         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
210             contextAlbumEntry.getKey().clean();
211             contextAlbumEntry.getValue().clean();
212         }
213     }
214
215     /*
216      * (non-Javadoc)
217      *
218      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
219      */
220     @Override
221     public String toString() {
222         final StringBuilder builder = new StringBuilder();
223         builder.append(this.getClass().getSimpleName());
224         builder.append(":(");
225         builder.append(this.getClass().getSimpleName());
226         builder.append(":(");
227         builder.append("key=");
228         builder.append(key);
229         builder.append(",albums=");
230         builder.append(albums);
231         builder.append(")");
232         return builder.toString();
233     }
234
235     /*
236      * (non-Javadoc)
237      *
238      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
239      * basicmodel.concepts.AxValidationResult)
240      */
241     @Override
242     public AxValidationResult validate(final AxValidationResult resultIn) {
243         AxValidationResult result = resultIn;
244
245         if (key.equals(AxArtifactKey.getNullKey())) {
246             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
247                             "key is a null key"));
248         }
249
250         result = key.validate(result);
251
252         if (albums.size() == 0) {
253             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
254                             "albums are empty"));
255         } else {
256             for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
257                 if (contextAlbumEntry.getKey().equals(AxArtifactKey.getNullKey())) {
258                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
259                                     "key on context album entry " + contextAlbumEntry.getKey()
260                                                     + " may not be the null key"));
261                 } else if (contextAlbumEntry.getValue() == null) {
262                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
263                                     "value on context album entry " + contextAlbumEntry.getKey() + " may not be null"));
264                 } else {
265                     validateContextAlbumKey(result, contextAlbumEntry);
266
267                     result = contextAlbumEntry.getValue().validate(result);
268                 }
269             }
270         }
271
272         return result;
273     }
274
275     private void validateContextAlbumKey(final AxValidationResult result,
276                     final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry) {
277         if (!contextAlbumEntry.getKey().equals(contextAlbumEntry.getValue().getKey())) {
278             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
279                             "key on context album entry key " + contextAlbumEntry.getKey()
280                                             + " does not equal context album value key "
281                                             + contextAlbumEntry.getValue().getKey()));
282         }
283     }
284
285     /*
286      * (non-Javadoc)
287      *
288      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
289      * basicmodel.concepts.AxConcept)
290      */
291     @Override
292     public AxConcept copyTo(final AxConcept target) {
293         Assertions.argumentNotNull(target, "target may not be null");
294
295         final Object copyObject = target;
296         Assertions.instanceOf(copyObject, AxContextAlbums.class);
297
298         final AxContextAlbums copy = ((AxContextAlbums) copyObject);
299         copy.setKey(key);
300         final Map<AxArtifactKey, AxContextAlbum> newContextAlbum = new TreeMap<>();
301         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
302             newContextAlbum.put(new AxArtifactKey(contextAlbumEntry.getKey()),
303                             new AxContextAlbum(contextAlbumEntry.getValue()));
304         }
305         copy.setAlbumsMap(newContextAlbum);
306
307         return copy;
308     }
309
310     /*
311      * (non-Javadoc)
312      *
313      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
314      */
315     @Override
316     public int hashCode() {
317         final int prime = 31;
318         int result = 1;
319         result = prime * result + key.hashCode();
320         result = prime * result + albums.hashCode();
321         return result;
322     }
323
324     /*
325      * (non-Javadoc)
326      *
327      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
328      */
329     @Override
330     public boolean equals(final Object obj) {
331         if (obj == null) {
332             return false;
333         }
334         if (this == obj) {
335             return true;
336         }
337
338         if (getClass() != obj.getClass()) {
339             return false;
340         }
341
342         final AxContextAlbums other = (AxContextAlbums) obj;
343         if (!key.equals(other.key)) {
344             return false;
345         }
346         return albums.equals(other.albums);
347     }
348
349     /*
350      * (non-Javadoc)
351      *
352      * @see java.lang.Comparable#compareTo(java.lang.Object)
353      */
354     @Override
355     public int compareTo(final AxConcept otherObj) {
356         if (otherObj == null) {
357             return -1;
358         }
359         if (this == otherObj) {
360             return 0;
361         }
362         if (getClass() != otherObj.getClass()) {
363             return this.hashCode() - otherObj.hashCode();
364         }
365
366         final AxContextAlbums other = (AxContextAlbums) otherObj;
367         if (!key.equals(other.key)) {
368             return key.compareTo(other.key);
369         }
370         if (!albums.equals(other.albums)) {
371             return (albums.hashCode() - other.albums.hashCode());
372         }
373
374         return 0;
375     }
376
377     /*
378      * (non-Javadoc)
379      *
380      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.
381      * model.basicmodel.concepts.AxArtifactKey)
382      */
383     @Override
384     public AxContextAlbum get(final AxArtifactKey conceptKey) {
385         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
386     }
387
388     /*
389      * (non-Javadoc)
390      *
391      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String)
392      */
393     @Override
394     public AxContextAlbum get(final String conceptKeyName) {
395         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
396     }
397
398     /*
399      * (non-Javadoc)
400      *
401      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String, java.lang.String)
402      */
403     @Override
404     public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
405         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
406                         conceptKeyVersion);
407     }
408
409     /*
410      * (non-Javadoc)
411      *
412      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String)
413      */
414     @Override
415     public Set<AxContextAlbum> getAll(final String conceptKeyName) {
416         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
417     }
418
419     /*
420      * (non-Javadoc)
421      *
422      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String, java.lang.String)
423      */
424     @Override
425     public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
426         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,
427                         conceptKeyVersion);
428     }
429 }