629e2556ccac49e9fa1e7a39296433c33d6b91f4
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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 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 org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
44 import org.onap.policy.common.utils.validation.Assertions;
45
46 /**
47  * This class is used to define an album of context.
48  *
49  * <p>A context album is a distributed map of context that will be distributed across all process instances that require
50  * access to it. This class defines the schema (structure) of the items in the context album, whether the items on the
51  * context album are writable or not, and what the scope of the context album is.
52  *
53  * <p>The structure of items (objects) the context album is defined as a schema, which is understood by whatever schema
54  * implementation is being used for the context album.
55  *
56  * <p>The scope of a context album is a string field, understood by whatever distribution mechanism is being used for
57  * the context album. The distribution mechanism uses the scope of the context album to decide to which executable
58  * entities a given context album is distributed.
59  *
60  * <p>The writable flag on a context album defines whether users of a context album can write to the context album or
61  * just read objects from the context album.
62  *
63  * <p>Validation checks that the album key and the context schema key are not null and that the scope field is not
64  * undefined and matches the regular expression SCOPE_REGEXP.
65  */
66 @Entity
67 @Table(name = "AxContextAlbum")
68
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" })
73
74 public class AxContextAlbum extends AxConcept {
75     private static final String SCOPE_STRING = "scope";
76
77     private static final long serialVersionUID = 4290442590545820316L;
78
79     /**
80      * The legal values for the scope of a context album is constrained by this regular expression.
81      */
82     public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";
83
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";
86
87     private static final int HASH_PRIME_0 = 1231;
88     private static final int HASH_PRIME_1 = 1237;
89
90     @EmbeddedId
91     @XmlElement(name = "key", required = true)
92     private AxArtifactKey key;
93
94     @Column(name = SCOPE_STRING)
95     @XmlElement(name = SCOPE_STRING, required = true)
96     private String scope;
97
98     @Column(name = "isWritable")
99     @XmlElement(name = "isWritable", required = true)
100     private boolean isWritable;
101
102     // @formatter:off
103     @Embedded
104     @AttributeOverride(name = "name", column = @Column(name = "itemSchemaName"))
105     @AttributeOverride(name = "version", column = @Column(name = "itemSchemaVersion"))
106     @Column(name = "itemSchema")
107     @XmlElement(name = "itemSchema", required = true)
108     private AxArtifactKey itemSchema;
109     // @formatter:on
110
111     /**
112      * The default constructor creates a context album with a null artifact key. The scope of the context album is set
113      * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null artifact
114      * key.
115      */
116     public AxContextAlbum() {
117         this(new AxArtifactKey());
118         scope = SCOPE_UNDEFINED;
119         isWritable = true;
120         itemSchema = AxArtifactKey.getNullKey();
121     }
122
123     /**
124      * Copy constructor.
125      *
126      * @param copyConcept the concept to copy from
127      */
128     public AxContextAlbum(final AxContextAlbum copyConcept) {
129         super(copyConcept);
130     }
131
132     /**
133      * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
134      * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
135      * artifact key.
136      *
137      * @param key the key of the context album
138      */
139     public AxContextAlbum(final AxArtifactKey key) {
140         this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
141     }
142
143     /**
144      * Constructor that sets all the fields of the context album.
145      *
146      * @param key the key of the context album
147      * @param scope the scope field, must match the regular expression SCOPE_REGEXP
148      * @param isWritable specifies whether the context album will be writable or not
149      * @param itemSchema the artifact key of the context schema to use for this context album
150      */
151     public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
152                     final AxArtifactKey itemSchema) {
153         super();
154         Assertions.argumentNotNull(key, "key may not be null");
155         Assertions.argumentNotNull(scope, "scope may not be null");
156         Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");
157
158         this.key = key;
159         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
160         this.isWritable = isWritable;
161         this.itemSchema = itemSchema;
162     }
163
164     /**
165      * {@inheritDoc}.
166      */
167     @Override
168     public AxArtifactKey getKey() {
169         return key;
170     }
171
172     /**
173      * {@inheritDoc}.
174      */
175     @Override
176     public List<AxKey> getKeys() {
177         final List<AxKey> keyList = key.getKeys();
178         keyList.add(new AxKeyUse(itemSchema.getKey()));
179
180         return keyList;
181     }
182
183     /**
184      * Sets the key of the context album.
185      *
186      * @param key the context album key
187      */
188     public void setKey(final AxArtifactKey key) {
189         Assertions.argumentNotNull(key, "key may not be null");
190         this.key = key;
191     }
192
193     /**
194      * Gets the scope of the context album.
195      *
196      * @return the context album scope
197      */
198     public String getScope() {
199         return scope;
200     }
201
202     /**
203      * Sets the scope of the context album.
204      *
205      * @param scope the context album scope
206      */
207     public void setScope(final String scope) {
208         Assertions.argumentNotNull(scope, "scope may not be null");
209         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
210     }
211
212     /**
213      * Sets whether the album is writable or not.
214      *
215      * @param writable the writable flag value
216      */
217     public void setWritable(final boolean writable) {
218         this.isWritable = writable;
219     }
220
221     /**
222      * Checks if the album is writable.
223      *
224      * @return true, if the album is writable
225      */
226     public boolean isWritable() {
227         return isWritable;
228     }
229
230     /**
231      * Gets the artifact key of the item schema of this context album.
232      *
233      * @return the item schema key
234      */
235     public AxArtifactKey getItemSchema() {
236         return itemSchema;
237     }
238
239     /**
240      * Sets the artifact key of the item schema of this context album.
241      *
242      * @param itemSchema the item schema key
243      */
244     public void setItemSchema(final AxArtifactKey itemSchema) {
245         Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
246         this.itemSchema = itemSchema;
247     }
248
249     /**
250      * {@inheritDoc}.
251      */
252     @Override
253     public AxValidationResult validate(final AxValidationResult resultIn) {
254         AxValidationResult result = resultIn;
255
256         if (key.equals(AxArtifactKey.getNullKey())) {
257             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
258                             "key is a null key"));
259         }
260         result = key.validate(result);
261
262         if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
263             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
264                             "scope is not defined"));
265         }
266
267         String stringCheckResult = Assertions.getStringParameterValidationMessage(SCOPE_STRING, scope, SCOPE_REGEXP);
268         if (stringCheckResult != null) {
269             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
270                             "scope invalid-" + stringCheckResult));
271         }
272
273         if (itemSchema.equals(AxArtifactKey.getNullKey())) {
274             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
275                             "itemSchema reference is a null key, an item schema must be specified"));
276         }
277         result = itemSchema.validate(result);
278
279         return result;
280     }
281
282     /**
283      * {@inheritDoc}.
284      */
285     @Override
286     public void clean() {
287         key.clean();
288         scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
289         itemSchema.clean();
290     }
291
292     /**
293      * {@inheritDoc}.
294      */
295     @Override
296     public String toString() {
297         final StringBuilder builder = new StringBuilder();
298         builder.append(this.getClass().getSimpleName());
299         builder.append(":(");
300         builder.append("key=");
301         builder.append(key);
302         builder.append(",scope=");
303         builder.append(scope);
304         builder.append(",isWritable=");
305         builder.append(isWritable);
306         builder.append(",itemSchema=");
307         builder.append(itemSchema);
308         builder.append(")");
309         return builder.toString();
310     }
311
312     /**
313      * {@inheritDoc}.
314      */
315     @Override
316     public AxConcept copyTo(final AxConcept target) {
317         Assertions.argumentNotNull(target, "targetObject may not be null");
318
319         final Object copyObject = target;
320         Assertions.instanceOf(copyObject, AxContextAlbum.class);
321
322         final AxContextAlbum copy = ((AxContextAlbum) copyObject);
323         copy.setKey(new AxArtifactKey(key));
324         copy.setScope(scope);
325         copy.setWritable(isWritable);
326         copy.setItemSchema(new AxArtifactKey(itemSchema));
327
328         return copy;
329     }
330
331     /**
332      * {@inheritDoc}.
333      */
334     @Override
335     public int hashCode() {
336         final int prime = 31;
337         int result = 1;
338         result = prime * result + key.hashCode();
339         result = prime * result + scope.hashCode();
340         result = prime * result + (isWritable ? HASH_PRIME_0 : HASH_PRIME_1);
341         result = prime * result + itemSchema.hashCode();
342         return result;
343     }
344
345     /**
346      * {@inheritDoc}.
347      */
348     @Override
349     public boolean equals(final Object obj) {
350         if (obj == null) {
351             return false;
352         }
353         if (this == obj) {
354             return true;
355         }
356
357         if (getClass() != obj.getClass()) {
358             return false;
359         }
360
361         final AxContextAlbum other = (AxContextAlbum) obj;
362         if (!key.equals(other.key)) {
363             return false;
364         }
365         if (!scope.equals(other.scope)) {
366             return false;
367         }
368         if (isWritable != other.isWritable) {
369             return (false);
370         }
371         return itemSchema.equals(other.itemSchema);
372     }
373
374     /**
375      * {@inheritDoc}.
376      */
377     @Override
378     public int compareTo(final AxConcept otherObj) {
379         if (otherObj == null) {
380             return -1;
381         }
382         if (this == otherObj) {
383             return 0;
384         }
385         if (getClass() != otherObj.getClass()) {
386             return this.hashCode() - otherObj.hashCode();
387         }
388
389         final AxContextAlbum other = (AxContextAlbum) otherObj;
390         if (!key.equals(other.key)) {
391             return key.compareTo(other.key);
392         }
393         if (!scope.equals(other.scope)) {
394             return scope.compareTo(other.scope);
395         }
396         if (isWritable != other.isWritable) {
397             return (isWritable ? 1 : -1);
398         }
399         return itemSchema.compareTo(other.itemSchema);
400     }
401 }