2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.messageadapter.impl;
24 import java.util.HashSet;
25 import java.util.Properties;
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;
39 import com.fasterxml.jackson.core.JsonProcessingException;
41 public class MessageAdapterDmaapImpl implements MessageAdapter{
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;
51 private Integer READ_TIMEOUT;
53 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapterDmaapImpl.class);
56 * Initialize dmaapProducer client to post messages using configuration properties
60 this.dmaapProducer = getDmaapProducer();
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);
72 private void updateProperties(Properties props) {
73 if (logger.isTraceEnabled()) {
74 logger.trace("Entering to updateProperties with Properties = "+ ObjectUtils.toString(props));
76 pool = new HashSet<>();
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");
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(",")) {
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
102 public boolean post(VNFOperation operation, String rpcName, ResponseContext asyncResponse){
104 if (logger.isTraceEnabled()) {
105 logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(asyncResponse));
110 jsonMessage = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(operation, rpcName, asyncResponse);
111 if (logger.isDebugEnabled()) {
112 logger.debug("DMaaP Response = " + jsonMessage);
114 success = dmaapProducer.post(this.partition, jsonMessage);
115 } catch (JsonProcessingException e1) {
116 logger.error("Error generating Jason from DMaaP message "+ e1.getMessage());
118 }catch (Exception e){
119 logger.error("Error sending message to DMaaP "+e.getMessage());
122 if (logger.isTraceEnabled()) {
123 logger.trace("Exiting from post with (success = "+ ObjectUtils.toString(success)+")");