9f74ad43849db96118b30390429e1b5e5ceeab61
[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 Integer READ_TIMEOUT;
52
53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapterDmaapImpl.class);
54
55     /**
56      * Initialize dmaapProducer client to post messages using configuration properties
57      */
58     @Override
59     public void init(){
60         this.dmaapProducer = getDmaapProducer();
61     }
62     private Producer getDmaapProducer() {
63         configuration = ConfigurationFactory.getConfiguration();
64         Properties properties=configuration.getProperties();
65         updateProperties(properties);
66         Producer producer=new DmaapProducer(pool,writeTopic);
67         producer.updateCredentials(apiKey, apiSecret);
68         return producer;
69     }
70
71
72     private void updateProperties(Properties props) {
73         if (logger.isTraceEnabled()) {
74             logger.trace("Entering to updateProperties with Properties = "+ ObjectUtils.toString(props));
75         }
76         pool = new HashSet<>();
77         if (props != null) {
78             // readTopic = props.getProperty("dmaap.topic.read");
79             writeTopic = props.getProperty("dmaap.topic.write");
80             apiKey = props.getProperty("dmaap.client.key");
81             apiSecret = props.getProperty("dmaap.client.secret");
82             /*          clientName = props.getProperty("dmaap.client.name", "APP-C");
83             clientId = props.getProperty("dmaap.client.name.id", "0");
84             filter_json = props.getProperty("dmaap.topic.read.filter");
85              */
86             //  READ_TIMEOUT = Integer.valueOf(props.getProperty("dmaap.topic.read.timeout", String.valueOf(READ_TIMEOUT)));
87             String hostnames = props.getProperty("dmaap.poolMembers");
88             if (hostnames != null && !hostnames.isEmpty()) {
89                 for (String name : hostnames.split(",")) {
90                     pool.add(name);
91                 }
92             }
93         }
94     }
95
96     /**
97      * Posts message to DMaaP. As DMaaP accepts only json messages this method first convert dmaapMessage to json format and post it to DMaaP.
98      * @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).
99      * @return True if message is postes successfully else False
100      */
101     @Override
102     public boolean post(VNFOperation operation, String rpcName, ResponseContext asyncResponse){
103         boolean success;
104         if (logger.isTraceEnabled()) {
105             logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(asyncResponse));
106         }
107
108         String jsonMessage;
109         try {
110             jsonMessage = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(operation, rpcName, asyncResponse);
111             if (logger.isDebugEnabled()) {
112                 logger.debug("DMaaP Response = " + jsonMessage);
113             }
114             success  = dmaapProducer.post(this.partition, jsonMessage);
115         } catch (JsonProcessingException e1) {
116             logger.error("Error generating Jason from DMaaP message "+ e1.getMessage());
117             success= false;
118         }catch (Exception e){
119             logger.error("Error sending message to DMaaP "+e.getMessage());
120             success= false;
121         }
122         if (logger.isTraceEnabled()) {
123             logger.trace("Exiting from post with (success = "+ ObjectUtils.toString(success)+")");
124         }
125         return success;
126     }
127 }