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