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