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