2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.contextmodel.concepts;
23 import java.util.List;
25 import java.util.Map.Entry;
26 import java.util.NavigableMap;
28 import java.util.TreeMap;
30 import javax.persistence.CascadeType;
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.JoinTable;
35 import javax.persistence.OneToMany;
36 import javax.persistence.Table;
37 import javax.xml.bind.Unmarshaller;
38 import javax.xml.bind.annotation.XmlAccessType;
39 import javax.xml.bind.annotation.XmlAccessorType;
40 import javax.xml.bind.annotation.XmlElement;
41 import javax.xml.bind.annotation.XmlType;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
51 import org.onap.policy.apex.model.utilities.Assertions;
54 * This class is a context album container and holds a map of the context albums for an entire Apex
55 * model. All Apex models that use context albums must have an {@link AxContextAlbums} field. The
56 * {@link AxContextAlbums} class implements the helper methods of the {@link AxConceptGetter}
57 * interface to allow {@link AxContextAlbum} instances to be retrieved by calling methods directly
58 * on this class without referencing the contained map.
60 * Validation checks that the container key is not null. An observation is issued if no context
61 * albums are defined in the container. If context albums do exist, they are checked to ensure that
62 * keys and values are not null and that the map key matches the key in the map value for all album
63 * entries. Each context album entry is then validated individually.
66 @Table(name = "AxContextAlbums")
68 @XmlAccessorType(XmlAccessType.FIELD)
69 @XmlType(name = "AxContextAlbums", namespace = "http://www.onap.org/policy/apex-pdp", propOrder = {"key", "albums"})
71 public final class AxContextAlbums extends AxConcept implements AxConceptGetter<AxContextAlbum> {
72 private static final long serialVersionUID = -4844259809024470975L;
75 @XmlElement(name = "key", required = true)
76 private AxArtifactKey key;
79 @OneToMany(cascade = CascadeType.ALL)
80 @JoinTable(joinColumns = {@JoinColumn(name = "contextName", referencedColumnName = "name"),
81 @JoinColumn(name = "contextVersion", referencedColumnName = "version")})
82 @XmlElement(name = "albums", required = true)
83 private Map<AxArtifactKey, AxContextAlbum> albums;
87 * The Default Constructor creates a {@link AxContextAlbums} object with a null artifact key and
88 * creates an empty context album map.
90 public AxContextAlbums() {
91 this(new AxArtifactKey());
97 * @param copyConcept the concept to copy from
99 public AxContextAlbums(final AxContextAlbums copyConcept) {
104 * The Key Constructor creates a {@link AxContextAlbums} object with the given artifact key and
105 * creates an empty context album map.
107 * @param key the key of the context album container
109 public AxContextAlbums(final AxArtifactKey key) {
110 this(key, new TreeMap<AxArtifactKey, AxContextAlbum>());
114 * Constructor that creates the context album map with the given albums and key.
116 * @param key the key of the context album container
117 * @param albums the context albums to place in this context album container
119 public AxContextAlbums(final AxArtifactKey key, final Map<AxArtifactKey, AxContextAlbum> albums) {
121 Assertions.argumentNotNull(key, "key may not be null");
122 Assertions.argumentNotNull(albums, "albums may not be null");
125 this.albums = new TreeMap<>();
126 this.albums.putAll(albums);
130 * When a model is unmarshalled from disk or from the database, the context album map is
131 * returned as a raw hash map. This method is called by JAXB after unmarshaling and is used to
132 * convert the hash map to a {@link NavigableMap} so that it will work with the
133 * {@link AxConceptGetter} interface.
135 * @param u 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 u, final Object parent) {
139 // The map must be navigable to allow name and version searching, unmarshaling returns a
141 final NavigableMap<AxArtifactKey, AxContextAlbum> navigableAlbums = new TreeMap<>();
142 navigableAlbums.putAll(albums);
143 albums = navigableAlbums;
149 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
152 public AxArtifactKey getKey() {
159 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
162 public List<AxKey> getKeys() {
163 final List<AxKey> keyList = key.getKeys();
165 for (final AxContextAlbum contextAlbum : albums.values()) {
166 keyList.addAll(contextAlbum.getKeys());
173 * Sets the key of the context album container.
175 * @param key the context album container key
177 public void setKey(final AxArtifactKey key) {
178 Assertions.argumentNotNull(key, "key may not be null");
183 * Gets the map of context albums from the context album container.
185 * @return the context album map
187 public Map<AxArtifactKey, AxContextAlbum> getAlbumsMap() {
192 * Sets the map of context albums from the context album container.
194 * @param albumsMap the map of context albums to place in the container
196 public void setAlbumsMap(final Map<AxArtifactKey, AxContextAlbum> albumsMap) {
197 Assertions.argumentNotNull(albumsMap, "albums may not be null");
198 this.albums = new TreeMap<>();
199 this.albums.putAll(albumsMap);
205 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
208 public void clean() {
210 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
211 contextAlbumEntry.getKey().clean();
212 contextAlbumEntry.getValue().clean();
219 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
222 public String toString() {
223 final StringBuilder builder = new StringBuilder();
224 builder.append(this.getClass().getSimpleName());
225 builder.append(":(");
226 builder.append(this.getClass().getSimpleName());
227 builder.append(":(");
228 builder.append("key=");
230 builder.append(",albums=");
231 builder.append(albums);
233 return builder.toString();
240 * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
241 * basicmodel.concepts.AxValidationResult)
244 public AxValidationResult validate(final AxValidationResult resultIn) {
245 AxValidationResult result = resultIn;
247 if (key.equals(AxArtifactKey.getNullKey())) {
248 result.addValidationMessage(
249 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
252 result = key.validate(result);
254 if (albums.size() == 0) {
255 result.addValidationMessage(
256 new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION, "albums are empty"));
258 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
259 if (contextAlbumEntry.getKey().equals(AxArtifactKey.getNullKey())) {
260 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
261 "key on context album entry " + contextAlbumEntry.getKey() + " may not be the null key"));
262 } else if (contextAlbumEntry.getValue() == null) {
263 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
264 "value on context album entry " + contextAlbumEntry.getKey() + " may not be null"));
266 validateContextAlbumKey(result, contextAlbumEntry);
268 result = contextAlbumEntry.getValue().validate(result);
276 private void validateContextAlbumKey(final AxValidationResult result,
277 final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry) {
278 if (!contextAlbumEntry.getKey().equals(contextAlbumEntry.getValue().getKey())) {
279 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
280 "key on context album entry key " + contextAlbumEntry.getKey()
281 + " does not equal context album value key " + contextAlbumEntry.getValue().getKey()));
289 * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
290 * basicmodel.concepts.AxConcept)
293 public AxConcept copyTo(final AxConcept target) {
294 Assertions.argumentNotNull(target, "target may not be null");
296 final Object copyObject = target;
297 Assertions.instanceOf(copyObject, AxContextAlbums.class);
299 final AxContextAlbums copy = ((AxContextAlbums) copyObject);
301 final Map<AxArtifactKey, AxContextAlbum> newContextAlbum = new TreeMap<>();
302 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
303 newContextAlbum.put(new AxArtifactKey(contextAlbumEntry.getKey()),
304 new AxContextAlbum(contextAlbumEntry.getValue()));
306 copy.setAlbumsMap(newContextAlbum);
314 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
317 public int hashCode() {
318 final int prime = 31;
320 result = prime * result + key.hashCode();
321 result = prime * result + albums.hashCode();
328 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
331 public boolean equals(final Object obj) {
339 if (getClass() != obj.getClass()) {
343 final AxContextAlbums other = (AxContextAlbums) obj;
344 if (!key.equals(other.key)) {
347 return albums.equals(other.albums);
353 * @see java.lang.Comparable#compareTo(java.lang.Object)
356 public int compareTo(final AxConcept otherObj) {
357 if (otherObj == null) {
360 if (this == otherObj) {
363 if (getClass() != otherObj.getClass()) {
364 return this.hashCode() - otherObj.hashCode();
367 final AxContextAlbums other = (AxContextAlbums) otherObj;
368 if (!key.equals(other.key)) {
369 return key.compareTo(other.key);
371 if (!albums.equals(other.albums)) {
372 return (albums.hashCode() - other.albums.hashCode());
381 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.
382 * model.basicmodel.concepts.AxArtifactKey)
385 public AxContextAlbum get(final AxArtifactKey conceptKey) {
386 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
392 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String)
395 public AxContextAlbum get(final String conceptKeyName) {
396 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
402 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String,
406 public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
407 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
414 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String)
417 public Set<AxContextAlbum> getAll(final String conceptKeyName) {
418 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
424 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String,
428 public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
429 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,