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.contextmodel.concepts;
24 import java.util.List;
26 import javax.persistence.AttributeOverride;
27 import javax.persistence.AttributeOverrides;
28 import javax.persistence.Column;
29 import javax.persistence.Embedded;
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;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
46 import org.onap.policy.common.utils.validation.Assertions;
49 * This class is used to define an album of context.
51 * <p>A context album is a distributed map of context that will be distributed across all process instances that require
52 * access to it. This class defines the schema (structure) of the items in the context album, whether the items on the
53 * context album are writable or not, and what the scope of the context album is.
55 * <p>The structure of items (objects) the context album is defined as a schema, which is understood by whatever schema
56 * implementation is being used for the context album.
58 * <p>The scope of a context album is a string field, understood by whatever distribution mechanism is being used for
59 * the context album. The distribution mechanism uses the scope of the context album to decide to which executable
60 * entities a given context album is distributed.
62 * <p>The writable flag on a context album defines whether users of a context album can write to the context album or
63 * just read objects from the context album.
65 * <p>Validation checks that the album key and the context schema key are not null and that the scope field is not
66 * undefined and matches the regular expression SCOPE_REGEXP.
69 @Table(name = "AxContextAlbum")
71 @XmlAccessorType(XmlAccessType.FIELD)
72 @XmlRootElement(name = "apexContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp")
73 @XmlType(name = "AxContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
74 { "key", "scope", "isWritable", "itemSchema" })
76 public class AxContextAlbum extends AxConcept {
77 private static final String SCOPE_STRING = "scope";
79 private static final long serialVersionUID = 4290442590545820316L;
82 * The legal values for the scope of a context album is constrained by this regular expression.
84 public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";
86 /** The value of scope for a context album for which a scope has not been specified. */
87 public static final String SCOPE_UNDEFINED = "UNDEFINED";
89 private static final int HASH_PRIME_0 = 1231;
90 private static final int HASH_PRIME_1 = 1237;
93 @XmlElement(name = "key", required = true)
94 private AxArtifactKey key;
96 @Column(name = SCOPE_STRING)
97 @XmlElement(name = SCOPE_STRING, required = true)
100 @Column(name = "isWritable")
101 @XmlElement(name = "isWritable", required = true)
102 private boolean isWritable;
106 @AttributeOverrides({
107 @AttributeOverride(name = "name", column = @Column(name = "itemSchemaName")),
108 @AttributeOverride(name = "version", column = @Column(name = "itemSchemaVersion"))
110 @Column(name = "itemSchema")
111 @XmlElement(name = "itemSchema", required = true)
112 private AxArtifactKey itemSchema;
116 * The default constructor creates a context album with a null artifact key. The scope of the context album is set
117 * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null artifact
120 public AxContextAlbum() {
121 this(new AxArtifactKey());
122 scope = SCOPE_UNDEFINED;
124 itemSchema = AxArtifactKey.getNullKey();
130 * @param copyConcept the concept to copy from
132 public AxContextAlbum(final AxContextAlbum copyConcept) {
137 * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
138 * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
141 * @param key the key of the context album
143 public AxContextAlbum(final AxArtifactKey key) {
144 this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
148 * Constructor that sets all the fields of the context album.
150 * @param key the key of the context album
151 * @param scope the scope field, must match the regular expression SCOPE_REGEXP
152 * @param isWritable specifies whether the context album will be writable or not
153 * @param itemSchema the artifact key of the context schema to use for this context album
155 public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
156 final AxArtifactKey itemSchema) {
158 Assertions.argumentNotNull(key, "key may not be null");
159 Assertions.argumentNotNull(scope, "scope may not be null");
160 Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");
163 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
164 this.isWritable = isWritable;
165 this.itemSchema = itemSchema;
171 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
174 public AxArtifactKey getKey() {
181 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
184 public List<AxKey> getKeys() {
185 final List<AxKey> keyList = key.getKeys();
186 keyList.add(new AxKeyUse(itemSchema.getKey()));
192 * Sets the key of the context album.
194 * @param key the context album key
196 public void setKey(final AxArtifactKey key) {
197 Assertions.argumentNotNull(key, "key may not be null");
202 * Gets the scope of the context album.
204 * @return the context album scope
206 public String getScope() {
211 * Sets the scope of the context album.
213 * @param scope the context album scope
215 public void setScope(final String scope) {
216 Assertions.argumentNotNull(scope, "scope may not be null");
217 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
221 * Sets whether the album is writable or not.
223 * @param writable the writable flag value
225 public void setWritable(final boolean writable) {
226 this.isWritable = writable;
230 * Checks if the album is writable.
232 * @return true, if the album is writable
234 public boolean isWritable() {
239 * Gets the artifact key of the item schema of this context album.
241 * @return the item schema key
243 public AxArtifactKey getItemSchema() {
248 * Sets the artifact key of the item schema of this context album.
250 * @param itemSchema the item schema key
252 public void setItemSchema(final AxArtifactKey itemSchema) {
253 Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
254 this.itemSchema = itemSchema;
260 * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
261 * basicmodel.concepts.AxValidationResult)
264 public AxValidationResult validate(final AxValidationResult resultIn) {
265 AxValidationResult result = resultIn;
267 if (key.equals(AxArtifactKey.getNullKey())) {
268 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
269 "key is a null key"));
271 result = key.validate(result);
273 if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
274 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
275 "scope is not defined"));
278 String stringCheckResult = Assertions.getStringParameterValidationMessage(SCOPE_STRING, scope, SCOPE_REGEXP);
279 if (stringCheckResult != null) {
280 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
281 "scope invalid-" + stringCheckResult));
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);