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();
137 if (thread != null) {
143 public void addPublisher(EPApp app) {
144 // TODO Auto-generated method stub
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);
153 public boolean checkAvailability() {
156 // Test existence of topic at UEB url
158 // (ie http://uebsb91kcdc.it.att.com:3904/topics/ECOMP-PORTAL-INBOX)
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()) {
166 URL siteURL = new URL(url);
167 HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
168 connection.setRequestMethod("GET");
169 connection.connect();
171 int code = connection.getResponseCode();
176 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, url);
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 );
182 catch (Exception e) {
184 String stackTrace = EcompPortalUtils.getStackTrace(e);
185 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the UEB Healthcheck. Details: " + stackTrace);
192 public boolean MessageCanBeSentToTopic() {
194 boolean sentMsgSuccessfully = false;
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);
202 // epPublisher.send(msg);
203 sentMsgSuccessfully = true;
205 catch (Exception e) {
206 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckUebClusterError, e);
207 sentMsgSuccessfully = false;
208 logger.warn(EELFLoggerDelegate.errorLogger, "Warning! could not successfully publish a UEB msg to "
209 + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME), e);
212 return sentMsgSuccessfully;