2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ===================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.champ.event;
24 import java.io.IOException;
25 import java.net.MalformedURLException;
26 import java.security.GeneralSecurityException;
28 import java.util.Optional;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.RejectedExecutionException;
31 import java.util.concurrent.ThreadPoolExecutor;
32 import java.util.concurrent.TimeUnit;
33 import java.util.stream.Stream;
35 import org.onap.aai.champ.ChampCapabilities;
36 import org.onap.aai.champ.ChampGraph;
37 import org.onap.aai.champ.event.ChampEvent.ChampOperation;
38 import org.onap.aai.champ.exceptions.ChampIndexNotExistsException;
39 import org.onap.aai.champ.exceptions.ChampMarshallingException;
40 import org.onap.aai.champ.exceptions.ChampObjectNotExistsException;
41 import org.onap.aai.champ.exceptions.ChampRelationshipNotExistsException;
42 import org.onap.aai.champ.exceptions.ChampSchemaViolationException;
43 import org.onap.aai.champ.exceptions.ChampUnmarshallingException;
44 import org.onap.aai.champ.model.ChampObject;
45 import org.onap.aai.champ.model.ChampObjectConstraint;
46 import org.onap.aai.champ.model.ChampObjectIndex;
47 import org.onap.aai.champ.model.ChampPartition;
48 import org.onap.aai.champ.model.ChampRelationship;
49 import org.onap.aai.champ.model.ChampRelationshipConstraint;
50 import org.onap.aai.champ.model.ChampRelationshipIndex;
51 import org.onap.aai.champ.model.ChampSchema;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.att.nsa.cambria.client.CambriaClientBuilders;
56 import com.att.nsa.cambria.client.CambriaPublisher;
60 * This class provides the hooks to allow Champ operations to be logged to an event
63 public abstract class AbstractLoggingChampGraph implements ChampGraph {
65 private static final Logger logger = LoggerFactory.getLogger(AbstractLoggingChampGraph.class);
67 public abstract ChampObject executeStoreObject(ChampObject object) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException;
68 public abstract ChampObject executeReplaceObject(ChampObject object) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException;
69 public abstract Optional<ChampObject> retrieveObject(Object key) throws ChampUnmarshallingException;
70 public abstract void executeDeleteObject(Object key) throws ChampObjectNotExistsException;
71 public abstract Stream<ChampObject> queryObjects(Map<String, Object> queryParams);
72 public abstract ChampRelationship executeStoreRelationship(ChampRelationship relationship) throws ChampUnmarshallingException, ChampMarshallingException, ChampObjectNotExistsException, ChampSchemaViolationException, ChampRelationshipNotExistsException;
73 public abstract ChampRelationship executeReplaceRelationship(ChampRelationship relationship) throws ChampUnmarshallingException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException;
75 public abstract Optional<ChampRelationship> retrieveRelationship(Object key) throws ChampUnmarshallingException;
76 public abstract void executeDeleteRelationship(ChampRelationship relationship) throws ChampRelationshipNotExistsException;
78 public abstract Stream<ChampRelationship> retrieveRelationships(ChampObject object) throws ChampUnmarshallingException, ChampObjectNotExistsException;
80 public abstract Stream<ChampRelationship> queryRelationships(Map<String, Object> queryParams);
81 public abstract ChampPartition executeStorePartition(ChampPartition partition) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException;
82 public abstract void executeDeletePartition(ChampPartition graph);
83 public abstract void executeStoreObjectIndex(ChampObjectIndex index);
85 public abstract Optional<ChampObjectIndex> retrieveObjectIndex(String indexName);
86 public abstract Stream<ChampObjectIndex> retrieveObjectIndices();
87 public abstract void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException;
88 public abstract void executeStoreRelationshipIndex(ChampRelationshipIndex index);
89 public abstract Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName);
90 public abstract Stream<ChampRelationshipIndex> retrieveRelationshipIndices();
91 public abstract void executeDeleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException;
92 public abstract void storeSchema(ChampSchema schema) throws ChampSchemaViolationException;
93 public abstract ChampSchema retrieveSchema();
94 public abstract void updateSchema(ChampObjectConstraint objectConstraint) throws ChampSchemaViolationException;
95 public abstract void updateSchema(ChampRelationshipConstraint schema) throws ChampSchemaViolationException;
96 public abstract void deleteSchema();
97 public abstract ChampCapabilities capabilities();
100 /** Configuration property for setting the comma-separated list of servers to use for
101 * communicating with the event bus. */
102 public static final String PARAM_EVENT_STREAM_HOSTS = "champ.event.stream.hosts";
104 /** Configuration property for setting the number of events that we will try to 'batch'
105 * up before sending them to the event bus. */
106 public final static String PARAM_EVENT_STREAM_BATCH_SIZE = "champ.event.stream.batch-size";
107 public final static Integer DEFAULT_EVENT_STREAM_BATCH_SIZE = 1;
109 /** Configuration property for setting the maximum amount of time to wait for a batch of
110 * outgoing messages to fill up before sending the batch. */
111 public final static String PARAM_EVENT_STREAM_BATCH_TIMEOUT = "champ.event.stream.batch-timeout";
112 public final static Integer DEFAULT_EVENT_STREAM_BATCH_TIMEOUT_MS = 500;
114 public final static String PARAM_EVENT_STREAM_PUBLISHER_POOL_SIZE = "champ.event.stream.publisher-pool-size";
115 public final static Integer DEFAULT_EVENT_STREAM_PUBLISHER_POOL_SIZE = 100;
117 /** The event stream topic that we will publish Champ events to. */
118 public final static String EVENT_TOPIC = "champRawEvents";
120 /** Number of events to 'batch up' before actually publishing them to the event bus. */
121 private Integer eventStreamBatchSize;
123 private Integer eventStreamBatchTimeout;
125 private Integer eventStreamPublisherPoolSize;
127 /** Comma-separated list of hosts for connecting to the event bus. */
128 private String eventStreamHosts = null;
130 /** Client used for publishing messages to the event bus. */
131 protected CambriaPublisher producer;
133 /** Pool of worker threads that do the work of publishing the events to the event bus. */
134 protected ThreadPoolExecutor publisherPool;
138 * Create a new instance of the AbstractLoggingChampGraph.
140 * @param properties - Set of configuration properties for this graph instance.
142 protected AbstractLoggingChampGraph(Map<String, Object> properties) {
144 // Extract the necessary parameters from the configuration properties.
145 configure(properties);
147 // Create the executor pool that will do the work of publishing events to the event bus.
148 publisherPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(eventStreamPublisherPoolSize);
150 // Make sure that we are actually provided a list of hosts for connecting to the event
151 // bus before we actually try to do anything useful.
152 if(eventStreamHosts == null) {
154 // We were not supplied a list of event bus hosts, so just bail.
155 logger.error("Cannot initialize event stream publisher without at least one event bus host.");
156 logger.error("NOTE!! Champ events will NOT be published to the event stream!");
162 // Instantiate the producer that we will use to publish events to the event stream.
163 setProducer(new CambriaClientBuilders.PublisherBuilder()
164 .usingHosts(eventStreamHosts)
165 .onTopic(EVENT_TOPIC)
166 .limitBatch(eventStreamBatchSize, eventStreamBatchTimeout)
169 } catch (MalformedURLException | GeneralSecurityException e) {
171 logger.error("Could not instantiate event stream producer due to: " + e.getMessage());
172 logger.error("NOTE: Champ events will NOT be published to the event stream");
179 * Process the configuration properties supplied for this graph instance.
181 * @param properties - Configuration parameters.
183 private void configure(Map<String, Object> properties) {
185 eventStreamBatchSize =
186 (Integer) getProperty(properties, PARAM_EVENT_STREAM_BATCH_SIZE, DEFAULT_EVENT_STREAM_BATCH_SIZE);
187 eventStreamBatchTimeout =
188 (Integer) getProperty(properties, PARAM_EVENT_STREAM_BATCH_TIMEOUT, DEFAULT_EVENT_STREAM_BATCH_TIMEOUT_MS);
189 eventStreamPublisherPoolSize =
190 (Integer) getProperty(properties, PARAM_EVENT_STREAM_PUBLISHER_POOL_SIZE, DEFAULT_EVENT_STREAM_PUBLISHER_POOL_SIZE);
192 if(properties.containsKey(PARAM_EVENT_STREAM_HOSTS)) {
193 eventStreamHosts = (String) properties.get(PARAM_EVENT_STREAM_HOSTS);
197 public void setProducer(CambriaPublisher aProducer) {
199 producer = aProducer;
202 private Object getProperty(Map<String, Object> properties, String property, Object defaultValue) {
204 if(properties.containsKey(property)) {
205 return properties.get(property);
212 public void shutdown() {
214 if(publisherPool != null) {
215 publisherPool.shutdown();
218 publisherPool.awaitTermination(1000, TimeUnit.MILLISECONDS);
219 } catch (InterruptedException e) {}
222 if(producer != null) {
227 public ChampObject storeObject(ChampObject object) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException {
229 ChampObject storedObject = executeStoreObject(object);
231 if(storedObject != null) {
233 // Update the event stream with the current operation.
234 logEvent(ChampEvent.builder()
235 .operation(ChampOperation.STORE)
236 .entity(storedObject)
244 public ChampObject replaceObject(ChampObject object)
245 throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException {
247 ChampObject replacedObject = executeReplaceObject(object);
249 if(replacedObject != null) {
251 // Update the event stream with the current operation.
252 logEvent(ChampEvent.builder()
253 .operation(ChampOperation.REPLACE)
254 .entity(replacedObject)
258 return replacedObject;
262 public void deleteObject(Object key) throws ChampObjectNotExistsException {
264 // Retrieve the object that we are deleting before it's gone, so that we can
265 // report it to the event stream.
266 Optional<ChampObject> objectToDelete = Optional.empty();
268 objectToDelete = retrieveObject(key);
270 } catch (ChampUnmarshallingException e) {
271 logger.error("Unable to generate delete object log: " + e.getMessage());
274 executeDeleteObject(key);
276 if(objectToDelete.isPresent()) {
277 // Update the event stream with the current operation.
278 logEvent(ChampEvent.builder()
279 .operation(ChampOperation.DELETE)
280 .entity(objectToDelete.get())
286 public ChampRelationship storeRelationship(ChampRelationship relationship)
287 throws ChampUnmarshallingException, ChampMarshallingException, ChampObjectNotExistsException, ChampSchemaViolationException, ChampRelationshipNotExistsException {
289 ChampRelationship storedRelationship = executeStoreRelationship(relationship);
291 if(storedRelationship != null) {
293 // Update the event stream with the current operation.
294 logEvent(ChampEvent.builder()
295 .operation(ChampOperation.STORE)
296 .entity(storedRelationship)
300 return storedRelationship;
304 public ChampRelationship replaceRelationship(ChampRelationship relationship)
305 throws ChampUnmarshallingException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException {
307 ChampRelationship replacedRelationship = executeReplaceRelationship(relationship);
309 if(replacedRelationship != null) {
311 // Update the event stream with the current operation.
312 logEvent(ChampEvent.builder()
313 .operation(ChampOperation.REPLACE)
314 .entity(replacedRelationship)
318 return replacedRelationship;
322 public void deleteRelationship(ChampRelationship relationship) throws ChampRelationshipNotExistsException {
324 executeDeleteRelationship(relationship);
326 // Update the event stream with the current operation.
327 logEvent(ChampEvent.builder()
328 .operation(ChampOperation.DELETE)
329 .entity(relationship)
334 public ChampPartition storePartition(ChampPartition partition) throws ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampMarshallingException, ChampObjectNotExistsException {
336 ChampPartition storedPartition = executeStorePartition(partition);
338 if(storedPartition != null) {
340 // Update the event stream with the current operation.
341 logEvent(ChampEvent.builder()
342 .operation(ChampOperation.STORE)
343 .entity(storedPartition)
347 return storedPartition;
351 public void deletePartition(ChampPartition graph) {
353 executeDeletePartition(graph);
355 // Update the event stream with the current operation.
356 logEvent(ChampEvent.builder()
357 .operation(ChampOperation.DELETE)
363 public void storeObjectIndex(ChampObjectIndex index) {
365 executeStoreObjectIndex(index);
367 // Update the event stream with the current operation.
368 logEvent(ChampEvent.builder()
369 .operation(ChampOperation.STORE)
375 public void deleteObjectIndex(String indexName) throws ChampIndexNotExistsException {
377 // Retrieve the index that we are deleting before it's gone, so that we can
378 // report it to the event stream.
379 Optional<ChampObjectIndex> indexToDelete = retrieveObjectIndex(indexName);
381 executeDeleteObjectIndex(indexName);
383 if(indexToDelete.isPresent()) {
384 // Update the event stream with the current operation.
385 logEvent(ChampEvent.builder()
386 .operation(ChampOperation.DELETE)
387 .entity(indexToDelete.get())
393 public void storeRelationshipIndex(ChampRelationshipIndex index) {
395 executeStoreRelationshipIndex(index);
397 // Update the event stream with the current operation.
398 logEvent(ChampEvent.builder()
399 .operation(ChampOperation.STORE)
405 public void deleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException {
407 // Retrieve the index that we are deleting before it's gone, so that we can
408 // report it to the event stream.
409 Optional<ChampRelationshipIndex> indexToDelete = retrieveRelationshipIndex(indexName);
411 executeDeleteRelationshipIndex(indexName);
413 if(indexToDelete.isPresent()) {
414 // Update the event stream with the current operation.
415 logEvent(ChampEvent.builder()
416 .operation(ChampOperation.DELETE)
417 .entity(indexToDelete.get())
424 * Submits an event to be published to the event stream.
426 * @param anEvent - The event to be published.
428 public void logEvent(ChampEvent anEvent) {
430 if(logger.isDebugEnabled()) {
431 logger.debug("Submitting event to be published to the event bus: " + anEvent.toString());
436 // Try submitting the event to be published to the event bus.
437 publisherPool.execute(new EventPublisher(anEvent));
439 } catch (RejectedExecutionException re) {
440 logger.error("Event could not be published to the event bus due to: " + re.getMessage());
442 } catch (NullPointerException npe) {
443 logger.error("Can not publish null event to event bus.");
449 * This class runs as a background thread and is responsible for pulling Champ events off
450 * of the internal queue and publishing them to the event stream.
452 private class EventPublisher implements Runnable {
454 /** Partition key to use when publishing events to the event stream. We WANT all events
455 * to go to a single partition, so we are just using a hard-coded key for every event. */
456 private static final String EVENTS_PARTITION_KEY = "champEventKey";
458 private ChampEvent event;
460 public EventPublisher(ChampEvent event) {
468 boolean done = false;
469 while(!done && !Thread.currentThread().isInterrupted()) {
472 // Make sure that we actually have a producer instance to use to publish
474 if(producer != null) {
476 // Try publishing the event to the event bus.
477 producer.send(EVENTS_PARTITION_KEY, event.toJson());
479 } else if (logger.isDebugEnabled()) {
480 logger.debug("Event bus producer is not instantiated - will not attempt to publish event");
485 } catch (IOException e) {
487 // We were unable to publish to the event bus, so wait a bit and then try
492 } catch (InterruptedException e1) {
493 logger.info("Stopping event publisher worker thread.");