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