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