public void refresh() { logger.debug("Refreshing gateway state..."); try { Set<io.gravitee.repository.management.model.Api> apis = apiRepository.findAll(); Map<String, Api> apisMap = apis.stream() .map(this::convert) .filter(api -> api != null) .collect(Collectors.toMap(Api::getId, api -> api)); // Determine APIs to undeploy Set<String> apiToRemove = apiManager .apis() .stream() .filter(apiId -> !apisMap.containsKey(apiId.getId())) .map(Api::getId) .collect(Collectors.toSet()); apiToRemove.stream().forEach(apiManager::undeploy); // Determine APIs to update apisMap .keySet() .stream() .filter(apiId -> apiManager.get(apiId) != null) .forEach( apiId -> { // Get local cached API Api deployedApi = apiManager.get(apiId); // Get API from store Api remoteApi = apisMap.get(apiId); if (deployedApi.getDeployedAt().before(remoteApi.getDeployedAt())) { apiManager.update(remoteApi); } }); // Determine APIs to create apisMap .keySet() .stream() .filter(apiId -> apiManager.get(apiId) == null) .forEach( apiId -> { Api newApi = apisMap.get(apiId); apiManager.deploy(newApi); }); } catch (TechnicalException te) { logger.error("Unable to sync instance", te); } }
private Api convert(io.gravitee.repository.management.model.Api remoteApi) { try { String definition = remoteApi.getDefinition(); if (definition != null && !definition.isEmpty()) { Api api = objectMapper.readValue(definition, Api.class); api.setId(remoteApi.getId()); api.setName(remoteApi.getName()); api.setVersion(remoteApi.getVersion()); api.setEnabled(remoteApi.getLifecycleState() == LifecycleState.STARTED); api.setDeployedAt(remoteApi.getUpdatedAt()); return api; } } catch (IOException ioe) { logger.error("Unable to prepare API definition from repository", ioe); } return null; }