Search in sources :

Example 6 with AlternateIdConstraint

use of com.emc.storageos.db.client.constraint.AlternateIdConstraint in project coprhd-controller by CoprHD.

the class SchedulerConfig method getMailAddressOfUser.

/**
 * get user's mail address from UserPreference CF
 *
 * @param userName
 * @return
 */
private String getMailAddressOfUser(String userName) {
    DataObjectType doType = TypeMap.getDoType(UserPreferences.class);
    AlternateIdConstraint constraint = new AlternateIdConstraintImpl(doType.getColumnField(UserPreferences.USER_ID), userName);
    NamedElementQueryResultList queryResults = new NamedElementQueryResultList();
    this.dbClient.queryByConstraint(constraint, queryResults);
    List<URI> userPrefsIds = new ArrayList<>();
    for (NamedElementQueryResultList.NamedElement namedElement : queryResults) {
        userPrefsIds.add(namedElement.getId());
    }
    if (userPrefsIds.isEmpty()) {
        return null;
    }
    final List<UserPreferences> userPrefs = new ArrayList<>();
    Iterator<UserPreferences> iter = this.dbClient.queryIterativeObjects(UserPreferences.class, userPrefsIds);
    while (iter.hasNext()) {
        userPrefs.add(iter.next());
    }
    if (userPrefs.size() > 1) {
        throw new IllegalStateException("There should only be 1 user preferences object for a user");
    }
    if (userPrefs.isEmpty()) {
        // if there isn't a user prefs object in the DB yet then we haven't saved one for this user yet.
        return null;
    }
    return userPrefs.get(0).getEmail();
}
Also used : UserPreferences(com.emc.storageos.db.client.model.UserPreferences) AlternateIdConstraintImpl(com.emc.storageos.db.client.constraint.impl.AlternateIdConstraintImpl) ArrayList(java.util.ArrayList) DataObjectType(com.emc.storageos.db.client.impl.DataObjectType) NamedElementQueryResultList(com.emc.storageos.db.client.constraint.NamedElementQueryResultList) URI(java.net.URI) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint)

Example 7 with AlternateIdConstraint

use of com.emc.storageos.db.client.constraint.AlternateIdConstraint in project coprhd-controller by CoprHD.

the class VPlexDeviceController method deleteMigrationSources.

/**
 * This step is executed after a virtual volume is successfully migrated to
 * delete the old source volumes. We create a sub workflow to perform this
 * task. We do this in a sub workflow because we don't want any of the steps
 * required to delete the sources to initiate rollback in the main workflow
 * if it fails.
 *
 * We also update the class of service, if required, for the virtual
 * volume when the migration is the result of a CoS change. When this
 * step is executed we know that the migrations have been committed and
 * the new CoS now applies to the virtual volume.
 *
 * @param vplexURI
 *            The URI of the VPlex storage system.
 * @param virtualVolumeURI
 *            The URI of the virtual volume.
 * @param newVpoolURI
 *            The CoS to be assigned to the virtual volume
 *            upon successful commit of the migration or null when not
 *            specified.
 * @param newVarrayURI
 *            The varray to be assigned to the virtual volume
 *            upon successful commit of the migration or null when not
 *            specified.
 * @param migrationSources
 *            The migration sources to delete.
 * @param stepId
 *            The workflow step id.
 * @throws WorkflowException
 */
public void deleteMigrationSources(URI vplexURI, URI virtualVolumeURI, URI newVpoolURI, URI newVarrayURI, List<URI> migrationSources, String stepId) throws WorkflowException {
    try {
        WorkflowStepCompleter.stepExecuting(stepId);
        // First update the virtual volume CoS, if necessary.
        Volume volume = _dbClient.queryObject(Volume.class, virtualVolumeURI);
        // or create a new exportGroup
        if (newVarrayURI != null && volume.isVolumeExported(_dbClient)) {
            URIQueryResultList exportGroupURIs = new URIQueryResultList();
            _dbClient.queryByConstraint(ContainmentConstraint.Factory.getBlockObjectExportGroupConstraint(virtualVolumeURI), exportGroupURIs);
            Iterator<URI> iterator = exportGroupURIs.iterator();
            while (iterator.hasNext()) {
                URI egUri = iterator.next();
                ExportGroup eg = _dbClient.queryObject(ExportGroup.class, egUri);
                if (eg != null) {
                    StringMap volumesMap = eg.getVolumes();
                    String lun = volumesMap.get(virtualVolumeURI.toString());
                    if (lun == null || lun.isEmpty()) {
                        lun = ExportGroup.LUN_UNASSIGNED_DECIMAL_STR;
                    }
                    List<URI> initiators = StringSetUtil.stringSetToUriList(eg.getInitiators());
                    ExportGroup newEg = null;
                    if (initiators != null && !initiators.isEmpty()) {
                        URI initiatorUri = initiators.get(0);
                        AlternateIdConstraint constraint = AlternateIdConstraint.Factory.getExportGroupInitiatorConstraint(initiatorUri.toString());
                        URIQueryResultList egUris = new URIQueryResultList();
                        _dbClient.queryByConstraint(constraint, egUris);
                        Iterator<URI> egIt = egUris.iterator();
                        while (egIt.hasNext()) {
                            ExportGroup theEg = _dbClient.queryObject(ExportGroup.class, egIt.next());
                            if (theEg.getVirtualArray().equals(newVarrayURI)) {
                                List<URI> theEgInits = StringSetUtil.stringSetToUriList(theEg.getInitiators());
                                if (theEgInits.containsAll(initiators) && theEgInits.size() == initiators.size()) {
                                    _log.info(String.format("Found existing exportGroup %s", theEg.getId().toString()));
                                    newEg = theEg;
                                    break;
                                }
                            }
                        }
                    }
                    if (newEg != null) {
                        // add the volume to the export group
                        newEg.addVolume(virtualVolumeURI, Integer.valueOf(lun));
                        _dbClient.updateObject(newEg);
                    } else {
                        // create a new export group
                        _log.info("Creating new ExportGroup");
                        createExportGroup(eg, volume, Integer.valueOf(lun));
                    }
                    eg.removeVolume(virtualVolumeURI);
                    _dbClient.updateObject(eg);
                }
            }
        }
        if (!migrationSources.isEmpty()) {
            final String workflowKey = "deleteOriginalSources";
            if (!WorkflowService.getInstance().hasWorkflowBeenCreated(stepId, workflowKey)) {
                // Now create and execute the sub workflow to delete the
                // migration source volumes if we have any. If the volume
                // migrated was ingested VPLEX volume we will not have
                // the sources.
                String subTaskId = stepId;
                Workflow subWorkflow = _workflowService.getNewWorkflow(this, DELETE_MIGRATION_SOURCES_WF_NAME, true, subTaskId);
                WorkflowTaskCompleter completer = new WorkflowTaskCompleter(subWorkflow.getWorkflowURI(), subTaskId);
                // Creates steps to remove the migration source volumes from all
                // export groups containing them and delete them.
                boolean unexportStepsAdded = vplexAddUnexportVolumeWfSteps(subWorkflow, null, migrationSources, null);
                // Only need to wait for unexport if there was a step for it added
                // to the workflow.
                String waitFor = null;
                if (unexportStepsAdded) {
                    waitFor = UNEXPORT_STEP;
                    // If the migration sources are unexported, Add a step to
                    // forget these backend volumes.
                    addStepToForgetVolumes(subWorkflow, vplexURI, migrationSources, waitFor);
                }
                // Add steps to delete the volumes.
                Iterator<URI> migrationSourcesIter = migrationSources.iterator();
                while (migrationSourcesIter.hasNext()) {
                    URI migrationSourceURI = migrationSourcesIter.next();
                    _log.info("Migration source URI is {}", migrationSourceURI);
                    Volume migrationSource = _dbClient.queryObject(Volume.class, migrationSourceURI);
                    URI sourceSystemURI = migrationSource.getStorageController();
                    _log.info("Source storage system URI is {}", sourceSystemURI);
                    StorageSystem sourceSystem = _dbClient.queryObject(StorageSystem.class, sourceSystemURI);
                    String subWFStepId = subWorkflow.createStepId();
                    Workflow.Method deleteVolumesMethod = new Workflow.Method(DELETE_VOLUMES_METHOD_NAME, sourceSystemURI, Arrays.asList(migrationSourceURI));
                    _log.info("Creating workflow step to delete source");
                    subWorkflow.createStep(MIGRATION_VOLUME_DELETE_STEP, String.format("Delete volume from storage system: %s", sourceSystemURI), waitFor, sourceSystemURI, sourceSystem.getSystemType(), BlockDeviceController.class, deleteVolumesMethod, null, subWFStepId);
                    _log.info("Created workflow step to delete source");
                }
                // Execute this sub workflow.
                subWorkflow.executePlan(completer, "Deleted migration sources");
                // Mark this workflow as created/executed so we don't do it again on retry/resume
                WorkflowService.getInstance().markWorkflowBeenCreated(stepId, workflowKey);
            }
        } else {
            // No sources to delete. Must have migrated an ingested volume.
            WorkflowStepCompleter.stepSucceded(stepId);
            _log.info("Updated workflow step to success");
        }
    } catch (Exception ex) {
        // Log the error.
        _log.error("Error deleting migration sources", ex);
        // Always return success. This is a cleanup step after a
        // successfully committed migration. We don't want rollback,
        // so we return success.
        WorkflowStepCompleter.stepSucceded(stepId);
    }
}
Also used : StringMap(com.emc.storageos.db.client.model.StringMap) Workflow(com.emc.storageos.workflow.Workflow) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) InternalException(com.emc.storageos.svcs.errorhandling.resources.InternalException) InternalServerErrorException(com.emc.storageos.svcs.errorhandling.resources.InternalServerErrorException) VPlexApiException(com.emc.storageos.vplex.api.VPlexApiException) ControllerException(com.emc.storageos.volumecontroller.ControllerException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) WorkflowException(com.emc.storageos.workflow.WorkflowException) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException) DeviceControllerException(com.emc.storageos.exceptions.DeviceControllerException) ExportGroup(com.emc.storageos.db.client.model.ExportGroup) Volume(com.emc.storageos.db.client.model.Volume) WorkflowTaskCompleter(com.emc.storageos.workflow.WorkflowTaskCompleter) StorageSystem(com.emc.storageos.db.client.model.StorageSystem)

Example 8 with AlternateIdConstraint

use of com.emc.storageos.db.client.constraint.AlternateIdConstraint in project coprhd-controller by CoprHD.

the class SchemaUtil method queryLocalVdc.

private VirtualDataCenter queryLocalVdc(DbClient dbClient) {
    // all vdc info stored in local db
    try {
        _log.debug("my vdcid: " + _vdcShortId);
        URIQueryResultList list = new URIQueryResultList();
        AlternateIdConstraint constraints = AlternateIdConstraint.Factory.getVirtualDataCenterByShortIdConstraint(_vdcShortId);
        dbClient.queryByConstraint(constraints, list);
        if (list.iterator().hasNext()) {
            URI vdcId = list.iterator().next();
            VirtualDataCenter vdc = dbClient.queryObject(VirtualDataCenter.class, vdcId);
            return vdc;
        } else {
            _log.info("vdc resource query returned no results");
            return null;
        }
    } catch (DatabaseException ex) {
        _log.error("failed querying for vdc resource", ex);
        // Throw an DatabaseException and retry
        throw ex;
    } catch (Exception ex) {
        _log.error("unexpected error during querying for vdc info", ex);
        // throw IllegalStateExcpetion and stop
        throw new IllegalStateException("vdc resource query failed");
    }
}
Also used : VirtualDataCenter(com.emc.storageos.db.client.model.VirtualDataCenter) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException) OperationException(com.netflix.astyanax.connectionpool.exceptions.OperationException) ConnectionException(com.netflix.astyanax.connectionpool.exceptions.ConnectionException) UnknownHostException(java.net.UnknownHostException)

Example 9 with AlternateIdConstraint

use of com.emc.storageos.db.client.constraint.AlternateIdConstraint in project coprhd-controller by CoprHD.

the class DbIndexTest method verifyAltIdDbIndex.

private void verifyAltIdDbIndex(DataObject obj, ColumnField field, Object val, boolean indexByKey, DbClient client) {
    switch(field.getType()) {
        case Primitive:
            {
                AlternateIdConstraint constraint = new AlternateIdConstraintImpl(field, (String) val);
                verifyContain(constraint, obj.getId(), -1, client);
            }
            break;
        case TrackingMap:
            for (Map.Entry entry : ((AbstractChangeTrackingMap<?>) val).entrySet()) {
                Object altId = indexByKey ? entry.getKey() : entry.getValue();
                AlternateIdConstraint constraint = new AlternateIdConstraintImpl(field, (String) altId);
                verifyContain(constraint, obj.getId(), -1, client);
            }
            break;
        case TrackingSet:
            for (String key : (AbstractChangeTrackingSet<String>) val) {
                AlternateIdConstraint constraint = new AlternateIdConstraintImpl(field, key);
                verifyContain(constraint, obj.getId(), -1, client);
            }
            break;
        case Id:
        case NamedURI:
        case NestedObject:
        case TrackingSetMap:
        default:
            throw new IllegalArgumentException(String.format("Field type %s is not supported by AltIdDbIndex", field.getType().toString()));
    }
}
Also used : AlternateIdConstraintImpl(com.emc.storageos.db.client.constraint.impl.AlternateIdConstraintImpl) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) TypeMap(com.emc.storageos.db.client.impl.TypeMap) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint)

Example 10 with AlternateIdConstraint

use of com.emc.storageos.db.client.constraint.AlternateIdConstraint in project coprhd-controller by CoprHD.

the class DbIndexTest method testAlternativeIdSetIndex.

@Test
public void testAlternativeIdSetIndex() {
    URI id = URIUtil.createId(AuthnProvider.class);
    String key0 = "abcd1234";
    String key1 = "efgh5678";
    AlternateIdConstraint constraint0 = AlternateIdConstraint.Factory.getAuthnProviderDomainConstraint(key0);
    AlternateIdConstraint constraint1 = AlternateIdConstraint.Factory.getAuthnProviderDomainConstraint(key1);
    {
        AuthnProvider obj = new AuthnProvider();
        obj.setId(id);
        obj.setDomains(new StringSet());
        obj.getDomains().add(key0);
        _dbClient.createObject(obj);
    }
    verifyContain(constraint0, id, 1);
    verifyContain(constraint1, null, 0);
    {
        AuthnProvider obj = _dbClient.queryObject(AuthnProvider.class, id);
        obj.getDomains().add(key1);
        _dbClient.persistObject(obj);
    }
    verifyContain(constraint0, id, 1);
    verifyContain(constraint1, id, 1);
    {
        AuthnProvider obj = _dbClient.queryObject(AuthnProvider.class, id);
        obj.getDomains().remove(key0);
        _dbClient.persistObject(obj);
    }
    verifyContain(constraint1, id, 1);
    verifyContain(constraint0, null, 0);
}
Also used : URI(java.net.URI) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) Test(org.junit.Test)

Aggregations

AlternateIdConstraint (com.emc.storageos.db.client.constraint.AlternateIdConstraint)17 URI (java.net.URI)11 AlternateIdConstraintImpl (com.emc.storageos.db.client.constraint.impl.AlternateIdConstraintImpl)7 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)6 DataObjectType (com.emc.storageos.db.client.impl.DataObjectType)6 NamedElementQueryResultList (com.emc.storageos.db.client.constraint.NamedElementQueryResultList)4 UserPreferences (com.emc.storageos.db.client.model.UserPreferences)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 ExportGroup (com.emc.storageos.db.client.model.ExportGroup)3 NamedURI (com.emc.storageos.db.client.model.NamedURI)2 DatabaseException (com.emc.storageos.db.exceptions.DatabaseException)2 DbClient (com.emc.storageos.db.client.DbClient)1 Constraint (com.emc.storageos.db.client.constraint.Constraint)1 ContainmentConstraint (com.emc.storageos.db.client.constraint.ContainmentConstraint)1 ContainmentPermissionsConstraint (com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint)1 ContainmentPrefixConstraint (com.emc.storageos.db.client.constraint.ContainmentPrefixConstraint)1 PrefixConstraint (com.emc.storageos.db.client.constraint.PrefixConstraint)1 TypeMap (com.emc.storageos.db.client.impl.TypeMap)1 DataObject (com.emc.storageos.db.client.model.DataObject)1