[POLICY-122] Policy GUI Fixes
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / notifications / NotificationServer.java
index 136f222..9bd2e1f 100644 (file)
@@ -42,6 +42,7 @@ import org.openecomp.policy.common.logging.eelf.MessageCodes;
 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
 import org.openecomp.policy.common.logging.flexlogger.Logger;
+import org.openecomp.policy.pdp.rest.api.services.NotificationService;
 import org.openecomp.policy.rest.XACMLRestProperties;
 import org.openecomp.policy.utils.BusPublisher;
 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
@@ -63,7 +64,7 @@ import com.att.research.xacml.util.XACMLProperties;
 @ServerEndpoint(value = "/notifications")
 public class NotificationServer {
        private static final Logger LOGGER      = FlexLogger.getLogger(NotificationServer.class);
-       private static Queue<Session> queue = new ConcurrentLinkedQueue<Session>();
+       private static Queue<Session> queue = new ConcurrentLinkedQueue<>();
        private static String update = null;
        private static  String hosts = null;
        private static URL aURL = null;
@@ -94,7 +95,7 @@ public class NotificationServer {
                                session.getBasicRemote().sendText(update);
                                session.close();
                        } catch (IOException e) {
-                               LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage());
+                               LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage() + e);
                                LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending Message update");
                        }       
                }
@@ -104,7 +105,7 @@ public class NotificationServer {
 
                LOGGER.debug("Notification set to " + propNotificationType);
                if (propNotificationType.equals("ueb")){
-                       
+
                        String topic = null;
                        try {
                                aURL = new URL(pdpURL);
@@ -118,7 +119,7 @@ public class NotificationServer {
                        hosts = XACMLProperties.getProperty(XACMLRestProperties.PROP_NOTIFICATION_SERVERS);
                        String apiKey = XACMLProperties.getProperty(XACMLRestProperties.PROP_UEB_API_KEY);
                        String apiSecret = XACMLProperties.getProperty(XACMLRestProperties.PROP_UEB_API_SECRET);
-                       
+
                        LOGGER.debug("Creating Publisher for host: " + hosts + " with topic: " + topic);
                        CambriaBatchingPublisher pub = null;
                        try {
@@ -126,7 +127,7 @@ public class NotificationServer {
                                        LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
                                        throw new Exception(XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
                                }
-                               
+
                                hosts.trim();
                                topic.trim();
                                apiKey.trim();
@@ -136,34 +137,33 @@ public class NotificationServer {
                                                .onTopic ( topic )
                                                .authenticatedBy ( apiKey, apiSecret )
                                                .build ()
-                                       ;
-                               
+                                               ;
+
                        } catch (MalformedURLException e1) {
                                LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error creating the UEB publisher" + e1.getMessage());
                        } catch (GeneralSecurityException e1) {
-                               LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error creating the UEB publisher" + e1.getMessage());
+                               LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error creating the UEB publisher" + e1.getMessage() +e1);
                        }
+                       if(pub != null){
+                               try {
+                                       pub.send( "MyPartitionKey", notification );
+                               } catch (IOException e) {
+                                       LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending notification update" + e.getMessage() + e);
+                               }
+                               // close the publisher. The batching publisher does not send events
+                               // immediately, so you MUST use close to send any remaining messages.
+                               // You provide the amount of time you're willing to wait for the sends
+                               // to succeed before giving up. If any messages are unsent after that time,
+                               // they're returned to your app. You could, for example, persist to disk
+                               // and try again later.
+                               final List<?> stuck = pub.close ( 20, TimeUnit.SECONDS );
 
-                       try {
-                               pub.send( "MyPartitionKey", notification );
-                       } catch (IOException e) {
-                               LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending notification update" + e.getMessage());
-                       }
-                       
-                       // close the publisher. The batching publisher does not send events
-                       // immediately, so you MUST use close to send any remaining messages.
-                       // You provide the amount of time you're willing to wait for the sends
-                       // to succeed before giving up. If any messages are unsent after that time,
-                       // they're returned to your app. You could, for example, persist to disk
-                       // and try again later.
-                       final List<?> stuck = pub.close ( 20, TimeUnit.SECONDS );
-                       
-                       if ( stuck.size () > 0 ){
-                               LOGGER.error( stuck.size() + " messages unsent" );
-                       }else{
-                               LOGGER.info( "Clean exit; all messages sent: " + notification );
+                               if (!stuck.isEmpty()){
+                                       LOGGER.error( stuck.size() + " messages unsent" );
+                               }else{
+                                       LOGGER.info( "Clean exit; all messages sent: " + notification );
+                               }
                        }
-                       
                } else if (propNotificationType.equals("dmaap")) {
                        
                        // Setting up the Publisher for DMaaP MR
@@ -185,7 +185,7 @@ public class NotificationServer {
                                
                                List<String> dmaapList = null;
                                if(dmaapServers.contains(",")) {
-                                       dmaapList = new ArrayList<String>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
+                                       dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
                                } else {
                                        dmaapList = new ArrayList<>();
                                        dmaapList.add(dmaapServers);
@@ -203,7 +203,7 @@ public class NotificationServer {
                                publisher.close();
                                
                        } catch (Exception e) {
-                               LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending notification update" + e.getMessage());
+                               LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error sending notification update" + e.getMessage() + e);
                        }
                }
                
@@ -211,12 +211,14 @@ public class NotificationServer {
                        try {
                                session.getBasicRemote().sendText(notification);
                        } catch (IOException e) {
-                               LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage());
+                               LOGGER.info(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error in sending the Event Notification: "+ e.getMessage() + e);
                        }
                }
+               NotificationService.sendNotification(notification);
        }
-       
+
        public static void setUpdate(String update) {
                NotificationServer.update = update;
        }
+       
 }