mirror of https://github.com/apache/cloudstack.git
publish actione events, usage events and alerts on the event bus
This commit is contained in:
parent
967289d8c5
commit
600a166d4f
|
|
@ -32,6 +32,11 @@
|
|||
<artifactId>cloud-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cloudstack</groupId>
|
||||
<artifactId>cloud-framework-events</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>servlet-api</artifactId>
|
||||
|
|
|
|||
|
|
@ -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<EventVO> {
|
||||
|
||||
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<String, String> eventDescription = new HashMap<String, String>();
|
||||
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<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
|
||||
if (eventBusImpls != null) {
|
||||
Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
|
||||
_eventBus = eventBusenum.nextElement();
|
||||
}
|
||||
_eventBusLoaded = true;
|
||||
}
|
||||
}
|
||||
return _eventBus;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String, String> eventDescription = new HashMap<String, String>();
|
||||
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<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
|
||||
if (eventBusImpls != null) {
|
||||
Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
|
||||
_eventBus = eventBusenum.nextElement();
|
||||
}
|
||||
_eventBusLoaded = true;
|
||||
}
|
||||
}
|
||||
return _eventBus;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String, String> eventDescription = new HashMap<String, String>();
|
||||
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<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
|
||||
if (eventBusImpls != null) {
|
||||
Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
|
||||
_eventBus = eventBusenum.nextElement();
|
||||
}
|
||||
_eventBusLoaded = true;
|
||||
}
|
||||
}
|
||||
return _eventBus;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue