aa5a89a4dd46f697bb12ddbcd0608642b1430d0b
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2021 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.contextmodel.concepts;
24
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.NavigableMap;
29 import java.util.Set;
30 import java.util.TreeMap;
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.OneToMany;
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 import lombok.AccessLevel;
44 import lombok.EqualsAndHashCode;
45 import lombok.Getter;
46 import lombok.ToString;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
52 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
53 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
54 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
55 import org.onap.policy.common.utils.validation.Assertions;
56
57 /**
58  * This class is a context album container and holds a map of the context albums for an entire Apex model. All Apex
59  * models that use context albums must have an {@link AxContextAlbums} field. The {@link AxContextAlbums} class
60  * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextAlbum} instances to
61  * be retrieved by calling methods directly on this class without referencing the contained map.
62  *
63  * <p>Validation checks that the container key is not null. An observation is issued if no context albums are defined in
64  * the container. If context albums do exist, they are checked to ensure that keys and values are not null and that the
65  * map key matches the key in the map value for all album entries. Each context album entry is then validated
66  * individually.
67  */
68 @Entity
69 @Table(name = "AxContextAlbums")
70
71 @Getter
72 @ToString
73 @EqualsAndHashCode(callSuper = false)
74
75 @XmlAccessorType(XmlAccessType.FIELD)
76 @XmlType(name = "AxContextAlbums", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
77     { "key", "albums" })
78
79 public final class AxContextAlbums extends AxConcept implements AxConceptGetter<AxContextAlbum> {
80     private static final long serialVersionUID = -4844259809024470975L;
81
82     @EmbeddedId
83     @XmlElement(name = "key", required = true)
84     private AxArtifactKey key;
85
86     // @formatter:off
87     @OneToMany(cascade = CascadeType.ALL)
88     @JoinTable(joinColumns = {@JoinColumn(name = "contextName", referencedColumnName = "name"),
89         @JoinColumn(name = "contextVersion", referencedColumnName = "version")})
90     @XmlElement(name = "albums", required = true)
91     @Getter(AccessLevel.NONE)
92     private Map<AxArtifactKey, AxContextAlbum> albums;
93     // @formatter:on
94
95     /**
96      * The Default Constructor creates a {@link AxContextAlbums} object with a null artifact key and creates an empty
97      * context album map.
98      */
99     public AxContextAlbums() {
100         this(new AxArtifactKey());
101     }
102
103     /**
104      * Copy constructor.
105      *
106      * @param copyConcept the concept to copy from
107      */
108     public AxContextAlbums(final AxContextAlbums copyConcept) {
109         super(copyConcept);
110     }
111
112     /**
113      * The Key Constructor creates a {@link AxContextAlbums} object with the given artifact key and creates an empty
114      * context album map.
115      *
116      * @param key the key of the context album container
117      */
118     public AxContextAlbums(final AxArtifactKey key) {
119         this(key, new TreeMap<>());
120     }
121
122     /**
123      * Constructor that creates the context album map with the given albums and key.
124      *
125      * @param key the key of the context album container
126      * @param albums the context albums to place in this context album container
127      */
128     public AxContextAlbums(final AxArtifactKey key, final Map<AxArtifactKey, AxContextAlbum> albums) {
129         super();
130         Assertions.argumentNotNull(key, "key may not be null");
131         Assertions.argumentNotNull(albums, "albums may not be null");
132
133         this.key = key;
134         this.albums = new TreeMap<>();
135         this.albums.putAll(albums);
136     }
137
138     /**
139      * When a model is unmarshalled from disk or from the database, the context album map is returned as a raw hash map.
140      * This method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so
141      * that it will work with the {@link AxConceptGetter} interface.
142      *
143      * @param unmarsaller the unmarshaler that is unmarshaling the model
144      * @param parent the parent object of this object in the unmarshaler
145      */
146     public void afterUnmarshal(final Unmarshaller unmarsaller, final Object parent) {
147         Assertions.argumentNotNull(unmarsaller, "unmarsaller should not be null");
148         Assertions.argumentNotNull(parent, "parent should not be null");
149
150         // The map must be navigable to allow name and version searching, unmarshaling returns a
151         // hash map
152         final NavigableMap<AxArtifactKey, AxContextAlbum> navigableAlbums = new TreeMap<>();
153         navigableAlbums.putAll(albums);
154         albums = navigableAlbums;
155     }
156
157     /**
158      * {@inheritDoc}.
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      * {@inheritDoc}.
203      */
204     @Override
205     public void clean() {
206         key.clean();
207         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
208             contextAlbumEntry.getKey().clean();
209             contextAlbumEntry.getValue().clean();
210         }
211     }
212
213     /**
214      * {@inheritDoc}.
215      */
216     @Override
217     public AxValidationResult validate(final AxValidationResult resultIn) {
218         AxValidationResult result = resultIn;
219
220         if (key.equals(AxArtifactKey.getNullKey())) {
221             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
222                             "key is a null key"));
223         }
224
225         result = key.validate(result);
226
227         if (albums.size() == 0) {
228             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
229                             "albums are empty"));
230         } else {
231             for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
232                 if (contextAlbumEntry.getKey().equals(AxArtifactKey.getNullKey())) {
233                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
234                                     "key on context album entry " + contextAlbumEntry.getKey()
235                                                     + " may not be the null key"));
236                 } else if (contextAlbumEntry.getValue() == null) {
237                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
238                                     "value on context album entry " + contextAlbumEntry.getKey() + " may not be null"));
239                 } else {
240                     validateContextAlbumKey(result, contextAlbumEntry);
241
242                     result = contextAlbumEntry.getValue().validate(result);
243                 }
244             }
245         }
246
247         return result;
248     }
249
250     private void validateContextAlbumKey(final AxValidationResult result,
251                     final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry) {
252         if (!contextAlbumEntry.getKey().equals(contextAlbumEntry.getValue().getKey())) {
253             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
254                             "key on context album entry key " + contextAlbumEntry.getKey()
255                                             + " does not equal context album value key "
256                                             + contextAlbumEntry.getValue().getKey()));
257         }
258     }
259
260     /**
261      * {@inheritDoc}.
262      */
263     @Override
264     public AxConcept copyTo(final AxConcept target) {
265         Assertions.argumentNotNull(target, "target may not be null");
266
267         final Object copyObject = target;
268         Assertions.instanceOf(copyObject, AxContextAlbums.class);
269
270         final AxContextAlbums copy = ((AxContextAlbums) copyObject);
271         copy.setKey(key);
272         final Map<AxArtifactKey, AxContextAlbum> newContextAlbum = new TreeMap<>();
273         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
274             newContextAlbum.put(new AxArtifactKey(contextAlbumEntry.getKey()),
275                             new AxContextAlbum(contextAlbumEntry.getValue()));
276         }
277         copy.setAlbumsMap(newContextAlbum);
278
279         return copy;
280     }
281
282     /**
283      * {@inheritDoc}.
284      */
285     @Override
286     public int compareTo(final AxConcept otherObj) {
287         if (otherObj == null) {
288             return -1;
289         }
290         if (this == otherObj) {
291             return 0;
292         }
293         if (getClass() != otherObj.getClass()) {
294             return this.hashCode() - otherObj.hashCode();
295         }
296
297         final AxContextAlbums other = (AxContextAlbums) otherObj;
298         if (!key.equals(other.key)) {
299             return key.compareTo(other.key);
300         }
301         if (!albums.equals(other.albums)) {
302             return (albums.hashCode() - other.albums.hashCode());
303         }
304
305         return 0;
306     }
307
308     /**
309      * {@inheritDoc}.
310      */
311     @Override
312     public AxContextAlbum get(final AxArtifactKey conceptKey) {
313         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
314     }
315
316     /**
317      * {@inheritDoc}.
318      */
319     @Override
320     public AxContextAlbum get(final String conceptKeyName) {
321         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
322     }
323
324     /**
325      * {@inheritDoc}.
326      */
327     @Override
328     public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
329         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
330                         conceptKeyVersion);
331     }
332
333     /**
334      * {@inheritDoc}.
335      */
336     @Override
337     public Set<AxContextAlbum> getAll(final String conceptKeyName) {
338         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
339     }
340
341     /**
342      * {@inheritDoc}.
343      */
344     @Override
345     public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
346         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,
347                         conceptKeyVersion);
348     }
349 }