65247ab80fe963099049e638b862554b033ddf93
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2022 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.xml.bind.Unmarshaller;
32 import javax.xml.bind.annotation.XmlAccessType;
33 import javax.xml.bind.annotation.XmlAccessorType;
34 import javax.xml.bind.annotation.XmlElement;
35 import javax.xml.bind.annotation.XmlType;
36 import lombok.AccessLevel;
37 import lombok.EqualsAndHashCode;
38 import lombok.Getter;
39 import lombok.ToString;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
48 import org.onap.policy.common.utils.validation.Assertions;
49
50 /**
51  * This class is a context album container and holds a map of the context albums for an entire Apex model. All Apex
52  * models that use context albums must have an {@link AxContextAlbums} field. The {@link AxContextAlbums} class
53  * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextAlbum} instances to
54  * be retrieved by calling methods directly on this class without referencing the contained map.
55  *
56  * <p>Validation checks that the container key is not null. An observation is issued if no context albums are defined in
57  * the container. If context albums do exist, they are checked to ensure that keys and values are not null and that the
58  * map key matches the key in the map value for all album entries. Each context album entry is then validated
59  * individually.
60  */
61 @Getter
62 @ToString
63 @EqualsAndHashCode(callSuper = false)
64
65 @XmlAccessorType(XmlAccessType.FIELD)
66 @XmlType(name = "AxContextAlbums", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
67     { "key", "albums" })
68
69 public final class AxContextAlbums extends AxConcept implements AxConceptGetter<AxContextAlbum> {
70     private static final long serialVersionUID = -4844259809024470975L;
71
72     @XmlElement(name = "key", required = true)
73     private AxArtifactKey key;
74
75     @XmlElement(name = "albums", required = true)
76     @Getter(AccessLevel.NONE)
77     private Map<AxArtifactKey, AxContextAlbum> albums;
78
79     /**
80      * The Default Constructor creates a {@link AxContextAlbums} object with a null artifact key and creates an empty
81      * context album map.
82      */
83     public AxContextAlbums() {
84         this(new AxArtifactKey());
85     }
86
87     /**
88      * Copy constructor.
89      *
90      * @param copyConcept the concept to copy from
91      */
92     public AxContextAlbums(final AxContextAlbums copyConcept) {
93         super(copyConcept);
94     }
95
96     /**
97      * The Key Constructor creates a {@link AxContextAlbums} object with the given artifact key and creates an empty
98      * context album map.
99      *
100      * @param key the key of the context album container
101      */
102     public AxContextAlbums(final AxArtifactKey key) {
103         this(key, new TreeMap<>());
104     }
105
106     /**
107      * Constructor that creates the context album map with the given albums and key.
108      *
109      * @param key the key of the context album container
110      * @param albums the context albums to place in this context album container
111      */
112     public AxContextAlbums(final AxArtifactKey key, final Map<AxArtifactKey, AxContextAlbum> albums) {
113         super();
114         Assertions.argumentNotNull(key, "key may not be null");
115         Assertions.argumentNotNull(albums, "albums may not be null");
116
117         this.key = key;
118         this.albums = new TreeMap<>();
119         this.albums.putAll(albums);
120     }
121
122     /**
123      * When a model is unmarshalled from disk or from the database, the context album map is returned as a raw hash map.
124      * This method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so
125      * that it will work with the {@link AxConceptGetter} interface.
126      *
127      * @param unmarsaller the unmarshaler that is unmarshaling the model
128      * @param parent the parent object of this object in the unmarshaler
129      */
130     public void afterUnmarshal(final Unmarshaller unmarsaller, final Object parent) {
131         Assertions.argumentNotNull(unmarsaller, "unmarsaller should not be null");
132         Assertions.argumentNotNull(parent, "parent should not be null");
133
134         // The map must be navigable to allow name and version searching, unmarshaling returns a
135         // hash map
136         final NavigableMap<AxArtifactKey, AxContextAlbum> navigableAlbums = new TreeMap<>();
137         navigableAlbums.putAll(albums);
138         albums = navigableAlbums;
139     }
140
141     /**
142      * {@inheritDoc}.
143      */
144     @Override
145     public List<AxKey> getKeys() {
146         final List<AxKey> keyList = key.getKeys();
147
148         for (final AxContextAlbum contextAlbum : albums.values()) {
149             keyList.addAll(contextAlbum.getKeys());
150         }
151
152         return keyList;
153     }
154
155     /**
156      * Sets the key of the context album container.
157      *
158      * @param key the context album container key
159      */
160     public void setKey(final AxArtifactKey key) {
161         Assertions.argumentNotNull(key, "key may not be null");
162         this.key = key;
163     }
164
165     /**
166      * Gets the map of context albums from the context album container.
167      *
168      * @return the context album map
169      */
170     public Map<AxArtifactKey, AxContextAlbum> getAlbumsMap() {
171         return albums;
172     }
173
174     /**
175      * Sets the map of context albums from the context album container.
176      *
177      * @param albumsMap the map of context albums to place in the container
178      */
179     public void setAlbumsMap(final Map<AxArtifactKey, AxContextAlbum> albumsMap) {
180         Assertions.argumentNotNull(albumsMap, "albums may not be null");
181         this.albums = new TreeMap<>();
182         this.albums.putAll(albumsMap);
183     }
184
185     /**
186      * {@inheritDoc}.
187      */
188     @Override
189     public void clean() {
190         key.clean();
191         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
192             contextAlbumEntry.getKey().clean();
193             contextAlbumEntry.getValue().clean();
194         }
195     }
196
197     /**
198      * {@inheritDoc}.
199      */
200     @Override
201     public AxValidationResult validate(final AxValidationResult resultIn) {
202         AxValidationResult result = resultIn;
203
204         if (key.equals(AxArtifactKey.getNullKey())) {
205             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
206                             "key is a null key"));
207         }
208
209         result = key.validate(result);
210
211         if (albums.size() == 0) {
212             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
213                             "albums are empty"));
214         } else {
215             for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
216                 if (contextAlbumEntry.getKey().equals(AxArtifactKey.getNullKey())) {
217                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
218                                     "key on context album entry " + contextAlbumEntry.getKey()
219                                                     + " may not be the null key"));
220                 } else if (contextAlbumEntry.getValue() == null) {
221                     result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
222                                     "value on context album entry " + contextAlbumEntry.getKey() + " may not be null"));
223                 } else {
224                     validateContextAlbumKey(result, contextAlbumEntry);
225
226                     result = contextAlbumEntry.getValue().validate(result);
227                 }
228             }
229         }
230
231         return result;
232     }
233
234     private void validateContextAlbumKey(final AxValidationResult result,
235                     final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry) {
236         if (!contextAlbumEntry.getKey().equals(contextAlbumEntry.getValue().getKey())) {
237             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
238                             "key on context album entry key " + contextAlbumEntry.getKey()
239                                             + " does not equal context album value key "
240                                             + contextAlbumEntry.getValue().getKey()));
241         }
242     }
243
244     /**
245      * {@inheritDoc}.
246      */
247     @Override
248     public AxConcept copyTo(final AxConcept target) {
249         Assertions.argumentNotNull(target, "target may not be null");
250
251         final Object copyObject = target;
252         Assertions.instanceOf(copyObject, AxContextAlbums.class);
253
254         final AxContextAlbums copy = ((AxContextAlbums) copyObject);
255         copy.setKey(key);
256         final Map<AxArtifactKey, AxContextAlbum> newContextAlbum = new TreeMap<>();
257         for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
258             newContextAlbum.put(new AxArtifactKey(contextAlbumEntry.getKey()),
259                             new AxContextAlbum(contextAlbumEntry.getValue()));
260         }
261         copy.setAlbumsMap(newContextAlbum);
262
263         return copy;
264     }
265
266     /**
267      * {@inheritDoc}.
268      */
269     @Override
270     public int compareTo(final AxConcept otherObj) {
271         if (otherObj == null) {
272             return -1;
273         }
274         if (this == otherObj) {
275             return 0;
276         }
277         if (getClass() != otherObj.getClass()) {
278             return this.hashCode() - otherObj.hashCode();
279         }
280
281         final AxContextAlbums other = (AxContextAlbums) otherObj;
282         if (!key.equals(other.key)) {
283             return key.compareTo(other.key);
284         }
285         if (!albums.equals(other.albums)) {
286             return (albums.hashCode() - other.albums.hashCode());
287         }
288
289         return 0;
290     }
291
292     /**
293      * {@inheritDoc}.
294      */
295     @Override
296     public AxContextAlbum get(final AxArtifactKey conceptKey) {
297         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
298     }
299
300     /**
301      * {@inheritDoc}.
302      */
303     @Override
304     public AxContextAlbum get(final String conceptKeyName) {
305         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
306     }
307
308     /**
309      * {@inheritDoc}.
310      */
311     @Override
312     public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
313         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
314                         conceptKeyVersion);
315     }
316
317     /**
318      * {@inheritDoc}.
319      */
320     @Override
321     public Set<AxContextAlbum> getAll(final String conceptKeyName) {
322         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
323     }
324
325     /**
326      * {@inheritDoc}.
327      */
328     @Override
329     public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
330         return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,
331                         conceptKeyVersion);
332     }
333 }