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.basicmodel.concepts;
24 import java.util.List;
26 import java.util.Map.Entry;
27 import java.util.NavigableMap;
29 import java.util.TreeMap;
30 import java.util.TreeSet;
31 import java.util.UUID;
32 import javax.persistence.CascadeType;
33 import javax.persistence.EmbeddedId;
34 import javax.persistence.Entity;
35 import javax.persistence.JoinColumn;
36 import javax.persistence.JoinTable;
37 import javax.persistence.ManyToMany;
38 import javax.persistence.Table;
39 import javax.xml.bind.Unmarshaller;
40 import javax.xml.bind.annotation.XmlAccessType;
41 import javax.xml.bind.annotation.XmlAccessorType;
42 import javax.xml.bind.annotation.XmlElement;
43 import javax.xml.bind.annotation.XmlType;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
45 import org.onap.policy.common.utils.validation.Assertions;
48 * The Class AxKeyInformation holds a map of the key information for the entire Apex model. All Apex models
49 * {@link AxModel} must have an {@link AxKeyInformation} field. The {@link AxKeyInformation} class implements the helper
50 * methods of the {@link AxConceptGetter} interface to allow {@link AxKeyInfo} instances to be retrieved by calling
51 * methods directly on this class without referencing the contained map.
53 * <p>Validation checks that the key is not null, that the key information map is not empty, that each key and value in
54 * the map is defined, that the key in each map entry matches the key if each entry value, and that no duplicate UUIDs
55 * exist. Each key information entry is then validated individually.
58 @Table(name = "AxKeyInformation")
60 @XmlAccessorType(XmlAccessType.FIELD)
61 @XmlType(name = "AxKeyInformation", namespace = "http://www.onap.org/policy/apex-pdp",
62 propOrder = { "key", "keyInfoMap" })
64 public class AxKeyInformation extends AxConcept implements AxConceptGetter<AxKeyInfo> {
65 private static final long serialVersionUID = -2746380769017043888L;
68 @XmlElement(name = "key", required = true)
69 private AxArtifactKey key;
72 @ManyToMany(cascade = CascadeType.ALL)
74 joinColumns = { @JoinColumn(name = "keyInfoMapName", referencedColumnName = "name"),
75 @JoinColumn(name = "keyInfoMapVersion", referencedColumnName = "version"), },
76 inverseJoinColumns = { @JoinColumn(name = "keyInfoName", referencedColumnName = "name"),
77 @JoinColumn(name = "keyInfoVersion", referencedColumnName = "version") })
78 private Map<AxArtifactKey, AxKeyInfo> keyInfoMap;
82 * The Default Constructor creates this concept with a null key.
84 public AxKeyInformation() {
85 this(new AxArtifactKey());
91 * @param copyConcept the concept to copy from
93 public AxKeyInformation(final AxKeyInformation copyConcept) {
98 * Constructor to create this concept with the specified key.
100 * @param key the key of the concept
102 public AxKeyInformation(final AxArtifactKey key) {
103 this(key, new TreeMap<AxArtifactKey, AxKeyInfo>());
107 * Constructor to create this concept and set all its fields.
109 * @param key the key of the concept
110 * @param keyInfoMap the key info map of the concept
112 public AxKeyInformation(final AxArtifactKey key, final Map<AxArtifactKey, AxKeyInfo> keyInfoMap) {
114 Assertions.argumentNotNull(key, "key may not be null");
115 Assertions.argumentNotNull(keyInfoMap, "keyInfoMap may not be null");
118 this.keyInfoMap = new TreeMap<>();
119 this.keyInfoMap.putAll(keyInfoMap);
123 * When a model is unmarshalled from disk or from the database, the key information map is returned as a raw Hash
124 * Map. This method is called by JAXB after unmarshaling and is used to convert the hash map to a
125 * {@link NavigableMap} so that it will work with the {@link AxConceptGetter} interface.
127 * @param unmarshaler the unmarshaler that is unmarshaling the model
128 * @param parent the parent object of this object in the unmarshaler
130 public void afterUnmarshal(final Unmarshaller unmarshaler, final Object parent) {
131 // The map must be navigable to allow name and version searching,
132 // unmarshaling returns a hash map
133 final NavigableMap<AxArtifactKey, AxKeyInfo> navigablekeyInfoMap = new TreeMap<>();
134 navigablekeyInfoMap.putAll(keyInfoMap);
135 keyInfoMap = navigablekeyInfoMap;
139 * This method generates default key information for all keys found in the concept passed in as a parameter that do
140 * not already have key information.
142 * @param concept the concept for which to generate key information
144 public void generateKeyInfo(final AxConcept concept) {
145 for (final AxKey axKey : concept.getKeys()) {
146 if (!(axKey instanceof AxArtifactKey)) {
150 final AxArtifactKey artifactKey = (AxArtifactKey) axKey;
151 if (!keyInfoMap.containsKey(artifactKey)) {
152 final AxKeyInfo keyInfo = new AxKeyInfo(artifactKey);
153 // generate a reproducible UUID
154 keyInfo.setUuid(AxKeyInfo.generateReproducibleUuid(keyInfo.getId() + keyInfo.getDescription()));
155 keyInfoMap.put(artifactKey, keyInfo);
164 public AxArtifactKey getKey() {
172 public List<AxKey> getKeys() {
173 final List<AxKey> keyList = key.getKeys();
174 keyList.addAll(keyInfoMap.keySet());
180 * Sets the key of this concept.
182 * @param key the key of this concept
184 public void setKey(final AxArtifactKey key) {
185 Assertions.argumentNotNull(key, "key may not be null");
190 * Gets the key info map of this concept.
192 * @return the key info map of this concept
194 public Map<AxArtifactKey, AxKeyInfo> getKeyInfoMap() {
199 * Sets the key info map of this concept.
201 * @param keyInfoMap the key info map of this concept
203 public void setKeyInfoMap(final Map<AxArtifactKey, AxKeyInfo> keyInfoMap) {
204 Assertions.argumentNotNull(keyInfoMap, "keyInfoMap may not be null");
205 this.keyInfoMap = new TreeMap<>();
206 this.keyInfoMap.putAll(keyInfoMap);
213 public AxValidationResult validate(final AxValidationResult resultIn) {
214 AxValidationResult result = resultIn;
216 if (key.equals(AxArtifactKey.getNullKey())) {
217 result.addValidationMessage(
218 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
221 result = key.validate(result);
223 if (keyInfoMap.size() == 0) {
224 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
225 "keyInfoMap may not be empty"));
227 final Set<UUID> uuidSet = new TreeSet<>();
229 for (final Entry<AxArtifactKey, AxKeyInfo> keyInfoEntry : keyInfoMap.entrySet()) {
230 result = validateKeyInfoEntry(keyInfoEntry, uuidSet, result);
238 * Validate an key information entry.
240 * @param keyInfoEntry the key information entry
241 * @param uuidSet the set of UUIDs encountered in validation so far, the UUID of this entry is added to the set
242 * @param result the validation result to append to
243 * @return The validation result
245 private AxValidationResult validateKeyInfoEntry(final Entry<AxArtifactKey, AxKeyInfo> keyInfoEntry,
246 final Set<UUID> uuidSet, AxValidationResult result) {
247 if (keyInfoEntry.getKey().equals(AxArtifactKey.getNullKey())) {
248 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
249 "key on keyInfoMap entry " + keyInfoEntry.getKey() + " may not be the null key"));
250 } else if (keyInfoEntry.getValue() == null) {
251 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
252 "value on keyInfoMap entry " + keyInfoEntry.getKey() + " may not be null"));
254 if (!keyInfoEntry.getKey().equals(keyInfoEntry.getValue().getKey())) {
255 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
256 "key on keyInfoMap entry " + keyInfoEntry.getKey() + " does not equal entry key "
257 + keyInfoEntry.getValue().getKey()));
260 result = keyInfoEntry.getValue().validate(result);
262 if (uuidSet.contains(keyInfoEntry.getValue().getUuid())) {
263 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
264 "duplicate UUID found on keyInfoMap entry " + keyInfoEntry.getKey() + ":"
265 + keyInfoEntry.getValue().getUuid()));
267 uuidSet.add(keyInfoEntry.getValue().getUuid());
278 public void clean() {
280 for (final Entry<AxArtifactKey, AxKeyInfo> keyInfoEntry : keyInfoMap.entrySet()) {
281 keyInfoEntry.getKey().clean();
282 keyInfoEntry.getValue().clean();
290 public String toString() {
291 final StringBuilder builder = new StringBuilder();
292 builder.append(this.getClass().getSimpleName());
293 builder.append(":(");
294 builder.append("key=");
296 builder.append(",keyInfoMap=");
297 builder.append(keyInfoMap);
299 return builder.toString();
306 public AxConcept copyTo(final AxConcept target) {
307 Assertions.argumentNotNull(target, "target may not be null");
309 final Object copyObject = target;
310 Assertions.instanceOf(copyObject, AxKeyInformation.class);
312 final AxKeyInformation copy = ((AxKeyInformation) copyObject);
313 copy.setKey(new AxArtifactKey(key));
314 final Map<AxArtifactKey, AxKeyInfo> newKeyInfoMap = new TreeMap<>();
315 for (final Entry<AxArtifactKey, AxKeyInfo> keyInfoMapEntry : keyInfoMap.entrySet()) {
316 newKeyInfoMap.put(new AxArtifactKey(keyInfoMapEntry.getKey()), new AxKeyInfo(keyInfoMapEntry.getValue()));
318 copy.setKeyInfoMap(newKeyInfoMap);
327 public int hashCode() {
328 final int prime = 31;
330 result = prime * result + key.hashCode();
331 result = prime * result + keyInfoMap.hashCode();
339 public boolean equals(final Object obj) {
347 if (getClass() != obj.getClass()) {
351 final AxKeyInformation other = (AxKeyInformation) obj;
352 if (!key.equals(other.key)) {
355 return keyInfoMap.equals(other.keyInfoMap);
362 public int compareTo(final AxConcept otherObj) {
363 if (otherObj == null) {
366 if (this == otherObj) {
369 if (getClass() != otherObj.getClass()) {
370 return this.hashCode() - otherObj.hashCode();
373 final AxKeyInformation other = (AxKeyInformation) otherObj;
374 if (!key.equals(other.key)) {
375 return key.compareTo(other.key);
377 if (!keyInfoMap.equals(other.keyInfoMap)) {
378 return (keyInfoMap.hashCode() - other.keyInfoMap.hashCode());
388 public AxKeyInfo get(final AxArtifactKey conceptKey) {
389 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxKeyInfo>) keyInfoMap).get(conceptKey);
396 public AxKeyInfo get(final String conceptKeyName) {
397 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxKeyInfo>) keyInfoMap).get(conceptKeyName);
404 public AxKeyInfo get(final String conceptKeyName, final String conceptKeyVersion) {
405 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxKeyInfo>) keyInfoMap).get(conceptKeyName,
413 public Set<AxKeyInfo> getAll(final String conceptKeyName) {
414 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxKeyInfo>) keyInfoMap).getAll(conceptKeyName);
421 public Set<AxKeyInfo> getAll(final String conceptKeyName, final String conceptKeyVersion) {
422 return new AxConceptGetterImpl<>((NavigableMap<AxArtifactKey, AxKeyInfo>) keyInfoMap).getAll(conceptKeyName,