Remove commented methods/fields in APPC
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / main / java / org / openecomp / appc / messageadapter / impl / MessageAdapterDmaapImpl.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.messageadapter.impl;
23
24 import java.util.HashSet;
25 import java.util.Properties;
26
27 import org.apache.commons.lang.ObjectUtils;
28 import org.openecomp.appc.adapter.dmaap.Producer;
29 import org.openecomp.appc.adapter.dmaap.DmaapProducer;
30 import org.openecomp.appc.configuration.Configuration;
31 import org.openecomp.appc.configuration.ConfigurationFactory;
32 import org.openecomp.appc.domainmodel.lcm.ResponseContext;
33 import org.openecomp.appc.domainmodel.lcm.VNFOperation;
34 import org.openecomp.appc.messageadapter.MessageAdapter;
35 import org.openecomp.appc.requesthandler.conv.Converter;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38
39 import com.fasterxml.jackson.core.JsonProcessingException;
40
41 public class MessageAdapterDmaapImpl implements MessageAdapter{
42
43     private Producer dmaapProducer;
44     private String partition ;
45     private Configuration configuration;
46     private HashSet<String> pool;
47     private String writeTopic;
48     private String apiKey;
49     private String apiSecret;
50
51     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapterDmaapImpl.class);
52
53     /**
54      * Initialize dmaapProducer client to post messages using configuration properties
55      */
56     @Override
57     public void init(){
58         this.dmaapProducer = getDmaapProducer();
59     }
60     private Producer getDmaapProducer() {
61         configuration = ConfigurationFactory.getConfiguration();
62         Properties properties=configuration.getProperties();
63         updateProperties(properties);
64         Producer producer=new DmaapProducer(pool,writeTopic);
65         producer.updateCredentials(apiKey, apiSecret);
66         return producer;
67     }
68
69
70     private void updateProperties(Properties props) {
71         if (logger.isTraceEnabled()) {
72             logger.trace("Entering to updateProperties with Properties = "+ ObjectUtils.toString(props));
73         }
74         pool = new HashSet<>();
75         if (props != null) {
76             writeTopic = props.getProperty("dmaap.topic.write");
77             apiKey = props.getProperty("dmaap.client.key");
78             apiSecret = props.getProperty("dmaap.client.secret");
79             String hostnames = props.getProperty("dmaap.poolMembers");
80             if (hostnames != null && !hostnames.isEmpty()) {
81                 for (String name : hostnames.split(",")) {
82                     pool.add(name);
83                 }
84             }
85         }
86     }
87
88     /**
89      * Posts message to DMaaP. As DMaaP accepts only json messages this method first convert dmaapMessage to json format and post it to DMaaP.
90      * @param asyncResponse response data that based on it a message will be send to DMaaP (the format of the message that will be sent to DMaaP based on the action and its YANG domainmodel).
91      * @return True if message is postes successfully else False
92      */
93     @Override
94     public boolean post(VNFOperation operation, String rpcName, ResponseContext asyncResponse){
95         boolean success;
96         if (logger.isTraceEnabled()) {
97             logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(asyncResponse));
98         }
99
100         String jsonMessage;
101         try {
102             jsonMessage = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(operation, rpcName, asyncResponse);
103             if (logger.isDebugEnabled()) {
104                 logger.debug("DMaaP Response = " + jsonMessage);
105             }
106             success  = dmaapProducer.post(this.partition, jsonMessage);
107         } catch (JsonProcessingException e1) {
108             logger.error("Error generating Jason from DMaaP message "+ e1.getMessage());
109             success= false;
110         }catch (Exception e){
111             logger.error("Error sending message to DMaaP "+e.getMessage());
112             success= false;
113         }
114         if (logger.isTraceEnabled()) {
115             logger.trace("Exiting from post with (success = "+ ObjectUtils.toString(success)+")");
116         }
117         return success;
118     }
119 }