Skip to content

Commit

Permalink
commit for #772
Browse files Browse the repository at this point in the history
  • Loading branch information
chopadetejaswini committed Aug 18, 2023
1 parent 8811c86 commit 1f8be7f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import dev.sunbirdrc.pojos.ComponentHealthInfo;
import dev.sunbirdrc.pojos.Filter;
import dev.sunbirdrc.pojos.FilterOperators;
import dev.sunbirdrc.pojos.SearchQuery;
import dev.sunbirdrc.registry.middleware.util.Constants;
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;
import java.util.*;

import org.apache.commons.collections4.KeyValue;
import org.apache.commons.collections4.keyvalue.DefaultKeyValue;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
Expand All @@ -45,9 +35,17 @@
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;

import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

import static dev.sunbirdrc.registry.middleware.util.Constants.CONNECTION_FAILURE;
import static dev.sunbirdrc.registry.middleware.util.Constants.SUNBIRD_ELASTIC_SERVICE_NAME;

Expand All @@ -62,6 +60,8 @@ public class ElasticServiceImpl implements IElasticService {
private static String password;
private static String defaultScheme;

@Autowired
private static Environment environment;
public void setConnectionInfo(String connection) {
connectionInfo = connection;
}
Expand Down Expand Up @@ -89,32 +89,41 @@ public void init(Set<String> indices) throws RuntimeException {
/**
* This method creates the high-level-client w.r.to index, if client is not created. for every index one client object is created
*
* @param indexName for ElasticSearch
* @param connectionInfo of ElasticSearch
// * @param indexName for ElasticSearch
// * @param connectionInfo of ElasticSearch
*/

private static void createClient(String indexName, String connectionInfo) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, password));
if (!esClient.containsKey(indexName)) {
Map<String, KeyValue<Integer, String>> hostPort = new HashMap<>();
List<HttpHost> httpHosts = new ArrayList<>();
for (String info : connectionInfo.split(",")) {
try {
URL url = new URL(info);
httpHosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol()));
} catch (Exception e) {
String port = Optional.ofNullable(info.split(":").length == 1 ? "-1" : info.split(":")[1]).get();
httpHosts.add(new HttpHost(info.split(":")[0], Integer.valueOf(port), defaultScheme));
if (authEnabled) {
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
}

try {
if (!esClient.containsKey(indexName)) {
List<HttpHost> httpHosts = Arrays.stream(connectionInfo.split(","))
.map(info -> {
try {
URL url = new URL(info);
return new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
} catch (Exception e) {
String port = Optional.ofNullable(info.split(":").length == 1 ? "-1" : info.split(":")[1]).orElse("-1");
return new HttpHost(info.split(":")[0], Integer.valueOf(port), defaultScheme);
}
})
.collect(Collectors.toList());

RestClientBuilder restClientBuilder = RestClient.builder(httpHosts.toArray(new HttpHost[0]));
if (authEnabled) {
restClientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
}
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
if (null != client) {
esClient.put(indexName, client);
}
}
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
if(authEnabled) {
restClientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
}
RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
if (null != client)
esClient.put(indexName, client);
} catch (Exception e) {
e.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
Expand Down Expand Up @@ -298,8 +299,6 @@ public IReadService readService() {
ServiceProvider searchProvider = new ServiceProvider();
return searchProvider.getReadInstance(readProviderName, isElasticSearchEnabled());
}

@Bean
public boolean isElasticSearchEnabled() {
return (searchProviderName.equals("dev.sunbirdrc.registry.service.ElasticSearchService"));
}
Expand Down Expand Up @@ -414,6 +413,7 @@ public TaskExecutor auditTaskExecutor() {
* @throws IOException
*/
@Bean
@ConditionalOnProperty(name = "search.providerName",havingValue = "dev.sunbirdrc.registry.service.ElasticSearchService",matchIfMissing = false)
public IElasticService elasticService() throws IOException {
ElasticServiceImpl elasticService = new ElasticServiceImpl();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
package dev.sunbirdrc.registry.service;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import dev.sunbirdrc.elastic.IElasticService;
import dev.sunbirdrc.pojos.AuditRecord;
import dev.sunbirdrc.registry.exception.RecordNotFoundException;
import dev.sunbirdrc.registry.middleware.util.Constants;
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
import dev.sunbirdrc.registry.sink.shard.Shard;
import dev.sunbirdrc.registry.util.ReadConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
* This class provide read option with Elastic search
* database to operate
*
*/
@Component
@ConditionalOnProperty(name = "read.providerName",havingValue = "dev.sunbirdrc.registry.service.ElasticReadService",matchIfMissing = false)
public class ElasticReadService implements IReadService {

private static Logger logger = LoggerFactory.getLogger(ElasticReadService.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
package dev.sunbirdrc.registry.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import dev.sunbirdrc.elastic.IElasticService;
import dev.sunbirdrc.pojos.APIMessage;
import dev.sunbirdrc.pojos.AuditRecord;
import dev.sunbirdrc.pojos.Filter;
import dev.sunbirdrc.pojos.FilterOperators;
import dev.sunbirdrc.pojos.SearchQuery;
import dev.sunbirdrc.pojos.*;
import dev.sunbirdrc.registry.middleware.util.Constants;
import dev.sunbirdrc.registry.util.RecordIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
* This class provide search option with Elastic search Hits elastic search
* database to operate
*
*/
@Component
@ConditionalOnProperty(name = "search.providerName",havingValue = "dev.sunbirdrc.registry.service.ElasticSearchService",matchIfMissing = false)
public class ElasticSearchService implements ISearchService {
private static Logger logger = LoggerFactory.getLogger(ElasticSearchService.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ public class SchemaService {
private static final String STATUS = "status";
@Autowired
private IDefinitionsManager definitionsManager;

@Autowired
private boolean isElasticSearchEnabled;

@Autowired
private IValidate validator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import dev.sunbirdrc.registry.middleware.util.Constants;
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
import dev.sunbirdrc.registry.middleware.util.OSSystemFields;
import dev.sunbirdrc.registry.model.event.Event;
import dev.sunbirdrc.registry.model.EventType;
import dev.sunbirdrc.registry.model.event.Event;
import dev.sunbirdrc.registry.service.*;
import dev.sunbirdrc.registry.sink.DatabaseProvider;
import dev.sunbirdrc.registry.sink.OSGraph;
Expand All @@ -33,7 +33,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -128,7 +127,7 @@ public class RegistryServiceImpl implements RegistryService {
@Autowired
private SchemaService schemaService;

@Autowired
@Autowired(required = false)
private IElasticService elasticService;

@Autowired
Expand Down
6 changes: 1 addition & 5 deletions java/registry/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ keycloak-user:
claims:
url: ${claims_url:http://localhost:8082}
authentication:
enabled: ${authentication_enabled:true}
enabled: ${authentication_enabled:false}
publicKey: ${authentication_publickey:}
realm: ${authentication_realm:sunbird-rc}
url: ${authentication_url:localhost:8443/auth}
Expand Down Expand Up @@ -304,8 +304,6 @@ auditTaskExecutor:
# elastic-search configuration details
elastic:
search:
# elastic-search can be enable and disable through this flag
enabled: ${elastic_search_enabled:false}
# elastic-search connection info
connection_url: ${elastic_search_connection_url:localhost:9200}
add_shard_prefix: ${elastic_search_add_shard_prefix:true}
Expand Down Expand Up @@ -474,8 +472,6 @@ auditTaskExecutor:
# elastic-search configuration details
elastic:
search:
# elastic-search can be enable and disable through this flag
enabled: ${elastic_search_enabled:true}
# elastic-search connection info
connection_url: ${elastic_search_connection_url:localhost:9200}

0 comments on commit 1f8be7f

Please sign in to comment.