2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2022 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.xml.bind.annotation.XmlAccessType;
27 import javax.xml.bind.annotation.XmlAccessorType;
28 import javax.xml.bind.annotation.XmlElement;
29 import javax.xml.bind.annotation.XmlRootElement;
30 import javax.xml.bind.annotation.XmlType;
31 import lombok.EqualsAndHashCode;
34 import lombok.ToString;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
42 import org.onap.policy.common.utils.validation.Assertions;
45 * This class is used to define an album of context.
47 * <p>A context album is a distributed map of context that will be distributed across all process instances that require
48 * access to it. This class defines the schema (structure) of the items in the context album, whether the items on the
49 * context album are writable or not, and what the scope of the context album is.
51 * <p>The structure of items (objects) the context album is defined as a schema, which is understood by whatever schema
52 * implementation is being used for the context album.
54 * <p>The scope of a context album is a string field, understood by whatever distribution mechanism is being used for
55 * the context album. The distribution mechanism uses the scope of the context album to decide to which executable
56 * entities a given context album is distributed.
58 * <p>The writable flag on a context album defines whether users of a context album can write to the context album or
59 * just read objects from the context album.
61 * <p>Validation checks that the album key and the context schema key are not null and that the scope field is not
62 * undefined and matches the regular expression SCOPE_REGEXP.
67 @EqualsAndHashCode(callSuper = false)
69 @XmlAccessorType(XmlAccessType.FIELD)
70 @XmlRootElement(name = "apexContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp")
71 @XmlType(name = "AxContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp", propOrder =
72 { "key", "scope", "isWritable", "itemSchema" })
74 public class AxContextAlbum extends AxConcept {
75 private static final String SCOPE_STRING = "scope";
77 private static final long serialVersionUID = 4290442590545820316L;
80 * The legal values for the scope of a context album is constrained by this regular expression.
82 public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";
84 /** The value of scope for a context album for which a scope has not been specified. */
85 public static final String SCOPE_UNDEFINED = "UNDEFINED";
87 @XmlElement(name = "key", required = true)
88 private AxArtifactKey key;
90 @XmlElement(name = SCOPE_STRING, required = true)
93 @XmlElement(name = "isWritable", required = true)
95 private boolean isWritable;
97 @XmlElement(name = "itemSchema", required = true)
98 private AxArtifactKey itemSchema;
101 * The default constructor creates a context album with a null artifact key. The scope of the context album is set
102 * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null artifact
105 public AxContextAlbum() {
106 this(new AxArtifactKey());
107 scope = SCOPE_UNDEFINED;
109 itemSchema = AxArtifactKey.getNullKey();
115 * @param copyConcept the concept to copy from
117 public AxContextAlbum(final AxContextAlbum copyConcept) {
122 * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
123 * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
126 * @param key the key of the context album
128 public AxContextAlbum(final AxArtifactKey key) {
129 this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
133 * Constructor that sets all the fields of the context album.
135 * @param key the key of the context album
136 * @param scope the scope field, must match the regular expression SCOPE_REGEXP
137 * @param isWritable specifies whether the context album will be writable or not
138 * @param itemSchema the artifact key of the context schema to use for this context album
140 public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
141 final AxArtifactKey itemSchema) {
143 Assertions.argumentNotNull(key, "key may not be null");
144 Assertions.argumentNotNull(scope, "scope may not be null");
145 Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");
148 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
149 this.isWritable = isWritable;
150 this.itemSchema = itemSchema;
157 public List<AxKey> getKeys() {
158 final List<AxKey> keyList = key.getKeys();
159 keyList.add(new AxKeyUse(itemSchema.getKey()));
165 * Sets the key of the context album.
167 * @param key the context album key
169 public void setKey(final AxArtifactKey key) {
170 Assertions.argumentNotNull(key, "key may not be null");
175 * Sets the scope of the context album.
177 * @param scope the context album scope
179 public void setScope(final String scope) {
180 Assertions.argumentNotNull(scope, "scope may not be null");
181 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
185 * Sets the artifact key of the item schema of this context album.
187 * @param itemSchema the item schema key
189 public void setItemSchema(final AxArtifactKey itemSchema) {
190 Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
191 this.itemSchema = itemSchema;
198 public AxValidationResult validate(final AxValidationResult resultIn) {
199 AxValidationResult result = resultIn;
201 if (key.equals(AxArtifactKey.getNullKey())) {
202 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
203 "key is a null key"));
205 result = key.validate(result);
207 if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
208 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
209 "scope is not defined"));
212 var stringCheckResult = Assertions.getStringParameterValidationMessage(SCOPE_STRING, scope, SCOPE_REGEXP);
213 if (stringCheckResult != null) {
214 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
215 "scope invalid-" + stringCheckResult));
218 if (itemSchema.equals(AxArtifactKey.getNullKey())) {
219 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220 "itemSchema reference is a null key, an item schema must be specified"));
222 result = itemSchema.validate(result);
231 public void clean() {
233 scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
241 public AxConcept copyTo(final AxConcept target) {
242 Assertions.argumentNotNull(target, "targetObject may not be null");
244 final Object copyObject = target;
245 Assertions.instanceOf(copyObject, AxContextAlbum.class);
247 final AxContextAlbum copy = ((AxContextAlbum) copyObject);
248 copy.setKey(new AxArtifactKey(key));
249 copy.setScope(scope);
250 copy.setWritable(isWritable);
251 copy.setItemSchema(new AxArtifactKey(itemSchema));
260 public int compareTo(final AxConcept otherObj) {
261 if (otherObj == null) {
264 if (this == otherObj) {
267 if (getClass() != otherObj.getClass()) {
268 return this.hashCode() - otherObj.hashCode();
271 final AxContextAlbum other = (AxContextAlbum) otherObj;
272 if (!key.equals(other.key)) {
273 return key.compareTo(other.key);
275 if (!scope.equals(other.scope)) {
276 return scope.compareTo(other.scope);
278 if (isWritable != other.isWritable) {
279 return (isWritable ? 1 : -1);
281 return itemSchema.compareTo(other.itemSchema);