@Test @RunWithCustomExecutor public void switchIndexesTest() throws Exception { String mapping = ElasticsearchUtil .transferJsonToMapping(new FileInputStream(PropertiesUtils.findFile(TEST_ES_MAPPING_JSON))); FunctionalAdminCollections.ACCESS_CONTRACT.getEsClient().createIndexAndAliasIfAliasNotExists( FunctionalAdminCollections.CONTEXT.getVitamCollection().getName().toLowerCase(), mapping, null); String newIndex = FunctionalAdminCollections.ACCESS_CONTRACT.getEsClient().createIndexWithoutAlias( FunctionalAdminCollections.CONTEXT.getVitamCollection().getName().toLowerCase(), mapping, null); VitamThreadUtils.getVitamSession().setTenantId(TENANT_ID); List<SwitchIndexParameters> listSwitches = new ArrayList<>(); SwitchIndexParameters switchParams = new SwitchIndexParameters(); switchParams.setAlias(FunctionalAdminCollections.CONTEXT.getVitamCollection().getName().toLowerCase()); switchParams.setIndexName(newIndex); listSwitches.add(switchParams); given().contentType(ContentType.JSON).body(listSwitches) .header(GlobalDataRest.X_TENANT_ID, TENANT_ID).accept(ContentType.JSON) .when().post(ALIASES_URI) .then().statusCode(Status.OK.getStatusCode()); listSwitches = new ArrayList<>(); switchParams = new SwitchIndexParameters(); switchParams.setAlias(FunctionalAdminCollections.CONTEXT.getVitamCollection().getName().toLowerCase()); switchParams.setIndexName("unknown_index"); listSwitches.add(switchParams); given().contentType(ContentType.JSON).body(listSwitches) .header(GlobalDataRest.X_TENANT_ID, TENANT_ID).accept(ContentType.JSON) .when().post(ALIASES_URI) .then().statusCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()); }
@Path(ALIASES_URI) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response switchIndexes(@Valid List<SwitchIndexParameters> switchIndexParameters) { ParametersChecker.checkParameter(OPTIONS_IS_MANDATORY_PATAMETER, switchIndexParameters); List<IndexationResult> results = new ArrayList<IndexationResult>(); AtomicBoolean atLeastOneKO = new AtomicBoolean(false); AtomicBoolean atLeastOneOK = new AtomicBoolean(false); switchIndexParameters.forEach(switchIndex -> { try (MetaDataClient metaDataClient = metaDataClientFactory.getClient(); LogbookOperationsClient logbookOperationsClient = logbookOperationsClientFactory.getClient()){ if (VitamCollectionHelper.isLogbookCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(logbookOperationsClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else if (VitamCollectionHelper.isMetadataCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(metaDataClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else { try { indexationHelper.switchIndex(switchIndex.getIndexName().split("_")[0], switchIndex.getIndexName().toLowerCase(), FunctionalAdminCollections.ACCESS_CONTRACT.getEsClient()); atLeastOneOK.set(true); } catch (IllegalArgumentException e) { String message = String.format("Try to switch indexes on unknown collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); atLeastOneKO.set(true); LOGGER.error(message, e); } } } catch (DatabaseException | LogbookClientServerException | InvalidParseOperationException | MetaDataClientServerException | MetaDataNotFoundException e) { atLeastOneKO.set(true); String message = String.format("Error while switching indexes on collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); LOGGER.error(message, e); } }); if (atLeastOneKO.get() && !atLeastOneOK.get()) { final Status returnedStatus = Status.INTERNAL_SERVER_ERROR; Response response = Response.status(returnedStatus) .entity(new VitamError(returnedStatus.name()).setHttpCode(returnedStatus.getStatusCode()) .setContext(ServiceName.FUNCTIONAL_ADMINISTRATION.getName()) .setState("code_vitam") .setMessage(JsonHandler.unprettyPrint(results)) .setDescription("Internal error.")) .build(); return response; } else if (atLeastOneKO.get() && atLeastOneOK.get()) { return Response.status(Status.ACCEPTED).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.ACCEPTED.getStatusCode())).build(); } else { return Response.status(Status.OK).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.OK.getStatusCode())).build(); } }
ReindexationResource { @Path(ALIASES_URI) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response switchIndexes(@Valid List<SwitchIndexParameters> switchIndexParameters) { ParametersChecker.checkParameter(OPTIONS_IS_MANDATORY_PATAMETER, switchIndexParameters); List<IndexationResult> results = new ArrayList<IndexationResult>(); AtomicBoolean atLeastOneKO = new AtomicBoolean(false); AtomicBoolean atLeastOneOK = new AtomicBoolean(false); switchIndexParameters.forEach(switchIndex -> { try (MetaDataClient metaDataClient = metaDataClientFactory.getClient(); LogbookOperationsClient logbookOperationsClient = logbookOperationsClientFactory.getClient()){ if (VitamCollectionHelper.isLogbookCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(logbookOperationsClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else if (VitamCollectionHelper.isMetadataCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(metaDataClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else { try { indexationHelper.switchIndex(switchIndex.getIndexName().split("_")[0], switchIndex.getIndexName().toLowerCase(), FunctionalAdminCollections.ACCESS_CONTRACT.getEsClient()); atLeastOneOK.set(true); } catch (IllegalArgumentException e) { String message = String.format("Try to switch indexes on unknown collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); atLeastOneKO.set(true); LOGGER.error(message, e); } } } catch (DatabaseException | LogbookClientServerException | InvalidParseOperationException | MetaDataClientServerException | MetaDataNotFoundException e) { atLeastOneKO.set(true); String message = String.format("Error while switching indexes on collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); LOGGER.error(message, e); } }); if (atLeastOneKO.get() && !atLeastOneOK.get()) { final Status returnedStatus = Status.INTERNAL_SERVER_ERROR; Response response = Response.status(returnedStatus) .entity(new VitamError(returnedStatus.name()).setHttpCode(returnedStatus.getStatusCode()) .setContext(ServiceName.FUNCTIONAL_ADMINISTRATION.getName()) .setState("code_vitam") .setMessage(JsonHandler.unprettyPrint(results)) .setDescription("Internal error.")) .build(); return response; } else if (atLeastOneKO.get() && atLeastOneOK.get()) { return Response.status(Status.ACCEPTED).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.ACCEPTED.getStatusCode())).build(); } else { return Response.status(Status.OK).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.OK.getStatusCode())).build(); } } }
ReindexationResource { @Path(ALIASES_URI) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response switchIndexes(@Valid List<SwitchIndexParameters> switchIndexParameters) { ParametersChecker.checkParameter(OPTIONS_IS_MANDATORY_PATAMETER, switchIndexParameters); List<IndexationResult> results = new ArrayList<IndexationResult>(); AtomicBoolean atLeastOneKO = new AtomicBoolean(false); AtomicBoolean atLeastOneOK = new AtomicBoolean(false); switchIndexParameters.forEach(switchIndex -> { try (MetaDataClient metaDataClient = metaDataClientFactory.getClient(); LogbookOperationsClient logbookOperationsClient = logbookOperationsClientFactory.getClient()){ if (VitamCollectionHelper.isLogbookCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(logbookOperationsClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else if (VitamCollectionHelper.isMetadataCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(metaDataClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else { try { indexationHelper.switchIndex(switchIndex.getIndexName().split("_")[0], switchIndex.getIndexName().toLowerCase(), FunctionalAdminCollections.ACCESS_CONTRACT.getEsClient()); atLeastOneOK.set(true); } catch (IllegalArgumentException e) { String message = String.format("Try to switch indexes on unknown collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); atLeastOneKO.set(true); LOGGER.error(message, e); } } } catch (DatabaseException | LogbookClientServerException | InvalidParseOperationException | MetaDataClientServerException | MetaDataNotFoundException e) { atLeastOneKO.set(true); String message = String.format("Error while switching indexes on collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); LOGGER.error(message, e); } }); if (atLeastOneKO.get() && !atLeastOneOK.get()) { final Status returnedStatus = Status.INTERNAL_SERVER_ERROR; Response response = Response.status(returnedStatus) .entity(new VitamError(returnedStatus.name()).setHttpCode(returnedStatus.getStatusCode()) .setContext(ServiceName.FUNCTIONAL_ADMINISTRATION.getName()) .setState("code_vitam") .setMessage(JsonHandler.unprettyPrint(results)) .setDescription("Internal error.")) .build(); return response; } else if (atLeastOneKO.get() && atLeastOneOK.get()) { return Response.status(Status.ACCEPTED).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.ACCEPTED.getStatusCode())).build(); } else { return Response.status(Status.OK).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.OK.getStatusCode())).build(); } } @VisibleForTesting  ReindexationResource(LogbookOperationsClientFactory logbookOperationsClientFactory,
        MetaDataClientFactory metaDataClientFactory, IndexationHelper indexationHelper);  ReindexationResource(); }
ReindexationResource { @Path(ALIASES_URI) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response switchIndexes(@Valid List<SwitchIndexParameters> switchIndexParameters) { ParametersChecker.checkParameter(OPTIONS_IS_MANDATORY_PATAMETER, switchIndexParameters); List<IndexationResult> results = new ArrayList<IndexationResult>(); AtomicBoolean atLeastOneKO = new AtomicBoolean(false); AtomicBoolean atLeastOneOK = new AtomicBoolean(false); switchIndexParameters.forEach(switchIndex -> { try (MetaDataClient metaDataClient = metaDataClientFactory.getClient(); LogbookOperationsClient logbookOperationsClient = logbookOperationsClientFactory.getClient()){ if (VitamCollectionHelper.isLogbookCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(logbookOperationsClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else if (VitamCollectionHelper.isMetadataCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(metaDataClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else { try { indexationHelper.switchIndex(switchIndex.getIndexName().split("_")[0], switchIndex.getIndexName().toLowerCase(), FunctionalAdminCollections.ACCESS_CONTRACT.getEsClient()); atLeastOneOK.set(true); } catch (IllegalArgumentException e) { String message = String.format("Try to switch indexes on unknown collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); atLeastOneKO.set(true); LOGGER.error(message, e); } } } catch (DatabaseException | LogbookClientServerException | InvalidParseOperationException | MetaDataClientServerException | MetaDataNotFoundException e) { atLeastOneKO.set(true); String message = String.format("Error while switching indexes on collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); LOGGER.error(message, e); } }); if (atLeastOneKO.get() && !atLeastOneOK.get()) { final Status returnedStatus = Status.INTERNAL_SERVER_ERROR; Response response = Response.status(returnedStatus) .entity(new VitamError(returnedStatus.name()).setHttpCode(returnedStatus.getStatusCode()) .setContext(ServiceName.FUNCTIONAL_ADMINISTRATION.getName()) .setState("code_vitam") .setMessage(JsonHandler.unprettyPrint(results)) .setDescription("Internal error.")) .build(); return response; } else if (atLeastOneKO.get() && atLeastOneOK.get()) { return Response.status(Status.ACCEPTED).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.ACCEPTED.getStatusCode())).build(); } else { return Response.status(Status.OK).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.OK.getStatusCode())).build(); } } @VisibleForTesting  ReindexationResource(LogbookOperationsClientFactory logbookOperationsClientFactory,
        MetaDataClientFactory metaDataClientFactory, IndexationHelper indexationHelper);  ReindexationResource(); @Path(REINDEX_URI) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response reindex(@Valid List<IndexParameters> indexParameters); @Path(ALIASES_URI) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response switchIndexes(@Valid List<SwitchIndexParameters> switchIndexParameters); }
ReindexationResource { @Path(ALIASES_URI) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response switchIndexes(@Valid List<SwitchIndexParameters> switchIndexParameters) { ParametersChecker.checkParameter(OPTIONS_IS_MANDATORY_PATAMETER, switchIndexParameters); List<IndexationResult> results = new ArrayList<IndexationResult>(); AtomicBoolean atLeastOneKO = new AtomicBoolean(false); AtomicBoolean atLeastOneOK = new AtomicBoolean(false); switchIndexParameters.forEach(switchIndex -> { try (MetaDataClient metaDataClient = metaDataClientFactory.getClient(); LogbookOperationsClient logbookOperationsClient = logbookOperationsClientFactory.getClient()){ if (VitamCollectionHelper.isLogbookCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(logbookOperationsClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else if (VitamCollectionHelper.isMetadataCollection(switchIndex.getAlias())) { String[] splits = switchIndex.getIndexName().split("_"); switchIndex.setAlias(splits[0] + "_" + splits[1]); results.add( JsonHandler.getFromJsonNode(metaDataClient.switchIndexes(switchIndex), IndexationResult.class)); atLeastOneOK.set(true); } else { try { indexationHelper.switchIndex(switchIndex.getIndexName().split("_")[0], switchIndex.getIndexName().toLowerCase(), FunctionalAdminCollections.ACCESS_CONTRACT.getEsClient()); atLeastOneOK.set(true); } catch (IllegalArgumentException e) { String message = String.format("Try to switch indexes on unknown collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); atLeastOneKO.set(true); LOGGER.error(message, e); } } } catch (DatabaseException | LogbookClientServerException | InvalidParseOperationException | MetaDataClientServerException | MetaDataNotFoundException e) { atLeastOneKO.set(true); String message = String.format("Error while switching indexes on collection %s", switchIndex .getAlias()); results.add(indexationHelper.getKOResult(switchIndex, message)); LOGGER.error(message, e); } }); if (atLeastOneKO.get() && !atLeastOneOK.get()) { final Status returnedStatus = Status.INTERNAL_SERVER_ERROR; Response response = Response.status(returnedStatus) .entity(new VitamError(returnedStatus.name()).setHttpCode(returnedStatus.getStatusCode()) .setContext(ServiceName.FUNCTIONAL_ADMINISTRATION.getName()) .setState("code_vitam") .setMessage(JsonHandler.unprettyPrint(results)) .setDescription("Internal error.")) .build(); return response; } else if (atLeastOneKO.get() && atLeastOneOK.get()) { return Response.status(Status.ACCEPTED).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.ACCEPTED.getStatusCode())).build(); } else { return Response.status(Status.OK).entity(new RequestResponseOK() .addAllResults(results).setHttpCode(Status.OK.getStatusCode())).build(); } } @VisibleForTesting  ReindexationResource(LogbookOperationsClientFactory logbookOperationsClientFactory,
        MetaDataClientFactory metaDataClientFactory, IndexationHelper indexationHelper);  ReindexationResource(); @Path(REINDEX_URI) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response reindex(@Valid List<IndexParameters> indexParameters); @Path(ALIASES_URI) @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response switchIndexes(@Valid List<SwitchIndexParameters> switchIndexParameters);  }