Change code to use dmaap microservice
[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-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.messageadapter.impl;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import org.apache.commons.lang.ObjectUtils;
32 import org.onap.appc.domainmodel.lcm.ResponseContext;
33 import org.onap.appc.domainmodel.lcm.VNFOperation;
34 import org.onap.appc.messageadapter.MessageAdapter;
35 import org.onap.appc.requesthandler.conv.Converter;
36 import org.onap.appc.srvcomm.messaging.MessagingConnector;
37
38 public class MessageAdapterImpl implements MessageAdapter{
39
40     private MessagingConnector messageService;
41     private String partition ;
42
43     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapterImpl.class);
44
45     /**
46      * Initialize producer client to post messages using configuration properties
47      */
48     @Override
49     public void init(){
50         logger.debug("MessageAdapterImpl - init");
51         this.messageService = new MessagingConnector();
52     }
53     
54     public void init(MessagingConnector connector) {
55         logger.debug("MessageAdapterImpl - init");
56         this.messageService = connector;
57     }
58
59     /**
60      * Posts message to DMaaP. As DMaaP accepts only json messages this method first convert dmaapMessage to json format and post it to DMaaP.
61      * @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).
62      * @return True if message is postes successfully else False
63      */
64     @Override
65     public boolean post(VNFOperation operation, String rpcName, ResponseContext asyncResponse){
66         boolean success;
67         if (logger.isTraceEnabled()) {
68             logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(asyncResponse));
69         }
70         logger.debug("Entered MessageAdapterImpl.post()");
71         String jsonMessage;
72         try {
73                 logger.debug("Before converting Async Response");
74             jsonMessage = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(operation, rpcName, asyncResponse);
75             if (logger.isDebugEnabled()) {
76                 logger.debug("DMaaP Response = " + jsonMessage);
77             }
78             logger.debug("Before Invoking producer.post(): jsonMessage is::" + jsonMessage);
79             success = messageService.publishMessage("appc.LCM", this.partition, jsonMessage);
80             logger.debug("After Invoking producer.post()");
81         } catch (JsonProcessingException e1) {
82             logger.error("Error generating Json from DMaaP message " + e1.getMessage());
83             success = false;
84         }catch (Exception e){
85             logger.error("Error sending message to DMaaP " + e.getMessage());
86             success = false;
87         }
88         logger.debug("Exiting MesageAdapterImpl.post()");
89         if (logger.isTraceEnabled()) {
90             logger.trace("Exiting from post with (success = " + ObjectUtils.toString(success) + ")");
91         }
92         return success;
93     }
94 }