bf76490267a7adf6a436a1b6d7f007f5eca2e70e
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / openecomp / appc / adapter / messaging / dmaap / impl / EventSenderDmaapImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.messaging.dmaap.impl;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30
31 import java.util.*;
32 import java.util.concurrent.ConcurrentHashMap;
33
34 import org.onap.appc.adapter.message.EventSender;
35 import org.onap.appc.adapter.message.MessageDestination;
36 import org.onap.appc.adapter.message.Producer;
37 import org.onap.appc.adapter.message.event.EventHeader;
38 import org.onap.appc.adapter.message.event.EventMessage;
39 import org.onap.appc.adapter.message.event.EventStatus;
40 import org.onap.appc.adapter.messaging.dmaap.impl.DmaapProducerImpl;
41 import org.onap.appc.configuration.Configuration;
42 import org.onap.appc.configuration.ConfigurationFactory;
43 import org.onap.appc.exceptions.APPCException;
44
45 public class EventSenderDmaapImpl implements EventSender
46 {
47     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(EventSenderDmaapImpl.class);
48     public static final String EVENT_TOPIC_WRITE = "dmaap.event.topic.write";
49     public static final String DMAAP_USERNAME = "dmaap.appc.username";
50     public static final String DMAAP_PASSWORD = "dmaap.appc.password";
51     public static final String EVENT_POOL_MEMBERS = "dmaap.event.pool.members";
52
53     private static Configuration configuration = ConfigurationFactory.getConfiguration();
54
55     private Map<String,Producer> producerMap = new ConcurrentHashMap<>();
56
57     public Map<String, Producer> getProducerMap() {
58         return producerMap;
59     }
60
61     public void setProducerMap(Map<String, Producer> producerMap) {
62         this.producerMap = producerMap;
63     }
64
65     public EventSenderDmaapImpl(){
66
67     }
68
69     public void initialize(){
70         Properties properties = configuration.getProperties();
71         String writeTopic;
72         String username;
73         String password;
74         final List<String> pool = new ArrayList<>();
75
76         for(MessageDestination destination: MessageDestination.values()){
77             writeTopic = properties.getProperty(destination + "." +  EVENT_TOPIC_WRITE);
78             username = properties.getProperty(destination + "." + DMAAP_USERNAME);
79             password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
80             String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
81
82             if (hostNames != null && !hostNames.isEmpty()) {
83                 LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
84                 Collections.addAll(pool, hostNames.split(","));
85             }
86
87             LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
88             LOG.debug(String.format("writeTopic = %s, taken from property: %s", writeTopic, destination + "." + EVENT_TOPIC_WRITE));
89             LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
90             Producer producer = new DmaapProducerImpl(pool, writeTopic,username, password);
91
92             for (String url : pool) {
93                 if (url.contains("3905") || url.contains("https")) {
94                     LOG.debug("Producer should use HTTPS");
95                     producer.useHttps(true);
96                     break;
97                 }
98             }
99             producerMap.put(destination.toString(),producer);
100         }
101
102     }
103
104     @Override
105     public boolean sendEvent(MessageDestination destination, EventMessage msg) {
106         String jsonStr = msg.toJson();
107         String id = msg.getEventHeader().getEventId();
108         LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
109         Producer producer = producerMap.get(destination.toString());
110         return producer.post(id, jsonStr);
111     }
112
113     @Override
114     public boolean sendEvent(MessageDestination destination, EventMessage msg, String eventTopicName) {
115         String jsonStr = msg.toJson();
116         String id = msg.getEventHeader().getEventId();
117         LOG.info(String.format("Posting Message [%s - %s]", id, jsonStr));
118         Producer producer = createProducer(destination, eventTopicName);
119         return producer.post(id, jsonStr);
120     }
121     
122     private Producer createProducer(MessageDestination destination, String eventTopicName) {
123         Properties properties = configuration.getProperties();
124         final List<String> pool = new ArrayList<>();
125         String username = properties.getProperty(destination + "." + DMAAP_USERNAME);
126         String password = properties.getProperty(destination + "." + DMAAP_PASSWORD);
127         String hostNames = properties.getProperty(destination + "." + EVENT_POOL_MEMBERS);
128
129         if (hostNames != null && !hostNames.isEmpty()) {
130             LOG.debug(String.format("hostNames = %s, taken from property: %s", hostNames, destination + "." + EVENT_POOL_MEMBERS));
131             Collections.addAll(pool, hostNames.split(","));
132         }
133
134         LOG.debug(String.format("pool = %s, taken from property: %s", pool, destination + "." + EVENT_POOL_MEMBERS));
135         LOG.debug(String.format("writeTopic = %s, taken from property: %s", eventTopicName, destination + "." + EVENT_TOPIC_WRITE));
136         LOG.debug(String.format("username = %s, taken from property: %s", username, destination + "." + DMAAP_USERNAME));
137         Producer producer = new DmaapProducerImpl(pool, eventTopicName,username, password);
138
139         for (String url : pool) {
140             if (url.contains("3905") || url.contains("https")) {
141                 LOG.debug("Producer should use HTTPS");
142                 producer.useHttps(true);
143                 break;
144             }
145         }
146         return producer;
147     }
148
149     @Override
150     public boolean sendEvent(MessageDestination destination, Map<String, String> params, SvcLogicContext ctx) throws APPCException {
151
152         if (params == null) {
153             String message = "Parameters map is empty (null)";
154             LOG.error(message);
155             throw new APPCException(message);
156         }
157         String eventTime = new Date(System.currentTimeMillis()).toString();
158         String apiVer = params.get("apiVer");
159         String eventId = params.get("eventId");
160         String reason = params.get("reason");
161         String entityId=params.get("entityId");
162         if(entityId!=null){
163             reason=reason+"("+entityId+")";
164         }
165         Integer code = Integer.getInteger(params.get("code"), 500);
166
167         if (eventTime == null || apiVer == null || eventId == null || reason == null) {
168             String message = String.format("Missing input parameters: %s", params);
169             LOG.error(message);
170             throw new APPCException(message);
171         }
172         EventMessage eventMessage = new EventMessage(
173                         new EventHeader(eventTime, apiVer, eventId),
174                         new EventStatus(code, reason));
175
176         return sendEvent(destination,eventMessage);
177     }
178 }