aboutsummaryrefslogtreecommitdiffstats
path: root/model/context-model/src/main/java/org/onap/policy/apex/model/contextmodel/concepts/AxContextAlbum.java
blob: db64f8847819aec7f885e904f57c1607bf54ab46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019-2022 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.model.contextmodel.concepts;

import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationMessage;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
import org.onap.policy.common.utils.validation.Assertions;

/**
 * This class is used to define an album of context.
 *
 * <p>A context album is a distributed map of context that will be distributed across all process instances that require
 * access to it. This class defines the schema (structure) of the items in the context album, whether the items on the
 * context album are writable or not, and what the scope of the context album is.
 *
 * <p>The structure of items (objects) the context album is defined as a schema, which is understood by whatever schema
 * implementation is being used for the context album.
 *
 * <p>The scope of a context album is a string field, understood by whatever distribution mechanism is being used for
 * the context album. The distribution mechanism uses the scope of the context album to decide to which executable
 * entities a given context album is distributed.
 *
 * <p>The writable flag on a context album defines whether users of a context album can write to the context album or
 * just read objects from the context album.
 *
 * <p>Validation checks that the album key and the context schema key are not null and that the scope field is not
 * undefined and matches the regular expression SCOPE_REGEXP.
 */

@Getter
@ToString
@EqualsAndHashCode(callSuper = false)

public class AxContextAlbum extends AxConcept {
    private static final String SCOPE_STRING = "scope";

    private static final long serialVersionUID = 4290442590545820316L;

    /**
     * The legal values for the scope of a context album is constrained by this regular expression.
     */
    public static final String SCOPE_REGEXP = "[A-Za-z0-9\\-_]+";

    /** The value of scope for a context album for which a scope has not been specified. */
    public static final String SCOPE_UNDEFINED = "UNDEFINED";

    private AxArtifactKey key;
    private String scope;

    @Setter
    private boolean isWritable;

    private AxArtifactKey itemSchema;

    /**
     * The default constructor creates a context album with a null artifact key. The scope of the context album is set
     * as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null artifact
     * key.
     */
    public AxContextAlbum() {
        this(new AxArtifactKey());
        scope = SCOPE_UNDEFINED;
        isWritable = true;
        itemSchema = AxArtifactKey.getNullKey();
    }

    /**
     * Copy constructor.
     *
     * @param copyConcept the concept to copy from
     */
    public AxContextAlbum(final AxContextAlbum copyConcept) {
        super(copyConcept);
    }

    /**
     * The keyed constructor creates a context album with the specified artifact key. The scope of the context album is
     * set as SCOPE_UNDEFINED, the album is writable, and the artifact key of the context schema is set to the null
     * artifact key.
     *
     * @param key the key of the context album
     */
    public AxContextAlbum(final AxArtifactKey key) {
        this(key, SCOPE_UNDEFINED, true, AxArtifactKey.getNullKey());
    }

    /**
     * Constructor that sets all the fields of the context album.
     *
     * @param key the key of the context album
     * @param scope the scope field, must match the regular expression SCOPE_REGEXP
     * @param isWritable specifies whether the context album will be writable or not
     * @param itemSchema the artifact key of the context schema to use for this context album
     */
    public AxContextAlbum(final AxArtifactKey key, final String scope, final boolean isWritable,
                    final AxArtifactKey itemSchema) {
        super();
        Assertions.argumentNotNull(key, "key may not be null");
        Assertions.argumentNotNull(scope, "scope may not be null");
        Assertions.argumentNotNull(itemSchema, "itemSchema may not be null");

        this.key = key;
        this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
        this.isWritable = isWritable;
        this.itemSchema = itemSchema;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public List<AxKey> getKeys() {
        final List<AxKey> keyList = key.getKeys();
        keyList.add(new AxKeyUse(itemSchema.getKey()));

        return keyList;
    }

    /**
     * Sets the key of the context album.
     *
     * @param key the context album key
     */
    public void setKey(final AxArtifactKey key) {
        Assertions.argumentNotNull(key, "key may not be null");
        this.key = key;
    }

    /**
     * Sets the scope of the context album.
     *
     * @param scope the context album scope
     */
    public void setScope(final String scope) {
        Assertions.argumentNotNull(scope, "scope may not be null");
        this.scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
    }

    /**
     * Sets the artifact key of the item schema of this context album.
     *
     * @param itemSchema the item schema key
     */
    public void setItemSchema(final AxArtifactKey itemSchema) {
        Assertions.argumentNotNull(itemSchema, "itemSchema key may not be null");
        this.itemSchema = itemSchema;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public AxValidationResult validate(final AxValidationResult resultIn) {
        AxValidationResult result = resultIn;

        if (key.equals(AxArtifactKey.getNullKey())) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                            "key is a null key"));
        }
        result = key.validate(result);

        if (scope.replaceAll("\\s+$", "").length() == 0 || scope.equals(SCOPE_UNDEFINED)) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                            "scope is not defined"));
        }

        var stringCheckResult = Assertions.getStringParameterValidationMessage(SCOPE_STRING, scope, SCOPE_REGEXP);
        if (stringCheckResult != null) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                            "scope invalid-" + stringCheckResult));
        }

        if (itemSchema.equals(AxArtifactKey.getNullKey())) {
            result.addValidationMessage(new AxValidationMessage(key, this.getClass(), ValidationResult.INVALID,
                            "itemSchema reference is a null key, an item schema must be specified"));
        }
        result = itemSchema.validate(result);

        return result;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void clean() {
        key.clean();
        scope = Assertions.validateStringParameter(SCOPE_STRING, scope, SCOPE_REGEXP);
        itemSchema.clean();
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public AxConcept copyTo(final AxConcept target) {
        Assertions.argumentNotNull(target, "targetObject may not be null");

        final Object copyObject = target;
        Assertions.instanceOf(copyObject, AxContextAlbum.class);

        final AxContextAlbum copy = ((AxContextAlbum) copyObject);
        copy.setKey(new AxArtifactKey(key));
        copy.setScope(scope);
        copy.setWritable(isWritable);
        copy.setItemSchema(new AxArtifactKey(itemSchema));

        return copy;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public int compareTo(final AxConcept otherObj) {
        if (otherObj == null) {
            return -1;
        }
        if (this == otherObj) {
            return 0;
        }
        if (getClass() != otherObj.getClass()) {
            return this.hashCode() - otherObj.hashCode();
        }

        final AxContextAlbum other = (AxContextAlbum) otherObj;
        if (!key.equals(other.key)) {
            return key.compareTo(other.key);
        }
        if (!scope.equals(other.scope)) {
            return scope.compareTo(other.scope);
        }
        if (isWritable != other.isWritable) {
            return (isWritable ? 1 : -1);
        }
        return itemSchema.compareTo(other.itemSchema);
    }
}