2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.contextmodel.concepts;
24 import java.util.List;
26 import java.util.Map.Entry;
27 import java.util.NavigableMap;
29 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;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
52 import org.onap.policy.common.utils.validation.Assertions;
55 * This class is a context album container and holds a map of the context albums for an entire Apex model. All Apex
56 * models that use context albums must have an {@link AxContextAlbums} field. The {@link AxContextAlbums} class
57 * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextAlbum} instances to
58 * be retrieved by calling methods directly on this class without referencing the contained map.
60 * <p>Validation checks that the container key is not null. An observation is issued if no context albums are defined in
61 * the container. If context albums do exist, they are checked to ensure that keys and values are not null and that the
62 * map key matches the key in the map value for all album entries. Each context album entry is then validated
66 @Table(name = "AxContextAlbums")
68 @XmlAccessorType(XmlAccessType.FIELD)
69 @XmlType(name = "AxContextAlbums", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
72 public final class AxContextAlbums extends AxConcept implements AxConceptGetter<AxContextAlbum> {
73 private static final long serialVersionUID = -4844259809024470975L;
76 @XmlElement(name = "key", required = true)
77 private AxArtifactKey key;
80 @OneToMany(cascade = CascadeType.ALL)
81 @JoinTable(joinColumns = {@JoinColumn(name = "contextName", referencedColumnName = "name"),
82 @JoinColumn(name = "contextVersion", referencedColumnName = "version")})
83 @XmlElement(name = "albums", required = true)
84 private Map<AxArtifactKey, AxContextAlbum> albums;
88 * The Default Constructor creates a {@link AxContextAlbums} object with a null artifact key and creates an empty
91 public AxContextAlbums() {
92 this(new AxArtifactKey());
98 * @param copyConcept the concept to copy from
100 public AxContextAlbums(final AxContextAlbums copyConcept) {
105 * The Key Constructor creates a {@link AxContextAlbums} object with the given artifact key and creates an empty
108 * @param key the key of the context album container
110 public AxContextAlbums(final AxArtifactKey key) {
111 this(key, new TreeMap<AxArtifactKey, AxContextAlbum>());
115 * Constructor that creates the context album map with the given albums and key.
117 * @param key the key of the context album container
118 * @param albums the context albums to place in this context album container
120 public AxContextAlbums(final AxArtifactKey key, final Map<AxArtifactKey, AxContextAlbum> albums) {
122 Assertions.argumentNotNull(key, "key may not be null");
123 Assertions.argumentNotNull(albums, "albums may not be null");
126 this.albums = new TreeMap<>();
127 this.albums.putAll(albums);
131 * When a model is unmarshalled from disk or from the database, the context album map is returned as a raw hash map.
132 * This method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so
133 * that it will work with the {@link AxConceptGetter} interface.
135 * @param unmarsaller the unmarshaler that is unmarshaling the model
136 * @param parent the parent object of this object in the unmarshaler
138 public void afterUnmarshal(final Unmarshaller unmarsaller, final Object parent) {
139 Assertions.argumentNotNull(unmarsaller, "unmarsaller should not be null");
140 Assertions.argumentNotNull(parent, "parent should not be null");
142 // The map must be navigable to allow name and version searching, unmarshaling returns a
144 final NavigableMap<AxArtifactKey, AxContextAlbum> navigableAlbums = new TreeMap<>();
145 navigableAlbums.putAll(albums);
146 albums = navigableAlbums;
153 public AxArtifactKey getKey() {
161 public List<AxKey> getKeys() {
162 final List<AxKey> keyList = key.getKeys();
164 for (final AxContextAlbum contextAlbum : albums.values()) {
165 keyList.addAll(contextAlbum.getKeys());
172 * Sets the key of the context album container.
174 * @param key the context album container key
176 public void setKey(final AxArtifactKey key) {
177 Assertions.argumentNotNull(key, "key may not be null");
182 * Gets the map of context albums from the context album container.
184 * @return the context album map
186 public Map<AxArtifactKey, AxContextAlbum> getAlbumsMap() {
191 * Sets the map of context albums from the context album container.
193 * @param albumsMap the map of context albums to place in the container
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);
205 public void clean() {
207 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
208 contextAlbumEntry.getKey().clean();
209 contextAlbumEntry.getValue().clean();
217 public String toString() {
218 final StringBuilder builder = new StringBuilder();
219 builder.append(this.getClass().getSimpleName());
220 builder.append(":(");
221 builder.append(this.getClass().getSimpleName());
222 builder.append(":(");
223 builder.append("key=");
225 builder.append(",albums=");
226 builder.append(albums);
228 return builder.toString();
235 public AxValidationResult validate(final AxValidationResult resultIn) {
236 AxValidationResult result = resultIn;
238 if (key.equals(AxArtifactKey.getNullKey())) {
239 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
240 "key is a null key"));
243 result = key.validate(result);
245 if (albums.size() == 0) {
246 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
247 "albums are empty"));
249 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
250 if (contextAlbumEntry.getKey().equals(AxArtifactKey.getNullKey())) {
251 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
252 "key on context album entry " + contextAlbumEntry.getKey()
253 + " may not be the null key"));
254 } else if (contextAlbumEntry.getValue() == null) {
255 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
256 "value on context album entry " + contextAlbumEntry.getKey() + " may not be null"));
258 validateContextAlbumKey(result, contextAlbumEntry);
260 result = contextAlbumEntry.getValue().validate(result);
268 private void validateContextAlbumKey(final AxValidationResult result,
269 final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry) {
270 if (!contextAlbumEntry.getKey().equals(contextAlbumEntry.getValue().getKey())) {
271 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
272 "key on context album entry key " + contextAlbumEntry.getKey()
273 + " does not equal context album value key "
274 + contextAlbumEntry.getValue().getKey()));
282 public AxConcept copyTo(final AxConcept target) {
283 Assertions.argumentNotNull(target, "target may not be null");
285 final Object copyObject = target;
286 Assertions.instanceOf(copyObject, AxContextAlbums.class);
288 final AxContextAlbums copy = ((AxContextAlbums) copyObject);
290 final Map<AxArtifactKey, AxContextAlbum> newContextAlbum = new TreeMap<>();
291 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
292 newContextAlbum.put(new AxArtifactKey(contextAlbumEntry.getKey()),
293 new AxContextAlbum(contextAlbumEntry.getValue()));
295 copy.setAlbumsMap(newContextAlbum);
304 public int hashCode() {
305 final int prime = 31;
307 result = prime * result + key.hashCode();
308 result = prime * result + albums.hashCode();
316 public boolean equals(final Object obj) {
324 if (getClass() != obj.getClass()) {
328 final AxContextAlbums other = (AxContextAlbums) obj;
329 if (!key.equals(other.key)) {
332 return albums.equals(other.albums);
339 public int compareTo(final AxConcept otherObj) {
340 if (otherObj == null) {
343 if (this == otherObj) {
346 if (getClass() != otherObj.getClass()) {
347 return this.hashCode() - otherObj.hashCode();
350 final AxContextAlbums other = (AxContextAlbums) otherObj;
351 if (!key.equals(other.key)) {
352 return key.compareTo(other.key);
354 if (!albums.equals(other.albums)) {
355 return (albums.hashCode() - other.albums.hashCode());
365 public AxContextAlbum get(final AxArtifactKey conceptKey) {
366 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
373 public AxContextAlbum get(final String conceptKeyName) {
374 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
381 public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
382 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
390 public Set<AxContextAlbum> getAll(final String conceptKeyName) {
391 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
398 public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
399 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,