Search in sources :

Example 1 with XIVRestClient

use of com.emc.storageos.xiv.api.XIVRestClient in project coprhd-controller by CoprHD.

the class XIVRestOperationsHelper method getRestClient.

/**
 * Gets REST Client instance for a StorageSystem
 *
 * @param storage
 *            StorageSystem instance
 * @return XIVRESTExportOperations instance
 */
private XIVRestClient getRestClient(StorageSystem storage) {
    XIVRestClient restClient = null;
    StorageProvider provider = _dbClient.queryObject(StorageProvider.class, storage.getActiveProviderURI());
    String providerUser = provider.getSecondaryUsername();
    String providerPassword = provider.getSecondaryPassword();
    String providerURL = provider.getSecondaryURL();
    if (StringUtils.isNotEmpty(providerURL) && StringUtils.isNotEmpty(providerPassword) && StringUtils.isNotEmpty(providerUser)) {
        restClient = (XIVRestClient) _restClientFactory.getRESTClient(URI.create(providerURL), providerUser, providerPassword);
    }
    return restClient;
}
Also used : XIVRestClient(com.emc.storageos.xiv.api.XIVRestClient) StorageProvider(com.emc.storageos.db.client.model.StorageProvider)

Example 2 with XIVRestClient

use of com.emc.storageos.xiv.api.XIVRestClient in project coprhd-controller by CoprHD.

the class XIVRestOperationsHelper method refreshRESTExportMask.

/**
 * Refresh the export mask with the user added configuration
 *
 * @param storage
 *            XIV sotrage system
 * @param mask
 *            Export Mask instance
 * @param _networkDeviceController
 *            Network configuration instance
 */
public void refreshRESTExportMask(StorageSystem storage, ExportMask mask, NetworkDeviceController _networkDeviceController) {
    try {
        final String storageIP = storage.getSmisProviderIP();
        final String name = mask.getNativeId();
        XIVRestClient restExportOpr = getRestClient(storage);
        StringBuilder builder = new StringBuilder();
        Set<String> discoveredPorts = new HashSet<String>();
        Set<URI> hostURIs = new HashSet<URI>();
        Set<Initiator> exportMaskInits = ExportMaskUtils.getInitiatorsForExportMask(_dbClient, mask, null);
        Iterator<Initiator> exportMaskInitsItr = exportMaskInits.iterator();
        while (exportMaskInitsItr.hasNext()) {
            hostURIs.add(exportMaskInitsItr.next().getHost());
        }
        // Check the initiators and update the lists as necessary
        List<Host> hostList = _dbClient.queryObject(Host.class, hostURIs);
        for (Host host : hostList) {
            discoveredPorts.addAll(restExportOpr.getHostPorts(storageIP, host.getLabel()));
        }
        boolean addInitiators = false;
        List<String> initiatorsToAdd = new ArrayList<String>();
        List<Initiator> initiatorIdsToAdd = new ArrayList<>();
        for (String port : discoveredPorts) {
            String normalizedPort = Initiator.normalizePort(port);
            if (!mask.hasExistingInitiator(normalizedPort) && !mask.hasUserInitiator(normalizedPort)) {
                initiatorsToAdd.add(normalizedPort);
                Initiator existingInitiator = ExportUtils.getInitiator(Initiator.toPortNetworkId(port), _dbClient);
                if (existingInitiator != null) {
                    initiatorIdsToAdd.add(existingInitiator);
                }
                addInitiators = true;
            }
        }
        boolean removeInitiators = false;
        List<String> initiatorsToRemove = new ArrayList<String>();
        List<URI> initiatorIdsToRemove = new ArrayList<>();
        if (mask.getExistingInitiators() != null && !mask.getExistingInitiators().isEmpty()) {
            initiatorsToRemove.addAll(mask.getExistingInitiators());
            initiatorsToRemove.removeAll(discoveredPorts);
        }
        removeInitiators = !initiatorsToRemove.isEmpty();
        // Get Volumes mapped to a Host on Array
        Map<String, Integer> discoveredVolumes = new HashMap<String, Integer>();
        final String exportType = ExportMaskUtils.getExportType(_dbClient, mask);
        if (ExportGroup.ExportGroupType.Cluster.name().equals(exportType)) {
            discoveredVolumes.putAll(restExportOpr.getVolumesMappedToHost(storageIP, mask.getLabel(), null));
        } else {
            for (Host host : hostList) {
                discoveredVolumes.putAll(restExportOpr.getVolumesMappedToHost(storageIP, null, host.getLabel()));
            }
        }
        // Check the volumes and update the lists as necessary
        Map<String, Integer> volumesToAdd = ExportMaskUtils.diffAndFindNewVolumes(mask, discoveredVolumes);
        boolean addVolumes = !volumesToAdd.isEmpty();
        boolean removeVolumes = false;
        List<String> volumesToRemove = new ArrayList<String>();
        if (mask.getExistingVolumes() != null && !mask.getExistingVolumes().isEmpty()) {
            volumesToRemove.addAll(mask.getExistingVolumes().keySet());
            volumesToRemove.removeAll(discoveredVolumes.keySet());
            removeVolumes = !volumesToRemove.isEmpty();
        }
        builder.append(String.format("XM refresh: %s initiators; add:{%s} remove:{%s}%n", name, Joiner.on(',').join(initiatorsToAdd), Joiner.on(',').join(initiatorsToRemove)));
        builder.append(String.format("XM refresh: %s volumes; add:{%s} remove:{%s}%n", name, Joiner.on(',').join(volumesToAdd.keySet()), Joiner.on(',').join(volumesToRemove)));
        if (addInitiators || removeInitiators || addVolumes || removeVolumes) {
            builder.append("XM refresh: There are changes to mask, " + "updating it...\n");
            mask.removeFromExistingInitiators(initiatorsToRemove);
            if (initiatorIdsToRemove != null && !initiatorIdsToRemove.isEmpty()) {
                mask.removeInitiators(_dbClient.queryObject(Initiator.class, initiatorIdsToRemove));
            }
            List<Initiator> userAddedInitiators = ExportMaskUtils.findIfInitiatorsAreUserAddedInAnotherMask(mask, initiatorIdsToAdd, _dbClient);
            mask.addToUserCreatedInitiators(userAddedInitiators);
            mask.addToExistingInitiatorsIfAbsent(initiatorsToAdd);
            mask.addInitiators(initiatorIdsToAdd);
            mask.removeFromExistingVolumes(volumesToRemove);
            mask.setExistingVolumes(new StringMap());
            mask.addToExistingVolumesIfAbsent(volumesToAdd);
            ExportMaskUtils.sanitizeExportMaskContainers(_dbClient, mask);
            _dbClient.updateObject(mask);
        } else {
            builder.append("XM refresh: There are no changes to the mask\n");
        }
        _networkDeviceController.refreshZoningMap(mask, initiatorsToRemove, Collections.EMPTY_LIST, (addInitiators || removeInitiators), true);
        _log.info(builder.toString());
    } catch (Exception e) {
        String msg = "Error when attempting to query LUN masking information: " + e.getMessage();
        _log.error(MessageFormat.format("Encountered an error when attempting to refresh existing exports: {0}", msg), e);
        throw XIVRestException.exceptions.refreshExistingMaskFailure(msg);
    }
}
Also used : StringMap(com.emc.storageos.db.client.model.StringMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Host(com.emc.storageos.db.client.model.Host) URI(java.net.URI) XIVRestException(com.emc.storageos.xiv.api.XIVRestException) Initiator(com.emc.storageos.db.client.model.Initiator) XIVRestClient(com.emc.storageos.xiv.api.XIVRestClient) HashSet(java.util.HashSet)

Example 3 with XIVRestClient

use of com.emc.storageos.xiv.api.XIVRestClient in project coprhd-controller by CoprHD.

the class XIVRestOperationsHelper method deleteRESTExportMask.

/**
 * Deletes the Export Mask and its attributes
 *
 * @param storage
 *            XIV storage system
 * @param exportMaskURI
 *            Export mask URI
 * @param volumeURIList
 *            Volume URI as list
 * @param targetURIList
 *            target port URI as list [ not used for xiv]
 * @param initiatorList
 *            Initiator port URI as list
 * @param taskCompleter
 *            task completer instance
 */
public void deleteRESTExportMask(StorageSystem storage, URI exportMaskURI, List<URI> volumeURIList, List<URI> targetURIList, List<Initiator> initiatorList, TaskCompleter taskCompleter) {
    try {
        ExportMask exportMask = _dbClient.queryObject(ExportMask.class, exportMaskURI);
        final String storageIP = storage.getSmisProviderIP();
        final String exportType = ExportMaskUtils.getExportType(_dbClient, exportMask);
        final String name = exportMask.getNativeId();
        final StringSet emInitiatorURIs = exportMask.getInitiators();
        final StringMap emVolumeURIs = exportMask.getVolumes();
        XIVRestClient restExportOpr = getRestClient(storage);
        Set<URI> hostURIs = new HashSet<URI>();
        // Un export Volumes
        if (null != emVolumeURIs) {
            Iterator<Entry<String, String>> emVolumeURIItr = emVolumeURIs.entrySet().iterator();
            while (emVolumeURIItr.hasNext()) {
                URI volUri = URI.create(emVolumeURIItr.next().getKey());
                if (URIUtil.isType(volUri, Volume.class)) {
                    Volume volume = _dbClient.queryObject(Volume.class, volUri);
                    restExportOpr.unExportVolume(storageIP, exportType, name, volume.getLabel());
                }
            }
        }
        // Delete initiators
        if (null != emInitiatorURIs) {
            for (String initiatorURI : emInitiatorURIs) {
                Initiator initiator = _dbClient.queryObject(Initiator.class, URI.create(initiatorURI));
                Host host = _dbClient.queryObject(Host.class, initiator.getHost());
                hostURIs.add(host.getId());
                String normalizedPort = Initiator.normalizePort(initiator.getInitiatorPort());
                restExportOpr.deleteHostPort(storageIP, host.getLabel(), normalizedPort, initiator.getProtocol().toLowerCase(), false);
            }
        }
        // Delete Host if there are no associated Initiators/Volume to it.
        for (URI hostURI : hostURIs) {
            Host host = _dbClient.queryObject(Host.class, hostURI);
            boolean hostDeleted = restExportOpr.deleteHost(storageIP, host.getLabel(), false);
            // Perform post-mask-delete cleanup steps
            if (hostDeleted && emVolumeURIs.size() > 0) {
                unsetTag(host, storage.getSerialNumber());
            }
        }
        // Delete Cluster if there is no associated hosts to it.
        if (ExportGroup.ExportGroupType.Cluster.name().equals(exportType)) {
            restExportOpr.deleteCluster(storageIP, name);
        }
        ExportUtils.cleanupAssociatedMaskResources(_dbClient, exportMask);
        exportMask.setMaskName(NullColumnValueGetter.getNullURI().toString());
        exportMask.setLabel(NullColumnValueGetter.getNullURI().toString());
        exportMask.setNativeId(NullColumnValueGetter.getNullURI().toString());
        exportMask.setResource(NullColumnValueGetter.getNullURI().toString());
        _dbClient.updateObject(exportMask);
        taskCompleter.ready(_dbClient);
    } catch (Exception e) {
        _log.error("Unexpected error: deleteExportMask failed.", e);
        XIVRestException error = XIVRestException.exceptions.methodFailed("createExportMask", e);
        taskCompleter.error(_dbClient, error);
    }
}
Also used : StringMap(com.emc.storageos.db.client.model.StringMap) ExportMask(com.emc.storageos.db.client.model.ExportMask) Host(com.emc.storageos.db.client.model.Host) URI(java.net.URI) XIVRestException(com.emc.storageos.xiv.api.XIVRestException) Entry(java.util.Map.Entry) Volume(com.emc.storageos.db.client.model.Volume) Initiator(com.emc.storageos.db.client.model.Initiator) StringSet(com.emc.storageos.db.client.model.StringSet) XIVRestClient(com.emc.storageos.xiv.api.XIVRestClient) XIVRestException(com.emc.storageos.xiv.api.XIVRestException) HashSet(java.util.HashSet)

Example 4 with XIVRestClient

use of com.emc.storageos.xiv.api.XIVRestClient in project coprhd-controller by CoprHD.

the class XIVRestOperationsHelper method findRESTExportMasks.

/**
 * Find Export mask
 *
 * @param storage
 *            Storage System instance
 * @param initiatorNames
 *            Initiator names
 * @param mustHaveAllPorts
 *            Must have all the ports boolean
 * @return Initiator to Export mask map.
 */
public Map<String, Set<URI>> findRESTExportMasks(StorageSystem storage, List<String> initiatorNames, boolean mustHaveAllPorts) {
    long startTime = System.currentTimeMillis();
    Map<String, Set<URI>> matchingMasks = new HashMap<String, Set<URI>>();
    try {
        final String storageIP = storage.getSmisProviderIP();
        XIVRestClient restExportOpr = getRestClient(storage);
        StringBuilder builder = new StringBuilder();
        for (String initiatorName : initiatorNames) {
            final String hostName = restExportOpr.getHostPortContainer(storageIP, initiatorName);
            Set<String> exportMaskNames = new HashSet<String>();
            if (null != hostName) {
                exportMaskNames.add(hostName);
                final String clusterNames = restExportOpr.getHostContainer(storageIP, hostName);
                if (null != clusterNames) {
                    exportMaskNames.add(clusterNames);
                }
            }
            // Find the existing masks
            for (String exportMaskName : exportMaskNames) {
                URIQueryResultList uriQueryList = new URIQueryResultList();
                _dbClient.queryByConstraint(AlternateIdConstraint.Factory.getExportMaskByNameConstraint(exportMaskName), uriQueryList);
                ExportMask exportMask = null;
                while (uriQueryList.iterator().hasNext()) {
                    URI uri = uriQueryList.iterator().next();
                    exportMask = _dbClient.queryObject(ExportMask.class, uri);
                    if (exportMask != null && !exportMask.getInactive() && exportMask.getStorageDevice().equals(storage.getId())) {
                        ExportMaskUtils.sanitizeExportMaskContainers(_dbClient, exportMask);
                        _dbClient.updateAndReindexObject(exportMask);
                        Set<URI> maskURIs = matchingMasks.get(initiatorName);
                        if (maskURIs == null) {
                            maskURIs = new HashSet<URI>();
                            matchingMasks.put(initiatorName, maskURIs);
                        }
                        maskURIs.add(exportMask.getId());
                        break;
                    }
                }
                // update hosts
                Initiator initiator = ExportUtils.getInitiator(Initiator.toPortNetworkId(initiatorName), _dbClient);
                if (null != initiator && null != initiator.getHost()) {
                    Host hostIns = _dbClient.queryObject(Host.class, initiator.getHost());
                    String label = hostIns.getLabel();
                    if (label.equals(exportMaskName)) {
                        unsetTag(hostIns, storage.getSerialNumber());
                    } else {
                        setTag(hostIns, storage.getSerialNumber(), exportMaskName);
                    }
                }
            }
        }
        _log.info(builder.toString());
    } catch (Exception e) {
        String msg = "Error when attempting to query LUN masking information: " + e.getMessage();
        _log.error(MessageFormat.format("Encountered an SMIS error when attempting to query existing exports: {0}", msg), e);
        throw XIVRestException.exceptions.queryExistingMasksFailure(msg, e);
    } finally {
        long totalTime = System.currentTimeMillis() - startTime;
        _log.info(String.format("findExportMasks took %f seconds", (double) totalTime / (double) 1000));
    }
    return matchingMasks;
}
Also used : HashSet(java.util.HashSet) ScopedLabelSet(com.emc.storageos.db.client.model.ScopedLabelSet) Set(java.util.Set) StringSet(com.emc.storageos.db.client.model.StringSet) HashMap(java.util.HashMap) ExportMask(com.emc.storageos.db.client.model.ExportMask) Host(com.emc.storageos.db.client.model.Host) URI(java.net.URI) URIQueryResultList(com.emc.storageos.db.client.constraint.URIQueryResultList) XIVRestException(com.emc.storageos.xiv.api.XIVRestException) Initiator(com.emc.storageos.db.client.model.Initiator) XIVRestClient(com.emc.storageos.xiv.api.XIVRestClient) HashSet(java.util.HashSet)

Example 5 with XIVRestClient

use of com.emc.storageos.xiv.api.XIVRestClient in project coprhd-controller by CoprHD.

the class XIVRestOperationsHelper method addInitiatorsUsingREST.

/**
 * Add Initiators to an existing Export Mask.
 *
 * @param storage
 *            StorageSystem instance
 * @param exportMaskURI
 *            Export mask URI where the initiator needs to be added
 * @param volumeURIList
 *            Volume to be validated against this operation
 * @param initiatorList
 *            List of initiators need to be added
 * @param taskCompleter
 *            Task Completer instance
 */
public void addInitiatorsUsingREST(StorageSystem storage, URI exportMaskURI, List<URI> volumeURIs, List<Initiator> initiatorList, TaskCompleter taskCompleter) {
    try {
        ExportMask mask = _dbClient.queryObject(ExportMask.class, exportMaskURI);
        XIVRestClient restExportOpr = getRestClient(storage);
        final String storageIP = storage.getSmisProviderIP();
        List<Initiator> userAddedInitiators = new ArrayList<Initiator>();
        for (Initiator initiator : initiatorList) {
            final Host host = _dbClient.queryObject(Host.class, initiator.getHost());
            // Add Initiators to Host.
            if (!restExportOpr.createHostPort(storageIP, host.getLabel(), Initiator.normalizePort(initiator.getInitiatorPort()), initiator.getProtocol().toLowerCase())) {
                userAddedInitiators.add(initiator);
            }
        }
        mask.addToUserCreatedInitiators(userAddedInitiators);
        ExportMaskUtils.sanitizeExportMaskContainers(_dbClient, mask);
        _dbClient.updateObject(mask);
        taskCompleter.ready(_dbClient);
    } catch (Exception e) {
        _log.error("Unexpected error: addInitiator failed.", e);
        XIVRestException error = XIVRestException.exceptions.methodFailed("addInitiator", e);
        taskCompleter.error(_dbClient, error);
    }
}
Also used : Initiator(com.emc.storageos.db.client.model.Initiator) ExportMask(com.emc.storageos.db.client.model.ExportMask) ArrayList(java.util.ArrayList) XIVRestClient(com.emc.storageos.xiv.api.XIVRestClient) Host(com.emc.storageos.db.client.model.Host) XIVRestException(com.emc.storageos.xiv.api.XIVRestException) XIVRestException(com.emc.storageos.xiv.api.XIVRestException)

Aggregations

XIVRestClient (com.emc.storageos.xiv.api.XIVRestClient)11 XIVRestException (com.emc.storageos.xiv.api.XIVRestException)10 Initiator (com.emc.storageos.db.client.model.Initiator)9 Host (com.emc.storageos.db.client.model.Host)8 ExportMask (com.emc.storageos.db.client.model.ExportMask)7 URI (java.net.URI)7 HashSet (java.util.HashSet)6 ArrayList (java.util.ArrayList)5 Cluster (com.emc.storageos.db.client.model.Cluster)3 AlternateIdConstraint (com.emc.storageos.db.client.constraint.AlternateIdConstraint)2 BlockObject (com.emc.storageos.db.client.model.BlockObject)2 StringMap (com.emc.storageos.db.client.model.StringMap)2 StringSet (com.emc.storageos.db.client.model.StringSet)2 Volume (com.emc.storageos.db.client.model.Volume)2 VolumeURIHLU (com.emc.storageos.volumecontroller.impl.VolumeURIHLU)2 HashMap (java.util.HashMap)2 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)1 ScopedLabelSet (com.emc.storageos.db.client.model.ScopedLabelSet)1 StorageProvider (com.emc.storageos.db.client.model.StorageProvider)1 HOST_STATUS (com.emc.storageos.xiv.api.XIVRestClient.HOST_STATUS)1