Changed to unmaintained
[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  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.messageadapter.impl;
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.onap.appc.domainmodel.lcm.ResponseContext;
32 import org.onap.appc.domainmodel.lcm.VNFOperation;
33 import org.onap.appc.messageadapter.MessageAdapter;
34 import org.onap.appc.requesthandler.conv.Converter;
35 import org.onap.appc.srvcomm.messaging.MessagingConnector;
36
37 public class MessageAdapterImpl implements MessageAdapter{
38
39     private MessagingConnector messageService;
40     private String partition;
41
42     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapterImpl.class);
43
44     /**
45      * Initialize producer client to post messages using configuration properties
46      */
47     @Override
48     public void init() {
49         logger.debug("MessageAdapterImpl - init");
50         this.messageService = new MessagingConnector();
51     }
52
53     public void init(MessagingConnector connector) {
54         logger.debug("MessageAdapterImpl - init");
55         this.messageService = connector;
56     }
57
58     /**
59      * Posts message to DMaaP. As DMaaP accepts only json messages this method first converts dmaapMessage
60      * to json format and posts it to DMaaP.
61      * @param asyncResponse response data on which to base a message that will be sent to DMaaP
62      * (the format of the message that will be sent to DMaaP is based on the action and its YANG domainmodel).
63      * @return True if message is posted successfully, else False
64      */
65     @Override
66     public boolean post(VNFOperation operation, String rpcName, ResponseContext asyncResponse) {
67         boolean success;
68         if (logger.isTraceEnabled()) {
69             logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(asyncResponse));
70         }
71         logger.debug("Entered MessageAdapterImpl.post()");
72         String jsonMessage;
73         try {
74             logger.debug("Before converting Async Response");
75             jsonMessage =
76                     Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(operation, rpcName, asyncResponse);
77             if (logger.isDebugEnabled()) {
78                 logger.debug("DMaaP Response = " + jsonMessage);
79             }
80             logger.debug("Before Invoking messageService.publishMessage(): jsonMessage is::" + jsonMessage);
81             success = messageService.publishMessage("appc.LCM", this.partition, jsonMessage);
82             logger.debug("After Invoking messageService.publishMessage()");
83         } catch (JsonProcessingException e1) {
84             logger.error("Error generating Json from DMaaP message " + e1.getMessage());
85             success = false;
86         } catch (Exception e) {
87             logger.error("Error sending message to DMaaP " + e.getMessage());
88             success = false;
89         }
90         logger.debug("Exiting MesageAdapterImpl.post()");
91         if (logger.isTraceEnabled()) {
92             logger.trace("Exiting from post with (success = " + ObjectUtils.toString(success) + ")");
93         }
94         return success;
95     }
96 }