Changed to unmaintained
[appc.git] / appc-oam / appc-oam-bundle / src / main / java / org / onap / appc / oam / messageadapter / MessageAdapter.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  * 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.oam.messageadapter;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import org.apache.commons.lang.ObjectUtils;
30 import org.onap.appc.srvcomm.messaging.MessagingConnector;
31
32 public class MessageAdapter {
33
34     private final EELFLogger logger = EELFManager.getInstance().getLogger(MessageAdapter.class);
35
36     private static final String  PROPERTIES_PREFIX = "appc.OAM";
37
38     private MessagingConnector messagingConnector;
39     private String partition;
40     private boolean isDisabled;
41
42     /**
43      * Initialize producer client to post messages using configuration properties.
44      */
45     public void init() {
46
47         if (isAppcOamPropsListenerEnabled()) {
48             messagingConnector = new MessagingConnector();
49         } else {
50             logger.warn(String.format("The listener %s is disabled and will not be run", "appc.OAM"));
51         }
52     }
53     
54     public void init(MessagingConnector connector) {
55
56         if (isAppcOamPropsListenerEnabled()) {
57             messagingConnector = connector;
58         } else {
59             logger.warn(String.format("The listener %s is disabled and will not be run", "appc.OAM"));
60         }
61     }
62
63
64     /**
65      * Get producer. If it is null, call createProducer to create it again.
66      *
67      * @return Producer
68      */
69     MessagingConnector getMessagingConnector() {
70         if (messagingConnector == null) {
71             messagingConnector = new MessagingConnector();
72         }
73
74         return messagingConnector;
75     }
76
77     /**
78      * Posts message to UEB. As UEB accepts only json messages this method first convert uebMessage to json format
79      * and post it to UEB.
80      *
81      * @param oamContext response data that based on it a message will be send to UEB (the format of the message that
82      *                   will be sent to UEB based on the action and its YANG domainmodel).
83      */
84     public void post(OAMContext oamContext) {
85         if (logger.isTraceEnabled()) {
86             logger.trace("Entering to post with AsyncResponse = " + ObjectUtils.toString(oamContext));
87         }
88
89         boolean success;
90         String jsonMessage;
91         try {
92             jsonMessage = Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext);
93             if (logger.isDebugEnabled()) {
94                 logger.debug("UEB Response = " + jsonMessage);
95             }
96
97             MessagingConnector connector = getMessagingConnector();
98             success = connector != null && connector.publishMessage(PROPERTIES_PREFIX, this.partition, jsonMessage);
99         } catch (JsonProcessingException e1) {
100             logger.error("Error generating Json from UEB message " + e1.getMessage());
101             success = false;
102         } catch (Exception e) {
103             logger.error("Error sending message to UEB " + e.getMessage(), e);
104             success = false;
105         }
106
107         if (logger.isTraceEnabled()) {
108             logger.trace("Exiting from post with (success = " + ObjectUtils.toString(success) + ")");
109         }
110     }
111
112     private boolean isAppcOamPropsListenerEnabled() {
113         return !isDisabled;
114     }
115 }