Replace cambria with DmaaP client
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / common / EventSender.java
1 /*
2  * ============LICENSE_START=======================================================
3  * VES Collector
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2018-2021 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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  */
21 package org.onap.dcae.common;
22
23
24 import io.vavr.collection.Map;
25 import org.onap.dcae.common.model.VesEvent;
26 import org.onap.dcae.common.publishing.DMaaPEventPublisher;
27 import org.onap.dcae.restapi.EventValidatorException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.http.HttpStatus;
31
32 import java.util.List;
33
34 import static org.onap.dcae.restapi.ApiException.DOMAIN_NOT_DEFINED_FOR_STREAM_ID;
35
36
37 public class EventSender {
38
39     private Map<String, String> streamIdToDmaapIds;
40     private DMaaPEventPublisher eventPublisher;
41     private static final Logger log = LoggerFactory.getLogger(EventSender.class);
42
43     public EventSender(DMaaPEventPublisher eventPublisher, Map<String, String> streamIdToDmaapIds) {
44         this.eventPublisher = eventPublisher;
45         this.streamIdToDmaapIds = streamIdToDmaapIds;
46     }
47
48     public HttpStatus send(List<VesEvent> vesEvents) {
49         String topic = streamIdToDmaapIds
50                 .get(vesEvents.get(0).getStreamId())
51                 .getOrElse(() -> {
52                     log.error("No StreamID defined for publish - Message dropped " + vesEvents.get(0).asJsonObject());
53                     throw new EventValidatorException(DOMAIN_NOT_DEFINED_FOR_STREAM_ID);
54                 });
55         return eventPublisher.sendEvent(vesEvents, topic);
56     }
57 }