2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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============================================
38 package org.onap.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.onap.portalapp.portal.domain.EPApp;
50 import org.onap.portalapp.portal.domain.EcompApp;
51 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
52 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
53 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
54 import org.onap.portalapp.portal.service.EPAppService;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.onap.portalsdk.core.onboarding.ueb.Helper;
57 import org.onap.portalsdk.core.onboarding.ueb.Publisher;
58 import org.onap.portalsdk.core.onboarding.ueb.UebException;
59 import org.onap.portalsdk.core.onboarding.ueb.UebManager;
60 import org.onap.portalsdk.core.onboarding.ueb.UebMsg;
61 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
62 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
63 import org.onap.portalapp.portal.ueb.EPUebHelper;
64 import org.onap.portalapp.portal.ueb.EPUebMsgTypes;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.context.annotation.EnableAspectJAutoProxy;
67 import org.springframework.stereotype.Component;
68 import org.springframework.transaction.annotation.Transactional;
72 @org.springframework.context.annotation.Configuration
73 @EnableAspectJAutoProxy
74 public class EPUebHelper {
75 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);
78 private EPAppService appsService;
81 private SessionFactory sessionFactory;
83 @SuppressWarnings("unused")
84 private Publisher epPublisher;
86 public EPUebHelper() {
90 // This should only be called by the ONAP Portal App, other Apps have just one publisher and use appPublisher
92 @SuppressWarnings("unused")
94 public void refreshPublisherList()
96 Session localSession = null;
97 boolean addedPublisher = false;
100 localSession = sessionFactory.openSession();
102 List<EcompApp> apps = appsService.getEcompAppAppsFullList();
103 for (int i = 0; i < apps.size(); i++)
105 if ((apps.get(i).isEnabled()) &&
106 (apps.get(i).getUebTopicName() != null) &&
107 !(apps.get(i).getUebTopicName().toUpperCase().contains("ECOMP-PORTAL-INBOX")))
109 logger.debug(EELFLoggerDelegate.debugLogger, "UEBManager adding publisher for " + apps.get(i).getUebTopicName());
110 UebManager.getInstance().addPublisher(apps.get(i).getUebTopicName());
111 addedPublisher = true;
113 else if ((apps.get(i).getId() != 1) && // App may have been disabled, remove the publisher
114 !(apps.get(i).isEnabled()))
116 if(apps.get(i).getUebTopicName()!=null){
117 UebManager.getInstance().removePublisher(apps.get(i).getUebTopicName());
124 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebSystemError, e, "add/remove Publisher");
125 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while refreshing the publisher list", e);
128 //publisherList.print();
130 if (addedPublisher == true) // Give publishers time to initialize
138 public void initUeb() {
140 epPublisher = new Publisher(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY),
141 PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET),
142 PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));
143 } catch (Exception e) {
144 logger.error(EELFLoggerDelegate.errorLogger, "initUeb failed", e);
145 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, e);
148 Thread thread = new Thread("EPUebManager: postConstructMethod - refreshPublisherList") {
150 refreshPublisherList();
158 public void addPublisher(EPApp app) {
159 // TODO Auto-generated method stub
161 UebManager.getInstance().addPublisher(app.getUebTopicName());
162 } catch (UebException e) {
163 logger.error(EELFLoggerDelegate.errorLogger, "addPublisher failed", e);
167 public boolean checkAvailability() {
169 // Test existence of topic at UEB url
171 // (ie http://uebsb91kcdc.it.com:3904/topics/ECOMP-PORTAL-INBOX)
173 boolean available = true;
174 LinkedList<String> urlList = (LinkedList<String>) Helper.uebUrlList();
175 if (!urlList.isEmpty()) {
176 String url = "http://" + urlList.getFirst() + ":3904/topics/" + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME);
177 if (!url.isEmpty()) {
179 URL siteURL = new URL(url);
180 HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
181 connection.setRequestMethod("GET");
182 connection.connect();
184 int code = connection.getResponseCode();
189 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, url);
191 logger.warn(EELFLoggerDelegate.errorLogger, "Warning! UEB topic existence check failed, topic = " + url );
192 logger.debug(EELFLoggerDelegate.debugLogger, "Warning! UEB topic existence check failed, topic = " + url );
195 catch (Exception e) {
197 logger.error(EELFLoggerDelegate.errorLogger, "checkAvailability failed", e);
204 public boolean MessageCanBeSentToTopic() {
206 boolean sentMsgSuccessfully = false;
208 UebMsg msg = new UebMsg();
209 msg.putSourceTopicName(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));
210 msg.putPayload("Pinging topic for health check");
211 msg.putMsgType(EPUebMsgTypes.UEB_MSG_TYPE_HEALTH_CHECK);
214 // epPublisher.send(msg);
215 sentMsgSuccessfully = true;
217 catch (Exception e) {
218 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeHealthCheckUebClusterError, e);
219 sentMsgSuccessfully = false;
220 logger.warn(EELFLoggerDelegate.errorLogger, "Warning! could not successfully publish a UEB msg to "
221 + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME), e);
224 return sentMsgSuccessfully;