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.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.util.PortalApiConstants;
40 import org.openecomp.portalsdk.core.onboarding.util.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;
53 @org.springframework.context.annotation.Configuration
54 @EnableAspectJAutoProxy
55 public class EPUebHelper {
56 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);
59 EPAppService appsService;
63 private SessionFactory sessionFactory;
65 @SuppressWarnings("unused")
66 private Publisher epPublisher;
68 public EPUebHelper() {
72 // This should only be called by the ECOMP Portal App, other Apps have just one publisher and use appPublisher
74 @SuppressWarnings("unused")
76 public void refreshPublisherList()
78 Session localSession = null;
79 boolean addedPublisher = false;
82 localSession = sessionFactory.openSession();
84 List<EcompApp> apps = appsService.getEcompAppAppsFullList();
85 for (int i = 0; i < apps.size(); i++)
87 if ((apps.get(i).isEnabled()) &&
88 (apps.get(i).getUebTopicName() != null) &&
89 !(apps.get(i).getUebTopicName().toUpperCase().contains("ECOMP-PORTAL-INBOX")))
91 logger.debug(EELFLoggerDelegate.debugLogger, "UEBManager adding publisher for " + apps.get(i).getUebTopicName());
92 UebManager.getInstance().addPublisher(apps.get(i).getUebTopicName());
93 addedPublisher = true;
95 else if ((apps.get(i).getId() != 1) && // App may have been disabled, remove the publisher
96 !(apps.get(i).isEnabled()))
98 if(apps.get(i).getUebTopicName()!=null){
99 UebManager.getInstance().removePublisher(apps.get(i).getUebTopicName());
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);
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(EPAppMessagesEnum.BeUebConnectionError, e.getMessage());
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
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(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(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);
213 return sentMsgSuccessfully;