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;
28 import javax.persistence.Column;
29 import javax.persistence.Convert;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.Table;
33 import javax.xml.bind.annotation.XmlAccessType;
34 import javax.xml.bind.annotation.XmlAccessorType;
35 import javax.xml.bind.annotation.XmlElement;
36 import javax.xml.bind.annotation.XmlRootElement;
37 import javax.xml.bind.annotation.XmlType;
38 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
40 import org.apache.commons.lang3.StringUtils;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
42 import org.onap.policy.apex.model.basicmodel.dao.converters.CDataConditioner;
43 import org.onap.policy.apex.model.basicmodel.dao.converters.Uuid2String;
44 import org.onap.policy.common.utils.validation.Assertions;
47 * The key information on an {@link AxArtifactKey} key in an Apex policy model. Each {@link AxArtifactKey} must have an
48 * {@link AxKeyInfo} object. THe information held is the key's UUID and it's description.
50 * <p>Validation checks that all fields are defined and that the key is valid. It also observes that descriptions are
51 * blank and warns if the UUID is a zero UUID.
55 @Table(name = "AxKeyInfo")
57 @XmlAccessorType(XmlAccessType.FIELD)
58 @XmlRootElement(name = "apexKeyInfo", namespace = "http://www.onap.org/policy/apex-pdp")
59 @XmlType(name = "AxKeyInfo", namespace = "http://www.onap.org/policy/apex-pdp",
60 propOrder = { "key", "uuid", "description" })
62 public class AxKeyInfo extends AxConcept {
63 private static final long serialVersionUID = -4023935924068914308L;
65 private static final int MAX_DESCRIPTION_LENGTH_8192 = 8192;
66 private static final int UUID_BYTE_LENGTH_16 = 16;
68 private static final Random sharedRandom = new Random();
71 @XmlElement(name = "key", required = true)
72 private AxArtifactKey key;
74 @Column(name = "uuid")
75 @Convert(converter = Uuid2String.class)
76 @XmlJavaTypeAdapter(value = Uuid2String.class)
77 @XmlElement(name = "UUID", required = true)
80 @Column(name = "description", length = MAX_DESCRIPTION_LENGTH_8192)
81 @Convert(converter = CDataConditioner.class)
82 @XmlJavaTypeAdapter(value = CDataConditioner.class)
83 @XmlElement(required = true)
84 private String description;
87 * The Default Constructor creates this concept with a NULL artifact key.
90 this(new AxArtifactKey());
96 * @param copyConcept the concept to copy from
98 public AxKeyInfo(final AxKeyInfo copyConcept) {
103 * Constructor to create this concept with the specified key.
105 * @param key the key of the concept
107 public AxKeyInfo(final AxArtifactKey key) {
108 this(key, UUID.randomUUID(), "Generated description for concept referred to by key \"" + key.getId() + "\"");
112 * Constructor to create this concept and set all its fields.
114 * @param key the key of the concept
115 * @param uuid the UUID of the concept
116 * @param description the description of the concept
118 public AxKeyInfo(final AxArtifactKey key, final UUID uuid, final String description) {
120 Assertions.argumentNotNull(key, "key may not be null");
121 Assertions.argumentNotNull(uuid, "uuid may not be null");
122 Assertions.argumentNotNull(description, "description may not be null");
126 this.description = description.trim();
133 public AxArtifactKey getKey() {
141 public List<AxKey> getKeys() {
142 return key.getKeys();
146 * Sets the key of the concept.
148 * @param key the concept key
150 public void setKey(final AxArtifactKey key) {
151 Assertions.argumentNotNull(key, "key may not be null");
156 * Gets the UUID of the concept.
158 * @return the uuid of the concept
160 public UUID getUuid() {
165 * Sets the UUID of the concept.
167 * @param uuid the uuid of the concept
169 public void setUuid(final UUID uuid) {
170 Assertions.argumentNotNull(uuid, "uuid may not be null");
175 * Gets the description of the concept.
177 * @return the description of the concept
179 public String getDescription() {
184 * Sets the description of the concept.
186 * @param description the description of the concept
188 public void setDescription(final String description) {
189 Assertions.argumentNotNull(description, "description may not be null");
190 this.description = description.trim();
197 public AxValidationResult validate(final AxValidationResult resultIn) {
198 AxValidationResult result = resultIn;
200 if (key.equals(AxArtifactKey.getNullKey())) {
201 result.addValidationMessage(
202 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
205 result = key.validate(result);
207 if (description.trim().length() == 0) {
208 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
209 "description is blank"));
212 if (uuid.equals(new UUID(0, 0))) {
213 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.WARNING,
214 "UUID is a zero UUID: " + new UUID(0, 0)));
224 public void clean() {
226 description = description.trim();
233 public String toString() {
234 final StringBuilder builder = new StringBuilder();
235 builder.append(this.getClass().getSimpleName());
236 builder.append(":(");
237 builder.append("artifactId=");
239 builder.append(",uuid=");
240 builder.append(uuid);
241 builder.append(",description=");
242 builder.append(description);
244 return builder.toString();
251 public AxConcept copyTo(final AxConcept target) {
252 Assertions.argumentNotNull(target, "target may not be null");
254 final Object copyObject = target;
255 Assertions.instanceOf(copyObject, AxKeyInfo.class);
257 final AxKeyInfo copy = ((AxKeyInfo) copyObject);
258 copy.setKey(new AxArtifactKey(key));
259 copy.setUuid(UUID.fromString(uuid.toString()));
260 copy.setDescription(description);
269 public int hashCode() {
270 final int prime = 31;
272 result = prime * result + key.hashCode();
273 result = prime * result + uuid.hashCode();
274 result = prime * result + description.hashCode();
282 public boolean equals(final Object obj) {
289 if (getClass() != obj.getClass()) {
293 final AxKeyInfo other = (AxKeyInfo) obj;
294 if (!key.equals(other.key)) {
297 if (!uuid.equals(other.uuid)) {
300 final String thisdesc = CDataConditioner.clean(description);
301 final String otherdesc = CDataConditioner.clean(other.description);
302 return thisdesc.equals(otherdesc);
309 public int compareTo(final AxConcept otherObj) {
310 if (otherObj == null) {
313 if (this == otherObj) {
316 if (getClass() != otherObj.getClass()) {
317 return this.hashCode() - otherObj.hashCode();
320 final AxKeyInfo other = (AxKeyInfo) otherObj;
321 if (!key.equals(other.key)) {
322 return key.compareTo(other.key);
324 if (!uuid.equals(other.uuid)) {
325 return uuid.compareTo(other.uuid);
327 return description.compareTo(other.description);
331 * Generate a reproducible UUID for a given string seed.
333 * @param seed the seed
336 public static UUID generateReproducibleUuid(final String seed) {
337 Random random = sharedRandom;
338 if (!StringUtils.isEmpty(seed)) {
339 random = new Random(seed.hashCode());
341 final byte[] array = new byte[UUID_BYTE_LENGTH_16];
342 random.nextBytes(array);
343 return UUID.nameUUIDFromBytes(array);