package org.openstack.atlas.api.async; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.openstack.atlas.service.domain.entities.LoadBalancer; import org.openstack.atlas.service.domain.entities.LoadBalancerStatus; import org.openstack.atlas.service.domain.events.entities.EventSeverity; import org.openstack.atlas.service.domain.exceptions.EntityNotFoundException; import javax.jms.Message; import static org.openstack.atlas.service.domain.events.entities.CategoryType.UPDATE; import static org.openstack.atlas.service.domain.events.entities.EventSeverity.CRITICAL; import static org.openstack.atlas.service.domain.events.entities.EventType.UPDATE_CONNECTION_LOGGING; import static org.openstack.atlas.service.domain.services.helpers.AlertType.DATABASE_FAILURE; import static org.openstack.atlas.service.domain.services.helpers.AlertType.ZEUS_FAILURE; public class UpdateConnectionLoggingListener extends BaseListener { final Log LOG = LogFactory.getLog(UpdateConnectionLoggingListener.class); private static final String UPDATE_LOGGING_TITLE = "Update Connection Logging"; @Override public void doOnMessage(Message message) throws Exception { LOG.debug("Entering " + getClass()); LOG.debug(message); LoadBalancer queueLb = getLoadbalancerFromMessage(message); LoadBalancer dbLoadBalancer; try { dbLoadBalancer = loadBalancerService.get(queueLb.getId(), queueLb.getAccountId()); } catch (EntityNotFoundException enfe) { String alertDescription = String.format("Load balancer '%d' not found in database.", queueLb.getId()); LOG.error(alertDescription, enfe); notificationService.saveAlert(queueLb.getAccountId(), queueLb.getId(), enfe, DATABASE_FAILURE.name(), alertDescription); sendErrorToEventResource(queueLb); return; } try { if (isRestAdapter()) { LOG.debug(String.format("Updating connection logging for load balancer '%d' in STM...", dbLoadBalancer.getId())); reverseProxyLoadBalancerStmService.updateLoadBalancer(dbLoadBalancer, queueLb, loadBalancerService.getUserPages(queueLb.getId(), queueLb.getAccountId())); LOG.debug(String.format("Successfully updated connection logging for load balancer '%d' in Zeus.", dbLoadBalancer.getId())); } else { LOG.debug(String.format("Updating connection logging for load balancer '%d' in ZXTM...", dbLoadBalancer.getId())); reverseProxyLoadBalancerService.updateConnectionLogging(dbLoadBalancer); LOG.debug(String.format("Successfully updated connection logging for load balancer '%d' in Zeus.", dbLoadBalancer.getId())); } } catch (Exception e) { loadBalancerService.setStatus(dbLoadBalancer, LoadBalancerStatus.ERROR); String alertDescription = String.format("Error updating connection logging for load balancer '%d' in Zeus.", dbLoadBalancer.getId()); LOG.error(alertDescription, e); notificationService.saveAlert(dbLoadBalancer.getAccountId(), dbLoadBalancer.getId(), e, ZEUS_FAILURE.name(), alertDescription); sendErrorToEventResource(queueLb); return; } String desc = "Connection logging successully set to " + dbLoadBalancer.isConnectionLogging().toString(); notificationService.saveLoadBalancerEvent(queueLb.getUserName(), dbLoadBalancer.getAccountId(), dbLoadBalancer.getId(), UPDATE_LOGGING_TITLE, desc, UPDATE_CONNECTION_LOGGING, UPDATE, EventSeverity.INFO); // Update load balancer status in DB loadBalancerService.setStatus(dbLoadBalancer, LoadBalancerStatus.ACTIVE); LOG.info("Update connection logging operation complete."); } private void sendErrorToEventResource(LoadBalancer lb) { String title = "Error Updating Connection Logging"; String desc = "Could not update connection logging at this time."; notificationService.saveLoadBalancerEvent(lb.getUserName(), lb.getAccountId(), lb.getId(), title, desc, UPDATE_CONNECTION_LOGGING, UPDATE, CRITICAL); } }