2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2018,2020 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=========================================================
21 package org.onap.dcae.common;
23 import com.att.nsa.clock.SaClock;
24 import com.att.nsa.logging.LoggingContext;
25 import com.att.nsa.logging.log4j.EcompFields;
26 import io.vavr.collection.Map;
27 import org.onap.dcae.common.model.VesEvent;
28 import org.onap.dcae.common.publishing.DMaaPEventPublisher;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.util.List;
34 public class EventSender {
36 private static final Logger metriclog = LoggerFactory.getLogger("com.att.ecomp.metrics");
37 private Map<String, String[]> streamIdToDmaapIds;
38 private DMaaPEventPublisher eventPublisher;
39 private static final Logger log = LoggerFactory.getLogger(EventSender.class);
41 public EventSender(DMaaPEventPublisher eventPublisher, Map<String, String[]> streamIdToDmaapIds) {
42 this.eventPublisher = eventPublisher;
43 this.streamIdToDmaapIds = streamIdToDmaapIds;
46 public void send(List<VesEvent> vesEvents) {
47 for (VesEvent vesEvent : vesEvents) {
48 metriclog.info("EVENT_PUBLISH_START");
49 setLoggingContext(vesEvent);
50 streamIdToDmaapIds.get(vesEvent.getStreamId())
51 .onEmpty(() -> log.error("No StreamID defined for publish - Message dropped" + vesEvent.asJsonObject()))
52 .forEach(dmaapIds -> sendEventsToStreams(vesEvent, dmaapIds));
53 log.debug("Message published" + vesEvent.asJsonObject());
55 log.debug("CommonStartup.handleEvents:EVENTS has been published successfully!");
56 metriclog.info("EVENT_PUBLISH_END");
59 private void sendEventsToStreams(VesEvent vesEvent, String[] dmaapIds) {
60 for (String dmaapId : dmaapIds) {
61 log.info("Invoking publisher for streamId/domain:" + dmaapId);
62 eventPublisher.sendEvent(vesEvent, dmaapId);
66 private void setLoggingContext(VesEvent vesEvent) {
67 LoggingContext localLC = VESLogger.getLoggingContextForThread(vesEvent.getUniqueId().toString());
68 localLC.put(EcompFields.kBeginTimestampMs, SaClock.now());
69 log.debug("event.VESuniqueId" + vesEvent.getUniqueId() + "event.commonEventHeader.domain:" + vesEvent.getDomain());