32555662e8e6d03c6982d242c44f03f34508e32f
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.contextmodel.concepts;
22
23 import java.util.List;
24
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
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
45 import org.onap.policy.apex.model.utilities.Assertions;
46
47 /**
48  * This class is used to define an album of context.
49  * <p>
50  * A context album is a distributed map of context that will be distributed across all process instances that require
51  * access to it. This class defines the schema (structure) of the items in the context album, whether the items on the
52  * context album are writable or not, and what the scope of the context album is.
53  * <p>
54  * The structure of items (objects) the context album is defined as a schema, which is understood by whatever schema
55  * implementation is being used for the context album.
56  * <p>
57  * The scope of a context album is a string field, understood by whatever distribution mechanism is being used for the
58  * context album. The distribution mechanism uses the scope of the context album to decide to which executable entities
59  * a given context album is distributed.
60  * <p>
61  * The writable flag on a context album defines whether users of a context album can write to the context album or just
62  * read objects from the context album.
63  * <p>
64  * Validation checks that the album key and the context schema key are not null and that the scope field is not
65  * undefined and matches the regular expression SCOPE_REGEXP.
66  */
67 @Entity
68 @Table(name = "AxContextAlbum")
69
70 @XmlAccessorType(XmlAccessType.FIELD)
71 @XmlRootElement(name = "apexContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp")
72 @XmlType(name = "AxContextAlbum", namespace = "http://www.onap.org/policy/apex-pdp",
73         propOrder = { "key", "scope", "isWritable", "itemSchema" })
74
75 public class AxContextAlbum extends AxConcept {
76     private static final String SCOPE_STRING = "scope";
77
78     private static final long serialVersionUID = 4290442590545820316L;
79
80     /**
81      * The legal values for the scope of a context album is constrained by this regular expression.
82      */
83     public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";
84
85     /** The value of scope for a context album for which a scope has not been specified. */
86     public static final String SCOPE_UNDEFINED = "UNDEFINED";
87
88     private static final int HASH_PRIME_0 = 1231;
89     private static final int HASH_PRIME_1 = 1237;
90
91     @EmbeddedId
92     @XmlElement(name = "key", required = true)
93     private AxArtifactKey key;
94
95     @Column(name = SCOPE_STRING)
96     @XmlElement(name = SCOPE_STRING, required = true)
97     private String scope;
98
99     @Column(name = "isWritable")
100     @XmlElement(name = "isWritable", required = true)
101     private boolean isWritable;
102
103     // @formatter:off
104     @Embedded
105     @AttributeOverrides({
106         @AttributeOverride(name = "name", column = @Column(name = "itemSchemaName")),
107         @AttributeOverride(name = "version", column = @Column(name = "itemSchemaVersion"))
108     })
109     @Column(name = "itemSchema")
110     @XmlElement(name = "itemSchema", required = true)
111     private AxArtifactKey itemSchema;
112     // @formatter:on
113
114     /**
115      * The default constructor creates a context album with a null artifact key. The scope of the context album is set
116      * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
117      * artifact key.
118      */
119     public AxContextAlbum() {
120         this(new AxArtifactKey());
121         scope = SCOPE_UNDEFINED;
122         isWritable = true;
123         itemSchema = AxArtifactKey.getNullKey();
124     }
125
126     /**
127      * Copy constructor
128      *
129      * @param copyConcept the concept to copy from
130      */
131     public AxContextAlbum(final AxContextAlbum copyConcept) {
132         super(copyConcept);
133     }
134
135     /**
136      * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
137      * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the
138      * null artifact key.
139      *
140      * @param key the key of the context album
141      */
142     public AxContextAlbum(final AxArtifactKey key) {
143         this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
144     }
145
146     /**
147      * Constructor that sets all the fields of the context album.
148      *
149      * @param key the key of the context album
150      * @param scope the scope field, must match the regular expression SCOPE_REGEXP
151      * @param isWritable specifies whether the context album will be writable or not
152      * @param itemSchema the artifact key of the context schema to use for this context album
153      */
154     public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
155             final AxArtifactKey itemSchema) {
156         super();
157         Assertions.argumentNotNull(key, "key may not be null");
158         Assertions.argumentNotNull(scope, "scope may not be null");
159         Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");
160
161         this.key = key;
162         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
163         this.isWritable = isWritable;
164         this.itemSchema = itemSchema;
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey()
171      */
172     @Override
173     public AxArtifactKey getKey() {
174         return key;
175     }
176
177     /*
178      * (non-Javadoc)
179      *
180      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys()
181      */
182     @Override
183     public List<AxKey> getKeys() {
184         final List<AxKey> keyList = key.getKeys();
185         keyList.add(new AxKeyUse(itemSchema.getKey()));
186
187         return keyList;
188     }
189
190     /**
191      * Sets the key of the context album.
192      *
193      * @param key the context album key
194      */
195     public void setKey(final AxArtifactKey key) {
196         Assertions.argumentNotNull(key, "key may not be null");
197         this.key = key;
198     }
199
200     /**
201      * Gets the scope of the context album.
202      *
203      * @return the context album scope
204      */
205     public String getScope() {
206         return scope;
207     }
208
209     /**
210      * Sets the scope of the context album.
211      *
212      * @param scope the context album scope
213      */
214     public void setScope(final String scope) {
215         Assertions.argumentNotNull(scope, "scope may not be null");
216         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
217     }
218
219     /**
220      * Sets whether the album is writable or not.
221      *
222      * @param writable the writable flag value
223      */
224     public void setWritable(final boolean writable) {
225         this.isWritable = writable;
226     }
227
228     /**
229      * Checks if the album is writable.
230      *
231      * @return true, if the album is writable
232      */
233     public boolean isWritable() {
234         return isWritable;
235     }
236
237     /**
238      * Gets the artifact key of the item schema of this context album.
239      *
240      * @return the item schema key
241      */
242     public AxArtifactKey getItemSchema() {
243         return itemSchema;
244     }
245
246     /**
247      * Sets the artifact key of the item schema of this context album.
248      *
249      * @param itemSchema the item schema key
250      */
251     public void setItemSchema(final AxArtifactKey itemSchema) {
252         Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
253         this.itemSchema = itemSchema;
254     }
255
256     /*
257      * (non-Javadoc)
258      *
259      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.
260      * basicmodel.concepts.AxValidationResult)
261      */
262     @Override
263     public AxValidationResult validate(final AxValidationResult resultIn) {
264         AxValidationResult result = resultIn;
265
266         if (key.equals(AxArtifactKey.getNullKey())) {
267             result.addValidationMessage(
268                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
269         }
270         result = key.validate(result);
271
272         if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
273             result.addValidationMessage(
274                     new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID, "scope is not defined"));
275         }
276
277         try {
278             Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
279         } catch (final IllegalArgumentException e) {
280             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
281                     "scope invalid-" + e.getMessage()));
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 }