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 lombok.EqualsAndHashCode;
29 import lombok.ToString;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
37 import org.onap.policy.common.utils.validation.Assertions;
40 * This class is used to define an album of context.
42 * <p>A context album is a distributed map of context that will be distributed across all process instances that require
43 * access to it. This class defines the schema (structure) of the items in the context album, whether the items on the
44 * context album are writable or not, and what the scope of the context album is.
46 * <p>The structure of items (objects) the context album is defined as a schema, which is understood by whatever schema
47 * implementation is being used for the context album.
49 * <p>The scope of a context album is a string field, understood by whatever distribution mechanism is being used for
50 * the context album. The distribution mechanism uses the scope of the context album to decide to which executable
51 * entities a given context album is distributed.
53 * <p>The writable flag on a context album defines whether users of a context album can write to the context album or
54 * just read objects from the context album.
56 * <p>Validation checks that the album key and the context schema key are not null and that the scope field is not
57 * undefined and matches the regular expression SCOPE_REGEXP.
62 @EqualsAndHashCode(callSuper = false)
64 public class AxContextAlbum extends AxConcept {
65 private static final String SCOPE_STRING = "scope";
67 private static final long serialVersionUID = 4290442590545820316L;
70 * The legal values for the scope of a context album is constrained by this regular expression.
72 public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";
74 /** The value of scope for a context album for which a scope has not been specified. */
75 public static final String SCOPE_UNDEFINED = "UNDEFINED";
77 private AxArtifactKey key;
81 private boolean isWritable;
83 private AxArtifactKey itemSchema;
86 * The default constructor creates a context album with a null artifact key. The scope of the context album is set
87 * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null artifact
90 public AxContextAlbum() {
91 this(new AxArtifactKey());
92 scope = SCOPE_UNDEFINED;
94 itemSchema = AxArtifactKey.getNullKey();
100 * @param copyConcept the concept to copy from
102 public AxContextAlbum(final AxContextAlbum copyConcept) {
107 * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
108 * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
111 * @param key the key of the context album
113 public AxContextAlbum(final AxArtifactKey key) {
114 this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
118 * Constructor that sets all the fields of the context album.
120 * @param key the key of the context album
121 * @param scope the scope field, must match the regular expression SCOPE_REGEXP
122 * @param isWritable specifies whether the context album will be writable or not
123 * @param itemSchema the artifact key of the context schema to use for this context album
125 public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
126 final AxArtifactKey itemSchema) {
128 Assertions.argumentNotNull(key, "key may not be null");
129 Assertions.argumentNotNull(scope, "scope may not be null");
130 Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");
133 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
134 this.isWritable = isWritable;
135 this.itemSchema = itemSchema;
142 public List<AxKey> getKeys() {
143 final List<AxKey> keyList = key.getKeys();
144 keyList.add(new AxKeyUse(itemSchema.getKey()));
150 * Sets the key of the context album.
152 * @param key the context album key
154 public void setKey(final AxArtifactKey key) {
155 Assertions.argumentNotNull(key, "key may not be null");
160 * Sets the scope of the context album.
162 * @param scope the context album scope
164 public void setScope(final String scope) {
165 Assertions.argumentNotNull(scope, "scope may not be null");
166 this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
170 * Sets the artifact key of the item schema of this context album.
172 * @param itemSchema the item schema key
174 public void setItemSchema(final AxArtifactKey itemSchema) {
175 Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
176 this.itemSchema = itemSchema;
183 public AxValidationResult validate(final AxValidationResult resultIn) {
184 AxValidationResult result = resultIn;
186 if (key.equals(AxArtifactKey.getNullKey())) {
187 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
188 "key is a null key"));
190 result = key.validate(result);
192 if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
193 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
194 "scope is not defined"));
197 var stringCheckResult = Assertions.getStringParameterValidationMessage(SCOPE_STRING, scope, SCOPE_REGEXP);
198 if (stringCheckResult != null) {
199 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
200 "scope invalid-" + stringCheckResult));
203 if (itemSchema.equals(AxArtifactKey.getNullKey())) {
204 result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
205 "itemSchema reference is a null key, an item schema must be specified"));
207 result = itemSchema.validate(result);
216 public void clean() {
218 scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
226 public AxConcept copyTo(final AxConcept target) {
227 Assertions.argumentNotNull(target, "targetObject may not be null");
229 final Object copyObject = target;
230 Assertions.instanceOf(copyObject, AxContextAlbum.class);
232 final AxContextAlbum copy = ((AxContextAlbum) copyObject);
233 copy.setKey(new AxArtifactKey(key));
234 copy.setScope(scope);
235 copy.setWritable(isWritable);
236 copy.setItemSchema(new AxArtifactKey(itemSchema));
245 public int compareTo(final AxConcept otherObj) {
246 if (otherObj == null) {
249 if (this == otherObj) {
252 if (getClass() != otherObj.getClass()) {
253 return this.hashCode() - otherObj.hashCode();
256 final AxContextAlbum other = (AxContextAlbum) otherObj;
257 if (!key.equals(other.key)) {
258 return key.compareTo(other.key);
260 if (!scope.equals(other.scope)) {
261 return scope.compareTo(other.scope);
263 if (isWritable != other.isWritable) {
264 return (isWritable ? 1 : -1);
266 return itemSchema.compareTo(other.itemSchema);