mirror of https://github.com/apache/cloudstack.git
add state change listner to Snapshot resource
This commit is contained in:
parent
e439d9824f
commit
5ac5036ae7
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue