[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / ueb / EPUebHelper.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.ueb;\r
21 \r
22 import java.net.HttpURLConnection;\r
23 import java.net.URL;\r
24 import java.util.LinkedList;\r
25 import java.util.List;\r
26 \r
27 import javax.annotation.PostConstruct;\r
28 \r
29 import org.hibernate.Session;\r
30 import org.hibernate.SessionFactory;\r
31 import org.springframework.beans.factory.annotation.Autowired;\r
32 import org.springframework.context.annotation.EnableAspectJAutoProxy;\r
33 import org.springframework.stereotype.Component;\r
34 import org.springframework.transaction.annotation.Transactional;\r
35 \r
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
37 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;\r
38 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;\r
39 import org.openecomp.portalsdk.core.onboarding.ueb.Helper;\r
40 import org.openecomp.portalsdk.core.onboarding.ueb.Publisher;\r
41 import org.openecomp.portalsdk.core.onboarding.ueb.UebException;\r
42 import org.openecomp.portalsdk.core.onboarding.ueb.UebManager;\r
43 import org.openecomp.portalsdk.core.onboarding.ueb.UebMsg;\r
44 import org.openecomp.portalapp.portal.domain.EPApp;\r
45 import org.openecomp.portalapp.portal.domain.EcompApp;\r
46 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;\r
47 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;\r
48 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;\r
49 import org.openecomp.portalapp.portal.service.EPAppService;\r
50 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;\r
51 \r
52 @Component\r
53 @Transactional\r
54 @org.springframework.context.annotation.Configuration\r
55 @EnableAspectJAutoProxy\r
56 public class EPUebHelper {\r
57         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);\r
58         \r
59         @Autowired\r
60         EPAppService appsService;\r
61         \r
62         \r
63         @Autowired\r
64         private SessionFactory sessionFactory;\r
65         \r
66         @SuppressWarnings("unused")\r
67         private Publisher epPublisher;\r
68         \r
69         public EPUebHelper() {\r
70                 \r
71         }\r
72         //\r
73         // This should only be called by the ECOMP Portal App, other Apps have just one publisher and use appPublisher\r
74         //\r
75         @SuppressWarnings("unused")\r
76         @EPMetricsLog\r
77         public void refreshPublisherList()\r
78         {\r
79                 Session localSession = null;\r
80             boolean addedPublisher = false;\r
81             \r
82                 try {\r
83                         localSession = sessionFactory.openSession();\r
84                 \r
85                         List<EcompApp> apps = appsService.getEcompAppAppsFullList();\r
86                         for (int i = 0; i < apps.size(); i++) \r
87                         {\r
88                                 if ((apps.get(i).isEnabled()) &&\r
89                                         (apps.get(i).getUebTopicName() != null) &&\r
90                                         !(apps.get(i).getUebTopicName().toUpperCase().contains("ECOMP-PORTAL-INBOX")))\r
91                                 {\r
92                                         logger.debug(EELFLoggerDelegate.debugLogger, "UEBManager adding publisher for " + apps.get(i).getUebTopicName());\r
93                                         UebManager.getInstance().addPublisher(apps.get(i).getUebTopicName());\r
94                         addedPublisher = true;\r
95                             }\r
96                                 else if ((apps.get(i).getId() != 1) && // App may have been disabled, remove the publisher\r
97                                                  !(apps.get(i).isEnabled()))\r
98                                 {\r
99                                         if(apps.get(i).getUebTopicName()!=null){\r
100                                                 UebManager.getInstance().removePublisher(apps.get(i).getUebTopicName());\r
101                                         }\r
102                                 }\r
103                         }\r
104                 }\r
105                 catch (Exception e)\r
106                 {\r
107                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebSystemError, e, "add/remove Publisher");\r
108                         logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while refreshing the publisher list", e);\r
109                 }\r
110                 \r
111                 //publisherList.print();\r
112                 \r
113                 if (addedPublisher == true) // Give publishers time to initialize\r
114                 {\r
115                         Helper.sleep(400);\r
116                 }\r
117         }\r
118         \r
119         @PostConstruct\r
120         @EPMetricsLog\r
121         public void initUeb() {\r
122                 try {\r
123                         epPublisher = new Publisher(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY), \r
124                                                             PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET), \r
125                                                             PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));\r
126                 } catch (Exception e) {\r
127                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, e);\r
128                         String stackTrace = EcompPortalUtils.getStackTrace(e);\r
129                         logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while initializing the publisher. Details: " + stackTrace);\r
130                 }\r
131                 \r
132                 Thread thread = new Thread("EPUebManager: postConstructMethod - refreshPublisherList") {\r
133                     public void run(){\r
134                         refreshPublisherList();\r
135                     }\r
136                 };\r
137                 if (thread != null) {\r
138                     thread.start();\r
139                 }\r
140         }\r
141 \r
142         @EPMetricsLog\r
143         public void addPublisher(EPApp app) {\r
144                 // TODO Auto-generated method stub\r
145                 try {\r
146                         UebManager.getInstance().addPublisher(app.getUebTopicName());\r
147                 } catch (UebException e) {\r
148                         String stackTrace = EcompPortalUtils.getStackTrace(e);\r
149                         logger.error(EELFLoggerDelegate.errorLogger, "Exception while adding a publisher. Details: " + stackTrace);\r
150                 }\r
151         }\r
152         \r
153         public boolean checkAvailability() {\r
154                 \r
155                 //\r
156                 //  Test existence of topic at UEB url\r
157                 //\r
158                 //  (ie http://uebsb91kcdc.it.att.com:3904/topics/ECOMP-PORTAL-INBOX)\r
159                 //\r
160                 boolean available = true;\r
161                 LinkedList<String> urlList = Helper.uebUrlList();\r
162                 if (!urlList.isEmpty()) {\r
163                     String url = "http://" + urlList.getFirst() + ":3904/topics/" + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME);\r
164                     if (!url.isEmpty()) {\r
165                         try {\r
166                             URL siteURL = new URL(url);\r
167                             HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();\r
168                             connection.setRequestMethod("GET");\r
169                             connection.connect();\r
170                      \r
171                             int code = connection.getResponseCode();\r
172                             if (code == 200) {\r
173                                 available = true;\r
174                             }\r
175                             else {\r
176                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, url);\r
177                                 available = false;\r
178                                 logger.warn(EELFLoggerDelegate.errorLogger, "Warning! UEB topic existence check failed, topic = " + url );\r
179                                 logger.debug(EELFLoggerDelegate.debugLogger, "Warning! UEB topic existence check failed, topic = " + url );\r
180                             }\r
181                         }\r
182                         catch (Exception e) {\r
183                             available = false;\r
184                             String stackTrace = EcompPortalUtils.getStackTrace(e);\r
185                                         logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the UEB Healthcheck. Details: " + stackTrace);\r
186                         }\r
187                     }\r
188                 }\r
189                 return available;\r
190         }\r
191         \r
192     public boolean MessageCanBeSentToTopic() {\r
193     \r
194         boolean sentMsgSuccessfully = false;\r
195         \r
196             UebMsg msg = new UebMsg();\r
197             msg.putSourceTopicName(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));\r
198             msg.putPayload("Pinging topic for health check");\r
199             msg.putMsgType(EPUebMsgTypes.UEB_MSG_TYPE_HEALTH_CHECK);\r
200         \r
201             try {\r
202                    // epPublisher.send(msg);\r
203                     sentMsgSuccessfully = true;\r
204             } \r
205             catch (Exception e) {\r
206                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckUebClusterError, e);\r
207                     sentMsgSuccessfully = false;\r
208                     logger.warn(EELFLoggerDelegate.errorLogger, "Warning! could not successfully publish a UEB msg to " \r
209                                      + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME), e); \r
210             } \r
211             \r
212             return sentMsgSuccessfully;\r
213     }\r
214     \r
215 }\r
216 \r
217 \r