Fix sonar issues in portal
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / uebhandler / MainUebHandler.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software 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  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  *
37  */
38 package org.onap.portalapp.uebhandler;
39
40 import java.text.DateFormat;
41 import java.text.SimpleDateFormat;
42 import java.util.Date;
43 import java.util.concurrent.ConcurrentLinkedQueue;
44
45 import org.onap.portalapp.portal.ueb.EPUebMsgTypes;
46 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
47 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
48 import org.onap.portalsdk.core.onboarding.ueb.UebMsg;
49 import org.onap.portalsdk.core.onboarding.ueb.UebMsgTypes;
50 import org.slf4j.MDC;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.scheduling.annotation.Async;
53 import org.springframework.stereotype.Component;
54
55 import com.att.eelf.configuration.Configuration;
56
57 //-------------------------------------------------------------------------
58 // Listens for received UEB messages and handles the messages
59 //
60 // Note: To implement a synchronous reply call getMsgId on the request 
61 //       and putMsgId on the reply (echoing the request MsgId).
62 //       
63 //-------------------------------------------------------------------------
64 @Component("MainUebHandler")
65 public class MainUebHandler {
66         final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
67         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MainUebHandler.class);
68
69         private ConcurrentLinkedQueue<UebMsg> inboxQueue = null;
70
71         @Autowired
72         private FunctionalMenuHandler funcMenuHandler;
73
74         @Autowired
75         private WidgetNotificationHandler widgetNotificationHandler;
76
77         @Async
78         public void runHandler(ConcurrentLinkedQueue<UebMsg> queue) {
79                 inboxQueue = queue;
80                 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "==> MainUebHandler started");
81                 while (true) {
82                         UebMsg msg = null;
83                         while ((msg = inboxQueue.poll()) != null) {
84                                 if ((msg.getMsgType() != null)
85                                                 && (!msg.getMsgType().equalsIgnoreCase(EPUebMsgTypes.UEB_MSG_TYPE_HEALTH_CHECK))) {
86                                         // TODO: switch this back to debug
87                                         logger.info(EELFLoggerDelegate.errorLogger,
88                                                         dateFormat.format(new Date()) + "<== Received UEB message : " + msg.toString());
89                                         logger.info(EELFLoggerDelegate.debugLogger,
90                                                         dateFormat.format(new Date()) + "<== Received UEB message : " + msg.toString());
91                                         MDC.put(EPCommonSystemProperties.PARTNER_NAME, msg.getSourceTopicName());
92                                         MDC.put(Configuration.MDC_SERVICE_NAME, msg.getMsgType().toString());
93                                         switch (msg.getMsgType()) {
94                                         case UebMsgTypes.UEB_MSG_TYPE_GET_FUNC_MENU: {
95                                                 funcMenuHandler.getFunctionalMenu(msg);
96                                                 break;
97                                         }
98                                         case UebMsgTypes.UEB_MSG_TYPE_WIDGET_NOTIFICATION: {
99                                                 widgetNotificationHandler.handleWidgetNotification(msg);
100                                                 break;
101                                         }
102                                         default: {
103                                                 logger.info(EELFLoggerDelegate.debugLogger,
104                                                                 dateFormat.format(new Date()) + "Unknown UEB message type " + msg.toString());
105                                                 break;
106                                         }
107                                         }
108                                 }
109                         }
110
111                         if (Thread.interrupted()) {
112                                 logger.info(EELFLoggerDelegate.errorLogger, "==> UebMainHandler exiting");
113                                 break;
114                         }
115
116                         try {
117                                 Thread.sleep(10);
118                         } catch (InterruptedException e) {
119                                 logger.error(EELFLoggerDelegate.errorLogger, "runHandler interrupted", e);
120                                 Thread.currentThread().interrupt();
121                         } catch (Exception e) {
122                                 logger.error(EELFLoggerDelegate.errorLogger, "runHandler failed", e);
123                         }
124                 }
125         }
126 }