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