diff --git a/server/pom.xml b/server/pom.xml index f7178d8eb0a..d0099712254 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -32,6 +32,11 @@ cloud-core ${project.version} + + org.apache.cloudstack + cloud-framework-events + ${project.version} + javax.servlet servlet-api diff --git a/server/src/com/cloud/event/ActionEventCallback.java b/server/src/com/cloud/event/ActionEventCallback.java index fe93e23a026..8ec53a4ff93 100644 --- a/server/src/com/cloud/event/ActionEventCallback.java +++ b/server/src/com/cloud/event/ActionEventCallback.java @@ -25,9 +25,13 @@ import net.sf.cglib.proxy.MethodProxy; import com.cloud.user.UserContext; import com.cloud.utils.component.AnnotationInterceptor; +import org.apache.cloudstack.framework.events.EventBus; public class ActionEventCallback implements MethodInterceptor, AnnotationInterceptor { + protected static EventBus _eventBus = null; + protected static boolean _eventBusLoaded = false; + @Override public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { EventVO event = interceptStart(method); @@ -77,6 +81,7 @@ public class ActionEventCallback implements MethodInterceptor, AnnotationInterce eventDescription += ". "+ctx.getEventDetails(); } EventUtils.saveStartedActionEvent(userId, accountId, actionEvent.eventType(), eventDescription, startEventId); + publishOnEventBus(userId, accountId, actionEvent.eventType(), "Started", eventDescription); } } return event; @@ -98,9 +103,11 @@ public class ActionEventCallback implements MethodInterceptor, AnnotationInterce if(actionEvent.create()){ //This start event has to be used for subsequent events of this action startEventId = EventUtils.saveCreatedActionEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully created entity for "+eventDescription); + publishOnEventBus(userId, accountId, actionEvent.eventType(), "Successfully created entity for "+eventDescription); ctx.setStartEventId(startEventId); } else { EventUtils.saveActionEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully completed "+eventDescription, startEventId); + publishOnEventBus(userId, accountId, actionEvent.eventType(), "Successfully completed "+eventDescription, startEventId); } } } @@ -131,5 +138,31 @@ public class ActionEventCallback implements MethodInterceptor, AnnotationInterce public Callback getCallback() { return this; } - + + void publishOnEventBus(long userId, long accountId, String type, String state, String description) { + if (getEventBus() != null) { + Map eventDescription = new HashMap(); + eventDescription.put("user", String.valueOf(userId)); + eventDescription.put("account", String.valueOf(accountId)); + eventDescription.put("state", state); + eventDescription.put("description", description); + _eventBus.publish(EventCategory.ACTION_EVENT, type, eventDescription); + } + } + + private EventBus getEventBus() { + //TODO: check if there is way of getting single adapter + if (_eventBus == null) { + if (!_eventBusLoaded) { + ComponentLocator locator = ComponentLocator.getLocator("management-server"); + Adapters eventBusImpls = locator.getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration eventBusenum = eventBusImpls.enumeration(); + _eventBus = eventBusenum.nextElement(); + } + _eventBusLoaded = true; + } + } + return _eventBus; + } } diff --git a/server/src/com/cloud/event/AlertGenerator.java b/server/src/com/cloud/event/AlertGenerator.java index ad5a1cf81ed..08cf081806b 100644 --- a/server/src/com/cloud/event/AlertGenerator.java +++ b/server/src/com/cloud/event/AlertGenerator.java @@ -1,7 +1,40 @@ package com.cloud.event; +import import org.apache.cloudstack.framework.events.EventBus; + public class AlertGenerator { + protected static EventBus _eventBus = null; + protected static boolean _eventBusLoaded = false; + public static void publishAlert(String alertType, long dataCenterId, Long podId, String subject, String body) { } + + void publishOnEventBus(String alertType, long dataCenterId, Long podId, String subject, String body) { + if (getEventBus() != null) { + Map eventDescription = new HashMap(); + eventDescription.put("alertType", alertType); + eventDescription.put("dataCenterId", dataCenterId); + eventDescription.put("podId", podId); + eventDescription.put("subject", subject); + eventDescription.put("body", body); + _eventBus.publish(EventCategory.ALERT_EVENT, alertType, eventDescription); + } + } + + private EventBus getEventBus() { + //TODO: check if there is way of getting single adapter + if (_eventBus == null) { + if (!_eventBusLoaded) { + ComponentLocator locator = ComponentLocator.getLocator("management-server"); + Adapters eventBusImpls = locator.getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration eventBusenum = eventBusImpls.enumeration(); + _eventBus = eventBusenum.nextElement(); + } + _eventBusLoaded = true; + } + } + return _eventBus; + } } diff --git a/server/src/com/cloud/event/UsageEventGenerator.java b/server/src/com/cloud/event/UsageEventGenerator.java index 7f217e7db97..40bfbbe3f82 100644 --- a/server/src/com/cloud/event/UsageEventGenerator.java +++ b/server/src/com/cloud/event/UsageEventGenerator.java @@ -1,24 +1,69 @@ package com.cloud.event; +import import org.apache.cloudstack.framework.events.EventBus; + public class UsageEventGenerator { + protected static EventBus _eventBus = null; + protected static boolean _eventBusLoaded = false; + public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, Long size) { EventUtils.saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, size); + publishOnEventBus(usageType, accountId, zoneId, resourceId, resourceName, null); } public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName) { EventUtils.saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName); + publishOnEventBus(usageType, accountId, zoneId, resourceId, resourceName, null); } public static void publishUsageEvent(String usageType, long accountId, long zoneId, long ipAddressId, String ipAddress, boolean isSourceNat, String guestType, boolean isSystem) { EventUtils.saveUsageEvent(usageType, accountId, zoneId, ipAddressId, ipAddress, isSourceNat, guestType, isSystem); + publishOnEventBus(usageType, accountId, zoneId, ipAddressId, "IP address", null); } public static void publishUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId, String resourceType) { EventUtils.saveUsageEvent(usageType, accountId, zoneId, resourceId, resourceName, offeringId, templateId, resourceType); + publishOnEventBus(usageType, accountId, zoneId, resourceId, resourceName, resourceType); } public static void publishUsageEvent(String usageType, long accountId,long zoneId, long vmId, long securityGroupId) { EventUtils.saveUsageEvent(usageType, accountId, zoneId, vmId, securityGroupId); + publishOnEventBus((usageType, accountId, zoneId, vmId, null, null); + } + + void publishOnEventBus(String usageType, Long accountId, Long zoneId, Long resourceId, String resourceName, String resourceType) { + if (getEventBus() != null) { + Map eventDescription = new HashMap(); + eventDescription.put("usage type", usageType); + if (accountId != null) { + eventDescription.put("accountId", usageType) + } + if (zoneId != null) { + eventDescription.put("zoneId", String.valueOf(zoneId)) + } + if (resourceId != null) { + eventDescription.put("resourceId", String.valueOf(resourceId)) + } + eventDescription.put("resourceName", resourceName); + eventDescription.put("resourceType", resourceType); + _eventBus.publish(EventCategory.USAGE_EVENT, usageType, eventDescription); + } + } + + private EventBus getEventBus() { + //TODO: check if there is way of getting single adapter + if (_eventBus == null) { + if (!_eventBusLoaded) { + ComponentLocator locator = ComponentLocator.getLocator("management-server"); + Adapters eventBusImpls = locator.getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration eventBusenum = eventBusImpls.enumeration(); + _eventBus = eventBusenum.nextElement(); + } + _eventBusLoaded = true; + } + } + return _eventBus; } }