Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / concepts / AxKeyInfo.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.concepts;
23
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;
43
44 /**
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.
47  *
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.
50  */
51
52 @Entity
53 @Table(name = "AxKeyInfo")
54
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" })
59
60 public class AxKeyInfo extends AxConcept {
61     private static final long serialVersionUID = -4023935924068914308L;
62
63     private static final int MAX_DESCRIPTION_LENGTH_8192 = 8192;
64     private static final int UUID_BYTE_LENGTH_16 = 16;
65
66     private static final Random sharedRandom = new Random();
67
68     @EmbeddedId
69     @XmlElement(name = "key", required = true)
70     private AxArtifactKey key;
71
72     @Column(name = "uuid")
73     @Convert(converter = Uuid2String.class)
74     @XmlJavaTypeAdapter(value = Uuid2String.class)
75     @XmlElement(name = "UUID", required = true)
76     private UUID uuid;
77
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;
83
84     /**
85      * The Default Constructor creates this concept with a NULL artifact key.
86      */
87     public AxKeyInfo() {
88         this(new AxArtifactKey());
89     }
90
91     /**
92      * Copy constructor.
93      *
94      * @param copyConcept the concept to copy from
95      */
96     public AxKeyInfo(final AxKeyInfo copyConcept) {
97         super(copyConcept);
98     }
99
100     /**
101      * Constructor to create this concept with the specified key.
102      *
103      * @param key the key of the concept
104      */
105     public AxKeyInfo(final AxArtifactKey key) {
106         this(key, UUID.randomUUID(), "Generated description for concept referred to by key \"" + key.getId() + "\"");
107     }
108
109     /**
110      * Constructor to create this concept and set all its fields.
111      *
112      * @param key the key of the concept
113      * @param uuid the UUID of the concept
114      * @param description the description of the concept
115      */
116     public AxKeyInfo(final AxArtifactKey key, final UUID uuid, final String description) {
117         super();
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");
121
122         this.key = key;
123         this.uuid = uuid;
124         this.description = description.trim();
125     }
126
127     /**
128      * {@inheritDoc}.
129      */
130     @Override
131     public AxArtifactKey getKey() {
132         return key;
133     }
134
135     /**
136      * {@inheritDoc}.
137      */
138     @Override
139     public List<AxKey> getKeys() {
140         return key.getKeys();
141     }
142
143     /**
144      * Sets the key of the concept.
145      *
146      * @param key the concept key
147      */
148     public void setKey(final AxArtifactKey key) {
149         Assertions.argumentNotNull(key, "key may not be null");
150         this.key = key;
151     }
152
153     /**
154      * Gets the UUID of the concept.
155      *
156      * @return the uuid of the concept
157      */
158     public UUID getUuid() {
159         return uuid;
160     }
161
162     /**
163      * Sets the UUID of the concept.
164      *
165      * @param uuid the uuid of the concept
166      */
167     public void setUuid(final UUID uuid) {
168         Assertions.argumentNotNull(uuid, "uuid may not be null");
169         this.uuid = uuid;
170     }
171
172     /**
173      * Gets the description of the concept.
174      *
175      * @return the description of the concept
176      */
177     public String getDescription() {
178         return description;
179     }
180
181     /**
182      * Sets the description of the concept.
183      *
184      * @param description the description of the concept
185      */
186     public void setDescription(final String description) {
187         Assertions.argumentNotNull(description, "description may not be null");
188         this.description = description.trim();
189     }
190
191     /**
192      * {@inheritDoc}.
193      */
194     @Override
195     public AxValidationResult validate(final AxValidationResult resultIn) {
196         AxValidationResult result = resultIn;
197
198         if (key.equals(AxArtifactKey.getNullKey())) {
199             result.addValidationMessage(
200                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
201         }
202
203         result = key.validate(result);
204
205         if (description.trim().length() == 0) {
206             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.OBSERVATION,
207                     "description is blank"));
208         }
209
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)));
213         }
214
215         return result;
216     }
217
218     /**
219      * {@inheritDoc}.
220      */
221     @Override
222     public void clean() {
223         key.clean();
224         description = description.trim();
225     }
226
227     /**
228      * {@inheritDoc}.
229      */
230     @Override
231     public String toString() {
232         final StringBuilder builder = new StringBuilder();
233         builder.append(this.getClass().getSimpleName());
234         builder.append(":(");
235         builder.append("artifactId=");
236         builder.append(key);
237         builder.append(",uuid=");
238         builder.append(uuid);
239         builder.append(",description=");
240         builder.append(description);
241         builder.append(")");
242         return builder.toString();
243     }
244
245     /**
246      * {@inheritDoc}.
247      */
248     @Override
249     public AxConcept copyTo(final AxConcept target) {
250         Assertions.argumentNotNull(target, "target may not be null");
251
252         final Object copyObject = target;
253         Assertions.instanceOf(copyObject, AxKeyInfo.class);
254
255         final AxKeyInfo copy = ((AxKeyInfo) copyObject);
256         copy.setKey(new AxArtifactKey(key));
257         copy.setUuid(UUID.fromString(uuid.toString()));
258         copy.setDescription(description);
259
260         return copy;
261     }
262
263     /**
264      * {@inheritDoc}.
265      */
266     @Override
267     public int hashCode() {
268         final int prime = 31;
269         int result = 1;
270         result = prime * result + key.hashCode();
271         result = prime * result + uuid.hashCode();
272         result = prime * result + description.hashCode();
273         return result;
274     }
275
276     /**
277      * {@inheritDoc}.
278      */
279     @Override
280     public boolean equals(final Object obj) {
281         if (obj == null) {
282             return false;
283         }
284         if (this == obj) {
285             return true;
286         }
287         if (getClass() != obj.getClass()) {
288             return false;
289         }
290
291         final AxKeyInfo other = (AxKeyInfo) obj;
292         if (!key.equals(other.key)) {
293             return false;
294         }
295         if (!uuid.equals(other.uuid)) {
296             return false;
297         }
298         final String thisdesc = CDataConditioner.clean(description);
299         final String otherdesc = CDataConditioner.clean(other.description);
300         return thisdesc.equals(otherdesc);
301     }
302
303     /**
304      * {@inheritDoc}.
305      */
306     @Override
307     public int compareTo(final AxConcept otherObj) {
308         if (otherObj == null) {
309             return -1;
310         }
311         if (this == otherObj) {
312             return 0;
313         }
314         if (getClass() != otherObj.getClass()) {
315             return this.hashCode() - otherObj.hashCode();
316         }
317
318         final AxKeyInfo other = (AxKeyInfo) otherObj;
319         if (!key.equals(other.key)) {
320             return key.compareTo(other.key);
321         }
322         if (!uuid.equals(other.uuid)) {
323             return uuid.compareTo(other.uuid);
324         }
325         return description.compareTo(other.description);
326     }
327
328     /**
329      * Generate a reproducible UUID for a given string seed.
330      *
331      * @param seed the seed
332      * @return the uuid
333      */
334     public static UUID generateReproducibleUuid(final String seed) {
335         Random random = sharedRandom;
336         if (!StringUtils.isEmpty(seed)) {
337             random = new Random(seed.hashCode());
338         }
339         final byte[] array = new byte[UUID_BYTE_LENGTH_16];
340         random.nextBytes(array);
341         return UUID.nameUUIDFromBytes(array);
342     }
343 }