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