5c92b5ddc673204eb5289cb76ffee1cecb6afd26
[portal.git] / ecomp-portal-BE-os / src / main / java / org / onap / portalapp / portal / ueb / EPUebHelper.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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.onap.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.onap.portalapp.portal.domain.EPApp;
48 import org.onap.portalapp.portal.domain.EcompApp;
49 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
50 import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
51 import org.onap.portalapp.portal.logging.logic.EPLogUtil;
52 import org.onap.portalapp.portal.service.EPAppService;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalsdk.core.onboarding.ueb.Helper;
55 import org.onap.portalsdk.core.onboarding.ueb.Publisher;
56 import org.onap.portalsdk.core.onboarding.ueb.UebException;
57 import org.onap.portalsdk.core.onboarding.ueb.UebManager;
58 import org.onap.portalsdk.core.onboarding.ueb.UebMsg;
59 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
60 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.context.annotation.EnableAspectJAutoProxy;
63 import org.springframework.stereotype.Component;
64 import org.springframework.transaction.annotation.Transactional;
65
66 @Component
67 @Transactional
68 @org.springframework.context.annotation.Configuration
69 @EnableAspectJAutoProxy
70 public class EPUebHelper {
71         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);
72
73         @Autowired
74         EPAppService appsService;
75
76         @Autowired
77         private SessionFactory sessionFactory;
78
79         @SuppressWarnings("unused")
80         private Publisher epPublisher;
81
82         public EPUebHelper() {
83
84         }
85
86         //
87         // This should only be called by the ECOMP Portal App, other Apps have just one
88         // publisher and use appPublisher
89         //
90         @SuppressWarnings("unused")
91         @EPMetricsLog
92         public void refreshPublisherList() {
93                 Session localSession = null;
94                 boolean addedPublisher = false;
95
96                 try {
97                         localSession = sessionFactory.openSession();
98
99                         List<EcompApp> apps = appsService.getEcompAppAppsFullList();
100                         for (int i = 0; i < apps.size(); i++) {
101                                 if ((apps.get(i).isEnabled()) && (apps.get(i).getUebTopicName() != null)
102                                                 && !(apps.get(i).getUebTopicName().toUpperCase().contains("ECOMP-PORTAL-INBOX"))) {
103                                         logger.debug(EELFLoggerDelegate.debugLogger,
104                                                         "UEBManager adding publisher for " + apps.get(i).getUebTopicName());
105                                         UebManager.getInstance().addPublisher(apps.get(i).getUebTopicName());
106                                         addedPublisher = true;
107                                 } else if ((apps.get(i).getId() != 1) && // App may have been disabled, remove the publisher
108                                                 !(apps.get(i).isEnabled())) {
109                                         if (apps.get(i).getUebTopicName() != null) {
110                                                 UebManager.getInstance().removePublisher(apps.get(i).getUebTopicName());
111                                         }
112                                 }
113                         }
114                 } catch (Exception e) {
115                         EPLogUtil.logEcompError(EPAppMessagesEnum.BeUebSystemError, "add/remove Publisher");
116                         logger.error(EELFLoggerDelegate.errorLogger, "refreshPublisherList failed", e);
117                 }
118
119                 // publisherList.print();
120
121                 if (addedPublisher == true) // Give publishers time to initialize
122                 {
123                         Helper.sleep(400);
124                 }
125         }
126
127         // @PostConstruct
128         // @EPMetricsLog
129         public void initUeb() {
130                 try {
131                         epPublisher = new Publisher(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY),
132                                         PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET),
133                                         PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));
134                 } catch (Exception e) {
135                         EPLogUtil.logEcompError(EPAppMessagesEnum.BeUebConnectionError, e.getMessage());
136                         logger.error(EELFLoggerDelegate.errorLogger, "initUeb failed", e);
137                 }
138
139                 Thread thread = new Thread("EPUebManager: postConstructMethod - refreshPublisherList") {
140                         public void run() {
141                                 refreshPublisherList();
142                         }
143                 };
144                 if (thread != null) {
145                         thread.start();
146                 }
147         }
148
149         @EPMetricsLog
150         public void addPublisher(EPApp app) {
151                 // TODO Auto-generated method stub
152                 try {
153                         UebManager.getInstance().addPublisher(app.getUebTopicName());
154                 } catch (UebException e) {
155                         logger.error(EELFLoggerDelegate.errorLogger, "addPublisher failed", e);
156                 }
157         }
158
159         public boolean checkAvailability() {
160
161                 //
162                 // Test existence of topic at UEB url
163                 //
164                 //
165                 //
166                 boolean available = true;
167                 LinkedList<String> urlList = (LinkedList<String>) Helper.uebUrlList();
168                 if (!urlList.isEmpty()) {
169                         String url = "http://" + urlList.getFirst() + ":3904/topics/"
170                                         + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME);
171                         if (!url.isEmpty()) {
172                                 try {
173                                         URL siteURL = new URL(url);
174                                         HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
175                                         connection.setRequestMethod("GET");
176                                         connection.connect();
177
178                                         int code = connection.getResponseCode();
179                                         if (code == 200) {
180                                                 available = true;
181                                         } else {
182                                                 EPLogUtil.logEcompError(EPAppMessagesEnum.BeUebConnectionError, url);
183                                                 available = false;
184                                                 logger.warn(EELFLoggerDelegate.errorLogger,
185                                                                 "Warning! UEB topic existence check failed, topic = " + url);
186                                                 logger.debug(EELFLoggerDelegate.debugLogger,
187                                                                 "Warning! UEB topic existence check failed, topic = " + url);
188                                         }
189                                 } catch (Exception e) {
190                                         available = false;
191                                         logger.error(EELFLoggerDelegate.errorLogger, "checkAvailability failed", e);
192                                 }
193                         }
194                 }
195                 return available;
196         }
197
198         public boolean MessageCanBeSentToTopic() {
199
200                 boolean sentMsgSuccessfully = false;
201
202                 UebMsg msg = new UebMsg();
203                 msg.putSourceTopicName(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));
204                 msg.putPayload("Pinging topic for health check");
205                 msg.putMsgType(EPUebMsgTypes.UEB_MSG_TYPE_HEALTH_CHECK);
206
207                 try {
208                         // epPublisher.send(msg);
209                         sentMsgSuccessfully = true;
210                 } catch (Exception e) {
211                         EPLogUtil.logEcompError(EPAppMessagesEnum.BeHealthCheckUebClusterError);
212                         sentMsgSuccessfully = false;
213                         logger.warn(EELFLoggerDelegate.errorLogger, "Warning! could not successfully publish a UEB msg to "
214                                         + PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME), e);
215                 }
216
217                 return sentMsgSuccessfully;
218         }
219
220 }