'Check if a volume is valid for cinder management.
:param array: the array serial number
:param device_id: the device id
:param volume_id: the cinder volume id
:param external_ref: the external reference
:raises: ManageExistingInvalidReference, ManageExistingAlreadyManaged:'

def _check_lun_valid_for_cinder_management(self, array, device_id, volume_id, external_ref):

    volume_details = self.rest.get_volume(array, device_id)
    if (not volume_details):
        msg = (_('Unable    to    retrieve    volume    details    from    array    for    device    %(device_id)s') % {'device_id': device_id})
        raise exception.ManageExistingInvalidReference(existing_ref=external_ref, reason=msg)
    if volume_details.get('volume_identifier'):
        volume_identifier = volume_details['volume_identifier']
        if volume_identifier.startswith(utils.VOLUME_ELEMENT_NAME_PREFIX):
            raise exception.ManageExistingAlreadyManaged(volume_ref=volume_id)
    storagegrouplist = self.rest.get_storage_groups_from_volume(array, device_id)
    for sg in storagegrouplist:
        mvs = self.rest.get_masking_views_from_storage_group(array, sg)
        if mvs:
            msg = (_('Unable    to    import    volume    %(device_id)s    to    cinder.    Volume    is    in    masking    view(s):    %(mv)s.') % {'device_id': device_id, 'mv': mvs})
            raise exception.ManageExistingInvalidReference(existing_ref=external_ref, reason=msg)
    (snapvx_tgt, snapvx_src, rdf) = self.rest.is_vol_in_rep_session(array, device_id)
    if (snapvx_tgt or snapvx_src or rdf):
        msg = (_('Unable    to    import    volume    %(device_id)s    to    cinder.    It    is    part    of    a    replication    session.') % {'device_id': device_id})
        raise exception.ManageExistingInvalidReference(existing_ref=external_ref, reason=msg)
