6b7ee87a4cc518dfd9718daa280919b590260a3b
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / onap / appc / adapter / messaging / dmaap / impl / EventSenderDmaapImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.messaging.dmaap.impl;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
29 import org.onap.appc.adapter.message.EventSender;
30 import org.onap.appc.adapter.message.MessageDestination;
31 import org.onap.appc.adapter.message.Producer;
32 import org.onap.appc.adapter.message.event.EventHeader;
33 import org.onap.appc.adapter.message.event.EventMessage;
34 import org.onap.appc.adapter.message.event.EventStatus;
35 import org.onap.appc.configuration.Configuration;
36 import org.onap.appc.configuration.ConfigurationFactory;
37 import org.onap.appc.exceptions.APPCException;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.Date;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Properties;
44 import java.util.concurrent.ConcurrentHashMap;
45
46
47 public class EventSenderDmaapImpl implements EventSender
48 {
49     private final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSenderDmaapImpl.class);
50     public static final String EVENT_TOPIC_WRITE = "dmaap.event.topic.write";
51     public static final String DMAAP_USERNAME = "dmaap.appc.username";
52     public static final String DMAAP_PASSWORD = "dmaap.appc.password";
53     public static final String EVENT_POOL_MEMBERS = "dmaap.event.pool.members";
54
55     private static Configuration configuration = ConfigurationFactory.getConfiguration();
56
57     private Map<String,Producer> producerMap = new ConcurrentHashMap<>();
58
59     public Map<String, Producer> getProducerMap() {
60         return producerMap;
61     }
62
63     public void setProducerMap(Map<String, Producer> producerMap) {
64         this.producerMap = producerMap;
65     }
66
67     public EventSenderDmaapImpl(){
68
69     }
70
71     public void initialize(){
72         Properties properties = configuration.getProperties();
73         String writeTopic;
74         String username;
75         String password;
76         final List<String> pool = new ArrayList<>();
77
78         for(MessageDestination destination: MessageDestination.values()){
79             writeTopic = properties.getProperty(destination + "." +  EVENT_TOPIC_WRITE);
80             username = properties.getProperty(destination + "." + DMAAP_USERNAME);
81             password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
82             String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
83
84             if (hostNames != null && !hostNames.isEmpty()) {
85                 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
86                 Collections.addAll(pool, hostNames.split(","));
87             }
88
89             LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
90             LOG.debug(String.format("writeTopic = %s, taken from property: %s", writeTopic, destination + "." + EVENT_TOPIC_WRITE));
91             LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
92             Producer producer = new DmaapProducerImpl(pool, writeTopic,username, password);
93
94             for (String url : pool) {
95                 if (url.contains("3905") || url.contains("https")) {
96                     LOG.debug("Producer should use HTTPS");
97                     producer.useHttps(true);
98                     break;
99                 }
100             }
101             producerMap.put(destination.toString(),producer);
102         }
103
104     }
105
106     @Override
107     public boolean sendEvent(MessageDestination destination, EventMessage msg) {
108         String jsonStr = msg.toJson();
109         String id = msg.getEventHeader().getEventId();
110         LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
111         Producer producer = producerMap.get(destination.toString());
112         return producer.post(id, jsonStr);
113     }
114
115     @Override
116     public boolean sendEvent(MessageDestination destination, EventMessage msg, String eventTopicName) {
117         String jsonStr = msg.toJson();
118         String id = msg.getEventHeader().getEventId();
119         LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
120         Producer producer = createProducer(destination, eventTopicName);
121         return producer.post(id, jsonStr);
122     }
123     
124     private Producer createProducer(MessageDestination destination, String eventTopicName) {
125         Properties properties = configuration.getProperties();
126         final List<String> pool = new ArrayList<>();
127         String username = properties.getProperty(destination + "." + DMAAP_USERNAME);
128         String password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
129         String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
130
131         if (hostNames != null && !hostNames.isEmpty()) {
132             LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
133             Collections.addAll(pool, hostNames.split(","));
134         }
135
136         LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
137         LOG.debug(String.format("writeTopic = %s, taken from property: %s", eventTopicName, destination + "." + EVENT_TOPIC_WRITE));
138         LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
139         Producer producer = new DmaapProducerImpl(pool, eventTopicName,username, password);
140
141         for (String url : pool) {
142             if (url.contains("3905") || url.contains("https")) {
143                 LOG.debug("Producer should use HTTPS");
144                 producer.useHttps(true);
145                 break;
146             }
147         }
148         return producer;
149     }
150
151     @Override
152     public boolean sendEvent(MessageDestination destination, Map<String, String> params, SvcLogicContext ctx) throws APPCException {
153
154         if (params == null) {
155             String message = "Parameters map is empty (null)";
156             LOG.error(message);
157             throw new APPCException(message);
158         }
159         String eventTime = new Date(System.currentTimeMillis()).toString();
160         String apiVer = params.get("apiVer");
161         String eventId = params.get("eventId");
162         String reason = params.get("reason");
163         String entityId=params.get("entityId");
164         if(entityId!=null){
165             reason=reason+"("+entityId+")";
166         }
167         Integer code = Integer.getInteger(params.get("code"), 500);
168
169         if (eventTime == null || apiVer == null || eventId == null || reason == null) {
170             String message = String.format("Missing input parameters: %s", params);
171             LOG.error(message);
172             throw new APPCException(message);
173         }
174         EventMessage eventMessage = new EventMessage(
175                         new EventHeader(eventTime, apiVer, eventId),
176                         new EventStatus(code, reason));
177
178         return sendEvent(destination,eventMessage);
179     }
180 }