Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4c17a03
Issue #OS-131 Shard advisory for serialnum property
pc0202 Dec 7, 2018
9af1054
Issue #OS-131 addressed the PR comments
pc0202 Dec 9, 2018
79487ec
Issue #OS-131 addressed the PR comments
pc0202 Dec 10, 2018
80f4893
Issue #OS-131 addressed the PR comments for ShardAdvisor
pc0202 Dec 10, 2018
26fc67f
Issue #OS-131 Controller intantiate DB provider, Bean IShardAdvisor f…
pc0202 Dec 10, 2018
05476e9
Issue #OS-131 mechanism for initializing a DB shard while add end point
pc0202 Dec 10, 2018
de0d274
Issue #OS-131 IShardAdvisor: shardId, useful while caching
pc0202 Dec 10, 2018
4232821
Issue #OS-131 Introduced ShardManager for activating shard
pc0202 Dec 10, 2018
b7d014b
issue #OS-131 Modified the Test files with DBProviderFactory to get D…
pc0202 Dec 11, 2018
2a1974e
Issue #OS-131 Merge conflict form release-2.0.0
pc0202 Dec 11, 2018
8ef2c33
issue #OS-131 Addressed PR comments.
pc0202 Dec 11, 2018
08a195a
Issue #OS-131 Addressed PR comment
pc0202 Dec 11, 2018
b9272f1
Issue #OS-131 application yaml: enc url modified database shardProper…
pc0202 Dec 11, 2018
98c0c4d
Issue #OS-131 Application.yml.sample: signature service uri modified …
pc0202 Dec 11, 2018
089de1f
Issue #OS-131 Codacy issue resolved
pc0202 Dec 11, 2018
7d35bf3
Issue #OS-131 Igored testcase: RegistryServiceImplTest:test_health_ch…
pc0202 Dec 11, 2018
0d3c3fc
Issue #OS-131 Fixed and enable testcase RegistryServiceImplTest:test_…
pc0202 Dec 11, 2018
8b71ba4
Issue #OS-131 Minor clean up and refactoring
pc0202 Dec 11, 2018
f62f155
Issue #OS-131 JavaDoc and minor refactoring for SerialNumberShardAdvisor
pc0202 Dec 12, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
import java.util.HashMap;
import java.util.Map;

import io.opensaber.registry.util.TPGraphMain;
import org.apache.commons.validator.routines.UrlValidator;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -55,8 +52,10 @@
import io.opensaber.registry.schema.configurator.ISchemaConfigurator;
import io.opensaber.registry.schema.configurator.JsonSchemaConfigurator;
import io.opensaber.registry.schema.configurator.SchemaType;
import io.opensaber.registry.sink.DBShard;
import io.opensaber.registry.sink.DatabaseProvider;
import io.opensaber.registry.shard.advisory.IShardAdvisor;
import io.opensaber.registry.shard.advisory.SerialNumberShardAdvisor;
import io.opensaber.registry.shard.advisory.ShardAdvisor;
import io.opensaber.registry.sink.DBProviderFactory;
import io.opensaber.registry.transform.ConfigurationHelper;
import io.opensaber.registry.transform.Json2LdTransformer;
import io.opensaber.registry.transform.Ld2JsonTransformer;
Expand All @@ -72,7 +71,7 @@
public class GenericConfiguration implements WebMvcConfigurer {

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

@Autowired
private Environment environment;

Expand Down Expand Up @@ -275,8 +274,8 @@ public RestTemplate restTemaplteProvider() throws IOException {
return new RestTemplate(requestFactory);
}
@Bean
public DBShard dbshard(){
return new DBShard();
public DBProviderFactory dbProviderFactory(){
return new DBProviderFactory();
}

@Bean
Expand All @@ -285,10 +284,13 @@ public DBConnectionInfoMgr dBConnectionInfoMgr(){
}

@Bean
public DatabaseProvider databaseProvider() {
return dbshard().getInstance("shard1");
public IShardAdvisor shardAdvisor() throws IOException{
ShardAdvisor shardAdvisor = new ShardAdvisor();
DBConnectionInfoMgr connectionInforMgr = dBConnectionInfoMgr();
shardAdvisor.registerAdvisor(connectionInforMgr.getShardProperty(), new SerialNumberShardAdvisor(connectionInforMgr));
return shardAdvisor.getShardAdvisor(connectionInforMgr.getShardProperty());
}


@Bean
public UrlValidator urlValidator() {
Expand Down Expand Up @@ -358,21 +360,11 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
}

}

@Bean
public HandlerExceptionResolver customExceptionHandler() {
return new CustomExceptionHandler(gson());
}

@Bean
public Vertex parentVertex() {
Graph g = databaseProvider().getGraphStore();
Vertex parentV = TPGraphMain.createParentVertex(g);
try {
g.close();
} catch (Exception e) {
logger.info(e.getMessage());
}
return parentV;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import io.opensaber.registry.schema.configurator.ISchemaConfigurator;
import io.opensaber.registry.service.EncryptionService;
import io.opensaber.registry.sink.DatabaseProvider;
import io.opensaber.registry.util.TPGraphMain;
import org.apache.jena.rdf.model.Model;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
Expand All @@ -23,21 +19,45 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import io.opensaber.pojos.*;
import io.opensaber.registry.exception.*;
import io.opensaber.pojos.APIMessage;
import io.opensaber.pojos.HealthCheckResponse;
import io.opensaber.pojos.OpenSaberInstrumentation;
import io.opensaber.pojos.Response;
import io.opensaber.pojos.ResponseParams;
import io.opensaber.registry.exception.AuditFailedException;
import io.opensaber.registry.exception.EntityCreationException;
import io.opensaber.registry.exception.RecordNotFoundException;
import io.opensaber.registry.exception.TypeNotProvidedException;
import io.opensaber.registry.middleware.util.Constants;
import io.opensaber.registry.middleware.util.Constants.Direction;
import io.opensaber.registry.middleware.util.Constants.JsonldConstants;
import io.opensaber.registry.middleware.util.JSONUtil;
import io.opensaber.registry.schema.configurator.ISchemaConfigurator;
import io.opensaber.registry.service.EncryptionService;
import io.opensaber.registry.service.RegistryAuditService;
import io.opensaber.registry.service.RegistryService;
import io.opensaber.registry.service.SearchService;
import io.opensaber.registry.transform.*;
import io.opensaber.registry.shard.advisory.ShardManager;
import io.opensaber.registry.sink.DatabaseProvider;
import io.opensaber.registry.transform.Configuration;
import io.opensaber.registry.transform.ConfigurationHelper;
import io.opensaber.registry.transform.Data;
import io.opensaber.registry.transform.ITransformer;
import io.opensaber.registry.transform.TransformationException;
import io.opensaber.registry.transform.Transformer;
import io.opensaber.registry.util.TPGraphMain;

@RestController
public class RegistryController {
Expand All @@ -58,8 +78,6 @@ public class RegistryController {
@Autowired
private APIMessage apiMessage;
@Autowired
private DatabaseProvider databaseProvider;
@Autowired
private ISchemaConfigurator schemaConfigurator;
@Autowired
private EncryptionService encryptionService;
Expand All @@ -71,89 +89,9 @@ public class RegistryController {
@Autowired
private OpenSaberInstrumentation watch;
private List<String> keyToPurge = new java.util.ArrayList<>();
@Autowired
private Vertex parentVertex;

@RequestMapping(value = "/add2", method = RequestMethod.POST)
public ResponseEntity<Response> add(@RequestParam(value = "id", required = false) String id,
@RequestParam(value = "prop", required = false) String property) {

Model rdf = (Model) apiMessage.getLocalMap(Constants.CONTROLLER_INPUT);
ResponseParams responseParams = new ResponseParams();
Response response = new Response(Response.API_ID.CREATE, "OK", responseParams);
Map<String, Object> result = new HashMap<>();

try {
watch.start("RegistryController.addToExistingEntity");
String dataObject = apiMessage.getLocalMap(Constants.LD_OBJECT).toString();
String label = registryService.addEntity(rdf, dataObject, id, property);
result.put("entity", label);
response.setResult(result);
responseParams.setStatus(Response.Status.SUCCESSFUL);
watch.stop("RegistryController.addToExistingEntity");
logger.debug("RegistryController : Entity with label {} added !", label);
} catch (DuplicateRecordException | EntityCreationException e) {
logger.error("DuplicateRecordException|EntityCreationException in controller while adding entity !", e);
response.setResult(result);
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());
} catch (Exception e) {
logger.error("Exception in controller while adding entity !", e);
response.setResult(result);
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());
}
return new ResponseEntity<>(response, HttpStatus.OK);
}

/**
*
* Note: Only one mime type is supported at a time. Picks up the first mime
* type from the header.
*
* @return
*/
@RequestMapping(value = "/read2", method = RequestMethod.POST)
public ResponseEntity<Response> readEntity(@RequestHeader HttpHeaders header) {

ResponseParams responseParams = new ResponseParams();
Response response = new Response(Response.API_ID.READ, "OK", responseParams);
String dataObject = apiMessage.getRequest().getRequestMapAsString();
JSONParser parser = new JSONParser();
try {
JSONObject json = (JSONObject) parser.parse(dataObject);
String entityId = registryContext + json.get("id").toString();
boolean includeSign = Boolean.parseBoolean(json.getOrDefault("includeSignatures", false).toString());

watch.start("RegistryController.readEntity");
String content = registryService.getEntityFramedById(entityId, includeSign);
logger.info("RegistryController: Framed content " + content);

Configuration config = configurationHelper.getConfiguration(header.getAccept().iterator().next().toString(),
Direction.OUT);
Data<Object> data = new Data<Object>(content);
ITransformer<Object> responseTransformer = transformer.getInstance(config);
responseTransformer.setPurgeData(getKeysToPurge());
Data<Object> responseContent = responseTransformer.transform(data);
response.setResult(responseContent.getData());
responseParams.setStatus(Response.Status.SUCCESSFUL);
watch.stop("RegistryController.readEntity");
logger.debug("RegistryController: entity for {} read !", entityId);
} catch (ParseException | RecordNotFoundException | UnsupportedOperationException | TransformationException e) {
logger.error("RegistryController: Exception while reading entity !", e);
response.setResult(null);
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg(e.getMessage());
} catch (Exception e) {
logger.error("RegistryController: Exception while reading entity!", e);
response.setResult(null);
responseParams.setStatus(Response.Status.UNSUCCESSFUL);
responseParams.setErrmsg("Ding! You encountered an error!");
}

return new ResponseEntity<>(response, HttpStatus.OK);
}

@Autowired
ShardManager shardManager;
/**
*
* Note: Only one mime type is supported at a time. Pick up the first mime
Expand Down Expand Up @@ -327,10 +265,16 @@ public ResponseEntity<Response> addTP2Graph(@RequestParam(value = "id", required
Map<String, Object> result = new HashMap<>();
String jsonString = apiMessage.getRequest().getRequestMapAsString();
List<String> privateProperties = schemaConfigurator.getAllPrivateProperties();
//String jsonString = "{\"Teacher\":{\"signatures\":{\"@type\":\"sc:GraphSignature2012\",\"signatureFor\":\"http://localhost:8080/serialNum\",\"creator\":\"https://example.com/i/pat/keys/5\",\"created\":\"2017-09-23T20:21:34Z\",\"nonce\":\"2bbgh3dgjg2302d-d2b3gi423d42\",\"signatureValue\":\"eyiOiJKJ0eXA...OEjgFWFXk\"},\"serialNum\":6,\"teacherCode\":\"12234\",\"nationalIdentifier\":\"1234567890123456\",\"teacherName\":\"FromRajeshLaptop\",\"gender\":\"GenderTypeCode-MALE\",\"birthDate\":\"1990-12-06\",\"socialCategory\":\"SocialCategoryTypeCode-GENERAL\",\"highestAcademicQualification\":\"AcademicQualificationTypeCode-PHD\",\"highestTeacherQualification\":\"TeacherQualificationTypeCode-MED\",\"yearOfJoiningService\":\"2014\",\"teachingRole\":{\"@type\":\"TeachingRole\",\"teacherType\":\"TeacherTypeCode-HEAD\",\"appointmentType\":\"TeacherAppointmentTypeCode-REGULAR\",\"classesTaught\":\"ClassTypeCode-SECONDARYANDHIGHERSECONDARY\",\"appointedForSubjects\":\"SubjectCode-ENGLISH\",\"mainSubjectsTaught\":\"SubjectCode-SOCIALSTUDIES\",\"appointmentYear\":\"2015\"},\"inServiceTeacherTrainingFromBRC\":{\"@type\":\"InServiceTeacherTrainingFromBlockResourceCentre\",\"daysOfInServiceTeacherTraining\":\"10\"},\"inServiceTeacherTrainingFromCRC\":{\"@type\":\"InServiceTeacherTrainingFromClusterResourceCentre\",\"daysOfInServiceTeacherTraining\":\"2\"},\"inServiceTeacherTrainingFromDIET\":{\"@type\":\"InServiceTeacherTrainingFromDIET\",\"daysOfInServiceTeacherTraining\":\"5.5\"},\"inServiceTeacherTrainingFromOthers\":{\"@type\":\"InServiceTeacherTrainingFromOthers\",\"daysOfInServiceTeacherTraining\":\"3.5\"},\"nonTeachingAssignmentsForAcademicCalendar\":{\"@type\":\"NonTeachingAssignmentsForAcademicCalendar\",\"daysOfNonTeachingAssignments\":\"6\"},\"basicProficiencyLevel\":{\"@type\":\"BasicProficiencyLevel\",\"proficiencySubject\":\"SubjectCode-MATH\",\"proficiencyAcademicQualification\":\"AcademicQualificationTypeCode-PHD\"},\"disabilityType\":\"DisabilityCode-NA\",\"trainedForChildrenSpecialNeeds\":\"YesNoCode-YES\",\"trainedinUseOfComputer\":\"YesNoCode-YES\"}}}";
TPGraphMain tpGraph = new TPGraphMain(databaseProvider, parentVertex, privateProperties, encryptionService);
String entityType = apiMessage.getRequest().getEntityType();
int slNum = (int) ((HashMap<String, Object>) apiMessage.getRequest().getRequestMap().get(entityType))
.get(shardManager.getShardProperty());

try {
shardManager.activateDbShard(slNum);
DatabaseProvider databaseProvider = shardManager.getDatabaseProvider();
Vertex parentVertex = parentVertex(databaseProvider);
TPGraphMain tpGraph = new TPGraphMain(databaseProvider, parentVertex, privateProperties, encryptionService);

watch.start("RegistryController.addToExistingEntity");
JsonNode rootNode = tpGraph.createEncryptedJson(jsonString);
tpGraph.createTPGraph(rootNode);
Expand All @@ -357,6 +301,8 @@ public ResponseEntity<Response> readGraph2Json(@RequestHeader HttpHeaders header
String osIdVal = json.get("id").toString();
ResponseParams responseParams = new ResponseParams();
List<String> privateProperties = schemaConfigurator.getAllPrivateProperties();
DatabaseProvider databaseProvider = shardManager.getDatabaseProvider();
Vertex parentVertex = parentVertex(databaseProvider);
TPGraphMain tpGraph = new TPGraphMain(databaseProvider, parentVertex, privateProperties, encryptionService);
Response response = new Response(Response.API_ID.READ, "OK", responseParams);
response.setResult(tpGraph.readGraph2Json(osIdVal));
Expand All @@ -372,5 +318,16 @@ private List<String> getKeysToPurge() {
return keyToPurge;

}

private Vertex parentVertex(DatabaseProvider databaseProvider) {
Graph g = databaseProvider.getGraphStore();
Vertex parentV = TPGraphMain.createParentVertex(g);
try {
g.close();
} catch (Exception e) {
logger.info(e.getMessage());
}
return parentV;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opensaber.registry.exception.DuplicateRecordException;
import io.opensaber.registry.exception.EncryptionException;
import io.opensaber.registry.exception.RecordNotFoundException;
import io.opensaber.registry.sink.DatabaseProvider;

public interface RegistryDao {

Expand All @@ -33,4 +34,7 @@ public Graph getEntityByVertex(Vertex vertex)
public String getRootLabelForNodeLabel(String nodeLabel);

public String getTypeForNodeLabel(String label);

public void setDatabaseProvider(DatabaseProvider databaseProvider);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import io.opensaber.registry.exception.AuditFailedException;
import io.opensaber.registry.exception.EncryptionException;
import io.opensaber.registry.exception.RecordNotFoundException;
import io.opensaber.registry.sink.DatabaseProvider;

public interface SearchDao {

public Map<String, Graph> search(SearchQuery searchQuery)
throws AuditFailedException, EncryptionException, RecordNotFoundException;

public void setDatabaseProvider(DatabaseProvider databaseProvider);

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
package io.opensaber.registry.dao.impl;

import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.Stack;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.opensaber.registry.schema.configurator.ISchemaConfigurator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.routines.UrlValidator;
import org.apache.jena.rdf.model.*;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.NodeIterator;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.ResIterator;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.*;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Transaction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.javatuples.Pair;
Expand All @@ -35,6 +55,7 @@
import io.opensaber.registry.middleware.util.Constants;
import io.opensaber.registry.model.AuditRecord;
import io.opensaber.registry.schema.config.SchemaLoader;
import io.opensaber.registry.schema.configurator.ISchemaConfigurator;
import io.opensaber.registry.sink.DatabaseProvider;

@Component
Expand All @@ -45,7 +66,6 @@ public class RegistryDaoImpl implements RegistryDao {
private static Logger logger = LoggerFactory.getLogger(RegistryDaoImpl.class);
@Autowired
ApplicationContext appContext;
@Autowired
private DatabaseProvider databaseProvider;
@Value("${registry.context.base}")
private String registryContext;
Expand Down Expand Up @@ -1013,5 +1033,9 @@ private Long getValueConstraint(String constraint, Resource subject) {
}
return null;
}
@Override
public void setDatabaseProvider(DatabaseProvider databaseProvider) {
this.databaseProvider = databaseProvider;
}

}
Loading