mirror of
https://github.com/apache/cloudstack.git
synced 2025-12-17 11:04:00 +01:00
- 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:
parent
7079bc273f
commit
e439d9824f
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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();
|
|
||||||
}
|
|
||||||
@ -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();
|
|
||||||
}
|
|
||||||
@ -16,20 +16,20 @@
|
|||||||
// under the License.
|
// under the License.
|
||||||
package com.cloud.configuration;
|
package com.cloud.configuration;
|
||||||
|
|
||||||
import java.util.List;
|
import com.cloud.event.ActionEventUtils;
|
||||||
|
|
||||||
import com.cloud.event.ActionEventCallback;
|
|
||||||
import com.cloud.utils.component.AnnotationInterceptor;
|
import com.cloud.utils.component.AnnotationInterceptor;
|
||||||
import com.cloud.utils.component.InterceptorLibrary;
|
import com.cloud.utils.component.InterceptorLibrary;
|
||||||
import com.cloud.utils.db.DatabaseCallback;
|
import com.cloud.utils.db.DatabaseCallback;
|
||||||
import org.apache.cloudstack.framework.events.EventPublishCallback;
|
import org.apache.cloudstack.framework.events.EventPublishCallback;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class DefaultInterceptorLibrary implements InterceptorLibrary {
|
public class DefaultInterceptorLibrary implements InterceptorLibrary {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInterceptors(List<AnnotationInterceptor<?>> interceptors) {
|
public void addInterceptors(List<AnnotationInterceptor<?>> interceptors) {
|
||||||
interceptors.add(new DatabaseCallback());
|
interceptors.add(new DatabaseCallback());
|
||||||
interceptors.add(new ActionEventCallback());
|
interceptors.add(new ActionEventUtils.ActionEventCallback());
|
||||||
interceptors.add(new EventPublishCallback());
|
interceptors.add(new EventPublishCallback());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -22,145 +22,149 @@ import com.cloud.server.ManagementServer;
|
|||||||
import com.cloud.user.Account;
|
import com.cloud.user.Account;
|
||||||
import com.cloud.user.AccountVO;
|
import com.cloud.user.AccountVO;
|
||||||
import com.cloud.user.User;
|
import com.cloud.user.User;
|
||||||
|
import com.cloud.user.UserContext;
|
||||||
import com.cloud.user.dao.AccountDao;
|
import com.cloud.user.dao.AccountDao;
|
||||||
import com.cloud.user.dao.UserDao;
|
import com.cloud.user.dao.UserDao;
|
||||||
import com.cloud.utils.component.Adapters;
|
import com.cloud.utils.component.Adapters;
|
||||||
|
import com.cloud.utils.component.AnnotationInterceptor;
|
||||||
import com.cloud.utils.component.ComponentLocator;
|
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.EventBus;
|
||||||
import org.apache.cloudstack.framework.events.EventBusException;
|
import org.apache.cloudstack.framework.events.EventBusException;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import java.lang.reflect.AnnotatedElement;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class ActionEventUtils {
|
public class ActionEventUtils {
|
||||||
|
|
||||||
private static final Logger s_logger = Logger.getLogger(ActionEventUtils.class);
|
|
||||||
|
|
||||||
private static EventDao _eventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(EventDao.class);
|
private static EventDao _eventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(EventDao.class);
|
||||||
private static AccountDao _accountDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(AccountDao.class);
|
private static AccountDao _accountDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(AccountDao.class);
|
||||||
protected static UserDao _userDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UserDao.class);;
|
protected static UserDao _userDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UserDao.class);
|
||||||
|
private static final Logger s_logger = Logger.getLogger(ActionEventUtils.class);
|
||||||
|
|
||||||
|
// get the event bus provider if configured
|
||||||
protected static EventBus _eventBus = null;
|
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) {
|
public static Long onActionEvent(Long userId, Long accountId, Long domainId, String type, String description) {
|
||||||
|
|
||||||
publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(),
|
publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(),
|
||||||
type, com.cloud.event.Event.State.Scheduled);
|
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();
|
return event.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Save event after scheduling an async job
|
* 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);
|
com.cloud.event.Event.State.Scheduled);
|
||||||
|
|
||||||
EventVO event = new EventVO();
|
Event event = persistActionEvent(userId, accountId, null, null, type, Event.State.Scheduled,
|
||||||
event.setUserId(userId);
|
description, startEventId);
|
||||||
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);
|
|
||||||
return event.getId();
|
return event.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Save event after starting execution of an async job
|
* 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);
|
com.cloud.event.Event.State.Started);
|
||||||
|
|
||||||
EventVO event = new EventVO();
|
Event event = persistActionEvent(userId, accountId, null, null, type, Event.State.Started,
|
||||||
event.setUserId(userId);
|
description, startEventId);
|
||||||
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();
|
return event.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Long onCompletedActionEvent(Long userId, Long accountId, String level, String type, String description, long startEventId) {
|
public static Long onCompletedActionEvent(Long userId, Long accountId, String level, 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.Completed);
|
com.cloud.event.Event.State.Completed);
|
||||||
|
|
||||||
EventVO event = new EventVO();
|
Event event = persistActionEvent(userId, accountId, null, level, type, Event.State.Completed,
|
||||||
event.setUserId(userId);
|
description, startEventId);
|
||||||
event.setAccountId(accountId);
|
|
||||||
event.setDomainId(getDomainId(accountId));
|
return event.getId();
|
||||||
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) {
|
public static Long onCreatedActionEvent(Long userId, Long accountId, String level, String type, String description) {
|
||||||
|
|
||||||
publishActionEvent(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
|
publishOnEventBus(userId, accountId, EventCategory.ACTION_EVENT.getName(), type,
|
||||||
com.cloud.event.Event.State.Created);
|
com.cloud.event.Event.State.Created);
|
||||||
|
|
||||||
EventVO event = new EventVO();
|
Event event = persistActionEvent(userId, accountId, null, level, type, Event.State.Created, description, null);
|
||||||
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);
|
|
||||||
return event.getId();
|
return event.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long getDomainId(long accountId){
|
private static Event persistActionEvent(Long userId, Long accountId, Long domainId, String level, String type,
|
||||||
AccountVO account = _accountDao.findByIdIncludingRemoved(accountId);
|
Event.State state, String description, Long startEventId) {
|
||||||
return account.getDomainId();
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void publishActionEvent(long userId, long accountId, String eventCategory,
|
private static void publishOnEventBus(long userId, long accountId, String eventCategory,
|
||||||
String eventType, Event.State state) {
|
String eventType, Event.State state) {
|
||||||
|
if (_eventBus == null) {
|
||||||
if (getEventBusProvider() == null) {
|
return; // no provider is configured to provide events bus, so just return
|
||||||
return; // no provider is configured to provider events bus, so just return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
org.apache.cloudstack.framework.events.Event event = new org.apache.cloudstack.framework.events.Event(
|
org.apache.cloudstack.framework.events.Event event = new org.apache.cloudstack.framework.events.Event(
|
||||||
ManagementServer.Name,
|
ManagementServer.Name,
|
||||||
eventCategory,
|
eventCategory,
|
||||||
eventType,
|
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);
|
event.setDescription(eventDescription);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -170,18 +174,115 @@ public class ActionEventUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static EventBus getEventBusProvider() {
|
private static long getDomainId(long accountId){
|
||||||
if (!_eventBusProviderLoaded) {
|
AccountVO account = _accountDao.findByIdIncludingRemoved(accountId);
|
||||||
ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name);
|
return account.getDomainId();
|
||||||
Adapters<EventBus> eventBusImpls = locator.getAdapters(EventBus.class);
|
}
|
||||||
if (eventBusImpls != null) {
|
|
||||||
Enumeration<EventBus> eventBusenum = eventBusImpls.enumeration();
|
public static class ActionEventCallback implements MethodInterceptor, AnnotationInterceptor<EventVO> {
|
||||||
if (eventBusenum != null && eventBusenum.hasMoreElements()) {
|
|
||||||
_eventBus = eventBusenum.nextElement();
|
@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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,16 +34,32 @@ import java.util.Map;
|
|||||||
public class AlertGenerator {
|
public class AlertGenerator {
|
||||||
|
|
||||||
private static final Logger s_logger = Logger.getLogger(AlertGenerator.class);
|
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 DataCenterDao _dcDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(DataCenterDao.class);
|
||||||
private static HostPodDao _podDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(HostPodDao.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) {
|
// get the event bus provider if configured
|
||||||
if (getEventBusProvider() == null) {
|
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>();
|
Map<String, String> eventDescription = new HashMap<String, String>();
|
||||||
DataCenterVO dc = _dcDao.findById(dataCenterId);
|
DataCenterVO dc = _dcDao.findById(dataCenterId);
|
||||||
@ -55,38 +71,17 @@ public class AlertGenerator {
|
|||||||
} else {
|
} else {
|
||||||
eventDescription.put("dataCenterId", null);
|
eventDescription.put("dataCenterId", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pod != null) {
|
if (pod != null) {
|
||||||
eventDescription.put("podId", pod.getUuid());
|
eventDescription.put("podId", pod.getUuid());
|
||||||
} else {
|
} else {
|
||||||
eventDescription.put("podId", null);
|
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);
|
event.setDescription(eventDescription);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_eventBus.publish(event);
|
_eventBus.publish(event);
|
||||||
} catch (EventBusException e) {
|
} catch (EventBusException e) {
|
||||||
s_logger.warn("Failed to publish alert on the the event bus.");
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,10 +22,19 @@ public class UsageEventUtils {
|
|||||||
private static UsageEventDao _usageEventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UsageEventDao.class);
|
private static UsageEventDao _usageEventDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(UsageEventDao.class);
|
||||||
private static AccountDao _accountDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(AccountDao.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 DataCenterDao _dcDao = ComponentLocator.getLocator(ManagementServer.Name).getDao(DataCenterDao.class);
|
||||||
|
|
||||||
private static final Logger s_logger = Logger.getLogger(UsageEventUtils.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 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,
|
public static void publishUsageEvent(String usageType, long accountId, long zoneId,
|
||||||
long resourceId, String resourceName,
|
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) {
|
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
|
return; // no provider is configured to provider events bus, so just return
|
||||||
}
|
}
|
||||||
|
|
||||||
Account account = _accountDao.findById(accountId);
|
Account account = _accountDao.findById(accountId);
|
||||||
DataCenterVO dc = _dcDao.findById(zoneId);
|
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("account", account.getUuid());
|
||||||
eventDescription.put("zone", dc.getUuid());
|
eventDescription.put("zone", dc.getUuid());
|
||||||
eventDescription.put("event", usageEventType);
|
eventDescription.put("event", usageEventType);
|
||||||
eventDescription.put("resource", resourceType);
|
eventDescription.put("resource", resourceType);
|
||||||
eventDescription.put("id", resourceUUID);
|
eventDescription.put("id", resourceUUID);
|
||||||
|
|
||||||
Event event = new Event(ManagementServer.Name, EventCategory.USAGE_EVENT.getName(), usageEventType,
|
|
||||||
resourceType, resourceUUID);
|
|
||||||
event.setDescription(eventDescription);
|
event.setDescription(eventDescription);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_eventBus.publish(event);
|
_eventBus.publish(event);
|
||||||
} catch (EventBusException e) {
|
} catch (EventBusException e) {
|
||||||
s_logger.warn("Failed to publish usage event on the the event bus.");
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,14 +21,24 @@ public class NetworkStateListener implements StateListener<State, Event, Network
|
|||||||
|
|
||||||
protected UsageEventDao _usageEventDao;
|
protected UsageEventDao _usageEventDao;
|
||||||
protected NetworkDao _networkDao;
|
protected NetworkDao _networkDao;
|
||||||
|
|
||||||
|
// get the event bus provider if configured
|
||||||
protected static EventBus _eventBus = null;
|
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);
|
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
|
||||||
@ -44,7 +54,11 @@ public class NetworkStateListener implements StateListener<State, Event, Network
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void pubishOnEventBus(String event, String status, Network vo, State oldState, State newState) {
|
private void pubishOnEventBus(String event, String status, Network vo, State oldState, State newState) {
|
||||||
if (_eventBus != null) {
|
|
||||||
|
if (_eventBus == null) {
|
||||||
|
return; // no provider is configured to provide events bus, so just return
|
||||||
|
}
|
||||||
|
|
||||||
String resourceName = getEntityFromClassName(Network.class.getName());
|
String resourceName = getEntityFromClassName(Network.class.getName());
|
||||||
org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(
|
org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(
|
||||||
ManagementServer.Name,
|
ManagementServer.Name,
|
||||||
@ -64,7 +78,6 @@ public class NetworkStateListener implements StateListener<State, Event, Network
|
|||||||
s_logger.warn("Failed to publish action event on the the event bus.");
|
s_logger.warn("Failed to publish action event on the the event bus.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private String getEntityFromClassName(String entityClassName) {
|
private String getEntityFromClassName(String entityClassName) {
|
||||||
int index = entityClassName.lastIndexOf(".");
|
int index = entityClassName.lastIndexOf(".");
|
||||||
@ -74,16 +87,4 @@ public class NetworkStateListener implements StateListener<State, Event, Network
|
|||||||
}
|
}
|
||||||
return entityName;
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,12 +18,22 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class VolumeStateListener implements StateListener<State, Event, Volume> {
|
public class VolumeStateListener implements StateListener<State, Event, Volume> {
|
||||||
|
|
||||||
|
// get the event bus provider if configured
|
||||||
protected static EventBus _eventBus = null;
|
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);
|
private static final Logger s_logger = Logger.getLogger(VolumeStateListener.class);
|
||||||
|
|
||||||
public VolumeStateListener() {
|
public VolumeStateListener() {
|
||||||
initEventBusProvider();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -39,7 +49,11 @@ public class VolumeStateListener implements StateListener<State, Event, Volume>
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void pubishOnEventBus(String event, String status, Volume vo, State oldState, State newState) {
|
private void pubishOnEventBus(String event, String status, Volume vo, State oldState, State newState) {
|
||||||
if (_eventBus != null) {
|
|
||||||
|
if (_eventBus == null) {
|
||||||
|
return; // no provider is configured to provide events bus, so just return
|
||||||
|
}
|
||||||
|
|
||||||
String resourceName = getEntityFromClassName(Volume.class.getName());
|
String resourceName = getEntityFromClassName(Volume.class.getName());
|
||||||
org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(
|
org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(
|
||||||
ManagementServer.Name,
|
ManagementServer.Name,
|
||||||
@ -59,7 +73,6 @@ public class VolumeStateListener implements StateListener<State, Event, Volume>
|
|||||||
s_logger.warn("Failed to publish action event on the the event bus.");
|
s_logger.warn("Failed to publish action event on the the event bus.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private String getEntityFromClassName(String entityClassName) {
|
private String getEntityFromClassName(String entityClassName) {
|
||||||
int index = entityClassName.lastIndexOf(".");
|
int index = entityClassName.lastIndexOf(".");
|
||||||
@ -69,16 +82,4 @@ public class VolumeStateListener implements StateListener<State, Event, Volume>
|
|||||||
}
|
}
|
||||||
return entityName;
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,14 +44,24 @@ public class UserVmStateListener implements StateListener<State, 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);
|
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) {
|
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
|
||||||
@ -70,6 +80,8 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pubishOnEventBus(event.name(), "postStateTransitionEvent", vo, oldState, newState);
|
||||||
|
|
||||||
if (VirtualMachine.State.isVmCreated(oldState, event, newState)) {
|
if (VirtualMachine.State.isVmCreated(oldState, event, newState)) {
|
||||||
UsageEventUtils.saveUsageEvent(EventTypes.EVENT_VM_CREATE, vo.getAccountId(), vo.getDataCenterIdToDeployIn(), vo.getId(), vo.getHostName(), vo.getServiceOfferingId(),
|
UsageEventUtils.saveUsageEvent(EventTypes.EVENT_VM_CREATE, vo.getAccountId(), vo.getDataCenterIdToDeployIn(), vo.getId(), vo.getHostName(), vo.getServiceOfferingId(),
|
||||||
vo.getTemplateId(), vo.getHypervisorType().toString());
|
vo.getTemplateId(), vo.getHypervisorType().toString());
|
||||||
@ -91,7 +103,11 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void pubishOnEventBus(String event, String status, VirtualMachine vo, VirtualMachine.State oldState, VirtualMachine.State newState) {
|
private void pubishOnEventBus(String event, String status, VirtualMachine vo, VirtualMachine.State oldState, VirtualMachine.State newState) {
|
||||||
if (_eventBus != null) {
|
|
||||||
|
if (_eventBus == null) {
|
||||||
|
return; // no provider is configured to provide events bus, so just return
|
||||||
|
}
|
||||||
|
|
||||||
String resourceName = getEntityFromClassName(Network.class.getName());
|
String resourceName = getEntityFromClassName(Network.class.getName());
|
||||||
org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(
|
org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(
|
||||||
ManagementServer.Name,
|
ManagementServer.Name,
|
||||||
@ -110,7 +126,7 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
|
|||||||
} catch (EventBusException e) {
|
} catch (EventBusException e) {
|
||||||
s_logger.warn("Failed to publish action event on the the event bus.");
|
s_logger.warn("Failed to publish action event on the the event bus.");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getEntityFromClassName(String entityClassName) {
|
private String getEntityFromClassName(String entityClassName) {
|
||||||
@ -121,15 +137,4 @@ public class UserVmStateListener implements StateListener<State, VirtualMachine.
|
|||||||
}
|
}
|
||||||
return entityName;
|
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