2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the “License”);
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.portalapp.portal.ueb;
40 import java.net.HttpURLConnection;
42 import java.util.LinkedList;
43 import java.util.List;
45 import javax.annotation.PostConstruct;
47 import org.hibernate.Session;
48 import org.hibernate.SessionFactory;
49 import org.openecomp.portalapp.portal.domain.EPApp;
50 import org.openecomp.portalapp.portal.domain.EcompApp;
51 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
52 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
53 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
54 import org.openecomp.portalapp.portal.service.EPAppService;
55 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.openecomp.portalsdk.core.onboarding.ueb.Helper;
57 import org.openecomp.portalsdk.core.onboarding.ueb.Publisher;
58 import org.openecomp.portalsdk.core.onboarding.ueb.UebException;
59 import org.openecomp.portalsdk.core.onboarding.ueb.UebManager;
60 import org.openecomp.portalsdk.core.onboarding.ueb.UebMsg;
61 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
62 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.context.annotation.EnableAspectJAutoProxy;
65 import org.springframework.stereotype.Component;
66 import org.springframework.transaction.annotation.Transactional;
70 @org.springframework.context.annotation.Configuration
71 @EnableAspectJAutoProxy
72 public class EPUebHelper {
73 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);
76 private EPAppService appsService;
79 private SessionFactory sessionFactory;
81 @SuppressWarnings("unused")
82 private Publisher epPublisher;
84 public EPUebHelper() {
88 // This should only be called by the ECOMP Portal App, other Apps have just one publisher and use appPublisher
90 @SuppressWarnings("unused")
92 public void refreshPublisherList()
94 Session localSession = null;
95 boolean addedPublisher = false;
98 localSession = sessionFactory.openSession();
100 List<EcompApp> apps = appsService.getEcompAppAppsFullList();
101 for (int i = 0; i < apps.size(); i++)
103 if ((apps.get(i).isEnabled()) &&
104 (apps.get(i).getUebTopicName() != null) &&
105 !(apps.get(i).getUebTopicName().toUpperCase().contains("ECOMP-PORTAL-INBOX")))
107 logger.debug(EELFLoggerDelegate.debugLogger, "UEBManager adding publisher for " + apps.get(i).getUebTopicName());
108 UebManager.getInstance().addPublisher(apps.get(i).getUebTopicName());
109 addedPublisher = true;
111 else if ((apps.get(i).getId() != 1) && // App may have been disabled, remove the publisher
112 !(apps.get(i).isEnabled()))
114 if(apps.get(i).getUebTopicName()!=null){
115 UebManager.getInstance().removePublisher(apps.get(i).getUebTopicName());
122 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebSystemError, e, "add/remove Publisher");
123 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while refreshing the publisher list", e);
126 //publisherList.print();
128 if (addedPublisher == true) // Give publishers time to initialize
136 public void initUeb() {
138 epPublisher = new Publisher(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY),
139 PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET),
140 PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));
141 } catch (Exception e) {
142 logger.error(EELFLoggerDelegate.errorLogger, "initUeb failed", e);
143 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, e);
146 Thread thread = new Thread("EPUebManager: postConstructMethod - refreshPublisherList") {
148 refreshPublisherList();
156 public void addPublisher(EPApp app) {
157 // TODO Auto-generated method stub
159 UebManager.getInstance().addPublisher(app.getUebTopicName());
160 } catch (UebException e) {
161 logger.error(EELFLoggerDelegate.errorLogger, "addPublisher failed", e);
165 public boolean checkAvailability() {
167 // Test existence of topic at UEB url
169 // (ie http://uebsb91kcdc.it.com:3904/topics/ECOMP-PORTAL-INBOX)
171 boolean available = true;
172 LinkedList<String> urlList = Helper.uebUrlList();
173 if (!urlList.isEmpty()) {
174 String url = "http://" + urlList.getFirst() + ":3904/topics/" + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME);
175 if (!url.isEmpty()) {
177 URL siteURL = new URL(url);
178 HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
179 connection.setRequestMethod("GET");
180 connection.connect();
182 int code = connection.getResponseCode();
187 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, url);
189 logger.warn(EELFLoggerDelegate.errorLogger, "Warning! UEB topic existence check failed, topic = " + url );
190 logger.debug(EELFLoggerDelegate.debugLogger, "Warning! UEB topic existence check failed, topic = " + url );
193 catch (Exception e) {
195 logger.error(EELFLoggerDelegate.errorLogger, "checkAvailability failed", e);
202 public boolean MessageCanBeSentToTopic() {
204 boolean sentMsgSuccessfully = false;
206 UebMsg msg = new UebMsg();
207 msg.putSourceTopicName(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));
208 msg.putPayload("Pinging topic for health check");
209 msg.putMsgType(EPUebMsgTypes.UEB_MSG_TYPE_HEALTH_CHECK);
212 // epPublisher.send(msg);
213 sentMsgSuccessfully = true;
215 catch (Exception e) {
216 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckUebClusterError, e);
217 sentMsgSuccessfully = false;
218 logger.warn(EELFLoggerDelegate.errorLogger, "Warning! could not successfully publish a UEB msg to "
219 + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME), e);
222 return sentMsgSuccessfully;