Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / portalapp / uebhandler / MainUebHandler.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
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.util.concurrent.ConcurrentLinkedQueue;
23
24 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.openecomp.portalsdk.core.onboarding.ueb.UebMsg;
26 import org.openecomp.portalsdk.core.onboarding.ueb.UebMsgTypes;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.scheduling.annotation.Async;
29 import org.springframework.stereotype.Component;
30
31 //-------------------------------------------------------------------------
32 // Listens for received UEB messages and handles the messages
33 //
34 // Note: To implement a synchronous reply call getMsgId on the request 
35 //       and putMsgId on the reply (echoing the request MsgId).
36 //       
37 //-------------------------------------------------------------------------
38 @Component("MainUebHandler")
39 public class MainUebHandler {
40
41         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MainUebHandler.class);
42
43
44         ConcurrentLinkedQueue<UebMsg> inboxQueue = null;
45
46         @Autowired
47         WidgetNotificationHandler widgetNotificationHandler;
48
49         @Async
50         public void runHandler(ConcurrentLinkedQueue<UebMsg> queue) {
51                 inboxQueue = queue;
52
53                 logger.info(EELFLoggerDelegate.debugLogger, ("==> MainUebHandler started"));
54
55                 while (true) {
56                         UebMsg msg = null;
57                         while ((msg = inboxQueue.poll()) != null) {
58                                 if (msg.getMsgType() != null) {
59                                         logger.debug(EELFLoggerDelegate.debugLogger, ("<== Received UEB message : " + msg.toString()));
60
61                                         switch (msg.getMsgType()) {
62                                         /*
63                                          * Add your own defined handler objects, use @Component for
64                                          * the class. See WidgetNotificationHandler as an example.
65                                          *
66                                          * Use @Async on methods for performance
67                                          *
68                                          * For syncronous replies use UebManager publishReply and
69                                          * echo back the msgId in your response ie
70                                          * msg.putMsgId(requestMsg.getMsgId())
71                                          *
72                                          * case UebMsgTypes.UEB_MSG_TYPE_XYZ: {
73                                          * XYZHandler.handleMsg(msg); break; }
74                                          */
75                                         case UebMsgTypes.UEB_MSG_TYPE_WIDGET_NOTIFICATION: {
76                                                 widgetNotificationHandler.handleWidgetNotification(msg);
77                                                 break;
78                                         }
79                                         default: {
80                                                 
81                                                 logger.info(EELFLoggerDelegate.debugLogger, ("Unknown message type [" + msg.getMsgType() + "] from " + msg.getSourceTopicName()));
82
83                                                 break;
84                                         }
85                                         }
86                                 }
87                         }
88
89                         if (Thread.interrupted()) {
90                                 
91                                 logger.info(EELFLoggerDelegate.debugLogger, ("==> UebMainHandler exiting"));
92
93                                 break;
94                         }
95
96                         try {
97                                 Thread.sleep(10);
98                         } catch (InterruptedException e) {
99                                 logger.info(EELFLoggerDelegate.debugLogger, ("UebMainHandler interrupted during sleep" + e.getMessage()));
100
101                         }
102                 }
103         }
104 }