- remove Publish, Subscrie annotation as they are not used

- merge ActionEvent callback into ActionEventUtils
- static initialize event bus from component locator
This commit is contained in:
Murali Reddy 2013-01-31 15:54:57 +05:30
parent 7079bc273f
commit e439d9824f
11 changed files with 347 additions and 561 deletions

View File

@ -1,102 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cloudstack.framework.events;
import com.cloud.utils.component.Adapters;
import com.cloud.utils.component.AnnotationInterceptor;
import com.cloud.utils.component.ComponentLocator;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Enumeration;
public class EventPublishCallback implements MethodInterceptor, AnnotationInterceptor<Publish> {
private static EventBus _eventBus = null;
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Publish event = interceptStart(method);
boolean success = true;
try {
return methodProxy.invokeSuper(object, args);
} catch (Exception e){
success = false;
interceptException(method, event);
throw e;
} finally {
if(success){
interceptComplete(method, event);
}
}
}
@Override
public boolean needToIntercept(AnnotatedElement element) {
if (!(element instanceof Method)) {
return false;
}
Method method = (Method)element;
Publish event = method.getAnnotation(Publish.class);
if (event != null) {
return true;
}
return false;
}
@Override
public Publish interceptStart(AnnotatedElement element) {
return null;
}
@Override
public void interceptComplete(AnnotatedElement element, Publish event) {
_eventBus = getEventBus();
if (_eventBus != null) {
}
}
@Override
public void interceptException(AnnotatedElement element, Publish attach) {
return;
}
@Override
public Callback getCallback() {
return this;
}
private EventBus getEventBus() {
if (_eventBus == null) {
ComponentLocator locator = ComponentLocator.getLocator("management-server");
Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
if (eventBusImpls != null) {
Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
_eventBus = eventBusenum.nextElement();
}
}
return _eventBus;
}
}

View File

@ -1,38 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cloudstack.framework.events;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
public @interface Publish {
String eventCategory();
String eventType();
String eventDescription();
}

View File

@ -1,35 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cloudstack.framework.events;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({METHOD })
@Retention(RUNTIME)
public @interface Subscribe {
String eventCategory();
String eventType();
}

View File

@ -16,20 +16,20 @@
// under the License.
package com.cloud.configuration;
import java.util.List;
import com.cloud.event.ActionEventCallback;
import com.cloud.event.ActionEventUtils;
import com.cloud.utils.component.AnnotationInterceptor;
import com.cloud.utils.component.InterceptorLibrary;
import com.cloud.utils.db.DatabaseCallback;
import org.apache.cloudstack.framework.events.EventPublishCallback;
import java.util.List;
public class DefaultInterceptorLibrary implements InterceptorLibrary {
@Override
public void addInterceptors(List<AnnotationInterceptor<?>> interceptors) {
interceptors.add(new DatabaseCallback());
interceptors.add(new ActionEventCallback());
interceptors.add(new ActionEventUtils.ActionEventCallback());
interceptors.add(new EventPublishCallback());
}
}

View File

@ -1,136 +0,0 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.event;
import com.cloud.user.UserContext;
import com.cloud.utils.component.AnnotationInterceptor;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.log4j.Logger;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
public class ActionEventCallback implements MethodInterceptor, AnnotationInterceptor<EventVO> {
private static final Logger s_logger = Logger.getLogger(ActionEventCallback.class);
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
EventVO event = interceptStart(method);
boolean success = true;
try {
return methodProxy.invokeSuper(object, args);
} catch (Exception e){
success = false;
interceptException(method, event);
throw e;
} finally {
if(success){
interceptComplete(method, event);
}
}
}
@Override
public boolean needToIntercept(AnnotatedElement element) {
if (!(element instanceof Method)) {
return false;
}
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
return true;
}
return false;
}
@Override
public EventVO interceptStart(AnnotatedElement element) {
EventVO event = null;
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
boolean async = actionEvent.async();
if(async){
UserContext ctx = UserContext.current();
long userId = ctx.getCallerUserId();
long accountId = ctx.getAccountId();
long startEventId = ctx.getStartEventId();
String eventDescription = actionEvent.eventDescription();
if(ctx.getEventDetails() != null){
eventDescription += ". "+ctx.getEventDetails();
}
ActionEventUtils.onStartedActionEvent(userId, accountId, actionEvent.eventType(), eventDescription, startEventId);
}
}
return event;
}
@Override
public void interceptComplete(AnnotatedElement element, EventVO event) {
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
UserContext ctx = UserContext.current();
long userId = ctx.getCallerUserId();
long accountId = ctx.getAccountId();
long startEventId = ctx.getStartEventId();
String eventDescription = actionEvent.eventDescription();
if(ctx.getEventDetails() != null){
eventDescription += ". "+ctx.getEventDetails();
}
if(actionEvent.create()){
//This start event has to be used for subsequent events of this action
startEventId = ActionEventUtils.onCreatedActionEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully created entity for " + eventDescription);
ctx.setStartEventId(startEventId);
} else {
ActionEventUtils.onCompletedActionEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully completed " + eventDescription, startEventId);
}
}
}
@Override
public void interceptException(AnnotatedElement element, EventVO event) {
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
UserContext ctx = UserContext.current();
long userId = ctx.getCallerUserId();
long accountId = ctx.getAccountId();
long startEventId = ctx.getStartEventId();
String eventDescription = actionEvent.eventDescription();
if(ctx.getEventDetails() != null){
eventDescription += ". "+ctx.getEventDetails();
}
if(actionEvent.create()){
long eventId = ActionEventUtils.onCreatedActionEvent(userId, accountId, EventVO.LEVEL_ERROR, actionEvent.eventType(), "Error while creating entity for " + eventDescription);
ctx.setStartEventId(eventId);
} else {
ActionEventUtils.onCompletedActionEvent(userId, accountId, EventVO.LEVEL_ERROR, actionEvent.eventType(), "Error while " + eventDescription, startEventId);
}
}
}
@Override
public Callback getCallback() {
return this;
}
}

View File

@ -22,145 +22,149 @@ import com.cloud.server.ManagementServer;
import com.cloud.user.Account;
import com.cloud.user.AccountVO;
import com.cloud.user.User;
import com.cloud.user.UserContext;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDao;
import com.cloud.utils.component.Adapters;
import com.cloud.utils.component.AnnotationInterceptor;
import com.cloud.utils.component.ComponentLocator;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.cloudstack.framework.events.EventBus;
import org.apache.cloudstack.framework.events.EventBusException;
import org.apache.log4j.Logger;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class ActionEventUtils {
private static EventDao _eventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(EventDao.class);
private static AccountDao _accountDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(AccountDao.class);
protected static UserDao _userDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UserDao.class);
private static final Logger s_logger = Logger.getLogger(ActionEventUtils.class);
private static EventDao _eventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(EventDao.class);
private static AccountDao _accountDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(AccountDao.class);
protected static UserDao _userDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UserDao.class);;
// get the event bus provider if configured
protected static EventBus _eventBus = null;
protected static boolean _eventBusProviderLoaded = false;
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
}
}
}
public static Long onActionEvent(Long userId, Long accountId, Long domainId, String type, String description) {
publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(),
type, com.cloud.event.Event.State.Scheduled);
publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(),
type, com.cloud.event.Event.State.Completed);
Event event = persistActionEvent(userId, accountId, domainId, null, type, Event.State.Completed,
description, null);
EventVO event = new EventVO();
event.setUserId(userId);
event.setAccountId(accountId);
event.setDomainId(domainId);
event.setType(type);
event.setDescription(description);
event = _eventDao.persist(event);
return event.getId();
}
/*
* Save event after scheduling an async job
*/
public static Long onScheduledActionEvent(Long userId, Long accountId, String type, String description, long startEventId) {
public static Long onScheduledActionEvent(Long userId, Long accountId, String type, String description,
long startEventId) {
publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
com.cloud.event.Event.State.Scheduled);
EventVO event = new EventVO();
event.setUserId(userId);
event.setAccountId(accountId);
event.setDomainId(getDomainId(accountId));
event.setType(type);
event.setStartId(startEventId);
event.setState(Event.State.Scheduled);
event.setDescription("Scheduled async job for "+description);
event = _eventDao.persist(event);
Event event = persistActionEvent(userId, accountId, null, null, type, Event.State.Scheduled,
description, startEventId);
return event.getId();
}
/*
* Save event after starting execution of an async job
*/
public static Long onStartedActionEvent(Long userId, Long accountId, String type, String description, long startEventId) {
public static Long onStartedActionEvent(Long userId, Long accountId, String type, String description,
long startEventId) {
publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
com.cloud.event.Event.State.Started);
EventVO event = new EventVO();
event.setUserId(userId);
event.setAccountId(accountId);
event.setDomainId(getDomainId(accountId));
event.setType(type);
event.setState(Event.State.Started);
event.setDescription("Starting job for "+description);
event.setStartId(startEventId);
event = _eventDao.persist(event);
return event.getId();
}
public static Long onCompletedActionEvent(Long userId, Long accountId, String level, String type, String description, long startEventId) {
publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
com.cloud.event.Event.State.Completed);
EventVO event = new EventVO();
event.setUserId(userId);
event.setAccountId(accountId);
event.setDomainId(getDomainId(accountId));
event.setType(type);
event.setDescription(description);
event.setLevel(level);
event.setStartId(startEventId);
event = _eventDao.persist(event);
return (event != null ? event.getId() : null);
}
public static Long onCreatedActionEvent(Long userId, Long accountId, String level, String type, String description) {
publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
com.cloud.event.Event.State.Created);
EventVO event = new EventVO();
event.setUserId(userId);
event.setAccountId(accountId);
event.setDomainId(getDomainId(accountId));
event.setType(type);
event.setLevel(level);
event.setState(Event.State.Created);
event.setDescription(description);
event = _eventDao.persist(event);
Event event = persistActionEvent(userId, accountId, null, null, type, Event.State.Started,
description, startEventId);
return event.getId();
}
private static long getDomainId(long accountId){
AccountVO account = _accountDao.findByIdIncludingRemoved(accountId);
return account.getDomainId();
public static Long onCompletedActionEvent(Long userId, Long accountId, String level, String type,
String description, long startEventId) {
publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
com.cloud.event.Event.State.Completed);
Event event = persistActionEvent(userId, accountId, null, level, type, Event.State.Completed,
description, startEventId);
return event.getId();
}
public static void publishActionEvent(long userId, long accountId, String eventCategory,
String eventType, Event.State state) {
public static Long onCreatedActionEvent(Long userId, Long accountId, String level, String type, String description) {
if (getEventBusProvider() == null) {
return; // no provider is configured to provider events bus, so just return
publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
com.cloud.event.Event.State.Created);
Event event = persistActionEvent(userId, accountId, null, level, type, Event.State.Created, description, null);
return event.getId();
}
private static Event persistActionEvent(Long userId, Long accountId, Long domainId, String level, String type,
Event.State state, String description, Long startEventId) {
EventVO event = new EventVO();
event.setUserId(userId);
event.setAccountId(accountId);
event.setType(type);
event.setState(state);
event.setDescription(description);
if (domainId != null) {
event.setDomainId(domainId);
} else {
event.setDomainId(getDomainId(accountId));
}
if (level != null && !level.isEmpty()) {
event.setLevel(level);
}
if (startEventId != null) {
event.setStartId(startEventId);
}
event = _eventDao.persist(event);
return event;
}
Map<String, String> eventDescription = new HashMap<String, String>();
Account account = _accountDao.findById(accountId);
User user = _userDao.findById(userId);
eventDescription.put("user", user.getUuid());
eventDescription.put("account", account.getUuid());
eventDescription.put("event", eventType);
eventDescription.put("status", state.toString());
String resourceType = EventTypes.getEntityForEvent(eventType);
private static void publishOnEventBus(long userId, long accountId, String eventCategory,
String eventType, Event.State state) {
if (_eventBus == null) {
return; // no provider is configured to provide events bus, so just return
}
org.apache.cloudstack.framework.events.Event event = new org.apache.cloudstack.framework.events.Event(
ManagementServer.Name,
eventCategory,
eventType,
resourceType, null);
EventTypes.getEntityForEvent(eventType), null);
Map<String, String> eventDescription = new HashMap<String, String>();
Account account = _accountDao.findById(accountId);
User user = _userDao.findById(userId);
eventDescription.put("user", user.getUuid());
eventDescription.put("account", account.getUuid());
eventDescription.put("event", eventType);
eventDescription.put("status", state.toString());
event.setDescription(eventDescription);
try {
@ -170,18 +174,115 @@ public class ActionEventUtils {
}
}
private static EventBus getEventBusProvider() {
if (!_eventBusProviderLoaded) {
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();
private static long getDomainId(long accountId){
AccountVO account = _accountDao.findByIdIncludingRemoved(accountId);
return account.getDomainId();
}
public static class ActionEventCallback implements MethodInterceptor, AnnotationInterceptor<EventVO> {
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
EventVO event = interceptStart(method);
boolean success = true;
try {
return methodProxy.invokeSuper(object, args);
} catch (Exception e){
success = false;
interceptException(method, event);
throw e;
} finally {
if(success){
interceptComplete(method, event);
}
}
_eventBusProviderLoaded = true;
}
return _eventBus;
@Override
public boolean needToIntercept(AnnotatedElement element) {
if (!(element instanceof Method)) {
return false;
}
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
return true;
}
return false;
}
@Override
public EventVO interceptStart(AnnotatedElement element) {
EventVO event = null;
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
boolean async = actionEvent.async();
if(async){
UserContext ctx = UserContext.current();
long userId = ctx.getCallerUserId();
long accountId = ctx.getAccountId();
long startEventId = ctx.getStartEventId();
String eventDescription = actionEvent.eventDescription();
if(ctx.getEventDetails() != null){
eventDescription += ". "+ctx.getEventDetails();
}
ActionEventUtils.onStartedActionEvent(userId, accountId, actionEvent.eventType(), eventDescription, startEventId);
}
}
return event;
}
@Override
public void interceptComplete(AnnotatedElement element, EventVO event) {
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
UserContext ctx = UserContext.current();
long userId = ctx.getCallerUserId();
long accountId = ctx.getAccountId();
long startEventId = ctx.getStartEventId();
String eventDescription = actionEvent.eventDescription();
if(ctx.getEventDetails() != null){
eventDescription += ". "+ctx.getEventDetails();
}
if(actionEvent.create()){
//This start event has to be used for subsequent events of this action
startEventId = ActionEventUtils.onCreatedActionEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully created entity for " + eventDescription);
ctx.setStartEventId(startEventId);
} else {
ActionEventUtils.onCompletedActionEvent(userId, accountId, EventVO.LEVEL_INFO, actionEvent.eventType(), "Successfully completed " + eventDescription, startEventId);
}
}
}
@Override
public void interceptException(AnnotatedElement element, EventVO event) {
Method method = (Method)element;
ActionEvent actionEvent = method.getAnnotation(ActionEvent.class);
if (actionEvent != null) {
UserContext ctx = UserContext.current();
long userId = ctx.getCallerUserId();
long accountId = ctx.getAccountId();
long startEventId = ctx.getStartEventId();
String eventDescription = actionEvent.eventDescription();
if(ctx.getEventDetails() != null){
eventDescription += ". "+ctx.getEventDetails();
}
if(actionEvent.create()){
long eventId = ActionEventUtils.onCreatedActionEvent(userId, accountId, EventVO.LEVEL_ERROR, actionEvent.eventType(), "Error while creating entity for " + eventDescription);
ctx.setStartEventId(eventId);
} else {
ActionEventUtils.onCompletedActionEvent(userId, accountId, EventVO.LEVEL_ERROR, actionEvent.eventType(), "Error while " + eventDescription, startEventId);
}
}
}
@Override
public Callback getCallback() {
return this;
}
}
}

View File

@ -34,16 +34,32 @@ import java.util.Map;
public class AlertGenerator {
private static final Logger s_logger = Logger.getLogger(AlertGenerator.class);
protected static EventBus _eventBus = null;
protected static boolean _eventBusProviderLoaded = false;
private static DataCenterDao _dcDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(DataCenterDao.class);
private static HostPodDao _podDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(HostPodDao.class);
public static void publishAlertOnEventBus(String alertType, long dataCenterId, Long podId, String subject, String body) {
if (getEventBusProvider() == null) {
// 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
}
}
}
public static void publishAlertOnEventBus(String alertType, long dataCenterId, Long podId, String subject, String body) {
if (_eventBus == null) {
return; // no provider is configured to provider events bus, so just return
}
org.apache.cloudstack.framework.events.Event event =
new org.apache.cloudstack.framework.events.Event(ManagementServer.Name,
EventCategory.ALERT_EVENT.getName(),
alertType,
null,
null);
Map<String, String> eventDescription = new HashMap<String, String>();
DataCenterVO dc = _dcDao.findById(dataCenterId);
@ -55,38 +71,17 @@ public class AlertGenerator {
} else {
eventDescription.put("dataCenterId", null);
}
if (pod != null) {
eventDescription.put("podId", pod.getUuid());
} else {
eventDescription.put("podId", null);
}
org.apache.cloudstack.framework.events.Event event =
new org.apache.cloudstack.framework.events.Event(ManagementServer.Name,
EventCategory.ALERT_EVENT.getName(),
alertType,
null,
null);
event.setDescription(eventDescription);
try {
_eventBus.publish(event);
} catch (EventBusException e) {
s_logger.warn("Failed to publish alert on the the event bus.");
}
}
private static EventBus getEventBusProvider() {
if (!_eventBusProviderLoaded) {
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();
}
}
_eventBusProviderLoaded = true;
}
return _eventBus;
}
}

View File

@ -22,10 +22,19 @@ public class UsageEventUtils {
private static UsageEventDao _usageEventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UsageEventDao.class);
private static AccountDao _accountDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(AccountDao.class);
private static DataCenterDao _dcDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(DataCenterDao.class);
private static final Logger s_logger = Logger.getLogger(UsageEventUtils.class);
// get the event bus provider if configured
protected static EventBus _eventBus = null;
protected static boolean _eventBusLoaded = false;
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
}
}
}
public static void publishUsageEvent(String usageType, long accountId, long zoneId,
long resourceId, String resourceName,
@ -83,43 +92,28 @@ public class UsageEventUtils {
private static void publishUsageEvent(String usageEventType, Long accountId, Long zoneId, String resourceType, String resourceUUID) {
if (getEventBusProvider() == null) {
if (_eventBus == null) {
return; // no provider is configured to provider events bus, so just return
}
Account account = _accountDao.findById(accountId);
DataCenterVO dc = _dcDao.findById(zoneId);
Map<String, String> eventDescription = new HashMap<String, String>();
Event event = new Event(ManagementServer.Name, EventCategory.USAGE_EVENT.getName(), usageEventType,
resourceType, resourceUUID);
Map<String, String> eventDescription = new HashMap<String, String>();
eventDescription.put("account", account.getUuid());
eventDescription.put("zone", dc.getUuid());
eventDescription.put("event", usageEventType);
eventDescription.put("resource", resourceType);
eventDescription.put("id", resourceUUID);
Event event = new Event(ManagementServer.Name, EventCategory.USAGE_EVENT.getName(), usageEventType,
resourceType, resourceUUID);
event.setDescription(eventDescription);
try {
_eventBus.publish(event);
} catch (EventBusException e) {
s_logger.warn("Failed to publish usage event on the the event bus.");
}
}
private static EventBus getEventBusProvider() {
if (!_eventBusLoaded) {
_eventBusLoaded = true;
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();
}
}
}
return _eventBus;
}
}

View File

@ -21,14 +21,24 @@ public class NetworkStateListener implements StateListener<State, Event, Network
protected UsageEventDao _usageEventDao;
protected NetworkDao _networkDao;
// 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(NetworkStateListener.class);
public NetworkStateListener(UsageEventDao usageEventDao, NetworkDao networkDao) {
this._usageEventDao = usageEventDao;
this._networkDao = networkDao;
initEventBusProvider();
}
@Override
@ -44,25 +54,28 @@ public class NetworkStateListener implements StateListener<State, Event, Network
}
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.");
}
if (_eventBus == null) {
return; // no provider is configured to provide events bus, so just return
}
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.");
}
}
@ -74,16 +87,4 @@ public class NetworkStateListener implements StateListener<State, Event, Network
}
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();
}
}
}
}

View File

@ -18,12 +18,22 @@ import java.util.Map;
public class VolumeStateListener implements StateListener<State, Event, Volume> {
// 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 VolumeStateListener() {
initEventBusProvider();
}
@Override
@ -39,25 +49,28 @@ public class VolumeStateListener implements StateListener<State, Event, Volume>
}
private void pubishOnEventBus(String event, String status, Volume vo, State oldState, State newState) {
if (_eventBus != null) {
String resourceName = getEntityFromClassName(Volume.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.");
}
if (_eventBus == null) {
return; // no provider is configured to provide events bus, so just return
}
String resourceName = getEntityFromClassName(Volume.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.");
}
}
@ -69,16 +82,4 @@ public class VolumeStateListener implements StateListener<State, Event, Volume>
}
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();
}
}
}
}

View File

@ -44,14 +44,24 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
protected UsageEventDao _usageEventDao;
protected NetworkDao _networkDao;
protected NicDao _nicDao;
protected static EventBus _eventBus = null;
private static final Logger s_logger = Logger.getLogger(UserVmStateListener.class);
// 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
}
}
}
public UserVmStateListener(UsageEventDao usageEventDao, NetworkDao networkDao, NicDao nicDao) {
this._usageEventDao = usageEventDao;
this._networkDao = networkDao;
this._nicDao = nicDao;
initEventBusProvider();
}
@Override
@ -69,7 +79,9 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
if(vo.getType() != VirtualMachine.Type.User){
return true;
}
pubishOnEventBus(event.name(), "postStateTransitionEvent", vo, oldState, newState);
if (VirtualMachine.State.isVmCreated(oldState, event, newState)) {
UsageEventUtils.saveUsageEvent(EventTypes.EVENT_VM_CREATE, vo.getAccountId(), vo.getDataCenterIdToDeployIn(), vo.getId(), vo.getHostName(), vo.getServiceOfferingId(),
vo.getTemplateId(), vo.getHypervisorType().toString());
@ -91,26 +103,30 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
}
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.");
}
if (_eventBus == null) {
return; // no provider is configured to provide events bus, so just return
}
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) {
@ -121,15 +137,4 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
}
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();
}
}
}
}