d7d8a1d96917caf69d744021001be2cd0c33993c
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2022 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.xml.bind.annotation.XmlAccessType;
27 import javax.xml.bind.annotation.XmlAccessorType;
28 import javax.xml.bind.annotation.XmlElement;
29 import javax.xml.bind.annotation.XmlRootElement;
30 import javax.xml.bind.annotation.XmlType;
31 import lombok.EqualsAndHashCode;
32 import lombok.Getter;
33 import lombok.Setter;
34 import lombok.ToString;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
42 import org.onap.policy.common.utils.validation.Assertions;
43
44 /**
45  * This class is used to define an album of context.
46  *
47  * <p>A context album is a distributed map of context that will be distributed across all process instances that require
48  * access to it. This class defines the schema (structure) of the items in the context album, whether the items on the
49  * context album are writable or not, and what the scope of the context album is.
50  *
51  * <p>The structure of items (objects) the context album is defined as a schema, which is understood by whatever schema
52  * implementation is being used for the context album.
53  *
54  * <p>The scope of a context album is a string field, understood by whatever distribution mechanism is being used for
55  * the context album. The distribution mechanism uses the scope of the context album to decide to which executable
56  * entities a given context album is distributed.
57  *
58  * <p>The writable flag on a context album defines whether users of a context album can write to the context album or
59  * just read objects from the context album.
60  *
61  * <p>Validation checks that the album key and the context schema key are not null and that the scope field is not
62  * undefined and matches the regular expression SCOPE_REGEXP.
63  */
64
65 @Getter
66 @ToString
67 @EqualsAndHashCode(callSuper = false)
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     @XmlElement(name = "key", required = true)
88     private AxArtifactKey key;
89
90     @XmlElement(name = SCOPE_STRING, required = true)
91     private String scope;
92
93     @XmlElement(name = "isWritable", required = true)
94     @Setter
95     private boolean isWritable;
96
97     @XmlElement(name = "itemSchema", required = true)
98     private AxArtifactKey itemSchema;
99
100     /**
101      * The default constructor creates a context album with a null artifact key. The scope of the context album is set
102      * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null artifact
103      * key.
104      */
105     public AxContextAlbum() {
106         this(new AxArtifactKey());
107         scope = SCOPE_UNDEFINED;
108         isWritable = true;
109         itemSchema = AxArtifactKey.getNullKey();
110     }
111
112     /**
113      * Copy constructor.
114      *
115      * @param copyConcept the concept to copy from
116      */
117     public AxContextAlbum(final AxContextAlbum copyConcept) {
118         super(copyConcept);
119     }
120
121     /**
122      * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
123      * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
124      * artifact key.
125      *
126      * @param key the key of the context album
127      */
128     public AxContextAlbum(final AxArtifactKey key) {
129         this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
130     }
131
132     /**
133      * Constructor that sets all the fields of the context album.
134      *
135      * @param key the key of the context album
136      * @param scope the scope field, must match the regular expression SCOPE_REGEXP
137      * @param isWritable specifies whether the context album will be writable or not
138      * @param itemSchema the artifact key of the context schema to use for this context album
139      */
140     public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
141                     final AxArtifactKey itemSchema) {
142         super();
143         Assertions.argumentNotNull(key, "key may not be null");
144         Assertions.argumentNotNull(scope, "scope may not be null");
145         Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");
146
147         this.key = key;
148         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
149         this.isWritable = isWritable;
150         this.itemSchema = itemSchema;
151     }
152
153     /**
154      * {@inheritDoc}.
155      */
156     @Override
157     public List<AxKey> getKeys() {
158         final List<AxKey> keyList = key.getKeys();
159         keyList.add(new AxKeyUse(itemSchema.getKey()));
160
161         return keyList;
162     }
163
164     /**
165      * Sets the key of the context album.
166      *
167      * @param key the context album key
168      */
169     public void setKey(final AxArtifactKey key) {
170         Assertions.argumentNotNull(key, "key may not be null");
171         this.key = key;
172     }
173
174     /**
175      * Sets the scope of the context album.
176      *
177      * @param scope the context album scope
178      */
179     public void setScope(final String scope) {
180         Assertions.argumentNotNull(scope, "scope may not be null");
181         this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
182     }
183
184     /**
185      * Sets the artifact key of the item schema of this context album.
186      *
187      * @param itemSchema the item schema key
188      */
189     public void setItemSchema(final AxArtifactKey itemSchema) {
190         Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
191         this.itemSchema = itemSchema;
192     }
193
194     /**
195      * {@inheritDoc}.
196      */
197     @Override
198     public AxValidationResult validate(final AxValidationResult resultIn) {
199         AxValidationResult result = resultIn;
200
201         if (key.equals(AxArtifactKey.getNullKey())) {
202             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
203                             "key is a null key"));
204         }
205         result = key.validate(result);
206
207         if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
208             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
209                             "scope is not defined"));
210         }
211
212         var stringCheckResult = Assertions.getStringParameterValidationMessage(SCOPE_STRING, scope, SCOPE_REGEXP);
213         if (stringCheckResult != null) {
214             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
215                             "scope invalid-" + stringCheckResult));
216         }
217
218         if (itemSchema.equals(AxArtifactKey.getNullKey())) {
219             result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
220                             "itemSchema reference is a null key, an item schema must be specified"));
221         }
222         result = itemSchema.validate(result);
223
224         return result;
225     }
226
227     /**
228      * {@inheritDoc}.
229      */
230     @Override
231     public void clean() {
232         key.clean();
233         scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
234         itemSchema.clean();
235     }
236
237     /**
238      * {@inheritDoc}.
239      */
240     @Override
241     public AxConcept copyTo(final AxConcept target) {
242         Assertions.argumentNotNull(target, "targetObject may not be null");
243
244         final Object copyObject = target;
245         Assertions.instanceOf(copyObject, AxContextAlbum.class);
246
247         final AxContextAlbum copy = ((AxContextAlbum) copyObject);
248         copy.setKey(new AxArtifactKey(key));
249         copy.setScope(scope);
250         copy.setWritable(isWritable);
251         copy.setItemSchema(new AxArtifactKey(itemSchema));
252
253         return copy;
254     }
255
256     /**
257      * {@inheritDoc}.
258      */
259     @Override
260     public int compareTo(final AxConcept otherObj) {
261         if (otherObj == null) {
262             return -1;
263         }
264         if (this == otherObj) {
265             return 0;
266         }
267         if (getClass() != otherObj.getClass()) {
268             return this.hashCode() - otherObj.hashCode();
269         }
270
271         final AxContextAlbum other = (AxContextAlbum) otherObj;
272         if (!key.equals(other.key)) {
273             return key.compareTo(other.key);
274         }
275         if (!scope.equals(other.scope)) {
276             return scope.compareTo(other.scope);
277         }
278         if (isWritable != other.isWritable) {
279             return (isWritable ? 1 : -1);
280         }
281         return itemSchema.compareTo(other.itemSchema);
282     }
283 }