81b1c495b4b43c33426eb2f7df685f984ad7a817
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / ueb / EPUebHelper.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.ueb;
39
40 import java.net.HttpURLConnection;
41 import java.net.URL;
42 import java.util.LinkedList;
43 import java.util.List;
44
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;
66
67 @Component
68 @Transactional
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
71 public class EPUebHelper {
72         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);
73
74         @Autowired
75         EPAppService appsService;
76
77         @Autowired
78         private SessionFactory sessionFactory;
79
80         @SuppressWarnings("unused")
81         private Publisher epPublisher;
82
83         public EPUebHelper() {
84
85         }
86
87         //
88         // This should only be called by the ECOMP Portal App, other Apps have just one
89         // publisher and use appPublisher
90         //
91         @SuppressWarnings("unused")
92         @EPMetricsLog
93         public void refreshPublisherList() {
94                 Session localSession = null;
95                 boolean addedPublisher = false;
96
97                 try {
98                         localSession = sessionFactory.openSession();
99
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());
112                                         }
113                                 }
114                         }
115                 } catch (Exception e) {
116                         EPLogUtil.logEcompError(EPAppMessagesEnum.BeUebSystemError, "add/remove Publisher");
117                         logger.error(EELFLoggerDelegate.errorLogger, "refreshPublisherList failed", e);
118                 }
119
120                 // publisherList.print();
121
122                 if (addedPublisher == true) // Give publishers time to initialize
123                 {
124                         Helper.sleep(400);
125                 }
126         }
127
128         // @PostConstruct
129         // @EPMetricsLog
130         public void initUeb() {
131                 try {
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);
138                 }
139
140                 Thread thread = new Thread("EPUebManager: postConstructMethod - refreshPublisherList") {
141                         public void run() {
142                                 refreshPublisherList();
143                         }
144                 };
145                 if (thread != null) {
146                         thread.start();
147                 }
148         }
149
150         @EPMetricsLog
151         public void addPublisher(EPApp app) {
152                 // TODO Auto-generated method stub
153                 try {
154                         UebManager.getInstance().addPublisher(app.getUebTopicName());
155                 } catch (UebException e) {
156                         logger.error(EELFLoggerDelegate.errorLogger, "addPublisher failed", e);
157                 }
158         }
159
160         public boolean checkAvailability() {
161
162                 //
163                 // Test existence of topic at UEB url
164                 //
165                 //
166                 //
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()) {
173                                 try {
174                                         URL siteURL = new URL(url);
175                                         HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
176                                         connection.setRequestMethod("GET");
177                                         connection.connect();
178
179                                         int code = connection.getResponseCode();
180                                         if (code == 200) {
181                                                 available = true;
182                                         } else {
183                                                 EPLogUtil.logEcompError(EPAppMessagesEnum.BeUebConnectionError, url);
184                                                 available = false;
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);
189                                         }
190                                 } catch (Exception e) {
191                                         available = false;
192                                         logger.error(EELFLoggerDelegate.errorLogger, "checkAvailability failed", e);
193                                 }
194                         }
195                 }
196                 return available;
197         }
198
199         public boolean MessageCanBeSentToTopic() {
200
201                 boolean sentMsgSuccessfully = false;
202
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);
207
208                 try {
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);
216                 }
217
218                 return sentMsgSuccessfully;
219         }
220
221 }