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;
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;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetterImpl;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
48 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
50 import org.onap.policy.common.utils.validation.Assertions;
53 * This class is a context album container and holds a map of the context albums for an entire Apex model. All Apex
54 * models that use context albums must have an {@link AxContextAlbums} field. The {@link AxContextAlbums} class
55 * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextAlbum} instances to
56 * be retrieved by calling methods directly on this class without referencing the contained map.
58 * <p>Validation checks that the container key is not null. An observation is issued if no context albums are defined in
59 * the container. If context albums do exist, they are checked to ensure that keys and values are not null and that the
60 * map key matches the key in the map value for all album entries. Each context album entry is then validated
64 @Table(name = "AxContextAlbums")
66 @XmlAccessorType(XmlAccessType.FIELD)
67 @XmlType(name = "AxContextAlbums", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
70 public final class AxContextAlbums extends AxConcept implements AxConceptGetter<AxContextAlbum> {
71 private static final long serialVersionUID = -4844259809024470975L;
74 @XmlElement(name = "key", required = true)
75 private AxArtifactKey key;
78 @OneToMany(cascade = CascadeType.ALL)
79 @JoinTable(joinColumns = {@JoinColumn(name = "contextName", referencedColumnName = "name"),
80 @JoinColumn(name = "contextVersion", referencedColumnName = "version")})
81 @XmlElement(name = "albums", required = true)
82 private Map<AxArtifactKey, AxContextAlbum> albums;
86 * The Default Constructor creates a {@link AxContextAlbums} object with a null artifact key and creates an empty
89 public AxContextAlbums() {
90 this(new AxArtifactKey());
96 * @param copyConcept the concept to copy from
98 public AxContextAlbums(final AxContextAlbums copyConcept) {
103 * The Key Constructor creates a {@link AxContextAlbums} object with the given artifact key and creates an empty
106 * @param key the key of the context album container
108 public AxContextAlbums(final AxArtifactKey key) {
109 this(key, new TreeMap<AxArtifactKey, AxContextAlbum>());
113 * Constructor that creates the context album map with the given albums and key.
115 * @param key the key of the context album container
116 * @param albums the context albums to place in this context album container
118 public AxContextAlbums(final AxArtifactKey key, final Map<AxArtifactKey, AxContextAlbum> albums) {
120 Assertions.argumentNotNull(key, "key may not be null");
121 Assertions.argumentNotNull(albums, "albums may not be null");
124 this.albums = new TreeMap<>();
125 this.albums.putAll(albums);
129 * When a model is unmarshalled from disk or from the database, the context album map is returned as a raw hash map.
130 * This method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so
131 * that it will work with the {@link AxConceptGetter} interface.
133 * @param unmarsaller the unmarshaler that is unmarshaling the model
134 * @param parent the parent object of this object in the unmarshaler
136 public void afterUnmarshal(final Unmarshaller unmarsaller, final Object parent) {
137 Assertions.argumentNotNull(unmarsaller, "unmarsaller should not be null");
138 Assertions.argumentNotNull(parent, "parent should not be null");
140 // The map must be navigable to allow name and version searching, unmarshaling returns a
142 final NavigableMap<AxArtifactKey, AxContextAlbum> navigableAlbums = new TreeMap<>();
143 navigableAlbums.putAll(albums);
144 albums = navigableAlbums;
151 public AxArtifactKey getKey() {
159 public List<AxKey> getKeys() {
160 final List<AxKey> keyList = key.getKeys();
162 for (final AxContextAlbum contextAlbum : albums.values()) {
163 keyList.addAll(contextAlbum.getKeys());
170 * Sets the key of the context album container.
172 * @param key the context album container key
174 public void setKey(final AxArtifactKey key) {
175 Assertions.argumentNotNull(key, "key may not be null");
180 * Gets the map of context albums from the context album container.
182 * @return the context album map
184 public Map<AxArtifactKey, AxContextAlbum> getAlbumsMap() {
189 * Sets the map of context albums from the context album container.
191 * @param albumsMap the map of context albums to place in the container
193 public void setAlbumsMap(final Map<AxArtifactKey, AxContextAlbum> albumsMap) {
194 Assertions.argumentNotNull(albumsMap, "albums may not be null");
195 this.albums = new TreeMap<>();
196 this.albums.putAll(albumsMap);
203 public void clean() {
205 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
206 contextAlbumEntry.getKey().clean();
207 contextAlbumEntry.getValue().clean();
215 public String toString() {
216 final StringBuilder builder = new StringBuilder();
217 builder.append(this.getClass().getSimpleName());
218 builder.append(":(");
219 builder.append(this.getClass().getSimpleName());
220 builder.append(":(");
221 builder.append("key=");
223 builder.append(",albums=");
224 builder.append(albums);
226 return builder.toString();
233 public AxValidationResult validate(final AxValidationResult resultIn) {
234 AxValidationResult result = resultIn;
236 if (key.equals(AxArtifactKey.getNullKey())) {
237 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
238 "key is a null key"));
241 result = key.validate(result);
243 if (albums.size() == 0) {
244 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
245 "albums are empty"));
247 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
248 if (contextAlbumEntry.getKey().equals(AxArtifactKey.getNullKey())) {
249 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
250 "key on context album entry " + contextAlbumEntry.getKey()
251 + " may not be the null key"));
252 } else if (contextAlbumEntry.getValue() == null) {
253 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
254 "value on context album entry " + contextAlbumEntry.getKey() + " may not be null"));
256 validateContextAlbumKey(result, contextAlbumEntry);
258 result = contextAlbumEntry.getValue().validate(result);
266 private void validateContextAlbumKey(final AxValidationResult result,
267 final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry) {
268 if (!contextAlbumEntry.getKey().equals(contextAlbumEntry.getValue().getKey())) {
269 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
270 "key on context album entry key " + contextAlbumEntry.getKey()
271 + " does not equal context album value key "
272 + contextAlbumEntry.getValue().getKey()));
280 public AxConcept copyTo(final AxConcept target) {
281 Assertions.argumentNotNull(target, "target may not be null");
283 final Object copyObject = target;
284 Assertions.instanceOf(copyObject, AxContextAlbums.class);
286 final AxContextAlbums copy = ((AxContextAlbums) copyObject);
288 final Map<AxArtifactKey, AxContextAlbum> newContextAlbum = new TreeMap<>();
289 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
290 newContextAlbum.put(new AxArtifactKey(contextAlbumEntry.getKey()),
291 new AxContextAlbum(contextAlbumEntry.getValue()));
293 copy.setAlbumsMap(newContextAlbum);
302 public int hashCode() {
303 final int prime = 31;
305 result = prime * result + key.hashCode();
306 result = prime * result + albums.hashCode();
314 public boolean equals(final Object obj) {
322 if (getClass() != obj.getClass()) {
326 final AxContextAlbums other = (AxContextAlbums) obj;
327 if (!key.equals(other.key)) {
330 return albums.equals(other.albums);
337 public int compareTo(final AxConcept otherObj) {
338 if (otherObj == null) {
341 if (this == otherObj) {
344 if (getClass() != otherObj.getClass()) {
345 return this.hashCode() - otherObj.hashCode();
348 final AxContextAlbums other = (AxContextAlbums) otherObj;
349 if (!key.equals(other.key)) {
350 return key.compareTo(other.key);
352 if (!albums.equals(other.albums)) {
353 return (albums.hashCode() - other.albums.hashCode());
363 public AxContextAlbum get(final AxArtifactKey conceptKey) {
364 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
371 public AxContextAlbum get(final String conceptKeyName) {
372 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
379 public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
380 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
388 public Set<AxContextAlbum> getAll(final String conceptKeyName) {
389 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
396 public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
397 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,