3d008e99289008608d74d88754e79030ef8c3234
[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  * 
50  * <p>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  * 
54  * <p>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  * 
57  * <p>The scope of a context album is a string field, understood by whatever distribution mechanism is being used for
58  * the context album. The distribution mechanism uses the scope of the context album to decide to which executable
59  * entities a given context album is distributed.
60  * 
61  * <p>The writable flag on a context album defines whether users of a context album can write to the context album or
62  * just read objects from the context album.
63  * 
64  * <p>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", propOrder =
73     { "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 artifact
117      * 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 null
138      * 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(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
268                             "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(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
274                             "scope is not defined"));
275         }
276
277         String stringCheckResult = Assertions.getStringParameterValidationMessage(SCOPE_STRING, scope, SCOPE_REGEXP);
278         if (stringCheckResult != null) {
279             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
280                             "scope invalid-" + stringCheckResult));
281         }
282
283         if (itemSchema.equals(AxArtifactKey.getNullKey())) {
284             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
285                             "itemSchema reference is a null key, an item schema must be specified"));
286         }
287         result = itemSchema.validate(result);
288
289         return result;
290     }
291
292     /*
293      * (non-Javadoc)
294      *
295      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean()
296      */
297     @Override
298     public void clean() {
299         key.clean();
300         scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
301         itemSchema.clean();
302     }
303
304     /*
305      * (non-Javadoc)
306      *
307      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString()
308      */
309     @Override
310     public String toString() {
311         final StringBuilder builder = new StringBuilder();
312         builder.append(this.getClass().getSimpleName());
313         builder.append(":(");
314         builder.append("key=");
315         builder.append(key);
316         builder.append(",scope=");
317         builder.append(scope);
318         builder.append(",isWritable=");
319         builder.append(isWritable);
320         builder.append(",itemSchema=");
321         builder.append(itemSchema);
322         builder.append(")");
323         return builder.toString();
324     }
325
326     /*
327      * (non-Javadoc)
328      *
329      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.
330      * basicmodel.concepts.AxConcept)
331      */
332     @Override
333     public AxConcept copyTo(final AxConcept target) {
334         Assertions.argumentNotNull(target, "targetObject may not be null");
335
336         final Object copyObject = target;
337         Assertions.instanceOf(copyObject, AxContextAlbum.class);
338
339         final AxContextAlbum copy = ((AxContextAlbum) copyObject);
340         copy.setKey(new AxArtifactKey(key));
341         copy.setScope(scope);
342         copy.setWritable(isWritable);
343         copy.setItemSchema(new AxArtifactKey(itemSchema));
344
345         return copy;
346     }
347
348     /*
349      * (non-Javadoc)
350      *
351      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode()
352      */
353     @Override
354     public int hashCode() {
355         final int prime = 31;
356         int result = 1;
357         result = prime * result + key.hashCode();
358         result = prime * result + scope.hashCode();
359         result = prime * result + (isWritable ? HASH_PRIME_0 : HASH_PRIME_1);
360         result = prime * result + itemSchema.hashCode();
361         return result;
362     }
363
364     /*
365      * (non-Javadoc)
366      *
367      * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object)
368      */
369     @Override
370     public boolean equals(final Object obj) {
371         if (obj == null) {
372             return false;
373         }
374         if (this == obj) {
375             return true;
376         }
377
378         if (getClass() != obj.getClass()) {
379             return false;
380         }
381
382         final AxContextAlbum other = (AxContextAlbum) obj;
383         if (!key.equals(other.key)) {
384             return false;
385         }
386         if (!scope.equals(other.scope)) {
387             return false;
388         }
389         if (isWritable != other.isWritable) {
390             return (false);
391         }
392         return itemSchema.equals(other.itemSchema);
393     }
394
395     /*
396      * (non-Javadoc)
397      *
398      * @see java.lang.Comparable#compareTo(java.lang.Object)
399      */
400     @Override
401     public int compareTo(final AxConcept otherObj) {
402         if (otherObj == null) {
403             return -1;
404         }
405         if (this == otherObj) {
406             return 0;
407         }
408         if (getClass() != otherObj.getClass()) {
409             return this.hashCode() - otherObj.hashCode();
410         }
411
412         final AxContextAlbum other = (AxContextAlbum) otherObj;
413         if (!key.equals(other.key)) {
414             return key.compareTo(other.key);
415         }
416         if (!scope.equals(other.scope)) {
417             return scope.compareTo(other.scope);
418         }
419         if (isWritable != other.isWritable) {
420             return (isWritable ? 1 : -1);
421         }
422         return itemSchema.compareTo(other.itemSchema);
423     }
424 }