Ravi Nori has uploaded a new change for review. Change subject: core : Add Entity API for CoCo commands ......................................................................
core : Add Entity API for CoCo commands When executing CoCo commands the entity on which the command is run on can be provided. This information is persisted in the db Change-Id: I96b934ffc44f87407df77c8e097164ca30f3a6e6 Signed-off-by: Ravi Nori <[email protected]> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorUtil.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCache.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCacheImpl.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandCRUDOperations.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandScheduler.java A backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CoCoCmdEntity.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDao.java M backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java M backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/CommandEntityDaoTest.java M packaging/dbscripts/command_entities_sp.sql A packaging/dbscripts/upgrade/03_06_0830_add_coco_cmd_entities_table.sql 13 files changed, 318 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/63/37463/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java index cd2dbb7..419f922 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorImpl.java @@ -1,6 +1,7 @@ package org.ovirt.engine.core.bll.tasks; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; @@ -26,6 +27,7 @@ import org.ovirt.engine.core.common.asynctasks.AsyncTaskType; import org.ovirt.engine.core.common.businessentities.AsyncTask; import org.ovirt.engine.core.common.businessentities.AsyncTaskStatus; +import org.ovirt.engine.core.common.businessentities.CoCoCmdEntity; import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.common.vdscommands.IrsBaseVDSCommandParameters; import org.ovirt.engine.core.common.vdscommands.SPMTaskGuidBaseVDSCommandParameters; @@ -94,6 +96,10 @@ } } + public void persistCoCoCmdEntities(Collection<CoCoCmdEntity> coCoCmdEntities) { + commandsCache.persistCoCoCmdEntities(coCoCmdEntities); + } + void saveCommandContext(Guid cmdId, CommandContext cmdContext) { if (cmdContext != null) { contextsCache.put(cmdId, cmdContext); @@ -111,6 +117,19 @@ return cmdExecutor.executeAsyncCommand(actionType, parameters, cmdContext); } + /** + * Executes the action using a Thread Pool. Used when the calling function + * would like the execute the command with no delay + */ + @Override + public Future<VdcReturnValueBase> executeAsyncCommand(VdcActionType actionType, + VdcActionParametersBase parameters, + CommandContext cmdContext, + VdcObjectType entityType, + Guid... entityIds) { + return cmdExecutor.executeAsyncCommand(actionType, parameters, cmdContext, entityType, entityIds); + } + @Override public CommandEntity getCommandEntity(Guid commandId) { return Guid.isNullOrEmpty(commandId) ? null : commandsCache.get(commandId); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorUtil.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorUtil.java index c928eba..23a3374 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorUtil.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandCoordinatorUtil.java @@ -188,6 +188,14 @@ return coco.executeAsyncCommand(actionType, parameters, cmdContext); } + public static Future<VdcReturnValueBase> executeAsyncCommand(VdcActionType actionType, + VdcActionParametersBase parameters, + CommandContext cmdContext, + VdcObjectType entityType, + Guid... entityIds) { + return coco.executeAsyncCommand(actionType, parameters, cmdContext, entityType, entityIds); + } + public static VdcReturnValueBase getCommandReturnValue(Guid cmdId) { CommandEntity cmdEnity = coco.getCommandEntity(cmdId); return cmdEnity == null ? null : cmdEnity.getReturnValue(); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java index 6e6239e..78cd184 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandExecutor.java @@ -1,6 +1,11 @@ package org.ovirt.engine.core.bll.tasks; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.Callable; @@ -19,9 +24,11 @@ import org.ovirt.engine.core.bll.context.CommandContext; import org.ovirt.engine.core.bll.tasks.interfaces.CommandCallBack; import org.ovirt.engine.core.bll.utils.BackendUtils; +import org.ovirt.engine.core.common.VdcObjectType; import org.ovirt.engine.core.common.action.VdcActionParametersBase; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.action.VdcReturnValueBase; +import org.ovirt.engine.core.common.businessentities.CoCoCmdEntity; import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.common.config.Config; import org.ovirt.engine.core.common.config.ConfigValues; @@ -142,9 +149,33 @@ public Future<VdcReturnValueBase> executeAsyncCommand(final VdcActionType actionType, final VdcActionParametersBase parameters, final CommandContext cmdContext) { + return executeAsyncCommand(actionType, parameters, cmdContext, new HashMap<Guid, VdcObjectType>()); + } + + public Future<VdcReturnValueBase> executeAsyncCommand(final VdcActionType actionType, + final VdcActionParametersBase parameters, + final CommandContext cmdContext, + VdcObjectType entityType, + Guid... entityIds) { + return executeAsyncCommand(actionType, parameters, cmdContext, createEntitiesMapForSingleEntityType(entityType, entityIds)); + } + + private Map<Guid, VdcObjectType> createEntitiesMapForSingleEntityType(VdcObjectType entityType, Guid... entityIds) { + Map<Guid, VdcObjectType> entitiesMap = new HashMap<>(); + for (Guid entityId : entityIds) { + entitiesMap.put(entityId, entityType); + } + return entitiesMap; + } + + private Future<VdcReturnValueBase> executeAsyncCommand(final VdcActionType actionType, + final VdcActionParametersBase parameters, + final CommandContext cmdContext, + final Map<Guid, VdcObjectType> entityMap) { final CommandBase<?> command = CommandsFactory.createCommand(actionType, parameters, cmdContext); CommandCallBack callBack = command.getCallBack(); command.persistCommand(command.getParameters().getParentCommand(), cmdContext, callBack != null); + coco.persistCoCoCmdEntities(buildCoCoCmdEntities(command.getCommandId(), entityMap)); if (callBack != null) { cmdCallBackMap.put(command.getCommandId(), callBack); } @@ -166,6 +197,18 @@ return retVal; } + private Collection<CoCoCmdEntity> buildCoCoCmdEntities(Guid cmdId, Map<Guid, VdcObjectType> entityMap) { + if (entityMap == null) { + entityMap = Collections.emptyMap(); + } + List<CoCoCmdEntity> results = new ArrayList<>(entityMap.size()); + for (Map.Entry<Guid, VdcObjectType> entry : entityMap.entrySet()) { + results.add(new CoCoCmdEntity(cmdId, entry.getValue(), entry.getKey())); + + } + return results; + } + private VdcReturnValueBase executeCommand(final CommandBase<?> command, final CommandContext cmdContext) { CommandCallBack callBack = command.getCallBack(); VdcReturnValueBase result = BackendUtils.getBackendCommandObjectsHandler(log).runAction(command, null); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCache.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCache.java index 1468a88..f89f42e 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCache.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCache.java @@ -1,10 +1,12 @@ package org.ovirt.engine.core.bll.tasks; +import org.ovirt.engine.core.common.businessentities.CoCoCmdEntity; import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.compat.CommandStatus; import org.ovirt.engine.core.compat.DateTime; import org.ovirt.engine.core.compat.Guid; +import java.util.Collection; import java.util.List; import java.util.Set; @@ -18,6 +20,8 @@ public void put(CommandEntity cmdEntity); + public void persistCoCoCmdEntities(Collection<CoCoCmdEntity> coCoCmdEntities); + public void removeAllCommandsBeforeDate(DateTime cutoff); public void updateCommandStatus(Guid commandId, CommandStatus status); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCacheImpl.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCacheImpl.java index 0b91f0e..c728db0 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCacheImpl.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/CommandsCacheImpl.java @@ -1,8 +1,10 @@ package org.ovirt.engine.core.bll.tasks; +import java.util.Collection; import java.util.List; import java.util.Set; +import org.ovirt.engine.core.common.businessentities.CoCoCmdEntity; import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.compat.CommandStatus; import org.ovirt.engine.core.compat.DateTime; @@ -101,6 +103,10 @@ } } + public void persistCoCoCmdEntities(Collection<CoCoCmdEntity> coCoCmdEntities) { + DbFacade.getInstance().getCommandEntityDao().insertCoCoCmdEntities(coCoCmdEntities); + } + @Override public List<CommandEntity> getChildCmdsByParentCmdId(Guid cmdId) { return DbFacade.getInstance().getCommandEntityDao().getCmdEntitiesByParentCmdId(cmdId); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandCRUDOperations.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandCRUDOperations.java index 7698a5a..57b256a 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandCRUDOperations.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandCRUDOperations.java @@ -4,11 +4,13 @@ import org.ovirt.engine.core.bll.context.CommandContext; import org.ovirt.engine.core.common.action.VdcActionParametersBase; import org.ovirt.engine.core.common.action.VdcActionType; +import org.ovirt.engine.core.common.businessentities.CoCoCmdEntity; import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.compat.CommandStatus; import org.ovirt.engine.core.compat.DateTime; import org.ovirt.engine.core.compat.Guid; +import java.util.Collection; import java.util.List; public interface CommandCRUDOperations { @@ -20,6 +22,7 @@ public List<CommandEntity> getCommandsWithCallBackEnabled(); public void persistCommand(CommandEntity cmdEntity); public void persistCommand(CommandEntity cmdEntity, CommandContext cmdContext); + public void persistCoCoCmdEntities(Collection<CoCoCmdEntity> coCoCmdEntities);; public CommandBase<?> retrieveCommand(Guid commandId); public void removeCommand(Guid commandId); public void removeAllCommandsInHierarchy(Guid commandId); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandScheduler.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandScheduler.java index f5f5766..80313a0 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandScheduler.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/tasks/interfaces/CommandScheduler.java @@ -1,12 +1,15 @@ package org.ovirt.engine.core.bll.tasks.interfaces; import org.ovirt.engine.core.bll.context.CommandContext; +import org.ovirt.engine.core.common.VdcObjectType; import org.ovirt.engine.core.common.action.VdcActionParametersBase; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.action.VdcReturnValueBase; +import org.ovirt.engine.core.compat.Guid; import java.util.concurrent.Future; public interface CommandScheduler { public abstract Future<VdcReturnValueBase> executeAsyncCommand(VdcActionType actionType, VdcActionParametersBase parameters, CommandContext cmdContext); + public abstract Future<VdcReturnValueBase> executeAsyncCommand(VdcActionType actionType, VdcActionParametersBase parameters, CommandContext cmdContext, VdcObjectType entityType, Guid... entityIds); } diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CoCoCmdEntity.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CoCoCmdEntity.java new file mode 100644 index 0000000..da2ff98 --- /dev/null +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/businessentities/CoCoCmdEntity.java @@ -0,0 +1,77 @@ +package org.ovirt.engine.core.common.businessentities; + +import org.ovirt.engine.core.common.VdcObjectType; +import org.ovirt.engine.core.common.utils.ObjectUtils; +import org.ovirt.engine.core.compat.Guid; + +public class CoCoCmdEntity { + + private Guid commandId; + private VdcObjectType entityType; + private Guid entityId; + + public CoCoCmdEntity() { + } + + public CoCoCmdEntity(Guid commandId, VdcObjectType entityType, Guid entityId) { + this.commandId = commandId; + this.entityType = entityType; + this.entityId = entityId; + } + + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((entityId == null) ? 0 : entityId.hashCode()); + result = prime * result + + ((entityType == null) ? 0 : entityType.hashCode()); + result = prime * result + ((commandId == null) ? 0 : commandId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + CoCoCmdEntity other = (CoCoCmdEntity) obj; + return ObjectUtils.objectsEqual(commandId, other.commandId) && + ObjectUtils.objectsEqual(entityId, other.entityId) && + ObjectUtils.objectsEqual(entityType, other.entityType); + } + + + public Guid getEntityId() { + return entityId; + } + + public void setEntityId(Guid entityId) { + this.entityId = entityId; + } + + public Guid getCommandId() { + return commandId; + } + + public void setCommandId(Guid commandId) { + this.commandId = commandId; + } + + public VdcObjectType getEntityType() { + return entityType; + } + + public void setEntityType(VdcObjectType entityType) { + this.entityType = entityType; + } + +} diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDao.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDao.java index 3747899..ada0e01 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDao.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDao.java @@ -1,8 +1,10 @@ package org.ovirt.engine.core.dao; +import java.util.Collection; import java.util.Date; import java.util.List; +import org.ovirt.engine.core.common.businessentities.CoCoCmdEntity; import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.compat.CommandStatus; import org.ovirt.engine.core.compat.Guid; @@ -21,4 +23,9 @@ List<CommandEntity> getCmdEntitiesByParentCmdId(Guid parentId); + List<Guid> getCommandIdsByEntity(Guid entityId); + + void insertCoCoCmdEntities(Collection<CoCoCmdEntity> coCoCmdEntities); + + List<CoCoCmdEntity> getAllCoCoCmdEntities(Guid cmdId); } diff --git a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java index 5069cb6..c16037a 100644 --- a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java +++ b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/CommandEntityDaoDbFacadeImpl.java @@ -3,23 +3,52 @@ import java.io.Serializable; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Collection; import java.util.Date; import java.util.List; import org.apache.commons.lang.StringUtils; +import org.ovirt.engine.core.common.VdcObjectType; import org.ovirt.engine.core.common.action.VdcActionParametersBase; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.action.VdcReturnValueBase; +import org.ovirt.engine.core.common.businessentities.CoCoCmdEntity; import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.compat.CommandStatus; import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.dal.dbbroker.CustomMapSqlParameterSource; import org.ovirt.engine.core.dal.dbbroker.DbFacadeUtils; +import org.ovirt.engine.core.dal.dbbroker.MapSqlParameterMapper; import org.ovirt.engine.core.utils.ReflectionUtils; import org.ovirt.engine.core.utils.SerializationFactory; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; public class CommandEntityDaoDbFacadeImpl extends DefaultGenericDaoDbFacade<CommandEntity, Guid> implements CommandEntityDao { + + private static class IdRowMapper implements RowMapper<Guid> { + public static final IdRowMapper instance = new IdRowMapper(); + + @Override + public Guid mapRow(ResultSet rs, int rowNum) throws SQLException { + return getGuidDefaultEmpty(rs, "id"); + } + + } + + private static class CoCoCmdEntityRowMapper implements RowMapper<CoCoCmdEntity> { + + public static final RowMapper<CoCoCmdEntity> instance = new CoCoCmdEntityRowMapper(); + + @Override + public CoCoCmdEntity mapRow(ResultSet rs, int rowNum) throws SQLException { + CoCoCmdEntity entity = new CoCoCmdEntity(); + entity.setCommandId(getGuid(rs, "command_id")); + entity.setEntityId(getGuid(rs, "entity_id")); + entity.setEntityType(VdcObjectType.valueOf(rs.getString("entity_type"))); + return entity; + } + } private static RowMapper<CommandEntity> mapper = new RowMapper<CommandEntity>() { @@ -39,6 +68,19 @@ result.setCallBackEnabled(resultSet.getBoolean("callback_enabled")); result.setCallBackNotified(resultSet.getBoolean("callback_notified")); return result; + } + }; + + private MapSqlParameterMapper<CoCoCmdEntity> cocoCmdEntityMapper = new MapSqlParameterMapper<CoCoCmdEntity>() { + + @Override + public MapSqlParameterSource map(CoCoCmdEntity entity) { + CustomMapSqlParameterSource paramSource = getCustomMapSqlParameterSource(); + paramSource.addValue("command_id", entity.getCommandId()). + addValue("entity_id", entity.getEntityId()). + addValue("entity_type", entity.getEntityType().toString()); + return paramSource; + } }; @@ -147,4 +189,24 @@ parameterSource); } + @Override + public List<Guid> getCommandIdsByEntity(Guid entityId) { + MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource() + .addValue("entity_id", entityId); + return getCallsHandler().executeReadList("GetCommandIdsByEntityId", IdRowMapper.instance, parameterSource); + } + + public void insertCoCoCmdEntities(Collection<CoCoCmdEntity> coCoCmdEntities) { + getCallsHandler().executeStoredProcAsBatch("InsertCoCoCmdEntities", coCoCmdEntities, cocoCmdEntityMapper); + } + + @Override + public List<CoCoCmdEntity> getAllCoCoCmdEntities(Guid cmdId) { + MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource() + .addValue("command_id", cmdId); + return getCallsHandler().executeReadList("GetCoCoCmdEntities", + CoCoCmdEntityRowMapper.instance, + parameterSource); + } + } diff --git a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/CommandEntityDaoTest.java b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/CommandEntityDaoTest.java index a25e7c0..fd7ad50 100644 --- a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/CommandEntityDaoTest.java +++ b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/CommandEntityDaoTest.java @@ -1,15 +1,20 @@ package org.ovirt.engine.core.dao; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.junit.Test; +import org.ovirt.engine.core.common.VdcObjectType; import org.ovirt.engine.core.common.action.VdcActionParametersBase; import org.ovirt.engine.core.common.action.VdcActionType; +import org.ovirt.engine.core.common.businessentities.CoCoCmdEntity; import org.ovirt.engine.core.common.businessentities.CommandEntity; import org.ovirt.engine.core.compat.CommandStatus; import org.ovirt.engine.core.compat.Guid; @@ -71,4 +76,44 @@ assertTrue(cmds.size() > 0); } + @Test + public void testGetCommandIdsByEntity() { + Guid storageId = Guid.newGuid(); + CommandEntity cmdEntity1 = generateNewEntity(); + dao.save(cmdEntity1); + Set<CoCoCmdEntity> cocoCmdEntities1 = new HashSet<>(); + cocoCmdEntities1.add(new CoCoCmdEntity(cmdEntity1.getId(), VdcObjectType.Storage, storageId)); + cocoCmdEntities1.add(new CoCoCmdEntity(cmdEntity1.getId(), VdcObjectType.Disk, Guid.newGuid())); + dao.insertCoCoCmdEntities(cocoCmdEntities1); + + CommandEntity cmdEntity2 = generateNewEntity(); + dao.save(cmdEntity2); + Set<CoCoCmdEntity> cocoCmdEntities2 = new HashSet<>(); + cocoCmdEntities2.add(new CoCoCmdEntity(cmdEntity2.getId(), VdcObjectType.Storage, storageId)); + cocoCmdEntities2.add(new CoCoCmdEntity(cmdEntity2.getId(), VdcObjectType.Disk, Guid.newGuid())); + dao.insertCoCoCmdEntities(cocoCmdEntities2); + + List<Guid> cmIds = dao.getCommandIdsByEntity(storageId); + assertNotNull(cmIds); + assertEquals(2, cmIds.size()); + assertTrue(cmIds.contains(cmdEntity1.getId())); + assertTrue(cmIds.contains(cmdEntity2.getId())); + } + + @Test + public void testInsertAsyncTaskEntitities() { + CommandEntity cmdEntity = generateNewEntity(); + dao.save(cmdEntity); + Set<CoCoCmdEntity> cocoCmdEntities = new HashSet<>(); + cocoCmdEntities.add(new CoCoCmdEntity(cmdEntity.getId(), VdcObjectType.Storage, Guid.newGuid())); + cocoCmdEntities.add(new CoCoCmdEntity(cmdEntity.getId(), VdcObjectType.Disk, Guid.newGuid())); + dao.insertCoCoCmdEntities(cocoCmdEntities); + List<CoCoCmdEntity> entities = dao.getAllCoCoCmdEntities(cmdEntity.getId()); + assertNotNull(entities); + assertEquals(2, entities.size()); + for (CoCoCmdEntity entity : cocoCmdEntities) { + assertTrue(entities.contains(entity)); + } + + } } diff --git a/packaging/dbscripts/command_entities_sp.sql b/packaging/dbscripts/command_entities_sp.sql index c5c1c88..3967d3d 100644 --- a/packaging/dbscripts/command_entities_sp.sql +++ b/packaging/dbscripts/command_entities_sp.sql @@ -162,3 +162,37 @@ command_id NOT IN(SELECT command_id FROM async_tasks); END; $procedure$ LANGUAGE plpgsql; + +Create or replace FUNCTION InsertCoCoCmdEntities( + v_command_id UUID, + v_entity_id UUID, + v_entity_type varchar(128)) + +RETURNS VOID + AS $procedure$ +BEGIN + IF NOT EXISTS (SELECT 1 from coco_cmd_entities where command_id = v_command_id and entity_id = v_entity_id) THEN + INSERT INTO coco_cmd_entities (command_id,entity_id,entity_type) VALUES (v_command_id, v_entity_id, v_entity_type); + END IF; +END; $procedure$ +LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION GetCommandIdsByEntityId(v_entity_id UUID) +RETURNS SETOF idUuidType STABLE + AS $procedure$ +BEGIN + RETURN QUERY SELECT command_id from coco_cmd_entities where entity_id = v_entity_id; +END; $procedure$ +LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION GetCoCoCmdEntities(v_command_id UUID) +RETURNS SETOF coco_cmd_entities STABLE + AS $procedure$ +BEGIN + RETURN QUERY SELECT * + FROM coco_cmd_entities + WHERE command_id = v_command_id; +END; $procedure$ +LANGUAGE plpgsql; \ No newline at end of file diff --git a/packaging/dbscripts/upgrade/03_06_0830_add_coco_cmd_entities_table.sql b/packaging/dbscripts/upgrade/03_06_0830_add_coco_cmd_entities_table.sql new file mode 100644 index 0000000..d2637eb --- /dev/null +++ b/packaging/dbscripts/upgrade/03_06_0830_add_coco_cmd_entities_table.sql @@ -0,0 +1,7 @@ +CREATE TABLE coco_cmd_entities ( + command_id uuid NOT NULL, + entity_id uuid NOT NULL, + entity_type character varying(128) +); + +ALTER TABLE coco_cmd_entities ADD CONSTRAINT fk_coco_cmd_entity FOREIGN KEY (command_id) REFERENCES command_entities(command_id) ON DELETE CASCADE; -- To view, visit http://gerrit.ovirt.org/37463 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I96b934ffc44f87407df77c8e097164ca30f3a6e6 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Ravi Nori <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
