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 org.hibernate.Session;
46 import org.hibernate.SessionFactory;
47 import org.openecomp.portalapp.portal.domain.EPApp;
48 import org.openecomp.portalapp.portal.domain.EcompApp;
49 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
50 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
51 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
52 import org.openecomp.portalapp.portal.service.EPAppService;
53 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
54 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.openecomp.portalsdk.core.onboarding.ueb.Helper;
56 import org.openecomp.portalsdk.core.onboarding.ueb.Publisher;
57 import org.openecomp.portalsdk.core.onboarding.ueb.UebException;
58 import org.openecomp.portalsdk.core.onboarding.ueb.UebManager;
59 import org.openecomp.portalsdk.core.onboarding.ueb.UebMsg;
60 import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants;
61 import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.context.annotation.EnableAspectJAutoProxy;
64 import org.springframework.stereotype.Component;
65 import org.springframework.transaction.annotation.Transactional;
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
71 public class EPUebHelper {
72 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);
75 EPAppService appsService;
78 private SessionFactory sessionFactory;
80 @SuppressWarnings("unused")
81 private Publisher epPublisher;
83 public EPUebHelper() {
88 // This should only be called by the ECOMP Portal App, other Apps have just one
89 // publisher and use appPublisher
91 @SuppressWarnings("unused")
93 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++) {
102 if ((apps.get(i).isEnabled()) && (apps.get(i).getUebTopicName() != null)
103 && !(apps.get(i).getUebTopicName().toUpperCase().contains("ECOMP-PORTAL-INBOX"))) {
104 logger.debug(EELFLoggerDelegate.debugLogger,
105 "UEBManager adding publisher for " + apps.get(i).getUebTopicName());
106 UebManager.getInstance().addPublisher(apps.get(i).getUebTopicName());
107 addedPublisher = true;
108 } else if ((apps.get(i).getId() != 1) && // App may have been disabled, remove the publisher
109 !(apps.get(i).isEnabled())) {
110 if (apps.get(i).getUebTopicName() != null) {
111 UebManager.getInstance().removePublisher(apps.get(i).getUebTopicName());
115 } catch (Exception e) {
116 EPLogUtil.logEcompError(EPAppMessagesEnum.BeUebSystemError, "add/remove Publisher");
117 logger.error(EELFLoggerDelegate.errorLogger, "refreshPublisherList failed", e);
120 // publisherList.print();
122 if (addedPublisher == true) // Give publishers time to initialize
130 public void initUeb() {
132 epPublisher = new Publisher(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY),
133 PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET),
134 PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));
135 } catch (Exception e) {
136 EPLogUtil.logEcompError(EPAppMessagesEnum.BeUebConnectionError, e.getMessage());
137 logger.error(EELFLoggerDelegate.errorLogger, "initUeb failed", e);
140 Thread thread = new Thread("EPUebManager: postConstructMethod - refreshPublisherList") {
142 refreshPublisherList();
145 if (thread != null) {
151 public void addPublisher(EPApp app) {
152 // TODO Auto-generated method stub
154 UebManager.getInstance().addPublisher(app.getUebTopicName());
155 } catch (UebException e) {
156 logger.error(EELFLoggerDelegate.errorLogger, "addPublisher failed", e);
160 public boolean checkAvailability() {
163 // Test existence of topic at UEB url
167 boolean available = true;
168 LinkedList<String> urlList = Helper.uebUrlList();
169 if (!urlList.isEmpty()) {
170 String url = "http://" + urlList.getFirst() + ":3904/topics/"
171 + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME);
172 if (!url.isEmpty()) {
174 URL siteURL = new URL(url);
175 HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
176 connection.setRequestMethod("GET");
177 connection.connect();
179 int code = connection.getResponseCode();
183 EPLogUtil.logEcompError(EPAppMessagesEnum.BeUebConnectionError, url);
185 logger.warn(EELFLoggerDelegate.errorLogger,
186 "Warning! UEB topic existence check failed, topic = " + url);
187 logger.debug(EELFLoggerDelegate.debugLogger,
188 "Warning! UEB topic existence check failed, topic = " + url);
190 } catch (Exception e) {
192 logger.error(EELFLoggerDelegate.errorLogger, "checkAvailability failed", e);
199 public boolean MessageCanBeSentToTopic() {
201 boolean sentMsgSuccessfully = false;
203 UebMsg msg = new UebMsg();
204 msg.putSourceTopicName(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));
205 msg.putPayload("Pinging topic for health check");
206 msg.putMsgType(EPUebMsgTypes.UEB_MSG_TYPE_HEALTH_CHECK);
209 // epPublisher.send(msg);
210 sentMsgSuccessfully = true;
211 } catch (Exception e) {
212 EPLogUtil.logEcompError(EPAppMessagesEnum.BeHealthCheckUebClusterError);
213 sentMsgSuccessfully = false;
214 logger.warn(EELFLoggerDelegate.errorLogger, "Warning! could not successfully publish a UEB msg to "
215 + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME), e);
218 return sentMsgSuccessfully;