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;
25 import java.util.Random;
26 import java.util.UUID;
27 import javax.persistence.Column;
28 import javax.persistence.Convert;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
31 import javax.persistence.Table;
32 import javax.xml.bind.annotation.XmlAccessType;
33 import javax.xml.bind.annotation.XmlAccessorType;
34 import javax.xml.bind.annotation.XmlElement;
35 import javax.xml.bind.annotation.XmlRootElement;
36 import javax.xml.bind.annotation.XmlType;
37 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
38 import org.apache.commons.lang3.StringUtils;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
40 import org.onap.policy.apex.model.basicmodel.dao.converters.CDataConditioner;
41 import org.onap.policy.apex.model.basicmodel.dao.converters.Uuid2String;
42 import org.onap.policy.common.utils.validation.Assertions;
45 * The key information on an {@link AxArtifactKey} key in an Apex policy model. Each {@link AxArtifactKey} must have an
46 * {@link AxKeyInfo} object. THe information held is the key's UUID and it's description.
48 * <p>Validation checks that all fields are defined and that the key is valid. It also observes that descriptions are
49 * blank and warns if the UUID is a zero UUID.
53 @Table(name = "AxKeyInfo")
55 @XmlAccessorType(XmlAccessType.FIELD)
56 @XmlRootElement(name = "apexKeyInfo", namespace = "http://www.onap.org/policy/apex-pdp")
57 @XmlType(name = "AxKeyInfo", namespace = "http://www.onap.org/policy/apex-pdp",
58 propOrder = { "key", "uuid", "description" })
60 public class AxKeyInfo extends AxConcept {
61 private static final long serialVersionUID = -4023935924068914308L;
63 private static final int MAX_DESCRIPTION_LENGTH_8192 = 8192;
64 private static final int UUID_BYTE_LENGTH_16 = 16;
66 private static final Random sharedRandom = new Random();
69 @XmlElement(name = "key", required = true)
70 private AxArtifactKey key;
72 @Column(name = "uuid")
73 @Convert(converter = Uuid2String.class)
74 @XmlJavaTypeAdapter(value = Uuid2String.class)
75 @XmlElement(name = "UUID", required = true)
78 @Column(name = "description", length = MAX_DESCRIPTION_LENGTH_8192)
79 @Convert(converter = CDataConditioner.class)
80 @XmlJavaTypeAdapter(value = CDataConditioner.class)
81 @XmlElement(required = true)
82 private String description;
85 * The Default Constructor creates this concept with a NULL artifact key.
88 this(new AxArtifactKey());
94 * @param copyConcept the concept to copy from
96 public AxKeyInfo(final AxKeyInfo copyConcept) {
101 * Constructor to create this concept with the specified key.
103 * @param key the key of the concept
105 public AxKeyInfo(final AxArtifactKey key) {
106 this(key, UUID.randomUUID(), "Generated description for concept referred to by key \"" + key.getId() + "\"");
110 * Constructor to create this concept and set all its fields.
112 * @param key the key of the concept
113 * @param uuid the UUID of the concept
114 * @param description the description of the concept
116 public AxKeyInfo(final AxArtifactKey key, final UUID uuid, final String description) {
118 Assertions.argumentNotNull(key, "key may not be null");
119 Assertions.argumentNotNull(uuid, "uuid may not be null");
120 Assertions.argumentNotNull(description, "description may not be null");
124 this.description = description.trim();
131 public AxArtifactKey getKey() {
139 public List<AxKey> getKeys() {
140 return key.getKeys();
144 * Sets the key of the concept.
146 * @param key the concept key
148 public void setKey(final AxArtifactKey key) {
149 Assertions.argumentNotNull(key, "key may not be null");
154 * Gets the UUID of the concept.
156 * @return the uuid of the concept
158 public UUID getUuid() {
163 * Sets the UUID of the concept.
165 * @param uuid the uuid of the concept
167 public void setUuid(final UUID uuid) {
168 Assertions.argumentNotNull(uuid, "uuid may not be null");
173 * Gets the description of the concept.
175 * @return the description of the concept
177 public String getDescription() {
182 * Sets the description of the concept.
184 * @param description the description of the concept
186 public void setDescription(final String description) {
187 Assertions.argumentNotNull(description, "description may not be null");
188 this.description = description.trim();
195 public AxValidationResult validate(final AxValidationResult resultIn) {
196 AxValidationResult result = resultIn;
198 if (key.equals(AxArtifactKey.getNullKey())) {
199 result.addValidationMessage(
200 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
203 result = key.validate(result);
205 if (description.trim().length() == 0) {
206 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
207 "description is blank"));
210 if (uuid.equals(new UUID(0, 0))) {
211 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.WARNING,
212 "UUID is a zero UUID: " + new UUID(0, 0)));
222 public void clean() {
224 description = description.trim();
231 public String toString() {
232 final StringBuilder builder = new StringBuilder();
233 builder.append(this.getClass().getSimpleName());
234 builder.append(":(");
235 builder.append("artifactId=");
237 builder.append(",uuid=");
238 builder.append(uuid);
239 builder.append(",description=");
240 builder.append(description);
242 return builder.toString();
249 public AxConcept copyTo(final AxConcept target) {
250 Assertions.argumentNotNull(target, "target may not be null");
252 final Object copyObject = target;
253 Assertions.instanceOf(copyObject, AxKeyInfo.class);
255 final AxKeyInfo copy = ((AxKeyInfo) copyObject);
256 copy.setKey(new AxArtifactKey(key));
257 copy.setUuid(UUID.fromString(uuid.toString()));
258 copy.setDescription(description);
267 public int hashCode() {
268 final int prime = 31;
270 result = prime * result + key.hashCode();
271 result = prime * result + uuid.hashCode();
272 result = prime * result + description.hashCode();
280 public boolean equals(final Object obj) {
287 if (getClass() != obj.getClass()) {
291 final AxKeyInfo other = (AxKeyInfo) obj;
292 if (!key.equals(other.key)) {
295 if (!uuid.equals(other.uuid)) {
298 final String thisdesc = CDataConditioner.clean(description);
299 final String otherdesc = CDataConditioner.clean(other.description);
300 return thisdesc.equals(otherdesc);
307 public int compareTo(final AxConcept otherObj) {
308 if (otherObj == null) {
311 if (this == otherObj) {
314 if (getClass() != otherObj.getClass()) {
315 return this.hashCode() - otherObj.hashCode();
318 final AxKeyInfo other = (AxKeyInfo) otherObj;
319 if (!key.equals(other.key)) {
320 return key.compareTo(other.key);
322 if (!uuid.equals(other.uuid)) {
323 return uuid.compareTo(other.uuid);
325 return description.compareTo(other.description);
329 * Generate a reproducible UUID for a given string seed.
331 * @param seed the seed
334 public static UUID generateReproducibleUuid(final String seed) {
335 Random random = sharedRandom;
336 if (!StringUtils.isEmpty(seed)) {
337 random = new Random(seed.hashCode());
339 final byte[] array = new byte[UUID_BYTE_LENGTH_16];
340 random.nextBytes(array);
341 return UUID.nameUUIDFromBytes(array);