summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/champ/event/AbstractLoggingChampGraph.java
blob: 004e098c256400eac16afe3ee871865f82a2bea2 (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
/**
 * ============LICENSE_START==========================================
 * org.onap.aai
 * ===================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 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============================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.onap.aai.champ.event;

import java.io.IOException;
import java.net.MalformedURLException;
import java.security.GeneralSecurityException;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import org.onap.aai.champ.ChampCapabilities;
import org.onap.aai.champ.ChampGraph;
import org.onap.aai.champ.event.ChampEvent.ChampOperation;
import org.onap.aai.champ.exceptions.ChampIndexNotExistsException;
import org.onap.aai.champ.exceptions.ChampMarshallingException;
import org.onap.aai.champ.exceptions.ChampObjectNotExistsException;
import org.onap.aai.champ.exceptions.ChampRelationshipNotExistsException;
import org.onap.aai.champ.exceptions.ChampSchemaViolationException;
import org.onap.aai.champ.exceptions.ChampUnmarshallingException;
import org.onap.aai.champ.model.ChampObject;
import org.onap.aai.champ.model.ChampObjectConstraint;
import org.onap.aai.champ.model.ChampObjectIndex;
import org.onap.aai.champ.model.ChampPartition;
import org.onap.aai.champ.model.ChampRelationship;
import org.onap.aai.champ.model.ChampRelationshipConstraint;
import org.onap.aai.champ.model.ChampRelationshipIndex;
import org.onap.aai.champ.model.ChampSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.att.nsa.cambria.client.CambriaClientBuilders;
import com.att.nsa.cambria.client.CambriaPublisher;


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

  private static final Logger logger = LoggerFactory.getLogger(AbstractLoggingChampGraph.class);
  
  public abstract ChampObject                      executeStoreObject(ChampObject object) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException;
  public abstract ChampObject                      executeReplaceObject(ChampObject object) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException;
  public abstract Optional<ChampObject>            retrieveObject(Object key) throws ChampUnmarshallingException;
  public abstract void                             executeDeleteObject(Object key) throws ChampObjectNotExistsException;
  public abstract Stream<ChampObject>              queryObjects(Map<String, Object> queryParams);
  public abstract ChampRelationship                executeStoreRelationship(ChampRelationship relationship) throws ChampUnmarshallingException, ChampMarshallingException, ChampObjectNotExistsException, ChampSchemaViolationException, ChampRelationshipNotExistsException;  
  public abstract ChampRelationship                executeReplaceRelationship(ChampRelationship relationship) throws ChampUnmarshallingException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException; 
  @Override
  public abstract Optional<ChampRelationship>      retrieveRelationship(Object key) throws ChampUnmarshallingException;
  public abstract void                             executeDeleteRelationship(ChampRelationship relationship) throws ChampRelationshipNotExistsException;
  public abstract Stream<ChampRelationship>        retrieveRelationships(ChampObject object) throws ChampUnmarshallingException, ChampObjectNotExistsException;
  public abstract Stream<ChampRelationship>        queryRelationships(Map<String, Object> queryParams);
  public abstract ChampPartition                   executeStorePartition(ChampPartition partition) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException;
  public abstract void                             executeDeletePartition(ChampPartition graph);
  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();

   
  /** Configuration property for setting the comma-separated list of servers to use for
   *  communicating with the event bus. */
  public static final String  PARAM_EVENT_STREAM_HOSTS      = "champ.event.stream.hosts";
  
  /** Configuration property for setting the number of events that we will try to 'batch' 
   *  up before sending them to the event bus. */
  public final static String  PARAM_EVENT_STREAM_BATCH_SIZE = "champ.event.stream.batch-size";
  public final static Integer DEFAULT_EVENT_STREAM_BATCH_SIZE = 1;
  
  /** Configuration property for setting the maximum amount of time to wait for a batch of
   *  outgoing messages to fill up before sending the batch. */
  public final static String  PARAM_EVENT_STREAM_BATCH_TIMEOUT = "champ.event.stream.batch-timeout";
  public final static Integer DEFAULT_EVENT_STREAM_BATCH_TIMEOUT_MS = 500; 
  
  public final static String  PARAM_EVENT_STREAM_PUBLISHER_POOL_SIZE = "champ.event.stream.publisher-pool-size";
  public final static Integer DEFAULT_EVENT_STREAM_PUBLISHER_POOL_SIZE = 100;
  
  /** The event stream topic that we will publish Champ events to. */
  public final static String EVENT_TOPIC = "champRawEvents";
    
  /** Number of events to 'batch up' before actually publishing them to the event bus. */
  private Integer eventStreamBatchSize;
  
  private Integer eventStreamBatchTimeout;
  
  private Integer eventStreamPublisherPoolSize;
  
  /** Comma-separated list of hosts for connecting to the event bus. */
  private String  eventStreamHosts = null;
  
  /** Client used for publishing messages to the event bus. */
  protected CambriaPublisher producer;

  /** Pool of worker threads that do the work of publishing the events to the event bus. */
  protected ThreadPoolExecutor publisherPool;
  
  
  /**
   * 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);
      
    // Create the executor pool that will do the work of publishing events to the event bus.
    publisherPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(eventStreamPublisherPoolSize);
    
    // Make sure that we are actually provided a list of hosts for connecting to the event
    // bus before we actually try to do anything useful.
    if(eventStreamHosts == null) {
      
      // We were not supplied a list of event bus hosts, so just bail.
      logger.error("Cannot initialize event stream publisher without at least one event bus host.");
      logger.error("NOTE!! Champ events will NOT be published to the event stream!");
      return;
    }
         
    try {
        
      // Instantiate the producer that we will use to publish events to the event stream.
      setProducer(new CambriaClientBuilders.PublisherBuilder()
                        .usingHosts(eventStreamHosts)
                        .onTopic(EVENT_TOPIC)
                        .limitBatch(eventStreamBatchSize, eventStreamBatchTimeout)
                        .build());
      
    } catch (MalformedURLException | GeneralSecurityException e) {
      
      logger.error("Could not instantiate event stream producer due to: " + e.getMessage());
      logger.error("NOTE: Champ events will NOT be published to the event stream");
      producer = null;
    }
  }

      
  /**
   * Process the configuration properties supplied for this graph instance.
   * 
   * @param properties - Configuration parameters.
   */
  private void configure(Map<String, Object> properties) {
    
    eventStreamBatchSize = 
        (Integer) getProperty(properties, PARAM_EVENT_STREAM_BATCH_SIZE,    DEFAULT_EVENT_STREAM_BATCH_SIZE);
    eventStreamBatchTimeout = 
        (Integer) getProperty(properties, PARAM_EVENT_STREAM_BATCH_TIMEOUT, DEFAULT_EVENT_STREAM_BATCH_TIMEOUT_MS);
    eventStreamPublisherPoolSize = 
        (Integer) getProperty(properties, PARAM_EVENT_STREAM_PUBLISHER_POOL_SIZE, DEFAULT_EVENT_STREAM_PUBLISHER_POOL_SIZE);
    
    if(properties.containsKey(PARAM_EVENT_STREAM_HOSTS)) {
      eventStreamHosts = (String) properties.get(PARAM_EVENT_STREAM_HOSTS);
    } 
  }
  
  public void setProducer(CambriaPublisher 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) {}
    }
    
    if(producer != null) {
      producer.close();
    }
  }
  
  public ChampObject storeObject(ChampObject object) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException {
    
    ChampObject storedObject = executeStoreObject(object);
    
    if(storedObject != null) {
      
      // Update the event stream with the current operation.
      logEvent(ChampEvent.builder()
                    .operation(ChampOperation.STORE)
                    .entity(storedObject)
                    .build());
    }
    
    return storedObject;
  }
  
  
  public ChampObject replaceObject(ChampObject object)
      throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException {
    
    ChampObject replacedObject = executeReplaceObject(object);
    
    if(replacedObject != null) {
      
      // Update the event stream with the current operation.
      logEvent(ChampEvent.builder()
                    .operation(ChampOperation.REPLACE)
                    .entity(replacedObject)
                    .build());
    }
    
    return replacedObject;
  }
  

  public void deleteObject(Object key) throws ChampObjectNotExistsException {

    // 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);
      
    } catch (ChampUnmarshallingException e) {
      logger.error("Unable to generate delete object log: " + e.getMessage());
    }
    
    executeDeleteObject(key);
    
    if(objectToDelete.isPresent()) {
      // Update the event stream with the current operation.
      logEvent(ChampEvent.builder()
                    .operation(ChampOperation.DELETE)
                    .entity(objectToDelete.get())
                    .build());
    }
  }
  
  
  public ChampRelationship storeRelationship(ChampRelationship relationship)
      throws ChampUnmarshallingException, ChampMarshallingException, ChampObjectNotExistsException, ChampSchemaViolationException, ChampRelationshipNotExistsException {  

    ChampRelationship storedRelationship = executeStoreRelationship(relationship);
    
    if(storedRelationship != null) {
      
      // Update the event stream with the current operation.
      logEvent(ChampEvent.builder()
                    .operation(ChampOperation.STORE)
                    .entity(storedRelationship)
                    .build());
    }
    
    return storedRelationship;
  }
  
  
  public ChampRelationship replaceRelationship(ChampRelationship relationship)
      throws ChampUnmarshallingException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException { 

    ChampRelationship replacedRelationship = executeReplaceRelationship(relationship);
    
    if(replacedRelationship != null) {
      
      // Update the event stream with the current operation.
      logEvent(ChampEvent.builder()
                    .operation(ChampOperation.REPLACE)
                    .entity(replacedRelationship)
                    .build());
    }
    
    return replacedRelationship;
  }
  
  
  public void deleteRelationship(ChampRelationship relationship) throws ChampRelationshipNotExistsException {

    executeDeleteRelationship(relationship);
    
    // Update the event stream with the current operation.
    logEvent(ChampEvent.builder()
                  .operation(ChampOperation.DELETE)
                  .entity(relationship)
                  .build());
  }
  
  
  public ChampPartition storePartition(ChampPartition partition) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException {

    ChampPartition storedPartition = executeStorePartition(partition);
    
    if(storedPartition != null) {
      
      // Update the event stream with the current operation.
      logEvent(ChampEvent.builder()
                    .operation(ChampOperation.STORE)
                    .entity(storedPartition)
                    .build());
    }
    
    return storedPartition;
  }
  
  
  public void deletePartition(ChampPartition graph) {

    executeDeletePartition(graph);
    
    // Update the event stream with the current operation.
    logEvent(ChampEvent.builder()
                  .operation(ChampOperation.DELETE)
                  .entity(graph)
                  .build());
  }
  
  
  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());
    }
  }
  
  
  /**
   * Submits an event to be published to the event stream.
   * 
   * @param anEvent - The event to be published.
   */
  public void logEvent(ChampEvent anEvent) {
    
    if(logger.isDebugEnabled()) {
      logger.debug("Submitting event to be published to the event bus: " + anEvent.toString());
    }
    
    try {
      
      // Try submitting the event to be published to the event bus.
      publisherPool.execute(new EventPublisher(anEvent));
    
    } catch (RejectedExecutionException re) {
      logger.error("Event could not be published to the event bus due to: " + re.getMessage());
      
    } catch (NullPointerException npe) {
      logger.error("Can not publish null event to event bus.");
    }
  }
  
  
  /**
   * This class runs as a background thread and is responsible for pulling Champ events off
   * of the internal queue and publishing them to the event stream.
   */
  private class EventPublisher 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";
    
    private ChampEvent event;
    
    public EventPublisher(ChampEvent event) {
      this.event = event;
    }
    
    
    @Override
    public void run() {

      boolean done = false;
      while(!done && !Thread.currentThread().isInterrupted()) {
        try {
          
          // Make sure that we actually have a producer instance to use to publish
          // the event with.
          if(producer != null) {
            
            // Try publishing the event to the event bus.
            producer.send(EVENTS_PARTITION_KEY, event.toJson());
            
          } else if (logger.isDebugEnabled()) {            
            logger.debug("Event bus producer is not instantiated - will not attempt to publish event");
          }
          
          done = true;
          
        } catch (IOException e) {
  
          // We were unable to publish to the event bus, so wait a bit and then try
          // again.
          try {
            Thread.sleep(500);
            
          } catch (InterruptedException e1) {
            logger.info("Stopping event publisher worker thread.");
            return;
          }
        }           
      }
    }
  }
}