Add junit coverage to RequestInfoBuilder class
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / main / java / org / onap / appc / messageadapter / impl / MessageAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.messageadapter.impl;
26
27
28 import org.onap.appc.adapter.factory.MessageService;
29 import org.onap.appc.adapter.message.MessageAdapterFactory;
30 import org.onap.appc.adapter.message.Producer;
31 import org.onap.appc.configuration.Configuration;
32 import org.onap.appc.configuration.ConfigurationFactory;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import org.apache.commons.lang.ObjectUtils;
38 import org.onap.appc.domainmodel.lcm.ResponseContext;
39 import org.onap.appc.domainmodel.lcm.VNFOperation;
40 import org.onap.appc.messageadapter.MessageAdapter;
41 import org.onap.appc.requesthandler.conv.Converter;
42 import org.osgi.framework.BundleContext;
43 import org.osgi.framework.FrameworkUtil;
44 import org.osgi.framework.ServiceReference;
45
46 import java.util.HashSet;
47 import java.util.Properties;
48
49 public class MessageAdapterImpl implements MessageAdapter{
50
51     private MessageService messageService;
52     private Producer producer;
53     private String partition ;
54     private Configuration configuration;
55     private HashSet<String> pool;
56     private String writeTopic;
57     private String apiKey;
58     private String apiSecret;
59
60     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapterImpl.class);
61
62     /**
63      * Initialize producer client to post messages using configuration properties
64      */
65     @Override
66     public void init(){
67         this.producer = getProducer();
68     }
69
70     private Producer getProducer() {
71         configuration = ConfigurationFactory.getConfiguration();
72         Properties properties=configuration.getProperties();
73         updateProperties(properties);
74         
75         BundleContext ctx = FrameworkUtil.getBundle(MessageAdapterImpl.class).getBundleContext();
76         if (ctx != null) {
77             ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
78             if (svcRef != null) {
79                 producer = ((MessageAdapterFactory) ctx.getService(svcRef)).createProducer(pool, writeTopic,apiKey, apiSecret);
80             }
81         }
82         return producer;
83     }
84
85
86     private void updateProperties(Properties props) {
87         if (logger.isTraceEnabled()) {
88             logger.trace("Entering to updateProperties with Properties = "+ ObjectUtils.toString(props));
89         }
90         pool = new HashSet<>();
91         if (props != null) {
92             // readTopic = props.getProperty("dmaap.topic.read");
93             writeTopic = props.getProperty("appc.LCM.topic.write");
94             apiKey = props.getProperty("appc.LCM.client.key");
95             apiSecret = props.getProperty("appc.LCM.client.secret");
96             messageService = MessageService.parse(props.getProperty("message.service.type"));
97             //  READ_TIMEOUT = Integer.valueOf(props.getProperty("dmaap.topic.read.timeout", String.valueOf(READ_TIMEOUT)));
98             String hostnames = props.getProperty("appc.LCM.poolMembers");
99             if (hostnames != null && !hostnames.isEmpty()) {
100                 for (String name : hostnames.split(",")) {
101                     pool.add(name);
102                 }
103             }
104         }
105     }
106
107     /**
108      * Posts message to DMaaP. As DMaaP accepts only json messages this method first convert dmaapMessage to json format and post it to DMaaP.
109      * @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).
110      * @return True if message is postes successfully else False
111      */
112     @Override
113     public boolean post(VNFOperation operation, String rpcName, ResponseContext asyncResponse){
114         boolean success;
115         if (logger.isTraceEnabled()) {
116             logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(asyncResponse));
117         }
118
119         String jsonMessage;
120         try {
121             jsonMessage = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(operation, rpcName, asyncResponse);
122             if (logger.isDebugEnabled()) {
123                 logger.debug("DMaaP Response = " + jsonMessage);
124             }
125             success  = producer.post(this.partition, jsonMessage);
126         } catch (JsonProcessingException e1) {
127             logger.error("Error generating Json from DMaaP message "+ e1.getMessage());
128             success= false;
129         }catch (Exception e){
130             logger.error("Error sending message to DMaaP "+e.getMessage());
131             success= false;
132         }
133         if (logger.isTraceEnabled()) {
134             logger.trace("Exiting from post with (success = "+ ObjectUtils.toString(success)+")");
135         }
136         return success;
137     }
138 }