Search in sources :

Example 6 with DeleteResponse

use of ddf.catalog.operation.DeleteResponse in project ddf by codice.

the class CatalogComponentFrameworkTest method testDeleteWithListOfIds.

@Test
public /**
     * Operation: DELETE
     * Body contains: List<String>
     */
void testDeleteWithListOfIds() throws Exception {
    resetMocks();
    // Setup expectations to verify
    final MockEndpoint mockVerifierEndpoint = getMockEndpoint("mock:result");
    mockVerifierEndpoint.expectedMessageCount(1);
    final List<Metacard> metacards = new ArrayList<Metacard>();
    metacards.add(metacard1);
    metacards.add(metacard2);
    // setup mock catalog framework
    final String[] metacardIds = new String[metacards.size()];
    for (int i = 0; i < metacards.size(); i++) {
        metacardIds[i] = metacards.get(i).getId();
    }
    final List<String> metacardIdList = Arrays.asList(metacardIds);
    DeleteRequest deleteRequest = new DeleteRequestImpl(metacardIds);
    DeleteResponse deleteResponse = new DeleteResponseImpl(deleteRequest, new HashMap(), metacards);
    when(catalogFramework.delete(any(DeleteRequest.class))).thenReturn(deleteResponse);
    // Exercise the route with a DELETE operation
    template.sendBodyAndHeader("direct:sampleInput", metacardIdList, "Operation", "DELETE");
    // Verify that the number of metacards in the exchange after the records
    // is identical to the input
    assertListSize(mockVerifierEndpoint.getExchanges(), 1);
    final Exchange exchange = mockVerifierEndpoint.getExchanges().get(0);
    final List<Update> cardsDeleted = (List<Update>) exchange.getIn().getBody();
    assertListSize(cardsDeleted, 2);
    mockVerifierEndpoint.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HashMap(java.util.HashMap) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) DeleteResponseImpl(ddf.catalog.operation.impl.DeleteResponseImpl) ArrayList(java.util.ArrayList) List(java.util.List) DeleteRequest(ddf.catalog.operation.DeleteRequest) Test(org.junit.Test)

Example 7 with DeleteResponse

use of ddf.catalog.operation.DeleteResponse in project ddf by codice.

the class FrameworkProducer method delete.

/**
     * Deletes metacard(s) in the catalog using the Catalog Framework.
     *
     * @param exchange
     *            The {@link org.apache.camel.Exchange} can contain a
     *            {@link org.apache.camel.Message} with a body of type {@link java.util.List} of
     *            {@link String} or a single {@link String}. Each String represents the ID of a
     *            Metacard to be deleted.
     * @throws ddf.catalog.source.SourceUnavailableException
     * @throws ddf.catalog.source.IngestException
     * @throws ddf.camel.component.catalog.framework.FrameworkProducerException
     */
private void delete(final Exchange exchange) throws SourceUnavailableException, IngestException, FrameworkProducerException {
    DeleteResponse deleteResponse = null;
    // read in data
    final List<String> metacardIdsToBeDeleted = readBodyDataAsMetacardIds(exchange);
    // process if data is valid
    if (!validateList(metacardIdsToBeDeleted, String.class)) {
        LOGGER.debug("Validation of Metacard id list failed");
        processCatalogResponse(deleteResponse, exchange);
        throw new FrameworkProducerException("Validation of Metacard id list failed");
    }
    LOGGER.debug("Validation of Metacard id list passed...");
    final String[] metacardIdsToBeDeletedArray = new String[metacardIdsToBeDeleted.size()];
    final DeleteRequest deleteRequest = new DeleteRequestImpl(metacardIdsToBeDeleted.toArray(metacardIdsToBeDeletedArray));
    final int expectedNumberOfDeletedMetacards = metacardIdsToBeDeleted.size();
    if (expectedNumberOfDeletedMetacards < 1) {
        LOGGER.debug("Empty list of Metacard id...nothing to process");
        processCatalogResponse(deleteResponse, exchange);
        return;
    }
    LOGGER.debug("Making DELETE call to Catalog Framework...");
    deleteResponse = catalogFramework.delete(deleteRequest);
    if (deleteResponse == null) {
        LOGGER.debug("DeleteResponse is null from catalog framework");
        processCatalogResponse(deleteResponse, exchange);
        return;
    }
    final List<Metacard> deletedMetacards = deleteResponse.getDeletedMetacards();
    if (deletedMetacards == null) {
        LOGGER.debug("DeleteResponse returned null metacards list");
        processCatalogResponse(deleteResponse, exchange);
        return;
    }
    final int numberOfDeletedMetacards = deletedMetacards.size();
    if (numberOfDeletedMetacards != expectedNumberOfDeletedMetacards) {
        LOGGER.debug("Expected {} metacards deleted but only {} were successfully deleted", expectedNumberOfDeletedMetacards, numberOfDeletedMetacards);
        processCatalogResponse(deleteResponse, exchange);
        return;
    }
    LOGGER.debug("Deleted {} metacards", numberOfDeletedMetacards);
    processCatalogResponse(deleteResponse, exchange);
}
Also used : Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) DeleteRequest(ddf.catalog.operation.DeleteRequest) Endpoint(org.apache.camel.Endpoint)

Example 8 with DeleteResponse

use of ddf.catalog.operation.DeleteResponse in project ddf by codice.

the class RemoveAllCommand method executeRemoveAllFromStore.

private Object executeRemoveAllFromStore() throws Exception {
    CatalogFacade catalog = getCatalog();
    QueryRequest firstQuery = getIntendedQuery(filterBuilder, true);
    QueryRequest subsequentQuery = getIntendedQuery(filterBuilder, false);
    long totalAmountDeleted = 0;
    long start = System.currentTimeMillis();
    SourceResponse response;
    try {
        response = catalog.query(firstQuery);
    } catch (UnsupportedQueryException e) {
        firstQuery = getAlternateQuery(filterBuilder, true);
        subsequentQuery = getAlternateQuery(filterBuilder, false);
        response = catalog.query(firstQuery);
    }
    if (response == null) {
        printErrorMessage("No response from Catalog.");
        return null;
    }
    if (needsAlternateQueryAndResponse(response)) {
        firstQuery = getAlternateQuery(filterBuilder, true);
        subsequentQuery = getAlternateQuery(filterBuilder, false);
        response = catalog.query(firstQuery);
    }
    String totalAmount = getTotalAmount(response.getHits());
    while (response.getResults().size() > 0) {
        // Add metacard ids to string array
        List<String> ids = response.getResults().stream().filter(Objects::nonNull).map(Result::getMetacard).filter(Objects::nonNull).map(Metacard::getId).collect(Collectors.toList());
        // Delete the records
        DeleteRequestImpl request = new DeleteRequestImpl(ids.toArray(new String[ids.size()]));
        DeleteResponse deleteResponse = catalog.delete(request);
        int amountDeleted = deleteResponse.getDeletedMetacards().size();
        totalAmountDeleted += amountDeleted;
        console.print(String.format(PROGRESS_FORMAT, totalAmountDeleted, totalAmount));
        console.flush();
        // Break out if there are no more records to delete
        if (amountDeleted < batchSize || batchSize < 1) {
            break;
        }
        // Re-query when necessary
        response = catalog.query(subsequentQuery);
    }
    long end = System.currentTimeMillis();
    String info = String.format(" %d file(s) removed in %3.3f seconds%n", totalAmountDeleted, (end - start) / MS_PER_SECOND);
    LOGGER.info(info);
    LOGGER.info(totalAmountDeleted + " files removed using cache:removeAll command");
    console.println();
    console.print(info);
    return null;
}
Also used : DeleteResponse(ddf.catalog.operation.DeleteResponse) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) Objects(java.util.Objects) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade)

Example 9 with DeleteResponse

use of ddf.catalog.operation.DeleteResponse in project ddf by codice.

the class RemoveCommand method executeRemoveFromStore.

private Object executeRemoveFromStore() throws Exception {
    CatalogFacade catalogProvider = getCatalog();
    if (hasFilter()) {
        QueryImpl query = new QueryImpl(getFilter());
        query.setRequestsTotalResultsCount(true);
        query.setPageSize(-1);
        Map<String, Serializable> properties = new HashMap<>();
        properties.put("mode", "native");
        SourceResponse queryResponse = catalogProvider.query(new QueryRequestImpl(query, properties));
        final List<String> idsFromFilteredQuery = queryResponse.getResults().stream().map(result -> result.getMetacard().getId()).collect(Collectors.toList());
        if (ids == null) {
            ids = idsFromFilteredQuery;
        } else {
            ids = ids.stream().filter(id -> idsFromFilteredQuery.contains(id)).collect(Collectors.toList());
        }
    }
    final int numberOfMetacardsToRemove = ids.size();
    if (numberOfMetacardsToRemove > 0) {
        printSuccessMessage("Found " + numberOfMetacardsToRemove + " metacards to remove.");
    } else {
        printErrorMessage("No records found meeting filter criteria.");
        return null;
    }
    DeleteRequestImpl request = new DeleteRequestImpl(ids.toArray(new String[numberOfMetacardsToRemove]));
    DeleteResponse response = catalogProvider.delete(request);
    if (response.getDeletedMetacards().size() > 0) {
        printSuccessMessage(ids + " successfully deleted.");
        LOGGER.info(ids + " removed using catalog:remove command");
    } else {
        printErrorMessage(ids + " could not be deleted.");
        LOGGER.info(ids + " could not be deleted using catalog:remove command");
    }
    return null;
}
Also used : QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Arrays(java.util.Arrays) QueryImpl(ddf.catalog.operation.impl.QueryImpl) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Argument(org.apache.karaf.shell.api.action.Argument) DeleteResponse(ddf.catalog.operation.DeleteResponse) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Command(org.apache.karaf.shell.api.action.Command) List(java.util.List) SourceResponse(ddf.catalog.operation.SourceResponse) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) Service(org.apache.karaf.shell.api.action.lifecycle.Service) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) Option(org.apache.karaf.shell.api.action.Option) Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) HashMap(java.util.HashMap) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DeleteResponse(ddf.catalog.operation.DeleteResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade)

Example 10 with DeleteResponse

use of ddf.catalog.operation.DeleteResponse in project ddf by codice.

the class RemoveAllCommandTest method testExecuteWithSubject.

/**
     * Checks the forced (-f) generic case.
     *
     * @throws Exception
     */
@Test
public void testExecuteWithSubject() throws Exception {
    // given
    RemoveAllCommand removeAllCommand = new RemoveAllCommand();
    final CatalogFramework catalogFramework = mock(CatalogFramework.class);
    QueryResponse queryResponse = mock(QueryResponse.class);
    when(queryResponse.getResults()).thenReturn(getResultList(10));
    when(catalogFramework.query(isA(QueryRequest.class))).thenReturn(queryResponse);
    DeleteResponse deleteResponse = mock(DeleteResponse.class);
    when(deleteResponse.getDeletedMetacards()).thenReturn(getMetacardList(10));
    when(catalogFramework.delete(isA(DeleteRequest.class))).thenReturn(deleteResponse);
    removeAllCommand.catalogFramework = catalogFramework;
    removeAllCommand.filterBuilder = new GeotoolsFilterBuilder();
    removeAllCommand.batchSize = 11;
    removeAllCommand.force = true;
    // when
    removeAllCommand.executeWithSubject();
    // then
    verify(catalogFramework, times(1)).delete(isA(DeleteRequest.class));
}
Also used : DeleteResponse(ddf.catalog.operation.DeleteResponse) QueryRequest(ddf.catalog.operation.QueryRequest) QueryResponse(ddf.catalog.operation.QueryResponse) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) DeleteRequest(ddf.catalog.operation.DeleteRequest) Test(org.junit.Test)

Aggregations

DeleteResponse (ddf.catalog.operation.DeleteResponse)54 Test (org.junit.Test)37 Metacard (ddf.catalog.data.Metacard)23 DeleteRequest (ddf.catalog.operation.DeleteRequest)23 ArrayList (java.util.ArrayList)19 DeleteRequestImpl (ddf.catalog.operation.impl.DeleteRequestImpl)18 HashMap (java.util.HashMap)17 Serializable (java.io.Serializable)15 DeleteResponseImpl (ddf.catalog.operation.impl.DeleteResponseImpl)14 CreateResponse (ddf.catalog.operation.CreateResponse)13 List (java.util.List)11 QueryResponse (ddf.catalog.operation.QueryResponse)9 QueryRequest (ddf.catalog.operation.QueryRequest)8 IngestException (ddf.catalog.source.IngestException)8 Result (ddf.catalog.data.Result)7 UpdateResponse (ddf.catalog.operation.UpdateResponse)7 QueryImpl (ddf.catalog.operation.impl.QueryImpl)7 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)7 CatalogFramework (ddf.catalog.CatalogFramework)6 Update (ddf.catalog.operation.Update)6