mirror of
https://github.com/apache/cloudstack.git
synced 2025-12-17 19:14:40 +01:00
publish state change events on Network and VirtualMachine resource state
changes
This commit is contained in:
parent
117bce8c55
commit
053b6bd636
@ -161,8 +161,9 @@ public class RabbitMQEventBus implements EventBus {
|
|||||||
String eventCategory = getEventCategoryFromRoutingKey(routingKey);
|
String eventCategory = getEventCategoryFromRoutingKey(routingKey);
|
||||||
String eventType = getEventTypeFromRoutingKey(routingKey);
|
String eventType = getEventTypeFromRoutingKey(routingKey);
|
||||||
String resourceType = getResourceTypeFromRoutingKey(routingKey);
|
String resourceType = getResourceTypeFromRoutingKey(routingKey);
|
||||||
|
String resourceUUID = getResourceUUIDFromRoutingKey(routingKey);
|
||||||
Event event = new Event(eventSource, eventCategory, eventType,
|
Event event = new Event(eventSource, eventCategory, eventType,
|
||||||
resourceType, null);
|
resourceType, resourceUUID);
|
||||||
event.setDescription(new String(body));
|
event.setDescription(new String(body));
|
||||||
|
|
||||||
// deliver the event to call back object provided by subscriber
|
// deliver the event to call back object provided by subscriber
|
||||||
@ -526,10 +527,11 @@ public class RabbitMQEventBus implements EventBus {
|
|||||||
String eventCategory = getEventCategoryFromRoutingKey(routingKey);
|
String eventCategory = getEventCategoryFromRoutingKey(routingKey);
|
||||||
String eventType = getEventTypeFromRoutingKey(routingKey);
|
String eventType = getEventTypeFromRoutingKey(routingKey);
|
||||||
String resourceType = getResourceTypeFromRoutingKey(routingKey);
|
String resourceType = getResourceTypeFromRoutingKey(routingKey);
|
||||||
|
String resourceUUID = getResourceUUIDFromRoutingKey(routingKey);
|
||||||
|
|
||||||
// create event object from the message details obtained from AMQP server
|
// create event object from the message details obtained from AMQP server
|
||||||
Event event = new Event(eventSource, eventCategory, eventType,
|
Event event = new Event(eventSource, eventCategory, eventType,
|
||||||
resourceType, null);
|
resourceType, resourceUUID);
|
||||||
event.setDescription(new String(body));
|
event.setDescription(new String(body));
|
||||||
|
|
||||||
// deliver the event to call back object provided by subscriber
|
// deliver the event to call back object provided by subscriber
|
||||||
|
|||||||
@ -1,31 +1,89 @@
|
|||||||
package com.cloud.network;
|
package com.cloud.network;
|
||||||
|
|
||||||
|
import com.cloud.event.EventCategory;
|
||||||
import com.cloud.event.dao.UsageEventDao;
|
import com.cloud.event.dao.UsageEventDao;
|
||||||
import com.cloud.network.Network.Event;
|
import com.cloud.network.Network.Event;
|
||||||
import com.cloud.network.Network.State;
|
import com.cloud.network.Network.State;
|
||||||
import com.cloud.network.dao.NetworkDao;
|
import com.cloud.network.dao.NetworkDao;
|
||||||
|
import com.cloud.server.ManagementServer;
|
||||||
|
import com.cloud.utils.component.Adapters;
|
||||||
|
import com.cloud.utils.component.ComponentLocator;
|
||||||
import com.cloud.utils.fsm.StateListener;
|
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 NetworkStateListener implements StateListener<State, Event, Network> {
|
public class NetworkStateListener implements StateListener<State, Event, Network> {
|
||||||
|
|
||||||
protected UsageEventDao _usageEventDao;
|
protected UsageEventDao _usageEventDao;
|
||||||
protected NetworkDao _networkDao;
|
protected NetworkDao _networkDao;
|
||||||
|
protected static EventBus _eventBus = null;
|
||||||
|
|
||||||
|
private static final Logger s_logger = Logger.getLogger(NetworkStateListener.class);
|
||||||
|
|
||||||
public NetworkStateListener(UsageEventDao usageEventDao, NetworkDao networkDao) {
|
public NetworkStateListener(UsageEventDao usageEventDao, NetworkDao networkDao) {
|
||||||
this._usageEventDao = usageEventDao;
|
this._usageEventDao = usageEventDao;
|
||||||
this._networkDao = networkDao;
|
this._networkDao = networkDao;
|
||||||
|
initEventBusProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean preStateTransitionEvent(State oldState, Event event, State newState, Network vo, boolean status, Object opaque) {
|
public boolean preStateTransitionEvent(State oldState, Event event, State newState, Network vo, boolean status, Object opaque) {
|
||||||
// TODO Auto-generated method stub
|
pubishOnEventBus(event.name(), "preStateTransitionEvent", vo, oldState, newState);
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean postStateTransitionEvent(State oldState, Event event, State newState, Network vo, boolean status, Object opaque) {
|
public boolean postStateTransitionEvent(State oldState, Event event, State newState, Network vo, boolean status, Object opaque) {
|
||||||
// TODO Auto-generated method stub
|
pubishOnEventBus(event.name(), "postStateTransitionEvent", vo, oldState, newState);
|
||||||
return false;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void pubishOnEventBus(String event, String status, Network vo, State oldState, State newState) {
|
||||||
|
if (_eventBus != null) {
|
||||||
|
String resourceName = getEntityFromClassName(Network.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initEventBusProvider() {
|
||||||
|
ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
|
||||||
|
Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
|
||||||
|
if (eventBusImpls != null) {
|
||||||
|
Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
|
||||||
|
if (eventBusenum != null && eventBusenum.hasMoreElements()) {
|
||||||
|
_eventBus = eventBusenum.nextElement();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,32 +16,47 @@
|
|||||||
// under the License.
|
// under the License.
|
||||||
package com.cloud.vm;
|
package com.cloud.vm;
|
||||||
|
|
||||||
|
import com.cloud.event.EventCategory;
|
||||||
import com.cloud.event.EventTypes;
|
import com.cloud.event.EventTypes;
|
||||||
import com.cloud.event.UsageEventUtils;
|
import com.cloud.event.UsageEventUtils;
|
||||||
import com.cloud.event.dao.UsageEventDao;
|
import com.cloud.event.dao.UsageEventDao;
|
||||||
|
import com.cloud.network.Network;
|
||||||
import com.cloud.network.NetworkVO;
|
import com.cloud.network.NetworkVO;
|
||||||
import com.cloud.network.dao.NetworkDao;
|
import com.cloud.network.dao.NetworkDao;
|
||||||
|
import com.cloud.server.ManagementServer;
|
||||||
|
import com.cloud.utils.component.Adapters;
|
||||||
|
import com.cloud.utils.component.ComponentLocator;
|
||||||
import com.cloud.utils.fsm.StateListener;
|
import com.cloud.utils.fsm.StateListener;
|
||||||
import com.cloud.vm.VirtualMachine.Event;
|
import com.cloud.vm.VirtualMachine.Event;
|
||||||
import com.cloud.vm.VirtualMachine.State;
|
import com.cloud.vm.VirtualMachine.State;
|
||||||
import com.cloud.vm.dao.NicDao;
|
import com.cloud.vm.dao.NicDao;
|
||||||
|
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.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class UserVmStateListener implements StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
public class UserVmStateListener implements StateListener<State, VirtualMachine.Event, VirtualMachine> {
|
||||||
|
|
||||||
protected UsageEventDao _usageEventDao;
|
protected UsageEventDao _usageEventDao;
|
||||||
protected NetworkDao _networkDao;
|
protected NetworkDao _networkDao;
|
||||||
protected NicDao _nicDao;
|
protected NicDao _nicDao;
|
||||||
|
protected static EventBus _eventBus = null;
|
||||||
|
private static final Logger s_logger = Logger.getLogger(UserVmStateListener.class);
|
||||||
|
|
||||||
public UserVmStateListener(UsageEventDao usageEventDao, NetworkDao networkDao, NicDao nicDao) {
|
public UserVmStateListener(UsageEventDao usageEventDao, NetworkDao networkDao, NicDao nicDao) {
|
||||||
this._usageEventDao = usageEventDao;
|
this._usageEventDao = usageEventDao;
|
||||||
this._networkDao = networkDao;
|
this._networkDao = networkDao;
|
||||||
this._nicDao = nicDao;
|
this._nicDao = nicDao;
|
||||||
|
initEventBusProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean preStateTransitionEvent(State oldState, Event event, State newState, VirtualMachine vo, boolean status, Object opaque) {
|
public boolean preStateTransitionEvent(State oldState, Event event, State newState, VirtualMachine vo, boolean status, Object opaque) {
|
||||||
|
pubishOnEventBus(event.name(), "preStateTransitionEvent", vo, oldState, newState);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,4 +89,47 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void pubishOnEventBus(String event, String status, VirtualMachine vo, VirtualMachine.State oldState, VirtualMachine.State newState) {
|
||||||
|
if (_eventBus != null) {
|
||||||
|
String resourceName = getEntityFromClassName(Network.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initEventBusProvider() {
|
||||||
|
ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
|
||||||
|
Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
|
||||||
|
if (eventBusImpls != null) {
|
||||||
|
Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
|
||||||
|
if (eventBusenum != null && eventBusenum.hasMoreElements()) {
|
||||||
|
_eventBus = eventBusenum.nextElement();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user