2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.portal.ueb;
22 import java.net.HttpURLConnection;
24 import java.util.LinkedList;
25 import java.util.List;
27 import javax.annotation.PostConstruct;
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;
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;
54 @org.springframework.context.annotation.Configuration
55 @EnableAspectJAutoProxy
56 public class EPUebHelper {
57 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);
60 EPAppService appsService;
64 private SessionFactory sessionFactory;
66 @SuppressWarnings("unused")
67 private Publisher epPublisher;
69 public EPUebHelper() {
73 // This should only be called by the ECOMP Portal App, other Apps have just one publisher and use appPublisher
75 @SuppressWarnings("unused")
77 public void refreshPublisherList()
79 Session localSession = null;
80 boolean addedPublisher = false;
83 localSession = sessionFactory.openSession();
85 List<EcompApp> apps = appsService.getEcompAppAppsFullList();
86 for (int i = 0; i < apps.size(); i++)
88 if ((apps.get(i).isEnabled()) &&
89 (apps.get(i).getUebTopicName() != null) &&
90 !(apps.get(i).getUebTopicName().toUpperCase().contains("ECOMP-PORTAL-INBOX")))
92 logger.debug(EELFLoggerDelegate.debugLogger, "UEBManager adding publisher for " + apps.get(i).getUebTopicName());
93 UebManager.getInstance().addPublisher(apps.get(i).getUebTopicName());
94 addedPublisher = true;
96 else if ((apps.get(i).getId() != 1) && // App may have been disabled, remove the publisher
97 !(apps.get(i).isEnabled()))
99 if(apps.get(i).getUebTopicName()!=null){
100 UebManager.getInstance().removePublisher(apps.get(i).getUebTopicName());
107 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebSystemError, e, "add/remove Publisher");
108 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while refreshing the publisher list", e);
111 //publisherList.print();
113 if (addedPublisher == true) // Give publishers time to initialize
121 public void initUeb() {
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);
132 Thread thread = new Thread("EPUebManager: postConstructMethod - refreshPublisherList") {
134 refreshPublisherList();
142 public void addPublisher(EPApp app) {
143 // TODO Auto-generated method stub
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);
152 public boolean checkAvailability() {
155 // Test existence of topic at UEB url
157 // (ie http://uebsb91kcdc.it.com:3904/topics/ECOMP-PORTAL-INBOX)
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()) {
165 URL siteURL = new URL(url);
166 HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
167 connection.setRequestMethod("GET");
168 connection.connect();
170 int code = connection.getResponseCode();
175 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, url);
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 );
181 catch (Exception e) {
183 String stackTrace = EcompPortalUtils.getStackTrace(e);
184 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the UEB Healthcheck. Details: " + stackTrace);
191 public boolean MessageCanBeSentToTopic() {
193 boolean sentMsgSuccessfully = false;
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);
201 // epPublisher.send(msg);
202 sentMsgSuccessfully = true;
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);
211 return sentMsgSuccessfully;