aboutsummaryrefslogtreecommitdiffstats
path: root/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java
blob: 6382992d9a6445a71f9e1d494a2204205fb63085 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. 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.context.impl;

import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.onap.policy.apex.context.ContextAlbum;
import org.onap.policy.apex.context.ContextException;
import org.onap.policy.apex.context.ContextRuntimeException;
import org.onap.policy.apex.context.Distributor;
import org.onap.policy.apex.context.SchemaHelper;
import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
import org.onap.policy.apex.context.monitoring.ContextMonitor;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
import org.onap.policy.apex.model.utilities.Assertions;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * The Class ContextAlbumImpl implements the methods on the {@link ContextAlbum} interface. It implements the getters
 * and setters on the {@link Map} and uses the {@link Distributor} to handle distribution and locking.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public final class ContextAlbumImpl implements ContextAlbum {
    // Logger for this class
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumImpl.class);

    // Recurring string constants
    private static final String NULL_VALUES_ILLEGAL = "null values are illegal on method parameter \"key\"";
    private static final String ALBUM = "album \"";

    // The definition of this context album
    private final AxContextAlbum albumDefinition;

    /// The map holding the items and their values for this context album
    private final Map<String, Object> albumMap;

    // The artifact stack of the artifacts currently using the context album
    private AxConcept[] userArtifactStack = null;

    // The context distributor we are using
    private final Distributor distributor;

    // The schema helper that handles translations of Java objects for this album
    private SchemaHelper schemaHelper;

    // The context monitor for this context album
    private ContextMonitor monitor = null;

    /**
     * Constructor, instantiate the context album.
     *
     * @param albumDefinition The model definition of this context album
     * @param distributor The context distributor passed to us to distribute context across ContextAlbum instances
     * @param albumMap the album map
     * @throws ContextException on errors creating context albums
     */
    public ContextAlbumImpl(final AxContextAlbum albumDefinition, final Distributor distributor,
            final Map<String, Object> albumMap) throws ContextException {
        Assertions.argumentNotNull(albumDefinition, "Context album definition may not be null");
        Assertions.argumentNotNull(distributor, "Distributor may not be null");
        Assertions.argumentNotNull(albumMap, "Album map may not be null");

        this.albumDefinition = albumDefinition;

        // Use the context distributor passed to us
        this.distributor = distributor;

        // The map to use to store objects
        this.albumMap = albumMap;

        try {
            // Get a schema helper to manage the translations between objects on the album map for this album
            schemaHelper = new SchemaHelperFactory().createSchemaHelper(albumDefinition.getKey(),
                    albumDefinition.getItemSchema());
        } catch (final ContextRuntimeException e) {
            final String resultString = "could not initiate schema management for context album " + albumDefinition;
            LOGGER.warn(resultString, e);
            throw new ContextException(resultString, e);
        }

        // Create the context monitor
        monitor = new ContextMonitor();

    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.context.ContextAlbum#getKey()
     */
    @Override
    public AxArtifactKey getKey() {
        return albumDefinition.getKey();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.context.ContextAlbum#getName()
     */
    @Override
    public String getName() {
        return albumDefinition.getKey().getName();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.context.ContextAlbum#getAxContextAlbum()
     */
    @Override
    public AxContextAlbum getAlbumDefinition() {
        return albumDefinition;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.context.ContextAlbum#getSchemaHelper()
     */
    @Override
    public SchemaHelper getSchemaHelper() {
        return schemaHelper;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.context.ContextAlbum#lockForReading(java.lang.String)
     */
    @Override
    public void lockForReading(final String keyOnAlbum) throws ContextException {
        distributor.lockForReading(albumDefinition.getKey(), keyOnAlbum);
        monitor.monitorReadLock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum,
                userArtifactStack);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.context.ContextAlbum#lockForWriting(java.lang.String)
     */
    @Override
    public void lockForWriting(final String keyOnAlbum) throws ContextException {
        distributor.lockForWriting(albumDefinition.getKey(), keyOnAlbum);
        monitor.monitorWriteLock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum,
                userArtifactStack);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.context.ContextAlbum#unlockForReading(java.lang.String)
     */
    @Override
    public void unlockForReading(final String keyOnAlbum) throws ContextException {
        distributor.unlockForReading(albumDefinition.getKey(), keyOnAlbum);
        monitor.monitorReadUnlock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum,
                userArtifactStack);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.context.ContextAlbum#unlockForWriting(java.lang.String)
     */
    @Override
    public void unlockForWriting(final String keyOnAlbum) throws ContextException {
        distributor.unlockForWriting(albumDefinition.getKey(), keyOnAlbum);
        monitor.monitorWriteUnlock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum,
                userArtifactStack);
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * org.onap.policy.apex.context.ContextAlbum#getUserArtifactStack()
     */
    @Override
    public AxConcept[] getUserArtifactStack() {
        return userArtifactStack;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * org.onap.policy.apex.context.ContextAlbum#setUserArtifactStack(org.onap.policy.apex.model.basicmodel.concepts.
     * AxConcept [])
     */
    @Override
    public void setUserArtifactStack(final AxConcept[] userArtifactStack) {
        this.userArtifactStack = userArtifactStack;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.onap.policy.apex.context.ContextAlbum#flush()
     */
    @Override
    public void flush() throws ContextException {
        distributor.flushContextAlbum(this);
    }

    /*
     * The Map interface
     */

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#size()
     */
    @Override
    public int size() {
        return albumMap.size();
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#isEmpty()
     */
    @Override
    public boolean isEmpty() {
        return albumMap.isEmpty();
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#containsKey(java.lang.Object)
     */
    @Override
    public boolean containsKey(final Object key) {
        if (key == null) {
            LOGGER.warn(NULL_VALUES_ILLEGAL);
            throw new ContextRuntimeException(NULL_VALUES_ILLEGAL);
        }

        return albumMap.containsKey(key);
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#containsValue(java.lang.Object)
     */
    @Override
    public boolean containsValue(final Object value) {
        if (value == null) {
            LOGGER.warn("null values are illegal on method parameter \"value\"");
            throw new ContextRuntimeException("null values are illegal on method parameter \"value\"");
        }

        return albumMap.containsValue(value);
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#get(java.lang.Object)
     */
    @Override
    public Object get(final Object key) {
        if (key == null) {
            final String returnString =
                    ALBUM + albumDefinition.getId() + "\" null keys are illegal on keys for get()";
            LOGGER.warn(returnString);
            throw new ContextRuntimeException(returnString);
        }

        final Object item = albumMap.get(key);
        if (item == null) {
            return null;
        }

        // Get the context value and monitor it
        monitor.monitorGet(albumDefinition.getKey(), albumDefinition.getItemSchema(), key.toString(), item,
                userArtifactStack);
        return item;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#keySet()
     */
    @Override
    public Set<String> keySet() {
        return albumMap.keySet();
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#values()
     */
    @Override
    public Collection<Object> values() {
        // Build the key set and return it
        final ArrayList<Object> valueList = new ArrayList<>();

        for (final Entry<String, Object> contextAlbumEntry : albumMap.entrySet()) {
            final Object item = contextAlbumEntry.getValue();
            monitor.monitorGet(albumDefinition.getKey(), albumDefinition.getItemSchema(), contextAlbumEntry.getKey(),
                    item, userArtifactStack);
            valueList.add(contextAlbumEntry.getValue());
        }

        return valueList;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#entrySet()
     */
    @Override
    public Set<Entry<String, Object>> entrySet() {
        // Build the entry set and return it
        final Set<Entry<String, Object>> entrySet = new HashSet<>();

        for (final Entry<String, Object> contextAlbumEntry : albumMap.entrySet()) {
            final Object item = contextAlbumEntry.getValue();
            monitor.monitorGet(albumDefinition.getKey(), albumDefinition.getItemSchema(), contextAlbumEntry.getKey(),
                    item, userArtifactStack);
            entrySet.add(new SimpleEntry<>(contextAlbumEntry.getKey(), contextAlbumEntry.getValue()));
        }

        return entrySet;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#put(java.lang.Object, java.lang.Object)
     */
    @Override
    public Object put(final String key, final Object incomingValue) {
        if (key == null) {
            final String returnString =
                    ALBUM + albumDefinition.getId() + "\" null keys are illegal on keys for put()";
            LOGGER.warn(returnString);
            throw new ContextRuntimeException(returnString);
        }

        if (incomingValue == null) {
            final String returnString = ALBUM + albumDefinition.getId() + "\" null values are illegal on key \""
                    + key + "\" for put()";
            LOGGER.warn(returnString);
            throw new ContextRuntimeException(returnString);
        }

        if (!albumDefinition.isWritable()) {
            final String returnString = ALBUM + albumDefinition.getId()
                    + "\" put() not allowed on read only albums for key=\"" + key + "\", value=\"" + incomingValue;
            LOGGER.warn(returnString);
            throw new ContextRuntimeException(returnString);
        }

        try {
            // Translate the object to a schema object
            final Object valueToPut = schemaHelper.unmarshal(incomingValue);

            // Check if the key is already in the map
            if (albumMap.containsKey(key)) {
                // Update the value in the context item and in the context value map
                monitor.monitorSet(albumDefinition.getKey(), albumDefinition.getItemSchema(), key, incomingValue,
                        userArtifactStack);
            } else {
                // Update the value in the context item and in the context value map
                monitor.monitorInit(albumDefinition.getKey(), albumDefinition.getItemSchema(), key, incomingValue,
                        userArtifactStack);
            }

            // Put the translated value on the map and return the old map value
            return albumMap.put(key, valueToPut);
        } catch (final ContextRuntimeException e) {
            final String returnString = "Failed to set context value for key \"" + key + "\" in album \""
                    + albumDefinition.getId() + "\": " + e.getMessage();
            LOGGER.warn(returnString);
            throw new ContextRuntimeException(returnString, e);
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#putAll(java.util.Map)
     */
    @Override
    public void putAll(final Map<? extends String, ? extends Object> incomingContextAlbum) {
        if (!albumDefinition.isWritable()) {
            final String returnString =
                    ALBUM + albumDefinition.getId() + "\" putAll() not allowed on read only albums";
            LOGGER.warn(returnString);
            throw new ContextRuntimeException(returnString);
        }

        // Sanity check on incoming context
        Assertions.argumentOfClassNotNull(incomingContextAlbum, ContextRuntimeException.class,
                "cannot update context, context album is null");

        // Iterate over the incoming context
        for (final Entry<String, Object> entry : albumMap.entrySet()) {
            synchronized (albumDefinition) {
                // Get the key for the incoming name
                final Object incomingDataItem = incomingContextAlbum.get(entry.getKey());
                if (incomingDataItem != null) {
                    // Update the value the context album
                    put(entry.getKey(), incomingDataItem);
                }
            }
        }

        // Put all the objects on the context album
        for (final Entry<? extends String, ? extends Object> incomingMapEntry : incomingContextAlbum.entrySet()) {
            // Put the entry on the map
            this.put(incomingMapEntry.getKey(), incomingMapEntry.getValue());
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#remove(java.lang.Object)
     */
    @Override
    public Object remove(final Object key) {
        if (!albumDefinition.isWritable()) {
            final String returnString = ALBUM + albumDefinition.getId()
                    + "\" remove() not allowed on read only albums for key=\"" + key + "\"";
            LOGGER.warn(returnString);
            throw new ContextRuntimeException(returnString);
        }

        if (key == null) {
            LOGGER.warn(NULL_VALUES_ILLEGAL);
            throw new ContextRuntimeException("null values are illegal on method parameter \"keyID\"");
        }

        // Delete the item
        final Object removedValue = albumMap.remove(key);
        monitor.monitorDelete(albumDefinition.getKey(), albumDefinition.getItemSchema(), key.toString(), removedValue,
                userArtifactStack);

        // Return the value of the deleted item
        return removedValue;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.util.Map#clear()
     */
    @Override
    public void clear() {
        if (!albumDefinition.isWritable()) {
            final String returnString =
                    ALBUM + albumDefinition.getId() + "\" clear() not allowed on read only albums";
            LOGGER.warn(returnString);
            throw new ContextRuntimeException(returnString);
        }

        // Monitor deletion of each item
        for (final Entry<String, Object> contextAlbumEntry : albumMap.entrySet()) {
            final Object item = contextAlbumEntry.getValue();
            monitor.monitorDelete(albumDefinition.getKey(), albumDefinition.getItemSchema(), contextAlbumEntry.getKey(),
                    item, userArtifactStack);
        }

        // Clear the map
        albumMap.clear();
    }
}