db64f8847819aec7f885e904f57c1607bf54ab46
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.contextmodel.concepts;
24
25 import java.util.List;
26 import lombok.EqualsAndHashCode;
27 import lombok.Getter;
28 import lombok.Setter;
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;
38
39 /**
40  * This class is used to define an album of context.
41  *
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.
45  *
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.
48  *
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.
52  *
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.
55  *
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.
58  */
59
60 @Getter
61 @ToString
62 @EqualsAndHashCode(callSuper = false)
63
64 public class AxContextAlbum extends AxConcept {
65     private static final String SCOPE_STRING = "scope";
66
67     private static final long serialVersionUID = 4290442590545820316L;
68
69     /**
70      * The legal values for the scope of a context album is constrained by this regular expression.
71      */
72     public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";
73
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";
76
77     private AxArtifactKey key;
78     private String scope;
79
80     @Setter
81     private boolean isWritable;
82
83     private AxArtifactKey itemSchema;
84
85     /**
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
88      * key.
89      */
90     public AxContextAlbum() {
91         this(new AxArtifactKey());
92         scope = SCOPE_UNDEFINED;
93         isWritable = true;
94         itemSchema = AxArtifactKey.getNullKey();
95     }
96
97     /**
98      * Copy constructor.
99      *
100      * @param copyConcept the concept to copy from
101      */
102     public AxContextAlbum(final AxContextAlbum copyConcept) {
103         super(copyConcept);
104     }
105
106     /**
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
109      * artifact key.
110      *
111      * @param key the key of the context album
112      */
113     public AxContextAlbum(final AxArtifactKey key) {
114         this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
115     }
116
117     /**
118      * Constructor that sets all the fields of the context album.
119      *
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
124      */
125     public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
126                     final AxArtifactKey itemSchema) {
127         super();
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");
131
132         this.key = key;
133         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
134         this.isWritable = isWritable;
135         this.itemSchema = itemSchema;
136     }
137
138     /**
139      * {@inheritDoc}.
140      */
141     @Override
142     public List<AxKey> getKeys() {
143         final List<AxKey> keyList = key.getKeys();
144         keyList.add(new AxKeyUse(itemSchema.getKey()));
145
146         return keyList;
147     }
148
149     /**
150      * Sets the key of the context album.
151      *
152      * @param key the context album key
153      */
154     public void setKey(final AxArtifactKey key) {
155         Assertions.argumentNotNull(key, "key may not be null");
156         this.key = key;
157     }
158
159     /**
160      * Sets the scope of the context album.
161      *
162      * @param scope the context album scope
163      */
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);
167     }
168
169     /**
170      * Sets the artifact key of the item schema of this context album.
171      *
172      * @param itemSchema the item schema key
173      */
174     public void setItemSchema(final AxArtifactKey itemSchema) {
175         Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
176         this.itemSchema = itemSchema;
177     }
178
179     /**
180      * {@inheritDoc}.
181      */
182     @Override
183     public AxValidationResult validate(final AxValidationResult resultIn) {
184         AxValidationResult result = resultIn;
185
186         if (key.equals(AxArtifactKey.getNullKey())) {
187             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
188                             "key is a null key"));
189         }
190         result = key.validate(result);
191
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"));
195         }
196
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));
201         }
202
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"));
206         }
207         result = itemSchema.validate(result);
208
209         return result;
210     }
211
212     /**
213      * {@inheritDoc}.
214      */
215     @Override
216     public void clean() {
217         key.clean();
218         scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
219         itemSchema.clean();
220     }
221
222     /**
223      * {@inheritDoc}.
224      */
225     @Override
226     public AxConcept copyTo(final AxConcept target) {
227         Assertions.argumentNotNull(target, "targetObject may not be null");
228
229         final Object copyObject = target;
230         Assertions.instanceOf(copyObject, AxContextAlbum.class);
231
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));
237
238         return copy;
239     }
240
241     /**
242      * {@inheritDoc}.
243      */
244     @Override
245     public int compareTo(final AxConcept otherObj) {
246         if (otherObj == null) {
247             return -1;
248         }
249         if (this == otherObj) {
250             return 0;
251         }
252         if (getClass() != otherObj.getClass()) {
253             return this.hashCode() - otherObj.hashCode();
254         }
255
256         final AxContextAlbum other = (AxContextAlbum) otherObj;
257         if (!key.equals(other.key)) {
258             return key.compareTo(other.key);
259         }
260         if (!scope.equals(other.scope)) {
261             return scope.compareTo(other.scope);
262         }
263         if (isWritable != other.isWritable) {
264             return (isWritable ? 1 : -1);
265         }
266         return itemSchema.compareTo(other.itemSchema);
267     }
268 }