Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rc 175 create service to generate unique identity number #259

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RELEASE_VERSION = v0.0.14
IMAGES := ghcr.io/sunbird-rc/sunbird-rc-core ghcr.io/sunbird-rc/sunbird-rc-nginx ghcr.io/sunbird-rc/sunbird-rc-context-proxy-service \
ghcr.io/sunbird-rc/sunbird-rc-public-key-service ghcr.io/sunbird-rc/sunbird-rc-keycloak ghcr.io/sunbird-rc/sunbird-rc-certificate-api \
ghcr.io/sunbird-rc/sunbird-rc-certificate-signer ghcr.io/sunbird-rc/sunbird-rc-notification-service ghcr.io/sunbird-rc/sunbird-rc-claim-ms \
ghcr.io/sunbird-rc/sunbird-rc-digilocker-certificate-api ghcr.io/sunbird-rc/sunbird-rc-bulk-issuance ghcr.io/sunbird-rc/sunbird-rc-metrics
ghcr.io/sunbird-rc/sunbird-rc-digilocker-certificate-api ghcr.io/sunbird-rc/sunbird-rc-bulk-issuance ghcr.io/sunbird-rc/sunbird-rc-metrics ghcr.io/sunbird-rc/sunbird-rc-id-generation
build: java/registry/target/registry.jar
echo ${SOURCES}
rm -rf java/claim/target/*.jar
Expand All @@ -20,6 +20,7 @@ build: java/registry/target/registry.jar
make -C services/metrics docker
make -C services/digilocker-certificate-api docker
make -C services/bulk_issuance docker
make -C services/idgen-service docker
docker build -t ghcr.io/sunbird-rc/sunbird-rc-nginx .

java/registry/target/registry.jar: $(SOURCES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ public class Constants {
public static final String FILE="FILE";
public static final String DATABASE="DATABASE";

// ID Generation constants
public static final String APP_ID="sunbird_rc";
public static final String MSG_ID="request_from sunbird_RC to create a unique ID";
public static final String PREFIX_TENET_ID= "s.rc.";
public static final String PREFIX_ID_NAME= "sunbird.rc.";



//Expand config
public static final int MAX_EXPAND_LEVEL= 10;

Expand Down
17 changes: 17 additions & 0 deletions java/pojos/src/main/java/dev/sunbirdrc/pojos/UniqueIdRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.sunbirdrc.pojos;

import lombok.*;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class UniqueIdRequest {

String idName;
String tenantId;
String format;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.sunbirdrc.pojos;

import lombok.*;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class UniqueIdentifierFields {

String field;
String regularExpression;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.sunbirdrc.registry.exception;

public class UniqueIdentifierException extends Exception{


private static final long serialVersionUID = -6315798195661762883L;

public class CreationException extends CustomException {
private static final long serialVersionUID = 6174717850058203377L;

public CreationException(String msg) {
super("Unable to create unique ID: " + msg);
}
}

public class UnreachableException extends CustomException {

private static final long serialVersionUID = 5384120386096139084L;

public UnreachableException(String message) {
super("Unable to reach id-gen service: " + message);
}
}

public class KeyNotFoundException extends CustomException {

private static final long serialVersionUID = 8311355815972497248L;

public KeyNotFoundException(String message) {
super("Unable to get key: " + message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.sunbirdrc.registry.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import dev.sunbirdrc.registry.exception.UniqueIdentifierException;
import org.neo4j.causalclustering.core.state.machines.id.IdGenerationException;

import javax.json.Json;

public interface IdGenService {

Object createUniqueID (ObjectNode reqNode) throws UniqueIdentifierException.UnreachableException, UniqueIdentifierException.CreationException;

Object createUniqueIDsForAnEntity (String enitiyName, JsonNode inputNode) throws UniqueIdentifierException.UnreachableException, UniqueIdentifierException.CreationException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package dev.sunbirdrc.registry.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import dev.sunbirdrc.pojos.UniqueIdRequest;
import dev.sunbirdrc.pojos.UniqueIdentifierFields;
import dev.sunbirdrc.registry.exception.UniqueIdentifierException;
import dev.sunbirdrc.registry.service.IdGenService;
import dev.sunbirdrc.registry.util.Definition;
import dev.sunbirdrc.registry.util.IDefinitionsManager;
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.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import dev.sunbirdrc.registry.middleware.util.Constants;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
public class IdGenServiceImpl implements IdGenService {

private static Logger logger = LoggerFactory.getLogger(IdGenService.class);

@Autowired
private RetryRestTemplate retryRestTemplate;

@Autowired
private ObjectMapper objectMapper;

@Autowired
private IDefinitionsManager definitionsManager;

@Value("${idgen.createURL}")
private String idgenURL;

@Value("${idgen.host}")
private String idgenBase;

@Override
public Object createUniqueID (ObjectNode reqNode) throws UniqueIdentifierException.UnreachableException, UniqueIdentifierException.CreationException {
ResponseEntity<String> response = null;
JsonNode result = null;
try {
response = retryRestTemplate.postForEntity( idgenBase+idgenURL, reqNode);
result = objectMapper.readTree(response.getBody());
logger.info("Successfully generated unique ID");
} catch (RestClientException ex) {
logger.error("RestClientException while uniqueID generation: ", ex);
throw new UniqueIdentifierException().new UnreachableException(ex.getMessage());
} catch (Exception e) {
logger.error("RestClientException while uniqueID generation: : ", e);
throw new UniqueIdentifierException().new CreationException(e.getMessage());
}
return result;
}

@Override
public Object createUniqueIDsForAnEntity(String entityName, JsonNode inputNode) throws UniqueIdentifierException.UnreachableException, UniqueIdentifierException.CreationException {

try {
Definition definition = null;
definition = definitionsManager.getDefinition(entityName);
String prefixForTenetID = Constants.PREFIX_TENET_ID + entityName ;
String prefixForIdName = Constants.PREFIX_ID_NAME + entityName;

// Format request to send to ID generation service
HashMap reqJsonToSend = new HashMap();
ObjectNode reqInfo = new ObjectMapper().createObjectNode();
reqInfo.put("apiId", Constants.APP_ID).put("ver", "1").put("ts", 0);
reqInfo.put("msgId", Constants.MSG_ID);
ArrayList<UniqueIdRequest> idRequests = new ArrayList<>();
List<UniqueIdentifierFields> schemaDefinedUniqueIdentifierFields= new ArrayList<>();
schemaDefinedUniqueIdentifierFields = definition.getOsSchemaConfiguration().getUniqueIdentifierFields();
for(UniqueIdentifierFields uniqueIdFieldInSchema : schemaDefinedUniqueIdentifierFields) {
idRequests.add(UniqueIdRequest.builder()
.idName(prefixForIdName + '.' + uniqueIdFieldInSchema.getField())
.tenantId(prefixForTenetID + '.' + uniqueIdFieldInSchema.getField())
.format(uniqueIdFieldInSchema.getRegularExpression()).build());
}
reqJsonToSend.put("RequestInfo", reqInfo);
reqJsonToSend.put("idRequests", idRequests);
JsonNode reqToSend = new ObjectMapper().valueToTree(reqJsonToSend);

Object result = null;
if (definition.getOsSchemaConfiguration().getUniqueIdentifierFields().size() > 0) {
result = createUniqueID((ObjectNode) reqToSend);
// set the values that are returned from the ID_Gen service
for (int i = 0; i < definition.getOsSchemaConfiguration().getUniqueIdentifierFields().size(); i++) {
((ObjectNode) inputNode.at("/" + entityName)).put(definition.getOsSchemaConfiguration().getUniqueIdentifierFields().get(i).getField(), ((ObjectNode) result).get("idResponses").get(i).get("id"));
}
}

return result;

} catch (RestClientException ex) {
logger.error("RestClientException while uniqueID generation: ", ex);
throw new UniqueIdentifierException().new UnreachableException(ex.getMessage());
} catch (Exception e) {
logger.error("RestClientException while uniqueID generation: : ", e);
throw new UniqueIdentifierException().new CreationException(e.getMessage());
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import dev.sunbirdrc.actors.factory.MessageFactory;
import dev.sunbirdrc.elastic.IElasticService;
import dev.sunbirdrc.pojos.ComponentHealthInfo;
import dev.sunbirdrc.pojos.HealthCheckResponse;
import dev.sunbirdrc.pojos.HealthIndicator;
import dev.sunbirdrc.pojos.*;
import dev.sunbirdrc.registry.dao.*;
import dev.sunbirdrc.registry.exception.RecordNotFoundException;
import dev.sunbirdrc.registry.exception.SignatureException;
Expand Down Expand Up @@ -41,6 +39,7 @@
import org.sunbird.akka.core.MessageProtos;
import org.sunbird.akka.core.Router;

import javax.json.JsonObject;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -127,6 +126,9 @@ public class RegistryServiceImpl implements RegistryService {
@Autowired
private SchemaService schemaService;

@Autowired
private IdGenService idGenService;

@Autowired(required = false)
private List<HealthIndicator> healthIndicators;
public HealthCheckResponse health(Shard shard) throws Exception {
Expand Down Expand Up @@ -210,7 +212,12 @@ public String addEntity(Shard shard, String userId, JsonNode rootNode, boolean s
Transaction tx = null;
String entityId = "entityPlaceholderId";
String vertexLabel = rootNode.fieldNames().next();
Definition definition = null;
Definition definition = definitionsManager.getDefinition(vertexLabel);

// Create new Unique Identifiers if configured in schema
if (definition.getOsSchemaConfiguration().getUniqueIdentifierFields().size() > 0) {
idGenService.createUniqueIDsForAnEntity(vertexLabel, rootNode);
}

systemFieldsHelper.ensureCreateAuditFields(vertexLabel, rootNode.get(vertexLabel), userId);

Expand Down Expand Up @@ -287,6 +294,7 @@ private void generateCredentials(JsonNode rootNode, String vertexLabel) throws S
public void updateEntity(Shard shard, String userId, String id, String jsonString, boolean skipSignature) throws Exception {
JsonNode inputNode = objectMapper.readTree(jsonString);
String entityType = inputNode.fields().next().getKey();
systemFieldsHelper.ensureNotToUpdateUniqueIdentifierFields(entityType, inputNode.get(entityType));
systemFieldsHelper.ensureUpdateAuditFields(entityType, inputNode.get(entityType), userId);
if (encryptionEnabled) {
inputNode = encryptionHelper.getEncryptedJson(inputNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.sunbirdrc.pojos.OwnershipsAttributes;
import dev.sunbirdrc.pojos.UniqueIdentifierFields;
import dev.sunbirdrc.registry.middleware.util.Constants;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.exception.ExceptionUtils;
Expand Down Expand Up @@ -130,6 +131,16 @@ public List<OwnershipsAttributes> getOwnershipAttributes(String entity) {
}
}

@Override
public List<UniqueIdentifierFields> getUniqueIdentifierFields(String entity) {
Definition entityDefinition = definitionMap.get(entity);
if (entityDefinition != null) {
return entityDefinition.getOsSchemaConfiguration().getUniqueIdentifierFields();
} else {
return Collections.emptyList();
}
}

public boolean isValidEntityName(String entityName) {
return definitionMap.containsKey(entityName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.sunbirdrc.pojos.OwnershipsAttributes;
import dev.sunbirdrc.pojos.UniqueIdentifierFields;
import dev.sunbirdrc.registry.middleware.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -145,6 +146,23 @@ public List<OwnershipsAttributes> getOwnershipAttributes(String entity) {
}
}

@Override
public List<UniqueIdentifierFields> getUniqueIdentifierFields(String entity) {
try(Jedis jedis = jedisPool.getResource()) {
String value = jedis.get(SCHEMA + entity);
if(value != null) {
JsonNode schemaJson = objectMapper.readTree(value);
Definition definition = new Definition(schemaJson);
return definition.getOsSchemaConfiguration().getUniqueIdentifierFields();
}
return Collections.emptyList();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
} catch (NullPointerException e) {
return Collections.emptyList();
}
}

@Override
public boolean isValidEntityName(String entityName) {
try(Jedis jedis = jedisPool.getResource()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import dev.sunbirdrc.pojos.OwnershipsAttributes;
import dev.sunbirdrc.pojos.UniqueIdentifierFields;

import java.util.*;

Expand Down Expand Up @@ -73,6 +74,9 @@ default Set<String> getExcludingFieldsForEntity(String entity) {
return excludeFields;
}
List<OwnershipsAttributes> getOwnershipAttributes(String entity);

List<UniqueIdentifierFields> getUniqueIdentifierFields(String entity);

default Object getCredentialTemplate(String entityName) {
return getDefinition(entityName).getOsSchemaConfiguration().getCredentialTemplate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import dev.sunbirdrc.pojos.OwnershipsAttributes;
import dev.sunbirdrc.pojos.UniqueIdentifierFields;
import dev.sunbirdrc.pojos.attestation.auto.AutoAttestationPolicy;
import dev.sunbirdrc.registry.entities.AttestationPolicy;
import dev.sunbirdrc.registry.model.NotificationTemplates;
Expand Down Expand Up @@ -60,6 +61,11 @@ public class OSSchemaConfiguration {
* */
private List<OwnershipsAttributes> ownershipAttributes = new ArrayList<>();

/**
* Holds fields paths for uniqueIdentifierFields of the entity
* */
private List<UniqueIdentifierFields> uniqueIdentifierFields = new ArrayList<>();

/**
*
* Holds attestableFields info,
Expand Down
Loading
Loading