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

[+] Anchoring schemas to CORD Network #245

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion java/registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
import dev.sunbirdrc.registry.middleware.util.OSSystemFields;
import dev.sunbirdrc.registry.service.FileStorageService;
import dev.sunbirdrc.registry.service.ICertificateService;
import dev.sunbirdrc.registry.service.ICertificateService;
import dev.sunbirdrc.registry.transform.Configuration;
import dev.sunbirdrc.registry.transform.Data;
import dev.sunbirdrc.registry.transform.ITransformer;
Expand All @@ -42,10 +42,10 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import static dev.sunbirdrc.registry.Constants.*;
import static dev.sunbirdrc.registry.middleware.util.Constants.ENTITY_TYPE;
Expand Down Expand Up @@ -89,7 +89,6 @@ public ResponseEntity<Object> invite(
newRootNode.set(entityName, rootNode);
try {
checkEntityNameInDefinitionManager(entityName);
registryHelper.authorizeInviteEntity(request, entityName);
watch.start(TAG);
String entityId = registryHelper.inviteEntity(newRootNode, "");
registryHelper.autoRaiseClaim(entityName, entityId, "", null, newRootNode, dev.sunbirdrc.registry.Constants.USER_ANONYMOUS);
Expand Down Expand Up @@ -269,7 +268,7 @@ public ResponseEntity<Object> putEntity(
}
}


VenuChoudhary001 marked this conversation as resolved.
Show resolved Hide resolved
@RequestMapping(value = "/api/v1/{entityName}", method = RequestMethod.POST)
public ResponseEntity<Object> postEntity(
@PathVariable String entityName,
Expand All @@ -278,31 +277,49 @@ public ResponseEntity<Object> postEntity(
@RequestParam(defaultValue = "sync") String mode,
@RequestParam(defaultValue = "${webhook.url}") String callbackUrl,
HttpServletRequest request
) {

) throws Exception {
logger.info("MODE: {}", asyncRequest.isEnabled());
logger.info("MODE: {}", asyncRequest.getWebhookUrl());
logger.info("Adding entity {}", rootNode);
ResponseParams responseParams = new ResponseParams();
Response response = new Response(Response.API_ID.POST, "OK", responseParams);
logger.info("This is inside response {}",response);
Map<String, Object> result = new HashMap<>();
ObjectNode newRootNode = objectMapper.createObjectNode();
newRootNode.set(entityName, rootNode);

try {
checkEntityNameInDefinitionManager(entityName);
String userId = registryHelper.authorizeManageEntity(request, entityName);
String label = registryHelper.addEntity(newRootNode, userId);
String emailId = registryHelper.fetchEmailIdFromToken(request, entityName);

Map<String, String> resultMap = new HashMap<>();
if (asyncRequest.isEnabled()) {
resultMap.put(TRANSACTION_ID, label);
} else {
registryHelper.autoRaiseClaim(entityName, label, userId, null, newRootNode, emailId);
resultMap.put(dbConnectionInfoMgr.getUuidPropertyName(), label);
}
/** Anchoring schema to chain */
JsonNode np=rootNode.get("schema");
JsonNode str=new ObjectMapper().readTree(np.asText());
JsonNode schemaNode=str.get("definitions");

JsonNode props=schemaNode.get("Place");
JsonNode newProps=props.get("properties");

JsonNode outputSchema=new ObjectMapper().createObjectNode()
.put("title",str.get("title").asText())
.put("description",str.get("description").asText())
.set("properties",props.get("properties"));

JsonNode finalSchema=new ObjectMapper().createObjectNode()
.set("schema",outputSchema);
registryHelper.anchorSchemaAPI(finalSchema);


result.put(entityName, resultMap);
response.setResult(result);
response.setResult(result);
VenuChoudhary001 marked this conversation as resolved.
Show resolved Hide resolved
responseParams.setStatus(Response.Status.SUCCESSFUL);
watch.stop("RegistryController.addToExistingEntity");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.http.MediaType;
import reactor.core.publisher.Mono;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -72,7 +74,7 @@
import static dev.sunbirdrc.registry.exception.ErrorMessages.*;
import static dev.sunbirdrc.registry.middleware.util.Constants.*;
import static dev.sunbirdrc.registry.middleware.util.OSSystemFields.*;

import org.springframework.web.reactive.function.client.WebClient;
/**
* This is helper class, user-service calls this class in-order to access registry functionality
*/
Expand All @@ -91,6 +93,7 @@ public class RegistryHelper {
@Value("${notification.service.enabled}") boolean notificationEnabled;
@Value("${invite.required_validation_enabled}") boolean skipRequiredValidationForInvite = true;
@Value("${invite.signature_enabled}") boolean skipSignatureForInvite = true;
@Value("${cord.schemaURL}") String cord_schema_url;
@Autowired
private NotificationHelper notificationHelper;
@Autowired
Expand Down Expand Up @@ -191,6 +194,49 @@ public JsonNode removeFormatAttr(JsonNode requestBody) {
return requestBody;
}

/**
* REUSBALE METHOD FOR POST API CALLS
*/
public void apiHelper(JsonNode obj,String url){
WebClient.Builder builder = WebClient.builder();
try{
Mono<JsonNode> responseMono = builder.build()
.post()
.uri(url)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.bodyValue(obj)
.retrieve()
.bodyToMono(JsonNode.class)
.onErrorResume(throwable -> {
throwable.printStackTrace();
return Mono.empty();
});

JsonNode response = responseMono.block();
logger.info("RESPONSE {}",response);
}catch(Exception e){
logger.error("Exception occurred !" , e);
}
}

/**
* Anchors schema to the CORD CHAIN
*/
public void anchorSchemaAPI(JsonNode obj){
// apiHelper(obj,"http://172.24.0.1:5106/api/v1/schema");
apiHelper(obj,cord_schema_url); // considering issuer agent running in local
}


/**
* Anchors registry to the chain ,
* Before calling this api, schema must be created

*/
public void anchorRegistryAPI(){

}
/**
* calls validation and then persists the record to registry.
*
Expand All @@ -205,6 +251,7 @@ public String addEntity(JsonNode inputJson, String userId) throws Exception {
}

public String inviteEntity(JsonNode inputJson, String userId) throws Exception {
// System.out.println("lOL");
VenuChoudhary001 marked this conversation as resolved.
Show resolved Hide resolved
String entityId = addEntityHandler(inputJson, userId, skipRequiredValidationForInvite, skipSignatureForInvite);
notificationHelper.sendNotification(inputJson, INVITE);
return entityId;
Expand Down
2 changes: 2 additions & 0 deletions java/registry/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ registry:
redis:
host: ${redis_host:localhost}
port: ${redis_port:6379}
cord:
schemaURL: ${cord_schema_url:http://localhost:5106/api/v1/schema}

workflow:
enabled: ${workflow.enable:true}
Expand Down