add state change listner to Snapshot resource
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/5ac5036a Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/5ac5036a Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/5ac5036a Branch: refs/heads/events-framework Commit: 5ac5036ae798375262cb1db5cc748cb66641e2cf Parents: e439d98 Author: Murali Reddy <[email protected]> Authored: Thu Jan 31 16:19:14 2013 +0530 Committer: Murali Reddy <[email protected]> Committed: Thu Jan 31 19:37:51 2013 +0530 ---------------------------------------------------------------------- .../storage/listener/SnapshotStateListener.java | 85 +++++++++++++++ .../storage/snapshot/SnapshotManagerImpl.java | 2 + 2 files changed, 87 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5ac5036a/server/src/com/cloud/storage/listener/SnapshotStateListener.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/storage/listener/SnapshotStateListener.java b/server/src/com/cloud/storage/listener/SnapshotStateListener.java new file mode 100644 index 0000000..2d3e352 --- /dev/null +++ b/server/src/com/cloud/storage/listener/SnapshotStateListener.java @@ -0,0 +1,85 @@ +package com.cloud.storage.listener; + +import com.cloud.event.EventCategory; +import com.cloud.storage.Snapshot; +import com.cloud.storage.Snapshot.Event; +import com.cloud.storage.Snapshot.State; +import com.cloud.server.ManagementServer; +import com.cloud.utils.component.Adapters; +import com.cloud.utils.component.ComponentLocator; +import com.cloud.utils.fsm.StateListener; +import org.apache.cloudstack.framework.events.EventBus; +import org.apache.cloudstack.framework.events.EventBusException; +import org.apache.log4j.Logger; + +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +public class SnapshotStateListener implements StateListener<State, Event, Snapshot> { + + // get the event bus provider if configured + protected static EventBus _eventBus = null; + static { + Adapters<EventBus> eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration(); + if (eventBusenum != null && eventBusenum.hasMoreElements()) { + _eventBus = eventBusenum.nextElement(); // configure event bus if configured + } + } + } + + private static final Logger s_logger = Logger.getLogger(VolumeStateListener.class); + + public SnapshotStateListener() { + + } + + @Override + public boolean preStateTransitionEvent(State oldState, Event event, State newState, Snapshot vo, boolean status, Object opaque) { + pubishOnEventBus(event.name(), "preStateTransitionEvent", vo, oldState, newState); + return true; + } + + @Override + public boolean postStateTransitionEvent(State oldState, Event event, State newState, Snapshot vo, boolean status, Object opaque) { + pubishOnEventBus(event.name(), "postStateTransitionEvent", vo, oldState, newState); + return true; + } + + private void pubishOnEventBus(String event, String status, Snapshot vo, State oldState, State newState) { + + if (_eventBus == null) { + return; // no provider is configured to provide events bus, so just return + } + + String resourceName = getEntityFromClassName(Snapshot.class.getName()); + org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event( + ManagementServer.Name, + EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), + event, + resourceName, + vo.getUuid()); + Map<String, String> eventDescription = new HashMap<String, String>(); + eventDescription.put("resource", resourceName); + eventDescription.put("id", vo.getUuid()); + eventDescription.put("old-state", oldState.name()); + eventDescription.put("new-state", newState.name()); + eventMsg.setDescription(eventDescription); + try { + _eventBus.publish(eventMsg); + } catch (EventBusException e) { + s_logger.warn("Failed to publish action event on the the event bus."); + } + } + + private String getEntityFromClassName(String entityClassName) { + int index = entityClassName.lastIndexOf("."); + String entityName = entityClassName; + if (index != -1) { + entityName = entityClassName.substring(index+1); + } + return entityName; + } +} http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/5ac5036a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java index 9dc15a1..5a150a8 100755 --- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -48,6 +48,7 @@ import com.cloud.storage.*; import com.cloud.storage.Snapshot.Type; import com.cloud.storage.Storage.StoragePoolType; import com.cloud.storage.dao.*; +import com.cloud.storage.listener.SnapshotStateListener; import com.cloud.storage.s3.S3Manager; import com.cloud.storage.secondary.SecondaryStorageVmManager; import com.cloud.storage.swift.SwiftManager; @@ -1424,6 +1425,7 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma s_logger.info("Snapshot Manager is configured."); _snapshotFsm = Snapshot.State.getStateMachine(); + _snapshotFsm.registerListener(new SnapshotStateListener()); return true; }
