Third part of onap rename
[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.DmaapMessageAdapterFactoryImpl;
29 import org.onap.appc.adapter.factory.MessageService;
30 import org.onap.appc.adapter.message.MessageAdapterFactory;
31 import org.onap.appc.adapter.message.Producer;
32 import org.onap.appc.configuration.Configuration;
33 import org.onap.appc.configuration.ConfigurationFactory;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import org.apache.commons.lang.ObjectUtils;
39 import org.onap.appc.domainmodel.lcm.ResponseContext;
40 import org.onap.appc.domainmodel.lcm.VNFOperation;
41 import org.onap.appc.messageadapter.MessageAdapter;
42 import org.onap.appc.requesthandler.conv.Converter;
43 import org.osgi.framework.BundleContext;
44 import org.osgi.framework.FrameworkUtil;
45 import org.osgi.framework.ServiceReference;
46
47 import java.util.HashSet;
48 import java.util.Properties;
49
50 public class MessageAdapterImpl implements MessageAdapter{
51
52     private MessageService messageService;
53     private Producer producer;
54     private String partition ;
55     private Configuration configuration;
56     private HashSet<String> pool;
57     private String writeTopic;
58     private String apiKey;
59     private String apiSecret;
60
61     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapterImpl.class);
62
63     /**
64      * Initialize producer client to post messages using configuration properties
65      */
66     @Override
67     public void init(){
68         this.producer = getProducer();
69     }
70
71     private Producer getProducer() {
72         configuration = ConfigurationFactory.getConfiguration();
73         Properties properties=configuration.getProperties();
74         updateProperties(properties);
75         
76         BundleContext ctx = FrameworkUtil.getBundle(MessageAdapterImpl.class).getBundleContext();
77         if (ctx != null) {
78                 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
79                 if (svcRef != null) {
80                         producer = ((MessageAdapterFactory) ctx.getService(svcRef)).createProducer(pool, writeTopic,apiKey, apiSecret);
81                 }
82         }
83         return producer;
84     }
85
86
87     private void updateProperties(Properties props) {
88         if (logger.isTraceEnabled()) {
89             logger.trace("Entering to updateProperties with Properties = "+ ObjectUtils.toString(props));
90         }
91         pool = new HashSet<>();
92         if (props != null) {
93             // readTopic = props.getProperty("dmaap.topic.read");
94             writeTopic = props.getProperty("appc.LCM.topic.write");
95             apiKey = props.getProperty("appc.LCM.client.key");
96             apiSecret = props.getProperty("appc.LCM.client.secret");
97             messageService = MessageService.parse(props.getProperty("message.service.type"));
98             //  READ_TIMEOUT = Integer.valueOf(props.getProperty("dmaap.topic.read.timeout", String.valueOf(READ_TIMEOUT)));
99             String hostnames = props.getProperty("appc.LCM.poolMembers");
100             if (hostnames != null && !hostnames.isEmpty()) {
101                 for (String name : hostnames.split(",")) {
102                     pool.add(name);
103                 }
104             }
105         }
106     }
107
108     /**
109      * Posts message to DMaaP. As DMaaP accepts only json messages this method first convert dmaapMessage to json format and post it to DMaaP.
110      * @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).
111      * @return True if message is postes successfully else False
112      */
113     @Override
114     public boolean post(VNFOperation operation, String rpcName, ResponseContext asyncResponse){
115         boolean success;
116         if (logger.isTraceEnabled()) {
117             logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(asyncResponse));
118         }
119
120         String jsonMessage;
121         try {
122             jsonMessage = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(operation, rpcName, asyncResponse);
123             if (logger.isDebugEnabled()) {
124                 logger.debug("DMaaP Response = " + jsonMessage);
125             }
126             success  = producer.post(this.partition, jsonMessage);
127         } catch (JsonProcessingException e1) {
128             logger.error("Error generating Json from DMaaP message "+ e1.getMessage());
129             success= false;
130         }catch (Exception e){
131             logger.error("Error sending message to DMaaP "+e.getMessage());
132             success= false;
133         }
134         if (logger.isTraceEnabled()) {
135             logger.trace("Exiting from post with (success = "+ ObjectUtils.toString(success)+")");
136         }
137         return success;
138     }
139 }