From e439d9824f2068c2700636548327f80d0e39677d Mon Sep 17 00:00:00 2001 From: Murali Reddy Date: Thu, 31 Jan 2013 15:54:57 +0530 Subject: [PATCH] - remove Publish, Subscrie annotation as they are not used - merge ActionEvent callback into ActionEventUtils - static initialize event bus from component locator --- .../events/EventPublishCallback.java | 102 ------ .../cloudstack/framework/events/Publish.java | 38 --- .../framework/events/Subscribe.java | 35 -- .../DefaultInterceptorLibrary.java | 8 +- .../com/cloud/event/ActionEventCallback.java | 136 -------- .../src/com/cloud/event/ActionEventUtils.java | 299 ++++++++++++------ .../src/com/cloud/event/AlertGenerator.java | 51 ++- .../src/com/cloud/event/UsageEventUtils.java | 38 +-- .../cloud/network/NetworkStateListener.java | 65 ++-- .../storage/listener/VolumeStateListener.java | 65 ++-- .../src/com/cloud/vm/UserVmStateListener.java | 71 +++-- 11 files changed, 347 insertions(+), 561 deletions(-) delete mode 100644 framework/events/src/org/apache/cloudstack/framework/events/EventPublishCallback.java delete mode 100644 framework/events/src/org/apache/cloudstack/framework/events/Publish.java delete mode 100644 framework/events/src/org/apache/cloudstack/framework/events/Subscribe.java delete mode 100644 server/src/com/cloud/event/ActionEventCallback.java diff --git a/framework/events/src/org/apache/cloudstack/framework/events/EventPublishCallback.java b/framework/events/src/org/apache/cloudstack/framework/events/EventPublishCallback.java deleted file mode 100644 index 81c8ac4c2bf..00000000000 --- a/framework/events/src/org/apache/cloudstack/framework/events/EventPublishCallback.java +++ /dev/null @@ -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 { - - 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 eventBusImpls = locator.getAdapters(EventBus.class); - if (eventBusImpls != null) { - Enumeration eventBusenum = eventBusImpls.enumeration(); - _eventBus = eventBusenum.nextElement(); - } - } - return _eventBus; - } -} diff --git a/framework/events/src/org/apache/cloudstack/framework/events/Publish.java b/framework/events/src/org/apache/cloudstack/framework/events/Publish.java deleted file mode 100644 index 8f1097211fc..00000000000 --- a/framework/events/src/org/apache/cloudstack/framework/events/Publish.java +++ /dev/null @@ -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(); -} \ No newline at end of file diff --git a/framework/events/src/org/apache/cloudstack/framework/events/Subscribe.java b/framework/events/src/org/apache/cloudstack/framework/events/Subscribe.java deleted file mode 100644 index 74997f62a54..00000000000 --- a/framework/events/src/org/apache/cloudstack/framework/events/Subscribe.java +++ /dev/null @@ -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(); -} diff --git a/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java b/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java index 380552d1ade..6feff4c3d78 100644 --- a/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java +++ b/server/src/com/cloud/configuration/DefaultInterceptorLibrary.java @@ -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> interceptors) { interceptors.add(new DatabaseCallback()); - interceptors.add(new ActionEventCallback()); + interceptors.add(new ActionEventUtils.ActionEventCallback()); interceptors.add(new EventPublishCallback()); } } diff --git a/server/src/com/cloud/event/ActionEventCallback.java b/server/src/com/cloud/event/ActionEventCallback.java deleted file mode 100644 index 72ce4187ec9..00000000000 --- a/server/src/com/cloud/event/ActionEventCallback.java +++ /dev/null @@ -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 { - - 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; - } -} diff --git a/server/src/com/cloud/event/ActionEventUtils.java b/server/src/com/cloud/event/ActionEventUtils.java index f7157ba83c6..744f46f3bc2 100755 --- a/server/src/com/cloud/event/ActionEventUtils.java +++ b/server/src/com/cloud/event/ActionEventUtils.java @@ -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 eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration 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 eventDescription = new HashMap(); - 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 eventDescription = new HashMap(); + 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 eventBusImpls = locator.getAdapters(EventBus.class); - if (eventBusImpls != null) { - Enumeration 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 { + + @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; + } } } diff --git a/server/src/com/cloud/event/AlertGenerator.java b/server/src/com/cloud/event/AlertGenerator.java index 640f60406d9..42863778cfd 100644 --- a/server/src/com/cloud/event/AlertGenerator.java +++ b/server/src/com/cloud/event/AlertGenerator.java @@ -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 eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration 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 eventDescription = new HashMap(); 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 eventBusImpls = locator.getAdapters(EventBus.class); - if (eventBusImpls != null) { - Enumeration eventBusenum = eventBusImpls.enumeration(); - if (eventBusenum != null && eventBusenum.hasMoreElements()) { - _eventBus = eventBusenum.nextElement(); - } - } - _eventBusProviderLoaded = true; - } - return _eventBus; - } } diff --git a/server/src/com/cloud/event/UsageEventUtils.java b/server/src/com/cloud/event/UsageEventUtils.java index ebe8f0c59e5..904525ea2f0 100644 --- a/server/src/com/cloud/event/UsageEventUtils.java +++ b/server/src/com/cloud/event/UsageEventUtils.java @@ -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 eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration 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 eventDescription = new HashMap(); + Event event = new Event(ManagementServer.Name, EventCategory.USAGE_EVENT.getName(), usageEventType, + resourceType, resourceUUID); + Map eventDescription = new HashMap(); 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 eventBusImpls = locator.getAdapters(EventBus.class); - if (eventBusImpls != null) { - Enumeration eventBusenum = eventBusImpls.enumeration(); - if (eventBusenum != null && eventBusenum.hasMoreElements()) { - _eventBus = eventBusenum.nextElement(); - } - } - } - return _eventBus; - } } diff --git a/server/src/com/cloud/network/NetworkStateListener.java b/server/src/com/cloud/network/NetworkStateListener.java index 7235fea963e..b0522b2a0c4 100644 --- a/server/src/com/cloud/network/NetworkStateListener.java +++ b/server/src/com/cloud/network/NetworkStateListener.java @@ -21,14 +21,24 @@ public class NetworkStateListener implements StateListener eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration 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 eventDescription = new HashMap(); - 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 eventDescription = new HashMap(); + 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 eventBusImpls = locator.getAdapters(EventBus.class); - if (eventBusImpls != null) { - Enumeration eventBusenum = eventBusImpls.enumeration(); - if (eventBusenum != null && eventBusenum.hasMoreElements()) { - _eventBus = eventBusenum.nextElement(); - } - } - } - } diff --git a/server/src/com/cloud/storage/listener/VolumeStateListener.java b/server/src/com/cloud/storage/listener/VolumeStateListener.java index da0ed113d3c..46e2b978d2c 100644 --- a/server/src/com/cloud/storage/listener/VolumeStateListener.java +++ b/server/src/com/cloud/storage/listener/VolumeStateListener.java @@ -18,12 +18,22 @@ import java.util.Map; public class VolumeStateListener implements StateListener { + // get the event bus provider if configured protected static EventBus _eventBus = null; + static { + Adapters eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration 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 } 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 eventDescription = new HashMap(); - 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 eventDescription = new HashMap(); + 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 } return entityName; } - - private void initEventBusProvider() { - ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name); - Adapters eventBusImpls = locator.getAdapters(EventBus.class); - if (eventBusImpls != null) { - Enumeration eventBusenum = eventBusImpls.enumeration(); - if (eventBusenum != null && eventBusenum.hasMoreElements()) { - _eventBus = eventBusenum.nextElement(); - } - } - } - } diff --git a/server/src/com/cloud/vm/UserVmStateListener.java b/server/src/com/cloud/vm/UserVmStateListener.java index 8f8e3febba5..ab6b8a9e217 100644 --- a/server/src/com/cloud/vm/UserVmStateListener.java +++ b/server/src/com/cloud/vm/UserVmStateListener.java @@ -44,14 +44,24 @@ public class UserVmStateListener implements StateListener eventBusImpls = ComponentLocator.getLocator(ManagementServer.Name).getAdapters(EventBus.class); + if (eventBusImpls != null) { + Enumeration 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 eventDescription = new HashMap(); - 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 eventDescription = new HashMap(); + 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 eventBusImpls = locator.getAdapters(EventBus.class); - if (eventBusImpls != null) { - Enumeration eventBusenum = eventBusImpls.enumeration(); - if (eventBusenum != null && eventBusenum.hasMoreElements()) { - _eventBus = eventBusenum.nextElement(); - } - } - } }