Skip to content

Commit

Permalink
Merge pull request #193 from tejash-jl/exception-issues
Browse files Browse the repository at this point in the history
Exception issues
  • Loading branch information
tejash-jl authored Dec 15, 2022
2 parents b317061 + a2d8946 commit 3d07263
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public String getServiceName() {
public ComponentHealthInfo getHealthInfo() {
if (authenticationEnabled) {
try {
return new ComponentHealthInfo(getServiceName(), keycloak.realms().findAll().size() > 0);
return new ComponentHealthInfo(getServiceName(), keycloak.serverInfo().getInfo().getSystemInfo().getUptimeMillis() > 0);
} catch (Exception e) {
return new ComponentHealthInfo(getServiceName(), false, CONNECTION_FAILURE, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import dev.sunbirdrc.registry.service.ISearchService;
import dev.sunbirdrc.registry.service.SchemaService;
import dev.sunbirdrc.registry.util.IDefinitionsManager;
import dev.sunbirdrc.validators.IValidate;
import org.jetbrains.annotations.NotNull;
Expand All @@ -26,6 +27,9 @@
public class SchemaLoader implements ApplicationListener<ContextRefreshedEvent> {
public static final Logger logger = LoggerFactory.getLogger(SchemaLoader.class);

@Autowired
private SchemaService schemaService;

@Autowired
private ISearchService searchService;

Expand All @@ -51,9 +55,7 @@ private void loadSchemasFromDB() {
JsonNode searchResults = searchService.search(objectNode);
for (JsonNode schemaNode : searchResults.get(Schema)) {
try {
JsonNode schema = schemaNode.get(Schema.toLowerCase());
definitionsManager.appendNewDefinition(schema);
validator.addDefinitions(schema);
schemaService.addSchema(schemaNode);
} catch (Exception e) {
logger.error("Failed loading schema to definition manager:", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.sunbirdrc.registry.entities.SchemaStatus;
import dev.sunbirdrc.registry.exception.SchemaException;
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
import dev.sunbirdrc.registry.util.Definition;
import dev.sunbirdrc.registry.util.IDefinitionsManager;
import dev.sunbirdrc.validators.IValidate;
import org.apache.tinkerpop.gremlin.structure.Vertex;
Expand Down Expand Up @@ -41,14 +42,19 @@ public void deleteSchemaIfExists(Vertex vertex) throws SchemaException {
}


public void addSchema(JsonNode schemaNode) throws IOException {
public void addSchema(JsonNode schemaNode) throws IOException, SchemaException {
if (schemaNode.get(Schema).get(STATUS) == null) {
((ObjectNode) schemaNode.get(Schema)).put(STATUS, SchemaStatus.DRAFT.toString());
}
JsonNode schema = schemaNode.get(Schema).get(Schema.toLowerCase());
if (schemaNode.get(Schema).get(STATUS).textValue().equals(SchemaStatus.PUBLISHED.toString())) {
definitionsManager.appendNewDefinition(schema);
validator.addDefinitions(schema);
Definition definition = Definition.toDefinition(schema);
if (definitionsManager.getDefinition(definition.getTitle()) == null) {
definitionsManager.appendNewDefinition(definition);
validator.addDefinitions(schema);
} else {
throw new SchemaException("Duplicate Error: Schema already exists");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,10 @@ public String getDefinitionNameForField(String name) {
public Map<String, String> getSubSchemaNames() {
return subSchemaNames;
}

public static Definition toDefinition(JsonNode jsonNode) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode schemaJsonNode = objectMapper.readTree(jsonNode.asText());
return new Definition(schemaJsonNode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,153 +8,161 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.Map.Entry;

import static dev.sunbirdrc.registry.Constants.TITLE;
import static dev.sunbirdrc.registry.Constants.USER_ANONYMOUS;


public class DefinitionsManager implements IDefinitionsManager{
private static Logger logger = LoggerFactory.getLogger(DefinitionsManager.class);
public class DefinitionsManager implements IDefinitionsManager {
private static final Logger logger = LoggerFactory.getLogger(DefinitionsManager.class);

private Map<String, Definition> definitionMap = new HashMap<>();
private Map<String, Definition> derivedDefinitionMap = new HashedMap();
private Map<String, Definition> definitionMap = new HashMap<>();
private Map<String, Definition> derivedDefinitionMap = new HashedMap();

private OSResourceLoader osResourceLoader;
private List<String> internalSchemas = new ArrayList<>();

@Autowired
private ResourceLoader resourceLoader;

@Autowired
private ObjectMapper objectMapper;

/**
* Loads the definitions from the _schemas folder
*/
@PostConstruct
@Override
public void loadDefinition() throws Exception {

loadResourcesFromPath(Constants.RESOURCE_LOCATION);
loadResourcesFromPath(Constants.INTERNAL_RESOURCE_LOCATION);

derivedDefinitionMap.putAll(definitionMap);
Set<Definition> loadedDefinitionsSet = new HashSet<>();
loadedDefinitionsSet.addAll(definitionMap.values());

// CAVEAT: attribute names must be distinct to not cause definition collisions.
loadedDefinitionsSet.forEach(def -> {
def.getSubSchemaNames().forEach((fieldName, defnName) -> {
Definition definition = definitionMap.getOrDefault(defnName, null);
if (null != definition) {
derivedDefinitionMap.putIfAbsent(fieldName, definitionMap.get(defnName));
} else {
logger.warn("{} definition not found for field {}", defnName, fieldName);
}
});
});

logger.info("loaded schema resource(s): " + definitionMap.size());
}

private void loadResourcesFromPath(String resourceLocation) throws Exception {
final ObjectMapper mapper = new ObjectMapper();
osResourceLoader = new OSResourceLoader(resourceLoader);
osResourceLoader.loadResource(resourceLocation);

for (Entry<String, String> entry : osResourceLoader.getNameContent().entrySet()) {
String filename = entry.getKey();
String filenameWithoutExtn = filename.substring(0, filename.indexOf('.'));
JsonNode jsonNode = mapper.readTree(entry.getValue());
Definition definition = new Definition(jsonNode);
logger.info("loading resource:" + entry.getKey() + " with private field size:"
+ definition.getOsSchemaConfiguration().getPrivateFields().size() + " & signed fields size:"
+ definition.getOsSchemaConfiguration().getSignedFields().size());
definitionMap.putIfAbsent(definition.getTitle(), definition);
definitionMap.putIfAbsent(filenameWithoutExtn, definition);
}
}

/**
* Returns the title for all definitions loaded
*
* @return
*/
public Set<String> getAllKnownDefinitions() {
return definitionMap.keySet();
}

/**
* Returns all definitions that are loaded
*
* @return
*/
public List<Definition> getAllDefinitions() {
List<Definition> definitions = new ArrayList<>();
for (Entry<String, Definition> entry : definitionMap.entrySet()) {
definitions.add(entry.getValue());
}
return definitions;
}

/**
* Provide a definition by given title which is already loaded
*
* @param title
* @return
*/
public Definition getDefinition(String title) {
return definitionMap.getOrDefault(title, null);
}

@Override
public Map<String, Definition> getDefinitionMap() {
return definitionMap;
}

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

public boolean isValidEntityName(String entityName) {
return definitionMap.containsKey(entityName);
}

public void appendNewDefinition(JsonNode jsonNode) {
try {
String schemaAsText = jsonNode.asText("{}");
JsonNode schemaJsonNode = objectMapper.readTree(schemaAsText);
Definition definition = new Definition(schemaJsonNode);
logger.info("loading resource:" + definition.getTitle() + " with private field size:"
+ definition.getOsSchemaConfiguration().getPrivateFields().size() + " & signed fields size:"
+ definition.getOsSchemaConfiguration().getSignedFields().size());
definitionMap.put(definition.getTitle(), definition);
} catch (Exception e) {
logger.error("Failed loading schema from DB", e);
}
}

public void removeDefinition(JsonNode jsonNode) {
try {
String schemaAsText = jsonNode.asText("{}");
JsonNode schemaJsonNode = objectMapper.readTree(schemaAsText);
String schemaTitle = schemaJsonNode.get(TITLE).asText();
definitionMap.remove(schemaTitle);
} catch (Exception e) {
logger.error("Failed removing schema from definition manager", e);
}
}
private ResourceLoader resourceLoader;

@Autowired
private ObjectMapper objectMapper;

/**
* Loads the definitions from the _schemas folder
*/
@PostConstruct
@Override
public void loadDefinition() throws Exception {

loadResourcesFromPath(Constants.RESOURCE_LOCATION);
loadResourcesFromPath(Constants.INTERNAL_RESOURCE_LOCATION);
setInternalSchemas();
derivedDefinitionMap.putAll(definitionMap);
Set<Definition> loadedDefinitionsSet = new HashSet<>(definitionMap.values());

// CAVEAT: attribute names must be distinct to not cause definition collisions.
loadedDefinitionsSet.forEach(def -> {
def.getSubSchemaNames().forEach((fieldName, defnName) -> {
Definition definition = definitionMap.getOrDefault(defnName, null);
if (null != definition) {
derivedDefinitionMap.putIfAbsent(fieldName, definitionMap.get(defnName));
} else {
logger.warn("{} definition not found for field {}", defnName, fieldName);
}
});
});

logger.info("loaded schema resource(s): " + definitionMap.size());
}

private void loadResourcesFromPath(String resourceLocation) throws Exception {
final ObjectMapper mapper = new ObjectMapper();
OSResourceLoader osResourceLoader = new OSResourceLoader(resourceLoader);
osResourceLoader.loadResource(resourceLocation);

for (Entry<String, String> entry : osResourceLoader.getNameContent().entrySet()) {
String filename = entry.getKey();
String filenameWithoutExtn = filename.substring(0, filename.indexOf('.'));
JsonNode jsonNode = mapper.readTree(entry.getValue());
Definition definition = new Definition(jsonNode);
logger.info("loading resource:" + entry.getKey() + " with private field size:"
+ definition.getOsSchemaConfiguration().getPrivateFields().size() + " & signed fields size:"
+ definition.getOsSchemaConfiguration().getSignedFields().size());
definitionMap.putIfAbsent(definition.getTitle(), definition);
definitionMap.putIfAbsent(filenameWithoutExtn, definition);
}
}

/**
* Returns the title for all definitions loaded
*
* @return
*/
public Set<String> getAllKnownDefinitions() {
return definitionMap.keySet();
}

/**
* Returns all definitions that are loaded
*
* @return
*/
public List<Definition> getAllDefinitions() {
List<Definition> definitions = new ArrayList<>();
for (Entry<String, Definition> entry : definitionMap.entrySet()) {
definitions.add(entry.getValue());
}
return definitions;
}

/**
* Provide a definition by given title which is already loaded
*
* @param title
* @return
*/
public Definition getDefinition(String title) {
return definitionMap.getOrDefault(title, null);
}

@Override
public Map<String, Definition> getDefinitionMap() {
return definitionMap;
}

@Override
public void setInternalSchemas() throws Exception {
internalSchemas = getInternalSchemasNames(resourceLoader);
}

@Override
public List<String> getInternalSchemas() {
return internalSchemas;
}

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

public boolean isValidEntityName(String entityName) {
return definitionMap.containsKey(entityName);
}

@Override
public void appendNewDefinition(JsonNode jsonNode) {
try {
appendNewDefinition(Definition.toDefinition(jsonNode));
} catch (Exception e) {
logger.error("Failed loading schema from DB", e);
}
}

@Override
public void appendNewDefinition(Definition definition) {
logger.info("loading resource:" + definition.getTitle() + " with private field size:"
+ definition.getOsSchemaConfiguration().getPrivateFields().size() + " & signed fields size:"
+ definition.getOsSchemaConfiguration().getSignedFields().size());
definitionMap.put(definition.getTitle(), definition);
}

public void removeDefinition(JsonNode jsonNode) {
try {
String schemaAsText = jsonNode.asText("{}");
JsonNode schemaJsonNode = objectMapper.readTree(schemaAsText);
String schemaTitle = schemaJsonNode.get(TITLE).asText();
definitionMap.remove(schemaTitle);
} catch (Exception e) {
logger.error("Failed removing schema from definition manager", e);
}
}

}
Loading

0 comments on commit 3d07263

Please sign in to comment.