You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by mu...@apache.org on 2012/09/17 05:11:14 UTC

[1/5] git commit: publish actione events, usage events and alerts on the event bus

Updated Branches:
  refs/heads/events-framework [created] 600a166d4


publish actione events, usage events and alerts on the event bus


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/600a166d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/600a166d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/600a166d

Branch: refs/heads/events-framework
Commit: 600a166d4f2da6540298746337fef346eb54159a
Parents: 967289d
Author: Murali Reddy <Mu...@citrix.com>
Authored: Mon Sep 17 03:19:01 2012 +0530
Committer: Murali Reddy <Mu...@citrix.com>
Committed: Mon Sep 17 03:19:01 2012 +0530

----------------------------------------------------------------------
 server/pom.xml                                     |    5 ++
 .../src/com/cloud/event/ActionEventCallback.java   |   35 +++++++++++-
 server/src/com/cloud/event/AlertGenerator.java     |   33 +++++++++++
 .../src/com/cloud/event/UsageEventGenerator.java   |   45 +++++++++++++++
 4 files changed, 117 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/600a166d/server/pom.xml
----------------------------------------------------------------------
diff --git a/server/pom.xml b/server/pom.xml
index f7178d8..d009971 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -33,6 +33,11 @@
       <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>
       <version>${cs.servlet.version}</version>

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/600a166d/server/src/com/cloud/event/ActionEventCallback.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/event/ActionEventCallback.java b/server/src/com/cloud/event/ActionEventCallback.java
index fe93e23..8ec53a4 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<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;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/600a166d/server/src/com/cloud/event/AlertGenerator.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/event/AlertGenerator.java b/server/src/com/cloud/event/AlertGenerator.java
index ad5a1cf..08cf081 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<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;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/600a166d/server/src/com/cloud/event/UsageEventGenerator.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/event/UsageEventGenerator.java b/server/src/com/cloud/event/UsageEventGenerator.java
index 7f217e7..40bfbbe 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<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;
     }
 }