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