Commit ddb41bd9 authored by Aman Verma's avatar Aman Verma
Browse files

introducing the bool flag to identify system schemas

parent c56173ae
Pipeline #62343 passed with stages
in 31 minutes and 21 seconds
......@@ -4,4 +4,6 @@ public interface IAuthorityService {
Boolean checkAndRegisterAuthorityIfNotPresent(String authorityId);
Boolean checkAndRegisterSystemAuthorityIfNotPresent(String authorityId);
}
......@@ -4,4 +4,6 @@ public interface IEntityTypeService {
Boolean checkAndRegisterEntityTypeIfNotPresent(String entityTypeId);
Boolean checkAndRegisterSystemEntityTypeIfNotPresent(String entityTypeId);
}
......@@ -4,4 +4,6 @@ public interface ISourceService {
Boolean checkAndRegisterSourceIfNotPresent(String sourceId);
Boolean checkAndRegisterSystemSourceIfNotPresent(String sourceId);
}
......@@ -21,7 +21,7 @@ public class AuthorityService implements IAuthorityService {
/**
* check and create authority if its not present in the tenant dataPartitionId
*
* @param authority id
* @param authorityId
* @return true or false of successful registration of authority.
*/
@Override
......@@ -38,4 +38,23 @@ public class AuthorityService implements IAuthorityService {
return true;
}
/**
* check and create System authority if its not present
* @param authorityId
* @return
*/
@Override
public Boolean checkAndRegisterSystemAuthorityIfNotPresent(String authorityId) {
try {
Authority authority = new Authority();
authority.setAuthorityId(authorityId);
authorityStore.createSystemAuthority(authority);
} catch (ApplicationException e) {
return false;
} catch (BadRequestException ex) {
return true;
}
return true;
}
}
......@@ -21,7 +21,7 @@ public class EntityTypeService implements IEntityTypeService {
/**
* check and create EntityType if its not present of dataPartitionId store
*
* @param EntityType id
* @param entityTypeId
* @return true or false of successful registration of EntityType.
*/
@Override
......@@ -40,4 +40,23 @@ public class EntityTypeService implements IEntityTypeService {
}
/**
* check and create EntityType if its not present
* @param entityTypeId
* @return
*/
@Override
public Boolean checkAndRegisterSystemEntityTypeIfNotPresent(String entityTypeId) {
try {
EntityType entityType = new EntityType();
entityType.setEntityTypeId(entityTypeId);
entityTypeStore.createSystemEntity(entityType);
} catch (ApplicationException e) {
return false;
} catch (BadRequestException ex) {
return true;
}
return true;
}
}
......@@ -79,9 +79,6 @@ public class SchemaService implements ISchemaService {
private SchemaResolver schemaResolver;
private final SchemaVersionValidatorFactory versionValidatorFactory;
@Value("${shared.tenant.name:common}")
private String sharedTenant;
final JaxRsDpsLog log;
......@@ -140,7 +137,7 @@ public class SchemaService implements ISchemaService {
if (schemaInfoStore.isUnique(schemaId, dataPartitionId)) {
setScope(schemaRequest,false);
String schema = resolveAndCheckBreakingChanges(schemaRequest);
String schema = resolveAndCheckBreakingChanges(schemaRequest, false);
Boolean authority = authorityService.checkAndRegisterAuthorityIfNotPresent(
schemaRequest.getSchemaInfo().getSchemaIdentity().getAuthority());
......@@ -175,13 +172,85 @@ public class SchemaService implements ISchemaService {
}
}
public SchemaInfo createSystemSchema(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
String dataPartitionId = headers.getPartitionId();
private SchemaInfo createSchemaInternal(SchemaRequest schemaRequest, Boolean isSystemSchema) throws ApplicationException, BadRequestException {
String schemaId = createAndSetSchemaId(schemaRequest);
if (isUniqueSchema(schemaId, isSystemSchema)) {
setScope(schemaRequest,isSystemSchema);
String schema = resolveAndCheckBreakingChanges(schemaRequest, isSystemSchema);
Boolean authority = this.checkAndRegisterAuthorityIfNotPresent(
schemaRequest.getSchemaInfo().getSchemaIdentity().getAuthority(), isSystemSchema);
Boolean source = this.checkAndRegisterSourceIfNotPresent(
schemaRequest.getSchemaInfo().getSchemaIdentity().getSource(), isSystemSchema);
Boolean entity = this.checkAndRegisterEntityTypeIfNotPresent(
schemaRequest.getSchemaInfo().getSchemaIdentity().getEntityType(), isSystemSchema);
if (authority && source && entity) {
log.info(SchemaConstants.SCHEMA_CREATION_STARTED);
try {
SchemaInfo schemaInfo = schemaInfoStore.createSchemaInfo(schemaRequest);
schemaStore.createSchema(schemaId, schema);
auditLogger.schemaRegisteredSuccess(Collections.singletonList(schemaRequest.toString()));
messageBus.publishMessage(schemaId, SchemaConstants.SCHEMA_CREATE_EVENT_TYPE);
return schemaInfo;
} catch (ApplicationException ex) {
auditLogger.schemaRegisteredFailure(
Collections.singletonList(schemaRequest.toString()));
log.warning(SchemaConstants.SCHEMA_CREATION_FAILED);
schemaInfoStore.cleanSchema(schemaId);
schemaStore.cleanSchemaProject(schemaId);
log.info(SchemaConstants.SCHEMA_CREATE_CLEAN);
throw ex;
}
} else {
log.error("The schema could not be created due invalid authority,source or entityType");
throw new ApplicationException(SchemaConstants.INTERNAL_SERVER_ERROR);
}
} else {
throw new BadRequestException(SchemaConstants.SCHEMA_ID_EXISTS);
}
}
private Boolean checkAndRegisterAuthorityIfNotPresent(String authorityId, Boolean isSystemSchema) {
if (isSystemSchema) {
return authorityService.checkAndRegisterSystemAuthorityIfNotPresent(authorityId);
} else {
return authorityService.checkAndRegisterAuthorityIfNotPresent(authorityId);
}
}
private Boolean checkAndRegisterSourceIfNotPresent(String sourceId, Boolean isSystemSchema) {
if (isSystemSchema) {
return sourceService.checkAndRegisterSystemSourceIfNotPresent(sourceId);
} else {
return sourceService.checkAndRegisterSourceIfNotPresent(sourceId);
}
}
private Boolean checkAndRegisterEntityTypeIfNotPresent(String entityTypeId, Boolean isSystemSchema) {
if (isSystemSchema) {
return entityTypeService.checkAndRegisterSystemEntityTypeIfNotPresent(entityTypeId);
} else {
return entityTypeService.checkAndRegisterEntityTypeIfNotPresent(entityTypeId);
}
}
private Boolean isUniqueSchema(String schemaId, Boolean isSystemSchema) throws ApplicationException {
if (isSystemSchema) {
return schemaInfoStore.isUniqueSystemSchema(schemaId);
} else {
return schemaInfoStore.isUnique(schemaId, headers.getPartitionId());
}
}
private SchemaInfo createSystemSchema(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
String schemaId = createAndSetSchemaId(schemaRequest);
if (schemaInfoStore.isUniqueSystemSchema(schemaId)) {
setScope(schemaRequest,false);
String schema = resolveAndCheckBreakingChanges(schemaRequest);
String schema = resolveAndCheckBreakingChanges(schemaRequest, true);
Boolean authority = authorityService.checkAndRegisterAuthorityIfNotPresent(
schemaRequest.getSchemaInfo().getSchemaIdentity().getAuthority());
......@@ -256,7 +325,7 @@ public class SchemaService implements ISchemaService {
if (SchemaStatus.DEVELOPMENT.equals(schemaInfo.getStatus())) {
log.info(MessageFormat.format(SchemaConstants.SCHEMA_UPDATION_STARTED, createdSchemaId));
setScope(schemaRequest, isSystemSchema);
String schema = resolveAndCheckBreakingChanges(schemaRequest);
String schema = resolveAndCheckBreakingChanges(schemaRequest, isSystemSchema);
SchemaInfo schInfo;
if (isSystemSchema) {
schInfo = schemaInfoStore.updateSystemSchemaInfo(schemaRequest);
......@@ -297,12 +366,12 @@ public class SchemaService implements ISchemaService {
return schemaId;
}
private String resolveAndCheckBreakingChanges(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
private String resolveAndCheckBreakingChanges(SchemaRequest schemaRequest, Boolean isSystemSchema) throws ApplicationException, BadRequestException {
Gson gson = new Gson();
String schemaInRequestPayload = gson.toJson(schemaRequest.getSchema());
String fullyResolvedInputSchema = schemaResolver.resolveSchema(schemaInRequestPayload);
compareFullyResolvedSchema(schemaRequest.getSchemaInfo(), fullyResolvedInputSchema);
compareFullyResolvedSchema(schemaRequest.getSchemaInfo(), fullyResolvedInputSchema, isSystemSchema);
return fullyResolvedInputSchema;
}
......@@ -318,18 +387,18 @@ public class SchemaService implements ISchemaService {
if (queryParams.getScope() != null) {
if (queryParams.getScope().equalsIgnoreCase(SchemaScope.SHARED.toString())) {
getSchemaInfos(queryParams, schemaList, sharedTenant);
getSchemaInfos(queryParams, schemaList, true);
}
else if (queryParams.getScope().equalsIgnoreCase(SchemaScope.INTERNAL.toString())) {
getSchemaInfos(queryParams, schemaList, tenantId);
getSchemaInfos(queryParams, schemaList, false);
}
} else {
getSchemaInfos(queryParams, schemaList, sharedTenant);
if (!sharedTenant.equalsIgnoreCase(tenantId)) {
getSchemaInfos(queryParams, schemaList, tenantId);
}
// Fetch all the system schemas satisfying this query parameters
getSchemaInfos(queryParams, schemaList, true);
// Fetch all the private schemas satisfying the query parameters and add
// to the list of system schemas.
getSchemaInfos(queryParams, schemaList, false);
}
if (queryParams.getLatestVersion() != null && queryParams.getLatestVersion()) {
......@@ -393,10 +462,14 @@ public class SchemaService implements ISchemaService {
return this.upsertSchemaInternal(schemaRequest, true);
}
private void getSchemaInfos(QueryParams queryParams, List<SchemaInfo> schemaList, String tenant)
private void getSchemaInfos(QueryParams queryParams, List<SchemaInfo> schemaList, Boolean isSystemSchema)
throws ApplicationException {
schemaInfoStore.getSchemaInfoList(queryParams, tenant).forEach(schemaList::add);
if (isSystemSchema) {
schemaInfoStore.getSystemSchemaInfoList(queryParams).forEach(schemaList::add);
} else {
String tenant = headers.getPartitionId();
schemaInfoStore.getSchemaInfoList(queryParams, tenant).forEach(schemaList::add);
}
}
private void latestVersionMajorMinorFiltersCheck(QueryParams queryParams) throws BadRequestException {
......@@ -461,15 +534,21 @@ public class SchemaService implements ISchemaService {
schemaInfo.getSchemaIdentity().getEntityType());
}
private void compareFullyResolvedSchema(SchemaInfo inputSchemaInfo, String resolvedInputSchema) throws BadRequestException, ApplicationException {
private void compareFullyResolvedSchema(SchemaInfo inputSchemaInfo, String resolvedInputSchema, Boolean isSystemSchema) throws BadRequestException, ApplicationException {
try {
SchemaInfo[] schemaInfoToCompareWith = schemaUtil.findSchemaToCompare(inputSchemaInfo);
SchemaInfo[] schemaInfoToCompareWith = schemaUtil.findSchemaToCompare(inputSchemaInfo, isSystemSchema);
for(SchemaInfo existingSchemaInfo : schemaInfoToCompareWith) {
if(null == existingSchemaInfo)
continue;
String existingSchemaInStore = getSchema(existingSchemaInfo.getSchemaIdentity().getId()).toString();
String existingSchemaInStore;
if (isSystemSchema) {
existingSchemaInStore = schemaStore.getSystemSchema(existingSchemaInfo.getSchemaIdentity().getId()).toString();
} else {
existingSchemaInStore = getSchema(existingSchemaInfo.getSchemaIdentity().getId()).toString();
}
try {
//Compare Major version of the schemas are different
......
......@@ -20,8 +20,7 @@ public class SourceService implements ISourceService {
/**
* check and create Source if its not present in dataPartitionId store
*
* @param Source id
* @param dataPartitionId
* @param sourceId
* @return true or false of successful registration of Source.
*/
@Override
......@@ -39,4 +38,23 @@ public class SourceService implements ISourceService {
return true;
}
/**
* check and create system Source if its not present
* @param sourceId
* @return true or false of successful registration of Source.
*/
@Override
public Boolean checkAndRegisterSystemSourceIfNotPresent(String sourceId) {
try {
Source source = new Source();
source.setSourceId(sourceId);
this.sourceStore.createSystemSource(source);
} catch (ApplicationException e) {
return false;
} catch (BadRequestException ex) {
return true;
}
return true;
}
}
......@@ -28,19 +28,19 @@ public class SchemaUtil {
@Value("${shared.tenant.name:common}")
private String sharedTenant;
public SchemaInfo[] findSchemaToCompare(SchemaInfo schemaInfo) throws ApplicationException {
public SchemaInfo[] findSchemaToCompare(SchemaInfo schemaInfo, Boolean isSystemSchema) throws ApplicationException {
SchemaInfo[] matchingSchemaInfo = findClosestSchemas(schemaInfo, true);
SchemaInfo[] matchingSchemaInfo = findClosestSchemas(schemaInfo, true, isSystemSchema);
if(null == matchingSchemaInfo[0] && null == matchingSchemaInfo[1]) {
log.info("Latest patch version is not found so trying to find the nearest matching major version");
matchingSchemaInfo = findClosestSchemas(schemaInfo, false);
matchingSchemaInfo = findClosestSchemas(schemaInfo, false, isSystemSchema);
}
return matchingSchemaInfo;
}
public SchemaInfo[] findClosestSchemas(SchemaInfo schemaInfo, boolean atPatchlevel) throws ApplicationException {
public SchemaInfo[] findClosestSchemas(SchemaInfo schemaInfo, boolean atPatchlevel, Boolean isSystemSchema) throws ApplicationException {
SchemaIdentity schemaIdentity = schemaInfo.getSchemaIdentity();
SchemaInfo[] schemaInfoArr = new SchemaInfo [2];
QueryParams.QueryParamsBuilder queryParamBuilder = QueryParams.builder().authority(schemaIdentity.getAuthority())
......@@ -52,8 +52,13 @@ public class SchemaUtil {
queryParamBuilder.schemaVersionMinor(schemaIdentity.getSchemaVersionMinor());
QueryParams queryParams = queryParamBuilder.build();
List<SchemaInfo> schemaInfoList;
List<SchemaInfo> schemaInfoList = schemaInfoStore.getSchemaInfoList(queryParams, headers.getPartitionId());
if (isSystemSchema) {
schemaInfoList = schemaInfoStore.getSystemSchemaInfoList(queryParams);
} else {
schemaInfoList = schemaInfoStore.getSchemaInfoList(queryParams, headers.getPartitionId());
}
if(null == schemaInfoList || schemaInfoList.isEmpty())
return schemaInfoArr;
......
......@@ -91,7 +91,7 @@ public class SchemaUtilTest {
Mockito.when(schemaInfoStore.getSchemaInfoList( Mockito.any(QueryParams.class), Mockito.anyString()))
.thenReturn(new LinkedList<>());
Mockito.when(headers.getPartitionId()).thenReturn("opendes");
SchemaInfo[] schemaInfoArr = schemaUtil.findSchemaToCompare(schInf);
SchemaInfo[] schemaInfoArr = schemaUtil.findSchemaToCompare(schInf, false);
assertNull(schemaInfoArr[0]);
assertNull(schemaInfoArr[1]);
......@@ -115,7 +115,7 @@ public class SchemaUtilTest {
Mockito.when(schemaInfoStore.getSchemaInfoList( Mockito.any(QueryParams.class), Mockito.anyString()))
.thenReturn(schemaInfoList);
Mockito.when(headers.getPartitionId()).thenReturn("opendes");
SchemaInfo[] schemaInfoArr = schemaUtil.findSchemaToCompare(inputSchemaInfo);
SchemaInfo[] schemaInfoArr = schemaUtil.findSchemaToCompare(inputSchemaInfo, false);
assertEquals(schemaInfoArr[0], schInfLatest);
assertNull(schemaInfoArr[1]);
......@@ -169,14 +169,14 @@ public class SchemaUtilTest {
Mockito.when(schemaInfoStore.getSchemaInfoList( latestPatchQueryParams, "opendes"))
.thenReturn(new LinkedList<>());
Mockito.when(schemaInfoStore.getSchemaInfoList( latestPatchQueryParams, "common"))
Mockito.when(schemaInfoStore.getSystemSchemaInfoList( latestPatchQueryParams))
.thenReturn(new LinkedList<>());
Mockito.when(schemaInfoStore.getSchemaInfoList( queryParamsForAllMajorVersion, "opendes"))
.thenReturn(getMockedList());
Mockito.when(headers.getPartitionId()).thenReturn("opendes");
SchemaInfo[] schemaInfoArr = schemaUtil.findSchemaToCompare(inputSchemaInfo);
SchemaInfo[] schemaInfoArr = schemaUtil.findSchemaToCompare(inputSchemaInfo, false);
assertEquals(schemaInfoArr[0], smaller);
assertEquals(schemaInfoArr[1], bigger);
......@@ -226,7 +226,7 @@ public class SchemaUtilTest {
Mockito.when(schemaInfoStore.getSchemaInfoList( queryParamsForAllMajorVersion, "opendes"))
.thenReturn(getMockedList());
Mockito.when(headers.getPartitionId()).thenReturn("opendes");
SchemaInfo[] schemaInfoArr = schemaUtil.findSchemaToCompare(inputSchemaInfo);
SchemaInfo[] schemaInfoArr = schemaUtil.findSchemaToCompare(inputSchemaInfo, false);
assertEquals(schemaInfoArr[0], schInfLatest);
assertNull(schemaInfoArr[1]);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment