7dddc4099b1f5061ae457179d94ffc87bdbda2fb
[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 import javax.persistence.AttributeOverride;
26 import javax.persistence.AttributeOverrides;
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     @AttributeOverrides({
105         @AttributeOverride(name = "name", column = @Column(name = "itemSchemaName")),
106         @AttributeOverride(name = "version", column = @Column(name = "itemSchemaVersion"))
107         })
108     @Column(name = "itemSchema")
109     @XmlElement(name = "itemSchema", required = true)
110     private AxArtifactKey itemSchema;
111     // @formatter:on
112
113     /**
114      * The default constructor creates a context album with a null artifact key. The scope of the context album is set
115      * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null artifact
116      * key.
117      */
118     public AxContextAlbum() {
119         this(new AxArtifactKey());
120         scope = SCOPE_UNDEFINED;
121         isWritable = true;
122         itemSchema = AxArtifactKey.getNullKey();
123     }
124
125     /**
126      * Copy constructor.
127      *
128      * @param copyConcept the concept to copy from
129      */
130     public AxContextAlbum(final AxContextAlbum copyConcept) {
131         super(copyConcept);
132     }
133
134     /**
135      * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
136      * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
137      * artifact key.
138      *
139      * @param key the key of the context album
140      */
141     public AxContextAlbum(final AxArtifactKey key) {
142         this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
143     }
144
145     /**
146      * Constructor that sets all the fields of the context album.
147      *
148      * @param key the key of the context album
149      * @param scope the scope field, must match the regular expression SCOPE_REGEXP
150      * @param isWritable specifies whether the context album will be writable or not
151      * @param itemSchema the artifact key of the context schema to use for this context album
152      */
153     public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
154                     final AxArtifactKey itemSchema) {
155         super();
156         Assertions.argumentNotNull(key, "key may not be null");
157         Assertions.argumentNotNull(scope, "scope may not be null");
158         Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");
159
160         this.key = key;
161         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
162         this.isWritable = isWritable;
163         this.itemSchema = itemSchema;
164     }
165
166     /*
167      * (non-Javadoc)
168      *
169      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
170      */
171     @Override
172     public AxArtifactKey getKey() {
173         return key;
174     }
175
176     /*
177      * (non-Javadoc)
178      *
179      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
180      */
181     @Override
182     public List<AxKey> getKeys() {
183         final List<AxKey> keyList = key.getKeys();
184         keyList.add(new AxKeyUse(itemSchema.getKey()));
185
186         return keyList;
187     }
188
189     /**
190      * Sets the key of the context album.
191      *
192      * @param key the context album key
193      */
194     public void setKey(final AxArtifactKey key) {
195         Assertions.argumentNotNull(key, "key may not be null");
196         this.key = key;
197     }
198
199     /**
200      * Gets the scope of the context album.
201      *
202      * @return the context album scope
203      */
204     public String getScope() {
205         return scope;
206     }
207
208     /**
209      * Sets the scope of the context album.
210      *
211      * @param scope the context album scope
212      */
213     public void setScope(final String scope) {
214         Assertions.argumentNotNull(scope, "scope may not be null");
215         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
216     }
217
218     /**
219      * Sets whether the album is writable or not.
220      *
221      * @param writable the writable flag value
222      */
223     public void setWritable(final boolean writable) {
224         this.isWritable = writable;
225     }
226
227     /**
228      * Checks if the album is writable.
229      *
230      * @return true, if the album is writable
231      */
232     public boolean isWritable() {
233         return isWritable;
234     }
235
236     /**
237      * Gets the artifact key of the item schema of this context album.
238      *
239      * @return the item schema key
240      */
241     public AxArtifactKey getItemSchema() {
242         return itemSchema;
243     }
244
245     /**
246      * Sets the artifact key of the item schema of this context album.
247      *
248      * @param itemSchema the item schema key
249      */
250     public void setItemSchema(final AxArtifactKey itemSchema) {
251         Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
252         this.itemSchema = itemSchema;
253     }
254
255     /*
256      * (non-Javadoc)
257      *
258      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
259      * basicmodel.concepts.AxValidationResult)
260      */
261     @Override
262     public AxValidationResult validate(final AxValidationResult resultIn) {
263         AxValidationResult result = resultIn;
264
265         if (key.equals(AxArtifactKey.getNullKey())) {
266             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
267                             "key is a null key"));
268         }
269         result = key.validate(result);
270
271         if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
272             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
273                             "scope is not defined"));
274         }
275
276         String stringCheckResult = Assertions.getStringParameterValidationMessage(SCOPE_STRING, scope, SCOPE_REGEXP);
277         if (stringCheckResult != null) {
278             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
279                             "scope invalid-" + stringCheckResult));
280         }
281
282         if (itemSchema.equals(AxArtifactKey.getNullKey())) {
283             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
284                             "itemSchema reference is a null key, an item schema must be specified"));
285         }
286         result = itemSchema.validate(result);
287
288         return result;
289     }
290
291     /*
292      * (non-Javadoc)
293      *
294      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
295      */
296     @Override
297     public void clean() {
298         key.clean();
299         scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
300         itemSchema.clean();
301     }
302
303     /*
304      * (non-Javadoc)
305      *
306      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
307      */
308     @Override
309     public String toString() {
310         final StringBuilder builder = new StringBuilder();
311         builder.append(this.getClass().getSimpleName());
312         builder.append(":(");
313         builder.append("key=");
314         builder.append(key);
315         builder.append(",scope=");
316         builder.append(scope);
317         builder.append(",isWritable=");
318         builder.append(isWritable);
319         builder.append(",itemSchema=");
320         builder.append(itemSchema);
321         builder.append(")");
322         return builder.toString();
323     }
324
325     /*
326      * (non-Javadoc)
327      *
328      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
329      * basicmodel.concepts.AxConcept)
330      */
331     @Override
332     public AxConcept copyTo(final AxConcept target) {
333         Assertions.argumentNotNull(target, "targetObject may not be null");
334
335         final Object copyObject = target;
336         Assertions.instanceOf(copyObject, AxContextAlbum.class);
337
338         final AxContextAlbum copy = ((AxContextAlbum) copyObject);
339         copy.setKey(new AxArtifactKey(key));
340         copy.setScope(scope);
341         copy.setWritable(isWritable);
342         copy.setItemSchema(new AxArtifactKey(itemSchema));
343
344         return copy;
345     }
346
347     /*
348      * (non-Javadoc)
349      *
350      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
351      */
352     @Override
353     public int hashCode() {
354         final int prime = 31;
355         int result = 1;
356         result = prime * result + key.hashCode();
357         result = prime * result + scope.hashCode();
358         result = prime * result + (isWritable ? HASH_PRIME_0 : HASH_PRIME_1);
359         result = prime * result + itemSchema.hashCode();
360         return result;
361     }
362
363     /*
364      * (non-Javadoc)
365      *
366      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
367      */
368     @Override
369     public boolean equals(final Object obj) {
370         if (obj == null) {
371             return false;
372         }
373         if (this == obj) {
374             return true;
375         }
376
377         if (getClass() != obj.getClass()) {
378             return false;
379         }
380
381         final AxContextAlbum other = (AxContextAlbum) obj;
382         if (!key.equals(other.key)) {
383             return false;
384         }
385         if (!scope.equals(other.scope)) {
386             return false;
387         }
388         if (isWritable != other.isWritable) {
389             return (false);
390         }
391         return itemSchema.equals(other.itemSchema);
392     }
393
394     /*
395      * (non-Javadoc)
396      *
397      * @see java.lang.Comparable#compareTo(java.lang.Object)
398      */
399     @Override
400     public int compareTo(final AxConcept otherObj) {
401         if (otherObj == null) {
402             return -1;
403         }
404         if (this == otherObj) {
405             return 0;
406         }
407         if (getClass() != otherObj.getClass()) {
408             return this.hashCode() - otherObj.hashCode();
409         }
410
411         final AxContextAlbum other = (AxContextAlbum) otherObj;
412         if (!key.equals(other.key)) {
413             return key.compareTo(other.key);
414         }
415         if (!scope.equals(other.scope)) {
416             return scope.compareTo(other.scope);
417         }
418         if (isWritable != other.isWritable) {
419             return (isWritable ? 1 : -1);
420         }
421         return itemSchema.compareTo(other.itemSchema);
422     }
423 }