2 * ============LICENSE_START=======================================================
3 * org.onap.dcaegen2.collectors.ves
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2018 Nokia. All rights reserved.
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=========================================================
22 package org.onap.dcae.common.publishing;
24 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
25 import com.att.nsa.clock.SaClock;
26 import com.att.nsa.logging.LoggingContext;
27 import com.att.nsa.logging.log4j.EcompFields;
28 import io.vavr.collection.Map;
29 import io.vavr.control.Try;
30 import org.json.JSONObject;
31 import org.onap.dcae.common.VESLogger;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.io.IOException;
37 import static org.onap.dcae.common.publishing.VavrUtils.f;
40 * @author Pawel Szalapski (pawel.szalapski@nokia.com)
42 class DMaaPEventPublisher implements EventPublisher {
43 private static final int PENDING_MESSAGE_LOG_THRESHOLD = 100;
44 private static final String VES_UNIQUE_ID = "VESuniqueId";
45 private static final Logger log = LoggerFactory.getLogger(DMaaPEventPublisher.class);
46 private final DMaaPPublishersCache publishersCache;
47 private final Logger outputLogger;
49 DMaaPEventPublisher(DMaaPPublishersCache DMaaPPublishersCache,
50 Logger outputLogger) {
51 this.publishersCache = DMaaPPublishersCache;
52 this.outputLogger = outputLogger;
56 public void sendEvent(JSONObject event, String domain) {
57 clearVesUniqueIdFromEvent(event);
58 publishersCache.getPublisher(domain)
60 log.warn(f("Could not find event publisher for domain: '%s', dropping message: '%s'", domain, event)))
61 .forEach(publisher -> sendEvent(event, domain, publisher));
65 public void reconfigure(Map<String, PublisherConfig> dMaaPConfig) {
66 publishersCache.reconfigure(dMaaPConfig);
69 private void sendEvent(JSONObject event, String domain, CambriaBatchingPublisher publisher) {
70 Try.run(() -> uncheckedSendEvent(event, domain, publisher))
71 .onFailure(exc -> closePublisher(event, domain, exc));
74 private void uncheckedSendEvent(JSONObject event, String domain, CambriaBatchingPublisher publisher)
76 int pendingMsgs = publisher.send("MyPartitionKey", event.toString());
77 if (pendingMsgs > PENDING_MESSAGE_LOG_THRESHOLD) {
78 log.info("Pending messages count: " + pendingMsgs);
80 String infoMsg = f("Event: '%s' scheduled to be send asynchronously on domain: '%s'", event, domain);
82 outputLogger.info(infoMsg);
85 private void closePublisher(JSONObject event, String domain, Throwable e) {
86 log.error(f("Unable to schedule event: '%s' on domain: '%s'. Closing publisher and dropping message.",
88 publishersCache.closePublisherFor(domain);
91 private void clearVesUniqueIdFromEvent(JSONObject event) {
92 if (event.has(VES_UNIQUE_ID)) {
93 String uuid = event.get(VES_UNIQUE_ID).toString();
94 LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid);
95 localLC.put(EcompFields.kBeginTimestampMs, SaClock.now());
96 log.debug("Removing VESuniqueid object from event");
97 event.remove(VES_UNIQUE_ID);