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