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 static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapterDmaapImpl.class);
54 * Initialize dmaapProducer client to post messages using configuration properties
58 this.dmaapProducer = getDmaapProducer();
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);
70 private void updateProperties(Properties props) {
71 if (logger.isTraceEnabled()) {
72 logger.trace("Entering to updateProperties with Properties = "+ ObjectUtils.toString(props));
74 pool = new HashSet<>();
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(",")) {
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
94 public boolean post(VNFOperation operation, String rpcName, ResponseContext asyncResponse){
96 if (logger.isTraceEnabled()) {
97 logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(asyncResponse));
102 jsonMessage = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(operation, rpcName, asyncResponse);
103 if (logger.isDebugEnabled()) {
104 logger.debug("DMaaP Response = " + jsonMessage);
106 success = dmaapProducer.post(this.partition, jsonMessage);
107 } catch (JsonProcessingException e1) {
108 logger.error("Error generating Jason from DMaaP message "+ e1.getMessage());
110 }catch (Exception e){
111 logger.error("Error sending message to DMaaP "+e.getMessage());
114 if (logger.isTraceEnabled()) {
115 logger.trace("Exiting from post with (success = "+ ObjectUtils.toString(success)+")");