aboutsummaryrefslogtreecommitdiffstats
path: root/champ-lib/champ-core/src/main/java/org/onap/aai/champcore/event/AbstractLoggingChampGraph.java
blob: 1ecf71b8558a491e90f6f36d146a353331b77a74 (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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/**
 * ============LICENSE_START==========================================
 * org.onap.aai
 * ===================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ===================================================================
 * 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.
 * ============LICENSE_END============================================
 */
package org.onap.aai.champcore.event;


import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.onap.aai.champcore.ChampCapabilities;
import org.onap.aai.champcore.ChampCoreMsgs;
import org.onap.aai.champcore.ChampGraph;
import org.onap.aai.champcore.ChampTransaction;
import org.onap.aai.champcore.event.ChampEvent.ChampOperation;
import org.onap.aai.champcore.event.envelope.ChampEventEnvelope;
import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
import org.onap.aai.champcore.exceptions.ChampMarshallingException;
import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
import org.onap.aai.champcore.exceptions.ChampTransactionException;
import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
import org.onap.aai.champcore.model.ChampObject;
import org.onap.aai.champcore.model.ChampObjectConstraint;
import org.onap.aai.champcore.model.ChampObjectIndex;
import org.onap.aai.champcore.model.ChampPartition;
import org.onap.aai.champcore.model.ChampRelationship;
import org.onap.aai.champcore.model.ChampRelationshipConstraint;
import org.onap.aai.champcore.model.ChampRelationshipIndex;
import org.onap.aai.champcore.model.ChampSchema;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.event.api.EventPublisher;




/**
 * This class provides the hooks to allow Champ operations to be logged to an event
 * stream.
 */
public abstract class AbstractLoggingChampGraph implements ChampGraph {

  public static final String  PARAM_EVENT_QUEUE_CAPACITY = "champcore.event.stream.buffer.capacity";
  public static final Integer DEFAULT_EVENT_QUEUE_CAPACITY = 10000;
  public static final String  PARAM_EVENT_STREAM_PUBLISHER_POOL_SIZE = "champcore.event.stream.publisher-pool-size";
  public static final Integer DEFAULT_EVENT_STREAM_PUBLISHER_POOL_SIZE = 5;
  public static final String PARAM_EVENT_STREAM_PRODUCER = "champcore.event.stream.publisher";
  protected static final String KEY_PROPERTY_NAME = "aai-uuid";
  protected static final String NODE_TYPE_PROPERTY_NAME = "aai-node-type";

  /** Pool of worker threads that do the work of publishing the events to the event bus. */
  protected ThreadPoolExecutor publisherPool;

  /** Client used for publishing events to the event bus. */
  protected EventPublisher producer;

  /** Internal queue where outgoing events will be buffered until they can be serviced by
   *  the event publisher worker threads. */
  protected BlockingQueue<ChampEvent> eventQueue;

  /** Number of events that can be queued up for publication before we begin dropping
   *  events. */
  private Integer eventQueueCapacity;

  /** Number of event publisher worker threads. */
  private Integer eventStreamPublisherPoolSize;

  private static final Logger logger = LoggerFactory.getInstance().getLogger(AbstractLoggingChampGraph.class);


  /**
   * Create a new instance of the AbstractLoggingChampGraph.
   *
   * @param properties - Set of configuration properties for this graph instance.
   */
  protected AbstractLoggingChampGraph(Map<String, Object> properties) {

    // Extract the necessary parameters from the configuration properties.
    configure(properties);

    // Make sure we were passed an event producer as one of our properties, otherwise
    // there is really nothing more we can do...
    if(producer == null) {
      logger.error(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_ERROR,
          "No event stream producer was supplied.");
      logger.error(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_ERROR,
          "NOTE!! Champ events will NOT be published to the event stream!");
      return;
    }

    // Create the blocking queue that we will use to buffer events that we want
    // published to the event bus.
    eventQueue = new ArrayBlockingQueue<ChampEvent>(eventQueueCapacity);

    // Create the executor pool that will do the work of publishing events to the event bus.
    publisherPool =
        (ThreadPoolExecutor) Executors.newFixedThreadPool(eventStreamPublisherPoolSize,
            new ProducerWorkerThreadFactory());

    try {

      // Start up the producer worker threads.
      for(int i=0; i<eventStreamPublisherPoolSize; i++) {
        publisherPool.submit(new EventPublisherWorker());
      }

    } catch (Exception e) {

      logger.error(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_ERROR,
          "Failed to instantiate event stream producer thread due to: '" + e.getMessage() + "'");
      logger.error(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_ERROR,
          "NOTE!! Champ events may NOT be published to the event stream!");
      return;
    }
  }



  public abstract Optional<ChampObject>       retrieveObject(Object key) throws ChampUnmarshallingException, ChampTransactionException;
  public abstract Optional<ChampObject>       retrieveObject(Object key, Optional<ChampTransaction> transaction) throws ChampUnmarshallingException, ChampTransactionException;
  public abstract Stream<ChampObject>         queryObjects(Map<String, Object> queryParams) throws ChampTransactionException;
  public abstract Stream<ChampObject>         queryObjects(Map<String, Object> queryParams, Optional<ChampTransaction> transaction) throws ChampTransactionException;
  @Override
  public abstract Optional<ChampRelationship> retrieveRelationship(Object key) throws ChampUnmarshallingException, ChampTransactionException;
  @Override
  public abstract Optional<ChampRelationship> retrieveRelationship(Object key, Optional<ChampTransaction> transaction) throws ChampUnmarshallingException, ChampTransactionException;
  public abstract Stream<ChampRelationship>   retrieveRelationships(ChampObject object) throws ChampUnmarshallingException, ChampObjectNotExistsException, ChampTransactionException;
  public abstract Stream<ChampRelationship>   retrieveRelationships(ChampObject object, Optional<ChampTransaction> transaction) throws ChampUnmarshallingException, ChampObjectNotExistsException, ChampTransactionException;
  public abstract Stream<ChampRelationship>   queryRelationships(Map<String, Object> queryParams) throws ChampTransactionException;

  public abstract Stream<ChampRelationship>   queryRelationships(Map<String, Object> queryParams, Optional<ChampTransaction> transaction) throws ChampTransactionException;

  /**
    * Creates or updates a vertex in the graph data store.
    * <p>
    * If a transaction context is not provided, then a transaction will be automatically
    * created and committed for this operation only, otherwise, the supplied transaction
    * will be used and it will be up to the caller to commit the transaction at its
    * discretion.
    *
    * @param object      - The vertex to be created or updated.
    * @param transaction - Optional transaction context to perform the operation in.
    *
    * @return - The vertex, as created, marshaled as a {@link ChampObject}
    *
    * @throws ChampMarshallingException     - If the {@code object} is not able to be marshalled
    *                                         into the backend representation
    * @throws ChampSchemaViolationException - If the {@code object} violates the constraints specifed
    *                                         by {@link ChampGraph#retrieveSchema}
    * @throws ChampObjectNotExistsException - If {@link org.onap.aai.champcore.model.ChampObject#getKey}
    *                                         is not present or object not found in the graph
    * @throws ChampTransactionException     - If an attempt to commit or rollback the transaction failed.
    */
  public abstract ChampObject executeStoreObject(ChampObject object, Optional<ChampTransaction> transaction) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException;

  /**
   * Updates an existing vertex in the graph store.
   * <p>
   * If a transaction context is not provided, then a transaction will be automatically
   * created and committed for this operation only, otherwise, the supplied transaction
   * will be used and it will be up to the caller to commit the transaction at its
   * discretion.
   *
   * @param object      - The vertex to be created or updated.
   * @param transaction - Optional transaction context to perform the operation in.
   *
   * @return - The updated vertex, marshaled as a {@link ChampObject}
   *
   * @throws ChampMarshallingException     - If the {@code object} is not able to be marshalled into
   *                                         the backend representation
   * @throws ChampSchemaViolationException - If the {@code object} violates the constraints specifed
   *                                         by {@link ChampGraph#retrieveSchema}
   * @throws ChampObjectNotExistsException - If {@link org.onap.aai.champcore.model.ChampObject#getKey}
   *                                         is not present or object not found in the graph
   * @throws ChampTransactionException     - If an attempt to commit or rollback the transaction failed.
   */
  public abstract ChampObject executeReplaceObject(ChampObject object, Optional<ChampTransaction> transaction) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException;

  /**
   * Deletes an existing vertex from the graph store.
   * <p>
   * If a transaction context is not provided, then a transaction will be automatically
   * created and committed for this operation only, otherwise, the supplied transaction
   * will be used and it will be up to the caller to commit the transaction at its
   * discretion.
   *
   * @param key         - The key of the ChampObject in the graph {@link ChampObject#getKey}
   * @param transaction - Optional transaction context to perform the operation in.
   *
   * @throws ChampObjectNotExistsException - If {@link org.onap.aai.champcore.model.ChampObject#getKey}
   *                                         is not present or object not found in the graph
   * @throws ChampTransactionException     - If an attempt to commit or rollback the transaction failed.
   */
  public abstract void executeDeleteObject(Object key, Optional<ChampTransaction> transaction) throws ChampObjectNotExistsException, ChampTransactionException;

  /**
   * Creates or updates an edge in the graph data store.
   * <p>
   * If a transaction context is not provided, then a transaction will be automatically
   * created and committed for this operation only, otherwise, the supplied transaction
   * will be used and it will be up to the caller to commit the transaction at its
   * discretion.
   *
   * @param relationship - The ChampRelationship that you wish to store in the graph
   * @param transaction  - Optional transaction context to perform the operation in.
   *
   * @return - The {@link ChampRelationship} as it was stored.
   *
   * @throws ChampUnmarshallingException         - If the edge which was created could not be
   *                                               unmarshalled into a ChampRelationship
   * @throws ChampMarshallingException           - If the {@code relationship} is not able to be
   *                                               marshalled into the backend representation
   * @throws ChampObjectNotExistsException       - If either the source or target object referenced
   *                                               by this relationship does not exist in the graph
   * @throws ChampSchemaViolationException       - If the {@code relationship} violates the constraints
   *                                               specifed by {@link ChampGraph#retrieveSchema}
   * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent()
   *                                               but the object cannot be found in the graph
   * @throws ChampTransactionException           - If an attempt to commit or rollback the transaction failed.
   */
  public abstract ChampRelationship executeStoreRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction) throws ChampUnmarshallingException, ChampMarshallingException, ChampObjectNotExistsException, ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampTransactionException;

  /**
   * Replaces an existing edge in the graph data store.
   * <p>
   * If a transaction context is not provided, then a transaction will be automatically
   * created and committed for this operation only, otherwise, the supplied transaction
   * will be used and it will be up to the caller to commit the transaction at its
   * discretion.
   *
   * @param relationship  - The ChampRelationship that you wish to replace in the graph
   * @param transaction   - Optional transaction context to perform the operation in.
   *
   * @return - The {@link ChampRelationship} as it was stored.
   *
   * @throws ChampUnmarshallingException         - If the edge which was created could not be
   *                                               unmarshalled into a ChampRelationship
   * @throws ChampMarshallingException           - If the {@code relationship} is not able to be
   *                                               marshalled into the backend representation
   * @throws ChampSchemaViolationException       - If the {@code relationship} violates the constraints
   *                                               specifed by {@link ChampGraph#retrieveSchema}
   * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent()
   *                                               but the object cannot be found in the graph
   * @throws ChampTransactionException           - If an attempt to commit or rollback the transaction failed.
   */
  public abstract ChampRelationship executeReplaceRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction) throws ChampUnmarshallingException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampTransactionException;

  /**
   * Removes an edge from the graph data store.
   * <p>
   * If a transaction context is not provided, then a transaction will be automatically
   * created and committed for this operation only, otherwise, the supplied transaction
   * will be used and it will be up to the caller to commit the transaction at its
   * discretion.
   *
   * @param relationship - The ChampRelationship that you wish to remove from the graph.
   * @param transaction  - Optional transaction context to perform the operation in.
   *
   * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent()
   *                                               but the object cannot be found in the graph
   * @throws ChampTransactionException           - If an attempt to commit or rollback the transaction failed.
   */
  public abstract void executeDeleteRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction) throws ChampRelationshipNotExistsException, ChampTransactionException;

  /**
   * Create or update a {@link ChampPartition}.
   * <p>
   * If a transaction context is not provided, then a transaction will be automatically
   * created and committed for this operation only, otherwise, the supplied transaction
   * will be used and it will be up to the caller to commit the transaction at its
   * discretion.
   *
   * @param partition   - The ChampPartition that you wish to create or update in the graph.
   * @param transaction - Optional transaction context to perform the operation in.
   *
   * @return - The {@link ChampPartition} as it was stored.
   *
   * @throws ChampSchemaViolationException       - If the {@code relationship} violates the constraints
   *                                               specifed by {@link ChampGraph#retrieveSchema}
   * @throws ChampRelationshipNotExistsException - If {@link org.onap.aai.champcore.model.ChampRelationship#getKey}.isPresent()
   *                                               but the object cannot be found in the graph
   * @throws ChampMarshallingException           - If the {@code relationship} is not able to be
   *                                               marshalled into the backend representation
   * @throws ChampObjectNotExistsException       - If either the source or target object referenced
   *                                               by this relationship does not exist in the graph
   * @throws ChampTransactionException           - If an attempt to commit or rollback the transaction failed.
   */
  public abstract ChampPartition executeStorePartition(ChampPartition partition, Optional<ChampTransaction> transaction) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException;

  /**
   * Removes a partition from the graph.
   * <p>
   * If a transaction context is not provided, then a transaction will be automatically
   * created and committed for this operation only, otherwise, the supplied transaction
   * will be used and it will be up to the caller to commit the transaction at its
   * discretion.
   *
   * @param graph       - The partition to be removed.
   * @param transaction - Optional transaction context to perform the operation in.
   *
   * @throws ChampTransactionException     - If an attempt to commit or rollback the transaction failed.
   */
  public abstract void executeDeletePartition(ChampPartition graph, Optional<ChampTransaction> transaction) throws ChampTransactionException;

  /**
   * Create or update an object index in the graph.
   *
   * @param index       - The object index to be created/updated.
   */
  public abstract void executeStoreObjectIndex(ChampObjectIndex index);
  public abstract Optional<ChampObjectIndex>       retrieveObjectIndex(String indexName);
  public abstract Stream<ChampObjectIndex>         retrieveObjectIndices();
  public abstract void                             executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException;
  public abstract void                             executeStoreRelationshipIndex(ChampRelationshipIndex index);
  public abstract Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName);
  public abstract Stream<ChampRelationshipIndex>   retrieveRelationshipIndices();
  public abstract void                             executeDeleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException;
  public abstract void                             storeSchema(ChampSchema schema) throws ChampSchemaViolationException;
  public abstract ChampSchema                      retrieveSchema();
  public abstract void                             updateSchema(ChampObjectConstraint objectConstraint) throws ChampSchemaViolationException;
  public abstract void                             updateSchema(ChampRelationshipConstraint schema) throws ChampSchemaViolationException;
  public abstract void                             deleteSchema();
  public abstract ChampCapabilities                capabilities();


  /**
   * Thread factory for the event producer workers.
   */
  private class ProducerWorkerThreadFactory implements ThreadFactory {

    private AtomicInteger threadNumber = new AtomicInteger(1);

    public Thread newThread(Runnable r) {
      return new Thread(r, "champEventStreamPublisher-" + threadNumber.getAndIncrement());
    }
  }


  /**
   * Process the configuration properties supplied for this graph instance.
   *
   * @param properties - Configuration parameters.
   */
  private void configure(Map<String, Object> properties) {

    producer = (EventPublisher) properties.get(PARAM_EVENT_STREAM_PRODUCER);

    eventQueueCapacity =
        (Integer) getProperty(properties, PARAM_EVENT_QUEUE_CAPACITY, DEFAULT_EVENT_QUEUE_CAPACITY);
    eventStreamPublisherPoolSize =
        (Integer) getProperty(properties, PARAM_EVENT_STREAM_PUBLISHER_POOL_SIZE, DEFAULT_EVENT_STREAM_PUBLISHER_POOL_SIZE);
  }


  public void setProducer(EventPublisher aProducer) {

    producer = aProducer;
  }

  private Object getProperty(Map<String, Object> properties, String property, Object defaultValue) {

    if(properties.containsKey(property)) {
      return properties.get(property);
    } else {
      return defaultValue;
    }
  }

  @Override
  public void shutdown() {

    if(publisherPool != null) {
      publisherPool.shutdown();

      try {
        publisherPool.awaitTermination(1000, TimeUnit.MILLISECONDS);
      } catch (InterruptedException e) {
        logger.warn(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_WARN, 
            "Termination interrupted");
        Thread.currentThread().interrupt();
      }
    }

    if(producer != null) {

      try {
        producer.close();

      } catch (Exception e) {
        logger.error(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_ERROR, 
            "Failed to stop event stream producer: " + e.getMessage());
      }
    }
  }

  @Override
  public void commitTransaction(ChampTransaction transaction) throws ChampTransactionException {

    try {

      // Commit the transaction.
      transaction.commit();

    } catch (ChampTransactionException e) {

      logger.warn(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_WARN, 
          "Events associated with transaction " + transaction.id() + " not generated due to transaction commit failure.");

      List<ChampEvent> enqueuedEvents = transaction.getEnqueuedEvents();
      for(ChampEvent event : enqueuedEvents) {

        logger.debug("Graph event " + event.toString() + " not published.");
      }
      throw e;
    }

    // Now that the transaction has been successfully committed, we need
    // to log the events that were produced within that transaction's
    // context.
    List<ChampEvent> enqueuedEvents = transaction.getEnqueuedEvents();
    for(ChampEvent event : enqueuedEvents) {
      logEvent(event);
    }
  }

  @Override
  public void rollbackTransaction(ChampTransaction transaction) throws ChampTransactionException {

    // Rollback the transaction.
    transaction.rollback();
  }

  @Override
  public ChampObject storeObject(ChampObject object) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException {
    return storeObject(object, Optional.empty());
  }

  @Override
  public ChampObject storeObject(ChampObject object, Optional<ChampTransaction> transaction) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException {

    ChampObject storedObject = executeStoreObject(object, transaction);

    if(storedObject != null) {

      logOrEnqueueEvent(ChampEvent.builder()
                                    .operation(ChampOperation.STORE)
                                    .entity(storedObject)
                                    .build(),
                        transaction);
    }

    return storedObject;
  }

  @Override
  public ChampObject replaceObject(ChampObject object)
      throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException {

    return replaceObject(object, Optional.empty());
  }

  @Override
  public ChampObject replaceObject(ChampObject object, Optional<ChampTransaction> transaction)
      throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException {

    ChampObject replacedObject = executeReplaceObject(object, transaction);

    if(replacedObject != null) {

      logOrEnqueueEvent(ChampEvent.builder()
                                  .operation(ChampOperation.REPLACE)
                                  .entity(replacedObject)
                                  .build(),
                        transaction);
    }

    return replacedObject;
  }

  @Override
  public void deleteObject(Object key) throws ChampObjectNotExistsException, ChampTransactionException {
    deleteObject(key, Optional.empty());
  }

  @Override
  public void deleteObject(Object key, Optional<ChampTransaction> transaction) throws ChampObjectNotExistsException, ChampTransactionException {

    // Retrieve the object that we are deleting before it's gone, so that we can
    // report it to the event stream.
    Optional<ChampObject> objectToDelete = Optional.empty();
    try {
      objectToDelete = retrieveObject(key, transaction);

    } catch (ChampUnmarshallingException e) {
      logger.error(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_ERROR, 
          "Unable to generate delete object log: " + e.getMessage());
    }

    executeDeleteObject(key, transaction);

    if(objectToDelete.isPresent()) {
      // Update the event stream with the current operation.
      logOrEnqueueEvent(ChampEvent.builder()
                                  .operation(ChampOperation.DELETE)
                                  .entity(objectToDelete.get())
                                  .build(),
                        transaction);
    }
  }

  @Override
  public ChampRelationship storeRelationship(ChampRelationship relationship)
      throws ChampUnmarshallingException,
             ChampMarshallingException,
             ChampObjectNotExistsException,
             ChampSchemaViolationException,
             ChampRelationshipNotExistsException, ChampTransactionException {
      return storeRelationship(relationship, Optional.empty());
  }

  @Override
  public ChampRelationship storeRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction)
      throws ChampUnmarshallingException,
             ChampMarshallingException,
             ChampObjectNotExistsException,
             ChampSchemaViolationException,
             ChampRelationshipNotExistsException, ChampTransactionException {

    ChampRelationship storedRelationship = executeStoreRelationship(relationship, transaction);

    if(storedRelationship != null) {

      // Update the event stream with the current operation.
      logOrEnqueueEvent(ChampEvent.builder()
                                  .operation(ChampOperation.STORE)
                                  .entity(storedRelationship)
                                  .build(),
                        transaction);
    }

    return storedRelationship;
  }

  @Override
  public ChampRelationship replaceRelationship(ChampRelationship relationship)
      throws ChampUnmarshallingException,
             ChampMarshallingException,
             ChampSchemaViolationException,
             ChampRelationshipNotExistsException, ChampTransactionException {
    return replaceRelationship(relationship, Optional.empty());
  }

  @Override
  public ChampRelationship replaceRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction)
      throws ChampUnmarshallingException,
             ChampMarshallingException,
             ChampSchemaViolationException,
             ChampRelationshipNotExistsException, ChampTransactionException {

    ChampRelationship replacedRelationship = executeReplaceRelationship(relationship, transaction);

    if(replacedRelationship != null) {

      // Update the event stream with the current operation.
      logOrEnqueueEvent(ChampEvent.builder()
                                  .operation(ChampOperation.REPLACE)
                                  .entity(replacedRelationship)
                                  .build(),
                        transaction);
    }

    return replacedRelationship;
  }

  @Override
  public void deleteRelationship(ChampRelationship relationship) throws ChampRelationshipNotExistsException, ChampTransactionException {
    deleteRelationship(relationship, Optional.empty());
  }

  @Override
  public void deleteRelationship(ChampRelationship relationship, Optional<ChampTransaction> transaction) throws ChampRelationshipNotExistsException, ChampTransactionException {

    executeDeleteRelationship(relationship, transaction);

    // Update the event stream with the current operation.
    logOrEnqueueEvent(ChampEvent.builder()
                                .operation(ChampOperation.DELETE)
                                .entity(relationship)
                                .build(),
                      transaction);
  }

  @Override
  public ChampPartition storePartition(ChampPartition partition) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException {
    return storePartition(partition, Optional.empty());
  }

  @Override
  public ChampPartition storePartition(ChampPartition partition, Optional<ChampTransaction> transaction) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException, ChampTransactionException {

    ChampPartition storedPartition = executeStorePartition(partition, transaction);

    if(storedPartition != null) {

      // Update the event stream with the current operation.
      logOrEnqueueEvent(ChampEvent.builder()
                                  .operation(ChampOperation.STORE)
                                  .entity(storedPartition)
                                  .build(),
                        transaction);
    }

    return storedPartition;
  }

  @Override
  public void deletePartition(ChampPartition graph) throws ChampTransactionException{
    deletePartition(graph, Optional.empty());
  }

  @Override
  public void deletePartition(ChampPartition graph, Optional<ChampTransaction> transaction) throws ChampTransactionException {

    executeDeletePartition(graph, transaction);

    // Update the event stream with the current operation.
    logOrEnqueueEvent(ChampEvent.builder()
                                .operation(ChampOperation.DELETE)
                                .entity(graph)
                                .build(),
                      transaction);
  }

  @Override
  public void storeObjectIndex(ChampObjectIndex index) {

    executeStoreObjectIndex(index);

    // Update the event stream with the current operation.
    logEvent(ChampEvent.builder()
                  .operation(ChampOperation.STORE)
                  .entity(index)
                  .build());
  }


  public void deleteObjectIndex(String indexName) throws ChampIndexNotExistsException {

    // Retrieve the index that we are deleting before it's gone, so that we can
    // report it to the event stream.
    Optional<ChampObjectIndex> indexToDelete = retrieveObjectIndex(indexName);

    executeDeleteObjectIndex(indexName);

    if(indexToDelete.isPresent()) {
      // Update the event stream with the current operation.
      logEvent(ChampEvent.builder()
                    .operation(ChampOperation.DELETE)
                    .entity(indexToDelete.get())
                    .build());
    }
  }


  public void storeRelationshipIndex(ChampRelationshipIndex index) {

    executeStoreRelationshipIndex(index);

    // Update the event stream with the current operation.
    logEvent(ChampEvent.builder()
                  .operation(ChampOperation.STORE)
                  .entity(index)
                  .build());
  }


  public void deleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException {

    // Retrieve the index that we are deleting before it's gone, so that we can
    // report it to the event stream.
    Optional<ChampRelationshipIndex> indexToDelete = retrieveRelationshipIndex(indexName);

    executeDeleteRelationshipIndex(indexName);

    if(indexToDelete.isPresent()) {
      // Update the event stream with the current operation.
      logEvent(ChampEvent.builder()
                    .operation(ChampOperation.DELETE)
                    .entity(indexToDelete.get())
                    .build());
    }
  }

  private void logOrEnqueueEvent(ChampEvent event, Optional<ChampTransaction> transaction) {

    if(!transaction.isPresent()) {
      // Update the event stream with the current operation.
      logEvent(event);
    } else {

      // when the TransactionID is present, add it to the event payload before logging/enqueing the event.
      event.setDbTransactionId ( transaction.get ().id () );
      transaction.get().logEvent(event);
    }
  }

  /**
   * Submits an event to be published to the event stream.
   *
   * @param anEvent - The event to be published.
   */
  public void logEvent(ChampEvent anEvent) {

    if(eventQueue == null) {
      return;
    }

    logger.info(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_INFO, 
        "Log champcore event with transaction id: " + anEvent.getTransactionId() + " to event bus");
    if(logger.isDebugEnabled()) {
      logger.debug("Event payload: " + anEvent.toString());
    }

    // Try to submit the event to be published to the event bus.
    if(!eventQueue.offer(anEvent)) {
      logger.error(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_ERROR, 
          "Event could not be published to the event bus due to: Internal buffer capacity exceeded.");
    }
  }


  /**
   * This class implements the worker threads for our thread pool which are responsible for
   * pulling the next outgoing event from the internal buffer and forwarding them to the event
   * bus client.
   * <p>
   * Each publish operation is performed synchronously, so that the thread will only move on
   * to the next available event once it has actually published the current event to the bus.
   */
  private class EventPublisherWorker implements Runnable {

    /** Partition key to use when publishing events to the event stream.  We WANT all events
     *  to go to a single partition, so we are just using a hard-coded key for every event. */
    private static final String EVENTS_PARTITION_KEY = "champEventKey";


    @Override
    public void run() {

      while(true) {
        ChampEvent event = null;
        try {

          // Get the next event to be published from the queue.
          event = eventQueue.take();

        } catch (InterruptedException e) {

          // Restore the interrupted status.
          Thread.currentThread().interrupt();
        }

        // Create new envelope containing an event header and ChampEvent
        ChampEventEnvelope eventEnvelope = new ChampEventEnvelope(event);

        // Try publishing the event to the event bus.  This call will block until
        try {
          producer.sendSync(EVENTS_PARTITION_KEY, eventEnvelope.toJson());

        } catch (Exception e) {

          logger.error(ChampCoreMsgs.CHAMPCORE_ABSTRACT_LOGGING_CHAMP_GRAPH_ERROR, 
              "Failed to publish event to event bus: " + e.getMessage());
        }
      }
    }
  }
}