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 model. All Apex
55 * models that use context albums must have an {@link AxContextAlbums} field. The {@link AxContextAlbums} class
56 * implements the helper methods of the {@link AxConceptGetter} interface to allow {@link AxContextAlbum} instances to
57 * be retrieved by calling methods directly on this class without referencing the contained map.
59 * <p>Validation checks that the container key is not null. An observation is issued if no context albums are defined in
60 * the container. If context albums do exist, they are checked to ensure that keys and values are not null and that the
61 * map key matches the key in the map value for all album entries. Each context album entry is then validated
65 @Table(name = "AxContextAlbums")
67 @XmlAccessorType(XmlAccessType.FIELD)
68 @XmlType(name = "AxContextAlbums", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
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 creates an empty
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 creates an empty
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 returned as a raw hash map.
131 * This method is called by JAXB after unmarshaling and is used to convert the hash map to a {@link NavigableMap} so
132 * that it will work with the {@link AxConceptGetter} interface.
134 * @param unmarsaller the unmarshaler that is unmarshaling the model
135 * @param parent the parent object of this object in the unmarshaler
137 public void afterUnmarshal(final Unmarshaller unmarsaller, final Object parent) {
138 Assertions.argumentNotNull(unmarsaller, "unmarsaller should not be null");
139 Assertions.argumentNotNull(parent, "parent should not be null");
141 // The map must be navigable to allow name and version searching, unmarshaling returns a
143 final NavigableMap<AxArtifactKey, AxContextAlbum> navigableAlbums = new TreeMap<>();
144 navigableAlbums.putAll(albums);
145 albums = navigableAlbums;
151 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
154 public AxArtifactKey getKey() {
161 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
164 public List<AxKey> getKeys() {
165 final List<AxKey> keyList = key.getKeys();
167 for (final AxContextAlbum contextAlbum : albums.values()) {
168 keyList.addAll(contextAlbum.getKeys());
175 * Sets the key of the context album container.
177 * @param key the context album container key
179 public void setKey(final AxArtifactKey key) {
180 Assertions.argumentNotNull(key, "key may not be null");
185 * Gets the map of context albums from the context album container.
187 * @return the context album map
189 public Map<AxArtifactKey, AxContextAlbum> getAlbumsMap() {
194 * Sets the map of context albums from the context album container.
196 * @param albumsMap the map of context albums to place in the container
198 public void setAlbumsMap(final Map<AxArtifactKey, AxContextAlbum> albumsMap) {
199 Assertions.argumentNotNull(albumsMap, "albums may not be null");
200 this.albums = new TreeMap<>();
201 this.albums.putAll(albumsMap);
207 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
210 public void clean() {
212 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
213 contextAlbumEntry.getKey().clean();
214 contextAlbumEntry.getValue().clean();
221 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
224 public String toString() {
225 final StringBuilder builder = new StringBuilder();
226 builder.append(this.getClass().getSimpleName());
227 builder.append(":(");
228 builder.append(this.getClass().getSimpleName());
229 builder.append(":(");
230 builder.append("key=");
232 builder.append(",albums=");
233 builder.append(albums);
235 return builder.toString();
241 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
242 * basicmodel.concepts.AxValidationResult)
245 public AxValidationResult validate(final AxValidationResult resultIn) {
246 AxValidationResult result = resultIn;
248 if (key.equals(AxArtifactKey.getNullKey())) {
249 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
250 "key is a null key"));
253 result = key.validate(result);
255 if (albums.size() == 0) {
256 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
257 "albums are empty"));
259 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
260 if (contextAlbumEntry.getKey().equals(AxArtifactKey.getNullKey())) {
261 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
262 "key on context album entry " + contextAlbumEntry.getKey()
263 + " may not be the null key"));
264 } else if (contextAlbumEntry.getValue() == null) {
265 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
266 "value on context album entry " + contextAlbumEntry.getKey() + " may not be null"));
268 validateContextAlbumKey(result, contextAlbumEntry);
270 result = contextAlbumEntry.getValue().validate(result);
278 private void validateContextAlbumKey(final AxValidationResult result,
279 final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry) {
280 if (!contextAlbumEntry.getKey().equals(contextAlbumEntry.getValue().getKey())) {
281 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
282 "key on context album entry key " + contextAlbumEntry.getKey()
283 + " does not equal context album value key "
284 + contextAlbumEntry.getValue().getKey()));
291 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
292 * basicmodel.concepts.AxConcept)
295 public AxConcept copyTo(final AxConcept target) {
296 Assertions.argumentNotNull(target, "target may not be null");
298 final Object copyObject = target;
299 Assertions.instanceOf(copyObject, AxContextAlbums.class);
301 final AxContextAlbums copy = ((AxContextAlbums) copyObject);
303 final Map<AxArtifactKey, AxContextAlbum> newContextAlbum = new TreeMap<>();
304 for (final Entry<AxArtifactKey, AxContextAlbum> contextAlbumEntry : albums.entrySet()) {
305 newContextAlbum.put(new AxArtifactKey(contextAlbumEntry.getKey()),
306 new AxContextAlbum(contextAlbumEntry.getValue()));
308 copy.setAlbumsMap(newContextAlbum);
316 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
319 public int hashCode() {
320 final int prime = 31;
322 result = prime * result + key.hashCode();
323 result = prime * result + albums.hashCode();
330 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
333 public boolean equals(final Object obj) {
341 if (getClass() != obj.getClass()) {
345 final AxContextAlbums other = (AxContextAlbums) obj;
346 if (!key.equals(other.key)) {
349 return albums.equals(other.albums);
355 * @see java.lang.Comparable#compareTo(java.lang.Object)
358 public int compareTo(final AxConcept otherObj) {
359 if (otherObj == null) {
362 if (this == otherObj) {
365 if (getClass() != otherObj.getClass()) {
366 return this.hashCode() - otherObj.hashCode();
369 final AxContextAlbums other = (AxContextAlbums) otherObj;
370 if (!key.equals(other.key)) {
371 return key.compareTo(other.key);
373 if (!albums.equals(other.albums)) {
374 return (albums.hashCode() - other.albums.hashCode());
383 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(org.onap.policy.apex.
384 * model.basicmodel.concepts.AxArtifactKey)
387 public AxContextAlbum get(final AxArtifactKey conceptKey) {
388 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKey);
394 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String)
397 public AxContextAlbum get(final String conceptKeyName) {
398 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName);
404 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#get(java.lang.String, java.lang.String)
407 public AxContextAlbum get(final String conceptKeyName, final String conceptKeyVersion) {
408 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).get(conceptKeyName,
415 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String)
418 public Set<AxContextAlbum> getAll(final String conceptKeyName) {
419 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName);
425 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConceptGetter#getAll(java.lang.String, java.lang.String)
428 public Set<AxContextAlbum> getAll(final String conceptKeyName, final String conceptKeyVersion) {
429 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxContextAlbum>) albums).getAll(conceptKeyName,