-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from holashchand/id-gen-service
Id gen service integration
- Loading branch information
Showing
117 changed files
with
3,014 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
java/pojos/src/main/java/dev/sunbirdrc/pojos/UniqueIdentifierField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dev.sunbirdrc.pojos; | ||
|
||
import lombok.*; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
public class UniqueIdentifierField { | ||
String field; | ||
String idName; | ||
String format; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
java/registry/src/main/java/dev/sunbirdrc/registry/exception/UniqueIdentifierException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dev.sunbirdrc.registry.exception; | ||
|
||
public class UniqueIdentifierException extends Exception{ | ||
|
||
|
||
private static final long serialVersionUID = -6315798195661762883L; | ||
|
||
public UniqueIdentifierException(CustomException e) { | ||
super(e); | ||
} | ||
|
||
public class CreationException extends CustomException { | ||
private static final long serialVersionUID = 6174717850058203377L; | ||
|
||
public CreationException(String msg) { | ||
super("Unable to create unique ID: " + msg); | ||
} | ||
} | ||
|
||
public static class UnreachableException extends CustomException { | ||
|
||
private static final long serialVersionUID = 5384120386096139086L; | ||
|
||
public UnreachableException(String message) { | ||
super("Unable to reach id-gen service: " + message); | ||
} | ||
} | ||
|
||
public static class GenerateException extends CustomException { | ||
|
||
private static final long serialVersionUID = 8311355815972497247L; | ||
|
||
public GenerateException(String message) { | ||
super("Unable to generate id: " + message); | ||
} | ||
} | ||
|
||
public static class IdFormatException extends CustomException { | ||
|
||
private static final long serialVersionUID = 8311355815972497248L; | ||
|
||
public IdFormatException(String message) { | ||
super("Unable to save UniqueIdentifierField format: " + message); | ||
} | ||
} | ||
|
||
public static class FieldConfigNotFoundException extends CustomException { | ||
|
||
private static final long serialVersionUID = 8311355815972497249L; | ||
|
||
public FieldConfigNotFoundException(String message) { | ||
super("Unable to find UniqueIdentifierField configuration in schema configuration: " + message); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
java/registry/src/main/java/dev/sunbirdrc/registry/service/IIdGenService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dev.sunbirdrc.registry.service; | ||
|
||
import dev.sunbirdrc.pojos.HealthIndicator; | ||
import dev.sunbirdrc.pojos.UniqueIdentifierField; | ||
import dev.sunbirdrc.registry.exception.CustomException; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface IIdGenService extends HealthIndicator { | ||
|
||
Map<String, String> generateId(List<UniqueIdentifierField> uniqueIdentifierFields) throws CustomException; | ||
|
||
void saveIdFormat(List<UniqueIdentifierField> uniqueIdentifierFields) throws CustomException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
java/registry/src/main/java/dev/sunbirdrc/registry/service/impl/IdGenService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package dev.sunbirdrc.registry.service.impl; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.google.gson.Gson; | ||
import dev.sunbirdrc.pojos.ComponentHealthInfo; | ||
import dev.sunbirdrc.pojos.SunbirdRCInstrumentation; | ||
import dev.sunbirdrc.pojos.UniqueIdentifierField; | ||
import dev.sunbirdrc.registry.exception.CustomException; | ||
import dev.sunbirdrc.registry.exception.UniqueIdentifierException.*; | ||
import dev.sunbirdrc.registry.middleware.util.JSONUtil; | ||
import dev.sunbirdrc.registry.service.IIdGenService; | ||
import dev.sunbirdrc.registry.util.IDefinitionsManager; | ||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
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.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.client.ResourceAccessException; | ||
import org.springframework.web.client.RestClientException; | ||
|
||
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_ID_GEN_SERVICE_NAME; | ||
|
||
@Service | ||
@ConditionalOnProperty(name = "idgen.enabled", havingValue = "true") | ||
public class IdGenService implements IIdGenService { | ||
private static final Logger logger = LoggerFactory.getLogger(IdGenService.class); | ||
|
||
@Value("${idgen.generateURL}") | ||
private String generateUrl; | ||
@Value("${idgen.idFormatURL}") | ||
private String idFormatUrl; | ||
@Value("${idgen.healthCheckURL}") | ||
private String healthCheckUrl; | ||
@Value("${idgen.tenantId}") | ||
private String tenantId; | ||
@Value("${idgen.enabled:false}") | ||
private boolean enabled; | ||
|
||
@Autowired | ||
private Gson gson; | ||
@Autowired | ||
private SunbirdRCInstrumentation watch; | ||
@Autowired | ||
private RetryRestTemplate retryRestTemplate; | ||
|
||
@Override | ||
public Map<String, String> generateId(List<UniqueIdentifierField> uniqueIdentifierFields) throws CustomException { | ||
if(!enabled) throw new UnreachableException("IDGEN service not enabled"); | ||
HttpEntity<String> entity = getIdgenRequest(uniqueIdentifierFields); | ||
|
||
try { | ||
watch.start("IdGenServiceImpl.generateId"); | ||
ResponseEntity<String> response = retryRestTemplate.postForEntity(generateUrl, entity); | ||
watch.stop("IdGenServiceImpl.generateId"); | ||
JsonNode results = JSONUtil.convertStringJsonNode(response.getBody()); | ||
if("SUCCESSFUL".equals(results.at("/responseInfo/status").asText())) { | ||
logger.info("Generated value successfully"); | ||
Map<String, String> resultMap = new HashMap<>(); | ||
Iterator<JsonNode> iterator = ((ArrayNode) results.at("/idResponses")).elements(); | ||
int i = 0; | ||
while(iterator.hasNext()) { | ||
resultMap.put(uniqueIdentifierFields.get(i).getField(), iterator.next().at("/id").asText()); | ||
i++; | ||
} | ||
return resultMap; | ||
} | ||
throw new GenerateException(results.at("/idResponses").asText()); | ||
} catch (ResourceAccessException e) { | ||
logger.error("Exception while connecting {} : {}", getServiceName(), ExceptionUtils.getStackTrace(e)); | ||
throw new UnreachableException("Exception while connecting idgen service ! "); | ||
} catch (Exception e) { | ||
logger.error("Exception in {}: {}", getServiceName(), ExceptionUtils.getStackTrace(e)); | ||
throw new GenerateException("Exception occurred while generating id"); | ||
} | ||
} | ||
|
||
@Override | ||
public void saveIdFormat(List<UniqueIdentifierField> uniqueIdentifierFields) throws CustomException { | ||
if(!enabled) throw new UnreachableException("IDGEN service not enabled"); | ||
HttpEntity<String> entity = getIdgenRequest(uniqueIdentifierFields); | ||
|
||
try { | ||
watch.start("IdGenServiceImpl.saveFormat"); | ||
ResponseEntity<String> response = retryRestTemplate.postForEntity(idFormatUrl, entity); | ||
watch.stop("IdGenServiceImpl.saveFormat"); | ||
JsonNode results = JSONUtil.convertStringJsonNode(response.getBody()); | ||
if(!"SUCCESSFUL".equals(results.at("/responseInfo/status").asText())) { | ||
Iterator<JsonNode> iterator = ((ArrayNode) results.at("/errorMsgs")).elements(); | ||
while(iterator.hasNext()) { | ||
JsonNode node = iterator.next(); | ||
if (node.isNull()) continue; | ||
if (!node.asText().contains("already exists")) { | ||
throw new IdFormatException(node.asText()); | ||
} | ||
} | ||
} | ||
} catch (ResourceAccessException e) { | ||
logger.error("Exception while connecting {} : {}", getServiceName(), ExceptionUtils.getStackTrace(e)); | ||
throw new UnreachableException("Exception while connecting idgen service ! "); | ||
} catch (Exception e) { | ||
logger.error("Exception in {}: {}", getServiceName(), ExceptionUtils.getStackTrace(e)); | ||
throw new GenerateException("Exception occurred while generating id"); | ||
} | ||
} | ||
|
||
private HttpEntity<String> getIdgenRequest(List<UniqueIdentifierField> uniqueIdentifierFields) { | ||
Map<String, Object> map = new HashMap<>(); | ||
map.put("RequestInfo", new HashMap<>()); | ||
List<Map<String, String>> idRequests = uniqueIdentifierFields.stream().map(field -> { | ||
Map<String, String> idRequest = new HashMap<>(); | ||
idRequest.put("idName", field.getIdName()); | ||
idRequest.put("tenantId", tenantId); | ||
idRequest.put("format", field.getFormat()); | ||
return idRequest; | ||
}).collect(Collectors.toList()); | ||
|
||
map.put("idRequests", idRequests); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
return new HttpEntity<>(gson.toJson(map), headers); | ||
} | ||
|
||
@Override | ||
public String getServiceName() { | ||
return SUNBIRD_ID_GEN_SERVICE_NAME; | ||
} | ||
|
||
@Override | ||
public ComponentHealthInfo getHealthInfo() { | ||
if (enabled) { | ||
try { | ||
ResponseEntity<String> response = retryRestTemplate.getForEntity(healthCheckUrl); | ||
if (!StringUtils.isEmpty(response.getBody()) && response.getBody().equalsIgnoreCase("UP")) { | ||
logger.debug(" running !"); | ||
return new ComponentHealthInfo(getServiceName(), true); | ||
} else { | ||
return new ComponentHealthInfo(getServiceName(), false, CONNECTION_FAILURE, response.getBody()); | ||
} | ||
} catch (RestClientException ex) { | ||
logger.error("RestClientException when checking the health of the idgen service: {}", ExceptionUtils.getStackTrace(ex)); | ||
return new ComponentHealthInfo(getServiceName(), false, CONNECTION_FAILURE, ex.getMessage()); | ||
} | ||
} else { | ||
return new ComponentHealthInfo(getServiceName(), true, "IDGEN_ENABLED", "false"); | ||
} | ||
} | ||
} |
Oops, something went wrong.