2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.oam.messageadapter;
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;
40 import java.util.HashSet;
41 import java.util.Properties;
43 public class MessageAdapter {
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;
53 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapter.class);
56 * Initialize producer client to post messages using configuration properties
59 this.producer = getProducer();
62 private Producer getProducer() {
63 configuration = ConfigurationFactory.getConfiguration();
64 Properties properties=configuration.getProperties();
65 updateProperties(properties);
66 Producer localProducer = null;
68 BundleContext ctx = FrameworkUtil.getBundle(MessageAdapter.class).getBundleContext();
70 ServiceReference svcRef = ctx.getServiceReference(MessageAdapterFactory.class.getName());
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);
85 private void updateProperties(Properties props) {
86 if (logger.isTraceEnabled()) {
87 logger.trace("Entering to updateProperties with Properties = "+ ObjectUtils.toString(props));
89 pool = new HashSet<>();
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(",")) {
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
108 public boolean post(OAMContext oamContext){
110 if (logger.isTraceEnabled()) {
111 logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(oamContext));
116 jsonMessage = Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext);
117 if (logger.isDebugEnabled()) {
118 logger.debug("UEB Response = " + jsonMessage);
120 success = producer.post(this.partition, jsonMessage);
121 } catch (JsonProcessingException e1) {
122 logger.error("Error generating Jason from UEB message "+ e1.getMessage());
124 }catch (Exception e){
125 logger.error("Error sending message to UEB "+e.getMessage());
128 if (logger.isTraceEnabled()) {
129 logger.trace("Exiting from post with (success = "+ ObjectUtils.toString(success)+")");