b3880bd8c83c5c322966e10ad668d224582f5053
[policy/apex-pdp.git] /
1 /*-
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
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 lombok.EqualsAndHashCode;
38 import lombok.Getter;
39 import lombok.Setter;
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;
49
50 /**
51  * This class is used to define an album of context.
52  *
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.
56  *
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.
59  *
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.
63  *
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.
66  *
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.
69  */
70 @Entity
71 @Table(name = "AxContextAlbum")
72
73 @Getter
74 @ToString
75 @EqualsAndHashCode(callSuper = false)
76
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" })
81
82 public class AxContextAlbum extends AxConcept {
83     private static final String SCOPE_STRING = "scope";
84
85     private static final long serialVersionUID = 4290442590545820316L;
86
87     /**
88      * The legal values for the scope of a context album is constrained by this regular expression.
89      */
90     public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";
91
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";
94
95     @EmbeddedId
96     @XmlElement(name = "key", required = true)
97     private AxArtifactKey key;
98
99     @Column(name = SCOPE_STRING)
100     @XmlElement(name = SCOPE_STRING, required = true)
101     private String scope;
102
103     @Column(name = "isWritable")
104     @XmlElement(name = "isWritable", required = true)
105     @Setter
106     private boolean isWritable;
107
108     // @formatter:off
109     @Embedded
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;
115     // @formatter:on
116
117     /**
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
120      * key.
121      */
122     public AxContextAlbum() {
123         this(new AxArtifactKey());
124         scope = SCOPE_UNDEFINED;
125         isWritable = true;
126         itemSchema = AxArtifactKey.getNullKey();
127     }
128
129     /**
130      * Copy constructor.
131      *
132      * @param copyConcept the concept to copy from
133      */
134     public AxContextAlbum(final AxContextAlbum copyConcept) {
135         super(copyConcept);
136     }
137
138     /**
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
141      * artifact key.
142      *
143      * @param key the key of the context album
144      */
145     public AxContextAlbum(final AxArtifactKey key) {
146         this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
147     }
148
149     /**
150      * Constructor that sets all the fields of the context album.
151      *
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
156      */
157     public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
158                     final AxArtifactKey itemSchema) {
159         super();
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");
163
164         this.key = key;
165         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
166         this.isWritable = isWritable;
167         this.itemSchema = itemSchema;
168     }
169
170     /**
171      * {@inheritDoc}.
172      */
173     @Override
174     public List<AxKey> getKeys() {
175         final List<AxKey> keyList = key.getKeys();
176         keyList.add(new AxKeyUse(itemSchema.getKey()));
177
178         return keyList;
179     }
180
181     /**
182      * Sets the key of the context album.
183      *
184      * @param key the context album key
185      */
186     public void setKey(final AxArtifactKey key) {
187         Assertions.argumentNotNull(key, "key may not be null");
188         this.key = key;
189     }
190
191     /**
192      * Sets the scope of the context album.
193      *
194      * @param scope the context album scope
195      */
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);
199     }
200
201     /**
202      * Sets the artifact key of the item schema of this context album.
203      *
204      * @param itemSchema the item schema key
205      */
206     public void setItemSchema(final AxArtifactKey itemSchema) {
207         Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
208         this.itemSchema = itemSchema;
209     }
210
211     /**
212      * {@inheritDoc}.
213      */
214     @Override
215     public AxValidationResult validate(final AxValidationResult resultIn) {
216         AxValidationResult result = resultIn;
217
218         if (key.equals(AxArtifactKey.getNullKey())) {
219             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220                             "key is a null key"));
221         }
222         result = key.validate(result);
223
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"));
227         }
228
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));
233         }
234
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"));
238         }
239         result = itemSchema.validate(result);
240
241         return result;
242     }
243
244     /**
245      * {@inheritDoc}.
246      */
247     @Override
248     public void clean() {
249         key.clean();
250         scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
251         itemSchema.clean();
252     }
253
254     /**
255      * {@inheritDoc}.
256      */
257     @Override
258     public AxConcept copyTo(final AxConcept target) {
259         Assertions.argumentNotNull(target, "targetObject may not be null");
260
261         final Object copyObject = target;
262         Assertions.instanceOf(copyObject, AxContextAlbum.class);
263
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));
269
270         return copy;
271     }
272
273     /**
274      * {@inheritDoc}.
275      */
276     @Override
277     public int compareTo(final AxConcept otherObj) {
278         if (otherObj == null) {
279             return -1;
280         }
281         if (this == otherObj) {
282             return 0;
283         }
284         if (getClass() != otherObj.getClass()) {
285             return this.hashCode() - otherObj.hashCode();
286         }
287
288         final AxContextAlbum other = (AxContextAlbum) otherObj;
289         if (!key.equals(other.key)) {
290             return key.compareTo(other.key);
291         }
292         if (!scope.equals(other.scope)) {
293             return scope.compareTo(other.scope);
294         }
295         if (isWritable != other.isWritable) {
296             return (isWritable ? 1 : -1);
297         }
298         return itemSchema.compareTo(other.itemSchema);
299     }
300 }