diff --git a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/service/ElasticSettingServiceIBMImpl.java b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/service/ElasticSettingServiceIBMImpl.java index 6c2f6ce6b94811a0609bfb4913a8748e7a5c46f0..fc8e83cabb79c4e5f8992eecdaeed91e4924adc8 100644 --- a/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/service/ElasticSettingServiceIBMImpl.java +++ b/provider/indexer-ibm/src/main/java/org/opengroup/osdu/indexer/service/ElasticSettingServiceIBMImpl.java @@ -6,13 +6,20 @@ package org.opengroup.osdu.indexer.service; import org.apache.http.HttpStatus; import org.opengroup.osdu.core.common.model.search.ClusterSettings; import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.common.multitenancy.ITenantInfoService; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.provider.interfaces.IElasticRepository; +import org.opengroup.osdu.indexer.config.IndexerConfigurationProperties; import org.opengroup.osdu.core.common.provider.interfaces.IElasticCredentialsCache; import org.opengroup.osdu.core.common.model.indexer.IElasticSettingService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + import javax.inject.Inject; @Service @@ -27,6 +34,10 @@ public class ElasticSettingServiceIBMImpl implements IElasticSettingService { private IElasticCredentialsCache elasticCredentialCache; @Inject private JaxRsDpsLog logger; + @Inject + private javax.inject.Provider<ITenantInfoService> tenantInfoServiceProvider; + @Inject + private IndexerConfigurationProperties configurationProperties; @Override public ClusterSettings getElasticClusterInformation() { @@ -47,4 +58,34 @@ public class ElasticSettingServiceIBMImpl implements IElasticSettingService { this.elasticCredentialCache.put(cacheKey, clusterInfo); return clusterInfo; } + + @Override + public Map<String, ClusterSettings> getAllClustersSettings() { + List<TenantInfo> tenantInfos = tenantInfoServiceProvider.get().getAllTenantInfos(); + return tenantInfos.stream() + .collect(Collectors.toMap(TenantInfo::getDataPartitionId, + this::getClusterSettingsByTenantInfo)); + } + + private ClusterSettings getClusterSettingsByTenantInfo(TenantInfo tenantInfo) { + String cacheKey = String.format("%s-%s", configurationProperties.getGaeService(), + tenantInfo.getName()); + + ClusterSettings clusterInfo = (ClusterSettings) this.elasticCredentialCache.get(cacheKey); + if (clusterInfo != null) { + return clusterInfo; + } + + logger.warning(String.format("elastic-credential cache missed for tenant: %s", + tenantInfo.getName())); + + clusterInfo = this.elasticRepository.getElasticClusterSettings(tenantInfo); + if (clusterInfo == null) { + throw new AppException(HttpStatus.SC_NOT_FOUND, "Tenant not found", + "No information about the given tenant was found"); + } + + this.elasticCredentialCache.put(cacheKey, clusterInfo); + return clusterInfo; + } }