2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.contextmodel.concepts;
25 import java.util.List;
26 import javax.persistence.AttributeOverride;
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;
37 import lombok.EqualsAndHashCode;
40 import lombok.ToString;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
47 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
48 import org.onap.policy.common.utils.validation.Assertions;
51 * This class is used to define an album of context.
53 * <p>A context album is a distributed map of context that will be distributed across all process instances that require
54 * access to it. This class defines the schema (structure) of the items in the context album, whether the items on the
55 * context album are writable or not, and what the scope of the context album is.
57 * <p>The structure of items (objects) the context album is defined as a schema, which is understood by whatever schema
58 * implementation is being used for the context album.
60 * <p>The scope of a context album is a string field, understood by whatever distribution mechanism is being used for
61 * the context album. The distribution mechanism uses the scope of the context album to decide to which executable
62 * entities a given context album is distributed.
64 * <p>The writable flag on a context album defines whether users of a context album can write to the context album or
65 * just read objects from the context album.
67 * <p>Validation checks that the album key and the context schema key are not null and that the scope field is not
68 * undefined and matches the regular expression SCOPE_REGEXP.
71 @Table(name = "AxContextAlbum")
75 @EqualsAndHashCode(callSuper = false)
77 @XmlAccessorType(XmlAccessType.FIELD)
78 @XmlRootElement(name = "apexContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp")
79 @XmlType(name = "AxContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
80 { "key", "scope", "isWritable", "itemSchema" })
82 public class AxContextAlbum extends AxConcept {
83 private static final String SCOPE_STRING = "scope";
85 private static final long serialVersionUID = 4290442590545820316L;
88 * The legal values for the scope of a context album is constrained by this regular expression.
90 public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";
92 /** The value of scope for a context album for which a scope has not been specified. */
93 public static final String SCOPE_UNDEFINED = "UNDEFINED";
96 @XmlElement(name = "key", required = true)
97 private AxArtifactKey key;
99 @Column(name = SCOPE_STRING)
100 @XmlElement(name = SCOPE_STRING, required = true)
101 private String scope;
103 @Column(name = "isWritable")
104 @XmlElement(name = "isWritable", required = true)
106 private boolean isWritable;
110 @AttributeOverride(name = "name", column = @Column(name = "itemSchemaName"))
111 @AttributeOverride(name = "version", column = @Column(name = "itemSchemaVersion"))
112 @Column(name = "itemSchema")
113 @XmlElement(name = "itemSchema", required = true)
114 private AxArtifactKey itemSchema;
118 * The default constructor creates a context album with a null artifact key. The scope of the context album is set
119 * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null artifact
122 public AxContextAlbum() {
123 this(new AxArtifactKey());
124 scope = SCOPE_UNDEFINED;
126 itemSchema = AxArtifactKey.getNullKey();
132 * @param copyConcept the concept to copy from
134 public AxContextAlbum(final AxContextAlbum copyConcept) {
139 * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
140 * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
143 * @param key the key of the context album
145 public AxContextAlbum(final AxArtifactKey key) {
146 this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
150 * Constructor that sets all the fields of the context album.
152 * @param key the key of the context album
153 * @param scope the scope field, must match the regular expression SCOPE_REGEXP
154 * @param isWritable specifies whether the context album will be writable or not
155 * @param itemSchema the artifact key of the context schema to use for this context album
157 public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
158 final AxArtifactKey itemSchema) {
160 Assertions.argumentNotNull(key, "key may not be null");
161 Assertions.argumentNotNull(scope, "scope may not be null");
162 Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");
165 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
166 this.isWritable = isWritable;
167 this.itemSchema = itemSchema;
174 public List<AxKey> getKeys() {
175 final List<AxKey> keyList = key.getKeys();
176 keyList.add(new AxKeyUse(itemSchema.getKey()));
182 * Sets the key of the context album.
184 * @param key the context album key
186 public void setKey(final AxArtifactKey key) {
187 Assertions.argumentNotNull(key, "key may not be null");
192 * Sets the scope of the context album.
194 * @param scope the context album scope
196 public void setScope(final String scope) {
197 Assertions.argumentNotNull(scope, "scope may not be null");
198 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
202 * Sets the artifact key of the item schema of this context album.
204 * @param itemSchema the item schema key
206 public void setItemSchema(final AxArtifactKey itemSchema) {
207 Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
208 this.itemSchema = itemSchema;
215 public AxValidationResult validate(final AxValidationResult resultIn) {
216 AxValidationResult result = resultIn;
218 if (key.equals(AxArtifactKey.getNullKey())) {
219 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220 "key is a null key"));
222 result = key.validate(result);
224 if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
225 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
226 "scope is not defined"));
229 var stringCheckResult = Assertions.getStringParameterValidationMessage(SCOPE_STRING, scope, SCOPE_REGEXP);
230 if (stringCheckResult != null) {
231 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
232 "scope invalid-" + stringCheckResult));
235 if (itemSchema.equals(AxArtifactKey.getNullKey())) {
236 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
237 "itemSchema reference is a null key, an item schema must be specified"));
239 result = itemSchema.validate(result);
248 public void clean() {
250 scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
258 public AxConcept copyTo(final AxConcept target) {
259 Assertions.argumentNotNull(target, "targetObject may not be null");
261 final Object copyObject = target;
262 Assertions.instanceOf(copyObject, AxContextAlbum.class);
264 final AxContextAlbum copy = ((AxContextAlbum) copyObject);
265 copy.setKey(new AxArtifactKey(key));
266 copy.setScope(scope);
267 copy.setWritable(isWritable);
268 copy.setItemSchema(new AxArtifactKey(itemSchema));
277 public int compareTo(final AxConcept otherObj) {
278 if (otherObj == null) {
281 if (this == otherObj) {
284 if (getClass() != otherObj.getClass()) {
285 return this.hashCode() - otherObj.hashCode();
288 final AxContextAlbum other = (AxContextAlbum) otherObj;
289 if (!key.equals(other.key)) {
290 return key.compareTo(other.key);
292 if (!scope.equals(other.scope)) {
293 return scope.compareTo(other.scope);
295 if (isWritable != other.isWritable) {
296 return (isWritable ? 1 : -1);
298 return itemSchema.compareTo(other.itemSchema);