d006f8eaa219c474a7c80597b0dfea2b368581bf
[portal.git] / ecomp-portal-BE-common / 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 javax.annotation.PostConstruct;
46
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;
69
70 @Component
71 @Transactional
72 @org.springframework.context.annotation.Configuration
73 @EnableAspectJAutoProxy
74 public class EPUebHelper {
75         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUebHelper.class);
76         
77         @Autowired
78         private EPAppService appsService;
79         
80         @Autowired
81         private SessionFactory sessionFactory;
82         
83         @SuppressWarnings("unused")
84         private Publisher epPublisher;
85         
86         public EPUebHelper() {
87                 
88         }
89         //
90         // This should only be called by the ECOMP Portal App, other Apps have just one publisher and use appPublisher
91         //
92         @SuppressWarnings("unused")
93         @EPMetricsLog
94         public void refreshPublisherList()
95         {
96                 Session localSession = null;
97             boolean addedPublisher = false;
98             
99                 try {
100                         localSession = sessionFactory.openSession();
101                 
102                         List<EcompApp> apps = appsService.getEcompAppAppsFullList();
103                         for (int i = 0; i < apps.size(); i++) 
104                         {
105                                 if ((apps.get(i).isEnabled()) &&
106                                         (apps.get(i).getUebTopicName() != null) &&
107                                         !(apps.get(i).getUebTopicName().toUpperCase().contains("ECOMP-PORTAL-INBOX")))
108                                 {
109                                         logger.debug(EELFLoggerDelegate.debugLogger, "UEBManager adding publisher for " + apps.get(i).getUebTopicName());
110                                         UebManager.getInstance().addPublisher(apps.get(i).getUebTopicName());
111                         addedPublisher = true;
112                             }
113                                 else if ((apps.get(i).getId() != 1) && // App may have been disabled, remove the publisher
114                                                  !(apps.get(i).isEnabled()))
115                                 {
116                                         if(apps.get(i).getUebTopicName()!=null){
117                                                 UebManager.getInstance().removePublisher(apps.get(i).getUebTopicName());
118                                         }
119                                 }
120                         }
121                 }
122                 catch (Exception e)
123                 {
124                         EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebSystemError, e, "add/remove Publisher");
125                         logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while refreshing the publisher list", e);
126                 }
127                 
128                 //publisherList.print();
129                 
130                 if (addedPublisher == true) // Give publishers time to initialize
131                 {
132                         Helper.sleep(400);
133                 }
134         }
135         
136         @PostConstruct
137         @EPMetricsLog
138         public void initUeb() {
139                 try {
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);
146                 }
147                 
148                 Thread thread = new Thread("EPUebManager: postConstructMethod - refreshPublisherList") {
149                     public void run(){
150                         refreshPublisherList();
151                     }
152                 };
153                 thread.start();
154                 
155         }
156
157         @EPMetricsLog
158         public void addPublisher(EPApp app) {
159                 // TODO Auto-generated method stub
160                 try {
161                         UebManager.getInstance().addPublisher(app.getUebTopicName());
162                 } catch (UebException e) {
163                         logger.error(EELFLoggerDelegate.errorLogger, "addPublisher failed", e);
164                 }
165         }
166         
167         public boolean checkAvailability() {
168                 //
169                 //  Test existence of topic at UEB url
170                 //
171                 //  (ie http://uebsb91kcdc.it.com:3904/topics/ECOMP-PORTAL-INBOX)
172                 //
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()) {
178                         try {
179                             URL siteURL = new URL(url);
180                             HttpURLConnection connection = (HttpURLConnection) siteURL.openConnection();
181                             connection.setRequestMethod("GET");
182                             connection.connect();
183                      
184                             int code = connection.getResponseCode();
185                             if (code == 200) {
186                                 available = true;
187                             }
188                             else {
189                                 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, url);
190                                 available = false;
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 );
193                             }
194                         }
195                         catch (Exception e) {
196                             available = false;
197                                         logger.error(EELFLoggerDelegate.errorLogger, "checkAvailability failed", e);
198                         }
199                     }
200                 }
201                 return available;
202         }
203         
204     public boolean MessageCanBeSentToTopic() {
205     
206         boolean sentMsgSuccessfully = false;
207         
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);
212         
213             try {
214                    // epPublisher.send(msg);
215                     sentMsgSuccessfully = true;
216             } 
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); 
222             } 
223             
224             return sentMsgSuccessfully;
225     }
226     
227 }
228
229