riteshchikatwar

Untitled

Feb 27th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.69 KB | None | 0 0
  1. package org.ovirt.engine.core.bll.storage.connection;
  2.  
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.stream.Collectors;
  7.  
  8. import javax.inject.Inject;
  9.  
  10. import org.apache.commons.lang.StringUtils;
  11. import org.ovirt.engine.core.bll.InternalCommandAttribute;
  12. import org.ovirt.engine.core.bll.LockMessagesMatchUtil;
  13. import org.ovirt.engine.core.bll.context.CommandContext;
  14. import org.ovirt.engine.core.common.action.LockProperties;
  15. import org.ovirt.engine.core.common.action.LockProperties.Scope;
  16. import org.ovirt.engine.core.common.action.StorageServerConnectionParametersBase;
  17. import org.ovirt.engine.core.common.businessentities.StorageServerConnections;
  18. import org.ovirt.engine.core.common.businessentities.VDSStatus;
  19. import org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeEntity;
  20. import org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeOptionEntity;
  21. import org.ovirt.engine.core.common.businessentities.storage.StorageType;
  22. import org.ovirt.engine.core.common.errors.EngineError;
  23. import org.ovirt.engine.core.common.errors.EngineException;
  24. import org.ovirt.engine.core.common.errors.EngineMessage;
  25. import org.ovirt.engine.core.common.locks.LockingGroup;
  26. import org.ovirt.engine.core.common.utils.Pair;
  27. import org.ovirt.engine.core.common.validation.group.CreateEntity;
  28. import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
  29. import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
  30. import org.ovirt.engine.core.common.vdscommands.gluster.GlusterVolumeInfoVDSParameters;
  31. import org.ovirt.engine.core.compat.Guid;
  32. import org.ovirt.engine.core.dao.StorageServerConnectionDao;
  33. import org.ovirt.engine.core.dao.gluster.GlusterVolumeDao;
  34.  
  35. @InternalCommandAttribute
  36. public class AddStorageServerConnectionCommand<T extends StorageServerConnectionParametersBase> extends
  37.         ConnectStorageToVdsCommand<T> {
  38.  
  39.     @Inject
  40.     private StorageServerConnectionDao storageServerConnectionDao;
  41.  
  42.     private static final String FEATURES_SHARD = "features.shard";
  43.  
  44.     private static final String PERFORMANCE_STRICT_O_DIRECT = "performance.strict-o-direct";
  45.  
  46.     @Inject
  47.     private GlusterVolumeDao glusterVolumeDao;
  48.  
  49.     public AddStorageServerConnectionCommand(T parameters, CommandContext cmdContext) {
  50.         super(parameters, cmdContext);
  51.     }
  52.  
  53.     @Override
  54.     protected LockProperties applyLockProperties(LockProperties lockProperties) {
  55.         return lockProperties.withScope(Scope.Execution);
  56.     }
  57.  
  58.     @Override
  59.     protected void executeCommand() {
  60.         // Attempt to connect only if a host is given.
  61.         // If not, just save the connection to the database
  62.         if (!Guid.isNullOrEmpty(getParameters().getVdsId())) {
  63.             Pair<Boolean, Integer> result = connectHostToStorage();
  64.             boolean isValidConnection = result.getFirst();
  65.  
  66.             // Process failure
  67.             if (!isValidConnection) {
  68.                 throw new EngineException(EngineError.forValue(result.getSecond()));
  69.             }
  70.         }
  71.  
  72.         StorageServerConnections connection = getConnection();
  73.         connection.setId(Guid.newGuid().toString());
  74.         saveConnection(connection);
  75.         getReturnValue().setActionReturnValue(connection.getId());
  76.  
  77.         setSucceeded(true);
  78.     }
  79.  
  80.     protected StorageServerConnections getConnectionFromDbById(String connectionId) {
  81.         return storageServerConnectionDao.get(connectionId);
  82.     }
  83.  
  84.     protected void saveConnection(StorageServerConnections connection) {
  85.         storageServerConnectionDao.save(connection);
  86.     }
  87.  
  88.     @Override
  89.     protected boolean validate() {
  90.         StorageServerConnections paramConnection = getConnection();
  91.         // if an id was sent - it's not ok since only the backend should allocate ids
  92.         if (StringUtils.isNotEmpty(paramConnection.getId())) {
  93.             return failValidation(EngineMessage.ACTION_TYPE_FAILED_STORAGE_CONNECTION_ID_NOT_EMPTY);
  94.         }
  95.  
  96.         if (!isValidConnection(paramConnection)) {
  97.             return false;
  98.         }
  99.  
  100.         Guid storagePoolId = Guid.isNullOrEmpty(getParameters().getVdsId()) ? null : getVds().getStoragePoolId();
  101.         String duplicateConnectionId = isConnWithSameDetailsExists(paramConnection, storagePoolId);
  102.  
  103.         if (!duplicateConnectionId.isEmpty() && !duplicateConnectionId.equalsIgnoreCase(paramConnection.getId())) {
  104.             String storageDomainName = getStorageNameByConnectionId(duplicateConnectionId);
  105.             addValidationMessageVariable("connectionId", duplicateConnectionId);
  106.             addValidationMessageVariable("storageDomainName", storageDomainName);
  107.             return failValidation(EngineMessage.ACTION_TYPE_FAILED_STORAGE_CONNECTION_ALREADY_EXISTS);
  108.         }
  109.  
  110.         GlusterVolumeEntity glusterVolumeEntity = glusterVolumeDao.getById(getStorageDomain().getStorageStaticData().getConnection().getGlusterVolumeId());
  111.  
  112.         VDSReturnValue volDetailsRetVal =
  113.                 runVdsCommand(VDSCommandType.GetGlusterVolumeInfo,
  114.                         new GlusterVolumeInfoVDSParameters(getVds().getId(),
  115.                                 glusterVolumeEntity.getClusterId(),
  116.                                 glusterVolumeEntity.getName()));
  117.         @SuppressWarnings("unchecked")
  118.         GlusterVolumeEntity fetchedVolume =
  119.                 ((Map<Guid, GlusterVolumeEntity>) volDetailsRetVal.getReturnValue()).get(getStorageDomain().getStorageStaticData().getConnection().getGlusterVolumeId());
  120.         Map<String, String> options = fetchedVolume.getOptions().stream().collect(Collectors.toMap(GlusterVolumeOptionEntity::getKey, GlusterVolumeOptionEntity::getValue));
  121.  
  122.         if (getStorageDomain().getStorageType() == StorageType.GLUSTERFS
  123.                 && options.get(PERFORMANCE_STRICT_O_DIRECT) != null
  124.                 && !options.get(PERFORMANCE_STRICT_O_DIRECT).equalsIgnoreCase("on")) {
  125.             return failValidation(EngineMessage.ACTION_TYPE_FAILED_STORAGE_DOMAIN_PERFORMANCE_O_DIRECT_DISABLE);
  126.         }
  127.  
  128.         if (getStorageDomain().getStorageType() == StorageType.GLUSTERFS
  129.                 && options.get(FEATURES_SHARD) != null
  130.                 && !options.get(FEATURES_SHARD).equalsIgnoreCase("on")) {
  131.             return failValidation(EngineMessage.ACTION_TYPE_FAILED_STORAGE_DOMAIN_SHARDING_DISABLE);
  132.         }
  133.  
  134.         // If a Guid is not supplied, we won't attempt to [dis]connect.
  135.         // If one is supplied, [dis]connecting will be attempted, so we need to
  136.         // validate that it's a valid VDS ID and that the VDS is up.
  137.         if (!Guid.isNullOrEmpty(getParameters().getVdsId())) {
  138.             if (getVds() == null) {
  139.                 return failValidation(EngineMessage.VDS_INVALID_SERVER_ID);
  140.             }
  141.             if (getVds().getStatus() != VDSStatus.Up) {
  142.                 return failValidation(EngineMessage.VDS_ADD_STORAGE_SERVER_STATUS_MUST_BE_UP);
  143.             }
  144.         }
  145.         return true;
  146.     }
  147.  
  148.     @Override
  149.     protected List<Class<?>> getValidationGroups() {
  150.         addValidationGroup(CreateEntity.class);
  151.         return super.getValidationGroups();
  152.     }
  153.  
  154.     @Override
  155.     protected Map<String, Pair<String, String>> getExclusiveLocks() {
  156.         if (getConnection().getStorageType().isFileDomain()) {
  157.             // lock the path to NFS to avoid at the same time if some other user tries to:
  158.             // add new storage domain to same path or edit another storage server connection to point to same path
  159.             return Collections.singletonMap(getParameters().getStorageServerConnection().getConnection(),
  160.                     LockMessagesMatchUtil.makeLockingPair(LockingGroup.STORAGE_CONNECTION,
  161.                             EngineMessage.ACTION_TYPE_FAILED_OBJECT_LOCKED));
  162.         } else { // lock target details
  163.             return Collections.singletonMap(getConnection().getConnection() + ";" + getConnection().getIqn() + ";"
  164.                     + getConnection().getPort() + ";" + getConnection().getUserName(),
  165.                     LockMessagesMatchUtil.makeLockingPair(LockingGroup.STORAGE_CONNECTION,
  166.                             EngineMessage.ACTION_TYPE_FAILED_OBJECT_LOCKED));
  167.         }
  168.     }
  169.  
  170.     @Override
  171.     protected void setActionMessageParameters() {
  172.         addValidationMessage(EngineMessage.VAR__ACTION__ADD);
  173.         addValidationMessage(EngineMessage.VAR__TYPE__STORAGE__CONNECTION);
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment