Merging in bug fixes
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / openecomp / appc / oam / messageadapter / MessageAdapter.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.openecomp.appc.oam.messageadapter;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import org.apache.commons.lang.ObjectUtils;
31 import org.openecomp.appc.adapter.message.MessageAdapterFactory;
32 import org.openecomp.appc.adapter.message.Producer;
33 import org.openecomp.appc.configuration.Configuration;
34 import org.openecomp.appc.configuration.ConfigurationFactory;
35 import org.openecomp.appc.listener.impl.EventHandlerImpl;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.FrameworkUtil;
38 import org.osgi.framework.ServiceReference;
39
40 import java.util.HashSet;
41 import java.util.Properties;
42
43 public class MessageAdapter {
44
45     private Producer producer;
46     private String partition ;
47     private Configuration configuration;
48     private HashSet<String> pool;
49     private String writeTopic;
50     private String apiKey;
51     private String apiSecret;
52
53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapter.class);
54
55     /**
56      * Initialize producer client to post messages using configuration properties
57      */
58     public void init(){
59         this.producer = getProducer();
60     }
61
62     private Producer getProducer() {
63         configuration = ConfigurationFactory.getConfiguration();
64         Properties properties=configuration.getProperties();
65         updateProperties(properties);
66         Producer localProducer = null;
67         
68         BundleContext ctx = FrameworkUtil.getBundle(MessageAdapter.class).getBundleContext();
69         if (ctx != null) {
70                 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
71                 if (svcRef != null) {
72                         localProducer = ((MessageAdapterFactory) ctx.getService(svcRef)).createProducer(pool, writeTopic, apiKey, apiSecret);
73                         for (String url : pool) {
74                             if (url.contains("3905") || url.contains("https")) {
75                                 localProducer.useHttps(true);
76                                 break;
77                             }
78                         }
79                 }
80         }
81
82         return localProducer;
83     }
84
85     private void updateProperties(Properties props) {
86         if (logger.isTraceEnabled()) {
87             logger.trace("Entering to updateProperties with Properties = "+ ObjectUtils.toString(props));
88         }
89         pool = new HashSet<>();
90         if (props != null) {
91             writeTopic = props.getProperty("appc.OAM.topic.write");
92             apiKey = props.getProperty("appc.OAM.client.key");
93             apiSecret = props.getProperty("appc.OAM.client.secret");
94             String hostnames = props.getProperty("appc.OAM.poolMembers");
95             if (hostnames != null && !hostnames.isEmpty()) {
96                 for (String name : hostnames.split(",")) {
97                     pool.add(name);
98                 }
99             }
100         }
101     }
102
103     /**
104      * Posts message to UEB. As UEB accepts only json messages this method first convert uebMessage to json format and post it to UEB.
105      * @param oamContext response data that based on it a message will be send to UEB (the format of the message that will be sent to UEB based on the action and its YANG domainmodel).
106      * @return True if message is postes successfully else False
107      */
108     public boolean post(OAMContext oamContext){
109         boolean success;
110         if (logger.isTraceEnabled()) {
111             logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(oamContext));
112         }
113
114         String jsonMessage;
115         try {
116             jsonMessage = Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext);
117             if (logger.isDebugEnabled()) {
118                 logger.debug("UEB Response = " + jsonMessage);
119             }
120             success  = producer.post(this.partition, jsonMessage);
121         } catch (JsonProcessingException e1) {
122             logger.error("Error generating Jason from UEB message "+ e1.getMessage());
123             success= false;
124         }catch (Exception e){
125             logger.error("Error sending message to UEB "+e.getMessage());
126             success= false;
127         }
128         if (logger.isTraceEnabled()) {
129             logger.trace("Exiting from post with (success = "+ ObjectUtils.toString(success)+")");
130         }
131         return success;
132     }
133 }