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 javax.persistence.AttributeOverride;
26 import javax.persistence.AttributeOverrides;
27 import javax.persistence.Column;
28 import javax.persistence.Embedded;
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;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
45 import org.onap.policy.apex.model.utilities.Assertions;
48 * This class is used to define an album of context.
50 * A context album is a distributed map of context that will be distributed across all process instances that require
51 * access to it. This class defines the schema (structure) of the items in the context album, whether the items on the
52 * context album are writable or not, and what the scope of the context album is.
54 * The structure of items (objects) the context album is defined as a schema, which is understood by whatever schema
55 * implementation is being used for the context album.
57 * The scope of a context album is a string field, understood by whatever distribution mechanism is being used for the
58 * context album. The distribution mechanism uses the scope of the context album to decide to which executable entities
59 * a given context album is distributed.
61 * The writable flag on a context album defines whether users of a context album can write to the context album or just
62 * read objects from the context album.
64 * Validation checks that the album key and the context schema key are not null and that the scope field is not
65 * undefined and matches the regular expression SCOPE_REGEXP.
68 @Table(name = "AxContextAlbum")
70 @XmlAccessorType(XmlAccessType.FIELD)
71 @XmlRootElement(name = "apexContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp")
72 @XmlType(name = "AxContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp",
73 propOrder = { "key", "scope", "isWritable", "itemSchema" })
75 public class AxContextAlbum extends AxConcept {
76 private static final String SCOPE_STRING = "scope";
78 private static final long serialVersionUID = 4290442590545820316L;
81 * The legal values for the scope of a context album is constrained by this regular expression.
83 public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";
85 /** The value of scope for a context album for which a scope has not been specified. */
86 public static final String SCOPE_UNDEFINED = "UNDEFINED";
88 private static final int HASH_PRIME_0 = 1231;
89 private static final int HASH_PRIME_1 = 1237;
92 @XmlElement(name = "key", required = true)
93 private AxArtifactKey key;
95 @Column(name = SCOPE_STRING)
96 @XmlElement(name = SCOPE_STRING, required = true)
99 @Column(name = "isWritable")
100 @XmlElement(name = "isWritable", required = true)
101 private boolean isWritable;
105 @AttributeOverrides({
106 @AttributeOverride(name = "name", column = @Column(name = "itemSchemaName")),
107 @AttributeOverride(name = "version", column = @Column(name = "itemSchemaVersion"))
109 @Column(name = "itemSchema")
110 @XmlElement(name = "itemSchema", required = true)
111 private AxArtifactKey itemSchema;
115 * The default constructor creates a context album with a null artifact key. The scope of the context album is set
116 * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
119 public AxContextAlbum() {
120 this(new AxArtifactKey());
121 scope = SCOPE_UNDEFINED;
123 itemSchema = AxArtifactKey.getNullKey();
129 * @param copyConcept the concept to copy from
131 public AxContextAlbum(final AxContextAlbum copyConcept) {
136 * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
137 * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the
140 * @param key the key of the context album
142 public AxContextAlbum(final AxArtifactKey key) {
143 this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
147 * Constructor that sets all the fields of the context album.
149 * @param key the key of the context album
150 * @param scope the scope field, must match the regular expression SCOPE_REGEXP
151 * @param isWritable specifies whether the context album will be writable or not
152 * @param itemSchema the artifact key of the context schema to use for this context album
154 public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
155 final AxArtifactKey itemSchema) {
157 Assertions.argumentNotNull(key, "key may not be null");
158 Assertions.argumentNotNull(scope, "scope may not be null");
159 Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");
162 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
163 this.isWritable = isWritable;
164 this.itemSchema = itemSchema;
170 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
173 public AxArtifactKey getKey() {
180 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
183 public List<AxKey> getKeys() {
184 final List<AxKey> keyList = key.getKeys();
185 keyList.add(new AxKeyUse(itemSchema.getKey()));
191 * Sets the key of the context album.
193 * @param key the context album key
195 public void setKey(final AxArtifactKey key) {
196 Assertions.argumentNotNull(key, "key may not be null");
201 * Gets the scope of the context album.
203 * @return the context album scope
205 public String getScope() {
210 * Sets the scope of the context album.
212 * @param scope the context album scope
214 public void setScope(final String scope) {
215 Assertions.argumentNotNull(scope, "scope may not be null");
216 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
220 * Sets whether the album is writable or not.
222 * @param writable the writable flag value
224 public void setWritable(final boolean writable) {
225 this.isWritable = writable;
229 * Checks if the album is writable.
231 * @return true, if the album is writable
233 public boolean isWritable() {
238 * Gets the artifact key of the item schema of this context album.
240 * @return the item schema key
242 public AxArtifactKey getItemSchema() {
247 * Sets the artifact key of the item schema of this context album.
249 * @param itemSchema the item schema key
251 public void setItemSchema(final AxArtifactKey itemSchema) {
252 Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
253 this.itemSchema = itemSchema;
259 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
260 * basicmodel.concepts.AxValidationResult)
263 public AxValidationResult validate(final AxValidationResult resultIn) {
264 AxValidationResult result = resultIn;
266 if (key.equals(AxArtifactKey.getNullKey())) {
267 result.addValidationMessage(
268 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
270 result = key.validate(result);
272 if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
273 result.addValidationMessage(
274 new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "scope is not defined"));
278 Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
279 } catch (final IllegalArgumentException e) {
280 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
281 "scope invalid-" + e.getMessage()));
284 if (itemSchema.equals(AxArtifactKey.getNullKey())) {
285 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
286 "itemSchema reference is a null key, an item schema must be specified"));
288 result = itemSchema.validate(result);
296 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
299 public void clean() {
301 scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
308 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
311 public String toString() {
312 final StringBuilder builder = new StringBuilder();
313 builder.append(this.getClass().getSimpleName());
314 builder.append(":(");
315 builder.append("key=");
317 builder.append(",scope=");
318 builder.append(scope);
319 builder.append(",isWritable=");
320 builder.append(isWritable);
321 builder.append(",itemSchema=");
322 builder.append(itemSchema);
324 return builder.toString();
330 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
331 * basicmodel.concepts.AxConcept)
334 public AxConcept copyTo(final AxConcept target) {
335 Assertions.argumentNotNull(target, "targetObject may not be null");
337 final Object copyObject = target;
338 Assertions.instanceOf(copyObject, AxContextAlbum.class);
340 final AxContextAlbum copy = ((AxContextAlbum) copyObject);
341 copy.setKey(new AxArtifactKey(key));
342 copy.setScope(scope);
343 copy.setWritable(isWritable);
344 copy.setItemSchema(new AxArtifactKey(itemSchema));
352 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
355 public int hashCode() {
356 final int prime = 31;
358 result = prime * result + key.hashCode();
359 result = prime * result + scope.hashCode();
360 result = prime * result + (isWritable ? HASH_PRIME_0 : HASH_PRIME_1);
361 result = prime * result + itemSchema.hashCode();
368 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
371 public boolean equals(final Object obj) {
379 if (getClass() != obj.getClass()) {
383 final AxContextAlbum other = (AxContextAlbum) obj;
384 if (!key.equals(other.key)) {
387 if (!scope.equals(other.scope)) {
390 if (isWritable != other.isWritable) {
393 return itemSchema.equals(other.itemSchema);
399 * @see java.lang.Comparable#compareTo(java.lang.Object)
402 public int compareTo(final AxConcept otherObj) {
403 if (otherObj == null) {
406 if (this == otherObj) {
409 if (getClass() != otherObj.getClass()) {
410 return this.hashCode() - otherObj.hashCode();
413 final AxContextAlbum other = (AxContextAlbum) otherObj;
414 if (!key.equals(other.key)) {
415 return key.compareTo(other.key);
417 if (!scope.equals(other.scope)) {
418 return scope.compareTo(other.scope);
420 if (isWritable != other.isWritable) {
421 return (isWritable ? 1 : -1);
423 return itemSchema.compareTo(other.itemSchema);