Ramesh N has uploaded a new change for review.

Change subject: <WIP>engine: support for external event handlers
......................................................................

<WIP>engine: support for external event handlers

Adding capability to hook event handlers for external
events. With this feature, new event handlers can be implemented
to handle specific external events.

 New attribute externalEventType and eventParams is added to
Event entity. Rest API and corresponding mappers are updated
to support these fields.

Event handlers can be implemeted for any ExternalEventType.
All event handler should implement the interface ExternalEventHandler.

 ExternalEventHandlerProvider will be used to register
and provide the event handler. It will be used by
AddExternalEventCommand BLL to process event handlers.

 New ExternalEventType should be defined for every external event
which needs to be handled by a seperate event handler.

Change-Id: I4b9c8638fbc8794cc196134093af9ff7e7cb4876
Signed-off-by: Ramesh Nachimuthu <rnach...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddExternalEventCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ExternalEventCommandBase.java
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/ExternalEventHandler.java
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/ExternalEventHandlerProvider.java
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/GlusterVolumeStartedEventHandler.java
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/GlusterVolumeStopedEventHandler.java
A 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/ExternalEventType.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java
M 
backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
M 
backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/EventMapper.java
M 
frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
11 files changed, 207 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/70/25270/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddExternalEventCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddExternalEventCommand.java
index 15f78cc..487e54c 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddExternalEventCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddExternalEventCommand.java
@@ -55,6 +55,8 @@
         if (auditLog != null) {
             setActionReturnValue(auditLog.getaudit_log_id());
             setSucceeded(true);
+            if (getParameters().getEvent().getExternalEventType() != null)
+                processExternalEventHandlers(getParameters().getEvent());
         }
     }
 
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ExternalEventCommandBase.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ExternalEventCommandBase.java
index 00657a5..d577f71 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ExternalEventCommandBase.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ExternalEventCommandBase.java
@@ -2,6 +2,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 import org.ovirt.engine.core.bll.utils.PermissionSubject;
 import org.ovirt.engine.core.common.VdcObjectType;
@@ -9,6 +10,8 @@
 import org.ovirt.engine.core.common.businessentities.ActionGroup;
 import org.ovirt.engine.core.common.businessentities.AuditLog;
 import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.events.ExternalEventHandler;
+import org.ovirt.engine.core.events.ExternalEventHandlerProvider;
 
 public abstract class ExternalEventCommandBase<T extends 
VdcActionParametersBase> extends CommandBase<T> {
 
@@ -57,4 +60,14 @@
         }
         return permissionList;
     }
+
+    protected void processExternalEventHandlers(AuditLog event) {
+        Set<ExternalEventHandler> eventHandlers =
+                
ExternalEventHandlerProvider.getInstance().getEventHandlers(event.getExternalEventType());
+        for (ExternalEventHandler eventHandler : eventHandlers) {
+            if (eventHandler.canHandleEvent(event)) {
+                eventHandler.handleEvent(event);
+            }
+        }
+    }
 }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/ExternalEventHandler.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/ExternalEventHandler.java
new file mode 100644
index 0000000..575e22a
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/ExternalEventHandler.java
@@ -0,0 +1,10 @@
+package org.ovirt.engine.core.events;
+
+import org.ovirt.engine.core.common.businessentities.AuditLog;
+
+public interface ExternalEventHandler {
+
+    void handleEvent(AuditLog event);
+
+    boolean canHandleEvent(AuditLog event);
+}
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/ExternalEventHandlerProvider.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/ExternalEventHandlerProvider.java
new file mode 100644
index 0000000..551142d
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/ExternalEventHandlerProvider.java
@@ -0,0 +1,40 @@
+package org.ovirt.engine.core.events;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.ovirt.engine.core.common.ExternalEventType;
+
+public class ExternalEventHandlerProvider {
+
+    private static final ExternalEventHandlerProvider 
externalEventHandlerProvider = new ExternalEventHandlerProvider();
+
+    private ExternalEventHandlerProvider(){
+        initAllEventHandlers();
+    }
+
+    private void initAllEventHandlers() {
+        registerExternalEventHandler(ExternalEventType.GLUSTER_VOLUME_STOPPED, 
new GlusterVolumeStopedEventHandler());
+        registerExternalEventHandler(ExternalEventType.GLUSTER_VOLUME_STARTED, 
new GlusterVolumeStartedEventHandler());
+    }
+
+    private void registerExternalEventHandler(ExternalEventType eventType, 
ExternalEventHandler eventHandler) {
+        Set<ExternalEventHandler> eventHandlers = 
externalEventHandlers.get(eventType);
+        if (eventHandlers == null || eventHandlers.isEmpty()) {
+            eventHandlers = new HashSet<ExternalEventHandler>();
+            externalEventHandlers.put(eventType, eventHandlers);
+        }
+        eventHandlers.add(eventHandler);
+    }
+
+    public static ExternalEventHandlerProvider getInstance() {
+        return externalEventHandlerProvider;
+    }
+
+    private static Map<ExternalEventType, Set<ExternalEventHandler>> 
externalEventHandlers;
+
+    public Set<ExternalEventHandler> getEventHandlers(ExternalEventType 
eventType) {
+        return externalEventHandlers.get(eventType);
+    }
+}
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/GlusterVolumeStartedEventHandler.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/GlusterVolumeStartedEventHandler.java
new file mode 100644
index 0000000..d7987e6
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/GlusterVolumeStartedEventHandler.java
@@ -0,0 +1,24 @@
+package org.ovirt.engine.core.events;
+
+import org.ovirt.engine.core.common.businessentities.AuditLog;
+import org.ovirt.engine.core.common.businessentities.gluster.GlusterStatus;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.dao.gluster.GlusterDBUtils;
+
+public class GlusterVolumeStartedEventHandler implements ExternalEventHandler {
+
+    private static final String VOLUME_ID = "volumeId";
+
+    @Override
+    public void handleEvent(AuditLog event) {
+        String volumeId = event.getEventParams().get(VOLUME_ID);
+        
GlusterDBUtils.getInstance().updateVolumeStatus(Guid.createGuidFromStringDefaultEmpty(volumeId),
+                GlusterStatus.UP);
+    }
+
+    @Override
+    public boolean canHandleEvent(AuditLog event) {
+        return true;
+    }
+
+}
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/GlusterVolumeStopedEventHandler.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/GlusterVolumeStopedEventHandler.java
new file mode 100644
index 0000000..1693e24
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/events/GlusterVolumeStopedEventHandler.java
@@ -0,0 +1,25 @@
+package org.ovirt.engine.core.events;
+
+import org.ovirt.engine.core.common.businessentities.AuditLog;
+import org.ovirt.engine.core.common.businessentities.gluster.GlusterStatus;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.dao.gluster.GlusterDBUtils;
+
+public class GlusterVolumeStopedEventHandler implements ExternalEventHandler {
+
+    private static final String VOLUME_ID = "volumeId";
+
+    @Override
+    public void handleEvent(AuditLog event) {
+
+        String volumeId = event.getEventParams().get(VOLUME_ID);
+        
GlusterDBUtils.getInstance().updateVolumeStatus(Guid.createGuidFromStringDefaultEmpty(volumeId),
+                GlusterStatus.DOWN);
+    }
+
+    @Override
+    public boolean canHandleEvent(AuditLog event) {
+        return true;
+    }
+
+}
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/ExternalEventType.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/ExternalEventType.java
new file mode 100644
index 0000000..55db1db
--- /dev/null
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/ExternalEventType.java
@@ -0,0 +1,32 @@
+package org.ovirt.engine.core.common;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public enum ExternalEventType {
+
+    GLUSTER_VOLUME_STARTED(3),
+    GLUSTER_VOLUME_STOPPED(4);
+
+    private int intValue;
+
+    private static final Map<Integer, ExternalEventType> mappings = new 
HashMap<Integer, ExternalEventType>();
+
+    static {
+        for (ExternalEventType eventType : values()) {
+            mappings.put(eventType.getValue(), eventType);
+        }
+    }
+
+    private ExternalEventType(int value) {
+        intValue = value;
+    }
+
+    public int getValue() {
+        return intValue;
+    }
+
+    public static ExternalEventType forValue(int value) {
+        return mappings.get(value);
+    }
+}
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java
index cbd7cb7..03a3f5f 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/AuditLog.java
@@ -2,9 +2,12 @@
 
 import java.io.Serializable;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.ovirt.engine.core.common.AuditLogSeverity;
 import org.ovirt.engine.core.common.AuditLogType;
+import org.ovirt.engine.core.common.ExternalEventType;
 import org.ovirt.engine.core.common.utils.ObjectUtils;
 import org.ovirt.engine.core.compat.Guid;
 
@@ -46,6 +49,8 @@
     private String compatibilityVersion;
     private String quotaEnforcementType;
     private String callStack;
+    private ExternalEventType externalEventType;
+    private Map<String, String> eventParams;
 
     public AuditLog() {
         logType = AuditLogType.UNASSIGNED.getValue();
@@ -54,6 +59,7 @@
         customEventId = -1;
         eventFloodInSec = 30;
         customData = "";
+        setEventParams(new HashMap<String, String>());
     }
 
     public AuditLog(AuditLogType al_type, AuditLogSeverity al_severity, String 
al_msg, Guid al_user_id,
@@ -495,4 +501,20 @@
         sb.append(message);
         return sb.toString();
     }
+
+    public ExternalEventType getExternalEventType() {
+        return externalEventType;
+    }
+
+    public void setExternalEventType(ExternalEventType externalEventType) {
+        this.externalEventType = externalEventType;
+    }
+
+    public Map<String, String> getEventParams() {
+        return eventParams;
+    }
+
+    public void setEventParams(Map<String, String> eventParams) {
+        this.eventParams = eventParams;
+    }
 }
diff --git 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
index 163a0ea..351baea 100644
--- 
a/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
+++ 
b/backend/manager/modules/restapi/interface/definition/src/main/resources/api.xsd
@@ -1804,11 +1804,24 @@
           <xs:element name="custom_id" type="xs:int" minOccurs="0" 
maxOccurs="1" />
           <xs:element name="flood_rate" type="xs:int" minOccurs="0" 
maxOccurs="1" />
           <xs:element name="custom_data" type="xs:string" minOccurs="0" 
maxOccurs="1" />
+          <xs:element name="external_event_code" type="xs:int" minOccurs="0" 
maxOccurs="1" />
+          <xs:element name="params" type="EventParameters" minOccurs="0" 
maxOccurs="1" />
         </xs:sequence>
       </xs:extension>
     </xs:complexContent>
   </xs:complexType>
 
+<xs:complexType name="EventParameters">
+        <xs:sequence>
+          <xs:annotation>
+            <xs:appinfo>
+              <jaxb:property name="param" />
+            </xs:appinfo>
+          </xs:annotation>
+          <xs:element name="param" type="KeyValuePair" minOccurs="1" 
maxOccurs="unbounded" />
+        </xs:sequence>
+  </xs:complexType>
+
   <xs:complexType name="Events">
     <xs:complexContent>
       <xs:extension base="BaseResources">
diff --git 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/EventMapper.java
 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/EventMapper.java
index c0431d9..f34b765 100644
--- 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/EventMapper.java
+++ 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/EventMapper.java
@@ -13,10 +13,15 @@
 import org.ovirt.engine.api.model.Template;
 import org.ovirt.engine.api.model.User;
 import org.ovirt.engine.api.model.VM;
+import org.ovirt.engine.api.model.EventParameters;
+import org.ovirt.engine.api.model.KeyValuePair;
 import org.ovirt.engine.api.restapi.utils.TypeConversionHelper;
 import org.ovirt.engine.core.common.AuditLogSeverity;
 import org.ovirt.engine.core.common.businessentities.AuditLog;
 import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.common.ExternalEventType;
+import java.util.HashMap;
+import java.util.Map;
 
 public class EventMapper {
 
@@ -135,8 +140,28 @@
         if (event.isSetCustomData()) {
             auditLog.setCustomData(event.getCustomData());
         }
+
+        if (event.isSetExternalEventCode()) {
+            
auditLog.setExternalEventType(ExternalEventType.forValue(event.getExternalEventCode()));
+        }
+
+        if(event.isSetParams()){
+            Map<String, String> paramsMap = 
convertEventParamsToMap(event.getParams());
+            auditLog.setEventParams(paramsMap);
+        }
         return auditLog;
     }
+
+    private static Map<String, String> convertEventParamsToMap(EventParameters 
params){
+        Map<String, String> paramsMap = new HashMap<String, String>();
+        for(KeyValuePair param:params.getParam()){
+            paramsMap.put(param.getKey(), param.getValue());
+        }
+
+        return paramsMap;
+
+    }
+
     @Mapping(from = AuditLogSeverity.class, to = LogSeverity.class)
     public static LogSeverity map(AuditLogSeverity entityStatus,
             LogSeverity template) {
diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
 
b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
index f9175c0..6ad6df6 100644
--- 
a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
@@ -21,6 +21,7 @@
                <include name="aaa/DirectoryUser.java" />
                <include name="aaa/Managed.java" />
                <include name="common/businessentities/AuditLog.java" />
+               <include name="common/ExternalEventType.java" />
                <include name="common/businessentities/Bookmark.java" />
                <include name="common/businessentities/BaseDisk.java" />
                <include name="common/businessentities/DiskLunMap.java" />


-- 
To view, visit http://gerrit.ovirt.org/25270
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4b9c8638fbc8794cc196134093af9ff7e7cb4876
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Ramesh N <rnach...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to