Search in sources :

Example 1 with VolumeAttachResponseAlt

use of com.emc.storageos.cinder.model.VolumeAttachResponseAlt in project coprhd-controller by CoprHD.

the class CinderApi method attachVolume.

/**
 * Attaches specified volume to the specified initiator on the host.
 * It is a synchronous operation.
 *
 * @param volumeId the volume id
 * @param initiator the initiator
 * @param host the host
 * @return the volume attachment response
 * @throws Exception the exception
 */
public VolumeAttachResponse attachVolume(String volumeId, String initiator, String[] wwpns, String[] wwnns, String host) throws Exception {
    _log.info("CinderApi - start attachVolume");
    Gson gson = new Gson();
    VolumeAttachRequest volumeAttach = new VolumeAttachRequest();
    if (initiator != null) {
        volumeAttach.initializeConnection.connector.initiator = initiator;
    } else {
        if (wwpns != null) {
            volumeAttach.initializeConnection.connector.wwpns = Arrays.copyOf(wwpns, wwpns.length);
        }
        if (null != wwnns) {
            volumeAttach.initializeConnection.connector.wwnns = Arrays.copyOf(wwnns, wwnns.length);
        }
    }
    volumeAttach.initializeConnection.connector.host = host;
    String volumeAttachmentUri = endPoint.getBaseUri() + String.format(CinderConstants.URI_VOLUME_ACTION, new Object[] { endPoint.getCinderTenantId(), volumeId });
    _log.debug("attaching volume to initiator with uri {}", volumeAttachmentUri);
    String json = gson.toJson(volumeAttach);
    _log.info("attaching volume with body {}", json);
    ClientResponse js_response = getClient().postWithHeader(URI.create(volumeAttachmentUri), json);
    String s = js_response.getEntity(String.class);
    _log.debug("Got the response {}", s);
    VolumeAttachResponse response = null;
    _log.debug("Response status {}", String.valueOf(js_response.getStatus()));
    if (js_response.getStatus() == ClientResponse.Status.OK.getStatusCode()) {
        // This means volume attachment request accepted and being processed (Synch opr)
        try {
            response = gson.fromJson(SecurityUtils.sanitizeJsonString(s), VolumeAttachResponse.class);
        } catch (JsonSyntaxException ex) {
            /**
             * Some drivers return 'target_wwn' as a string list but some returns it as string.
             * We observed in practice that IBM SVC returning string which could be a faulty one.
             * In such a case, we capture the response string in an Alternate Java object and
             * return the details in default response object.
             */
            VolumeAttachResponseAlt altResponse = gson.fromJson(SecurityUtils.sanitizeJsonString(s), VolumeAttachResponseAlt.class);
            response = getResponseInVolumeAttachResponseFormat(altResponse);
        }
    } else {
        throw CinderException.exceptions.volumeAttachFailed(s);
    }
    _log.info("CinderApi - end attachVolume");
    return response;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) VolumeAttachResponse(com.emc.storageos.cinder.model.VolumeAttachResponse) JsonSyntaxException(com.google.gson.JsonSyntaxException) VolumeAttachResponseAlt(com.emc.storageos.cinder.model.VolumeAttachResponseAlt) Gson(com.google.gson.Gson) VolumeAttachRequest(com.emc.storageos.cinder.model.VolumeAttachRequest)

Aggregations

VolumeAttachRequest (com.emc.storageos.cinder.model.VolumeAttachRequest)1 VolumeAttachResponse (com.emc.storageos.cinder.model.VolumeAttachResponse)1 VolumeAttachResponseAlt (com.emc.storageos.cinder.model.VolumeAttachResponseAlt)1 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1