2 * ================================================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
20 package org.openecomp.portalapp.portal.ueb;
\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
27 import javax.annotation.PostConstruct;
\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
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
54 @org.springframework.context.annotation.Configuration
\r
55 @EnableAspectJAutoProxy
\r
56 public class EPUebHelper {
\r
57 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);
\r
60 EPAppService appsService;
\r
64 private SessionFactory sessionFactory;
\r
66 @SuppressWarnings("unused")
\r
67 private Publisher epPublisher;
\r
69 public EPUebHelper() {
\r
73 // This should only be called by the ECOMP Portal App, other Apps have just one publisher and use appPublisher
\r
75 @SuppressWarnings("unused")
\r
77 public void refreshPublisherList()
\r
79 Session localSession = null;
\r
80 boolean addedPublisher = false;
\r
83 localSession = sessionFactory.openSession();
\r
85 List<EcompApp> apps = appsService.getEcompAppAppsFullList();
\r
86 for (int i = 0; i < apps.size(); i++)
\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
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
96 else if ((apps.get(i).getId() != 1) && // App may have been disabled, remove the publisher
\r
97 !(apps.get(i).isEnabled()))
\r
99 if(apps.get(i).getUebTopicName()!=null){
\r
100 UebManager.getInstance().removePublisher(apps.get(i).getUebTopicName());
\r
105 catch (Exception e)
\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
111 //publisherList.print();
\r
113 if (addedPublisher == true) // Give publishers time to initialize
\r
121 public void initUeb() {
\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
132 Thread thread = new Thread("EPUebManager: postConstructMethod - refreshPublisherList") {
\r
134 refreshPublisherList();
\r
137 if (thread != null) {
\r
143 public void addPublisher(EPApp app) {
\r
144 // TODO Auto-generated method stub
\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
153 public boolean checkAvailability() {
\r
156 // Test existence of topic at UEB url
\r
158 // (ie http://uebsb91kcdc.it.att.com:3904/topics/ECOMP-PORTAL-INBOX)
\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
166 URL siteURL = new URL(url);
\r
167 HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
\r
168 connection.setRequestMethod("GET");
\r
169 connection.connect();
\r
171 int code = connection.getResponseCode();
\r
176 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, url);
\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
182 catch (Exception e) {
\r
184 String stackTrace = EcompPortalUtils.getStackTrace(e);
\r
185 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the UEB Healthcheck. Details: " + stackTrace);
\r
192 public boolean MessageCanBeSentToTopic() {
\r
194 boolean sentMsgSuccessfully = false;
\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
202 // epPublisher.send(msg);
\r
203 sentMsgSuccessfully = true;
\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
212 return sentMsgSuccessfully;
\r