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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.contextmodel.concepts;
25 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.NavigableMap;
30 import java.util.TreeMap;
31 import lombok.AccessLevel;
32 import lombok.EqualsAndHashCode;
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;
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.
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
58 @EqualsAndHashCode(callSuper = false)
59 public final class AxContextAlbums extends AxConcept implements AxConceptGetter<AxContextAlbum> {
60 private static final long serialVersionUID = -4844259809024470975L;
62 private AxArtifactKey key;
64 @Getter(AccessLevel.NONE)
65 private Map<AxArtifactKey, AxContextAlbum> albums;
68 * The Default Constructor creates a {@link AxContextAlbums} object with a null artifact key and creates an empty
71 public AxContextAlbums() {
72 this(new AxArtifactKey());
78 * @param copyConcept the concept to copy from
80 public AxContextAlbums(final AxContextAlbums copyConcept) {
85 * The Key Constructor creates a {@link AxContextAlbums} object with the given artifact key and creates an empty
88 * @param key the key of the context album container
90 public AxContextAlbums(final AxArtifactKey key) {
91 this(key, new TreeMap<>());
95 * Constructor that creates the context album map with the given albums and key.
97 * @param key the key of the context album container
98 * @param albums the context albums to place in this context album container
100 public AxContextAlbums(final AxArtifactKey key, final Map<AxArtifactKey, AxContextAlbum> albums) {
102 Assertions.argumentNotNull(key, "key may not be null");
103 Assertions.argumentNotNull(albums, "albums may not be null");
106 this.albums = new TreeMap<>();
107 this.albums.putAll(albums);
114 public List<AxKey> getKeys() {
115 final List<AxKey> keyList = key.getKeys();
117 for (final AxContextAlbum contextAlbum : albums.values()) {
118 keyList.addAll(contextAlbum.getKeys());
128 public void buildReferences() {
129 albums.values().stream().forEach(album -> album.buildReferences());
133 * Sets the key of the context album container.
135 * @param key the context album container key
137 public void setKey(final AxArtifactKey key) {
138 Assertions.argumentNotNull(key, "key may not be null");
143 * Gets the map of context albums from the context album container.
145 * @return the context album map
147 public Map<AxArtifactKey, AxContextAlbum> getAlbumsMap() {
152 * Sets the map of context albums from the context album container.
154 * @param albumsMap the map of context albums to place in the container
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);
166 public void clean() {
168 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
169 contextAlbumEntry.getKey().clean();
170 contextAlbumEntry.getValue().clean();
178 public AxValidationResult validate(final AxValidationResult resultIn) {
179 AxValidationResult result = resultIn;
181 if (key.equals(AxArtifactKey.getNullKey())) {
182 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
183 "key is a null key"));
186 result = key.validate(result);
188 if (albums.size() == 0) {
189 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
190 "albums are empty"));
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"));
201 validateContextAlbumKey(result, contextAlbumEntry);
203 result = contextAlbumEntry.getValue().validate(result);
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()));
225 public AxConcept copyTo(final AxConcept target) {
226 Assertions.argumentNotNull(target, "target may not be null");
228 final Object copyObject = target;
229 Assertions.instanceOf(copyObject, AxContextAlbums.class);
231 final AxContextAlbums copy = ((AxContextAlbums) copyObject);
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()));
238 copy.setAlbumsMap(newContextAlbum);
247 public int compareTo(final AxConcept otherObj) {
248 if (otherObj == null) {
251 if (this == otherObj) {
254 if (getClass() != otherObj.getClass()) {
255 return this.hashCode() - otherObj.hashCode();
258 final AxContextAlbums other = (AxContextAlbums) otherObj;
259 if (!key.equals(other.key)) {
260 return key.compareTo(other.key);
262 if (!albums.equals(other.albums)) {
263 return (albums.hashCode() - other.albums.hashCode());
273 public AxContextAlbum get(final AxArtifactKey conceptKey) {
274 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
281 public AxContextAlbum get(final String conceptKeyName) {
282 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
289 public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
290 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
298 public Set<AxContextAlbum> getAll(final String conceptKeyName) {
299 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
306 public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
307 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,