Implementing FacetBased IS interaction

This commit is contained in:
Luca Frosini 2022-12-07 17:21:14 +01:00
parent c39484784f
commit 018cb95905
4 changed files with 183 additions and 13 deletions

View File

@ -98,6 +98,11 @@
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-publisher</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-query-template-client</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>common-gcore-resources</artifactId>

View File

@ -3,6 +3,8 @@ package org.gcube.gcat;
import java.io.IOException;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.gcat.configuration.isproxies.FacetBasedISConfigurationProxy;
import org.gcube.gcat.configuration.isproxies.FacetBasedISConfigurationProxyFactory;
import org.gcube.gcat.rest.RequestFilter;
import org.gcube.smartgears.ApplicationManager;
import org.slf4j.Logger;
@ -42,7 +44,13 @@ public class GCatInitializator implements ApplicationManager {
+ "-------------------------------------------------------",
context);
FacetBasedISConfigurationProxy facetBasedISConfigurationProxy = FacetBasedISConfigurationProxyFactory.getInstance(context);
try {
facetBasedISConfigurationProxy.createCallsForToVirtualService();
}catch (Exception e) {
logger.warn("Gcat is not configured through the Facet Based IS in context {}. Please create/addToContext the expected resources ASAP. The Gcore IS will be used.", context, e);
}
logger.trace(
"\n-------------------------------------------------------\n"
+ "GCat Started Successfully on context {}\n"
@ -74,6 +82,15 @@ public class GCatInitializator implements ApplicationManager {
+ "-------------------------------------------------------",
context);
/*
FacetBasedISConfigurationProxy facetBasedISConfigurationProxy = FacetBasedISConfigurationProxyFactory.getInstance(context);
try {
facetBasedISConfigurationProxy.deleteCallsForToVirtualService();
}catch (Exception e) {
logger.warn("Gcat is not configured through the Facet Based IS in context {}. Please create/addToContext the expected resources ASAP. The Gcore IS will be used.", context, e);
}
*/
logger.trace(
"\n-------------------------------------------------------\n"
+ "GCat Stopped Successfully on context {}\n"

View File

@ -1,6 +1,7 @@
package org.gcube.gcat.configuration.isproxies;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
@ -15,25 +16,43 @@ import javax.ws.rs.InternalServerErrorException;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
import org.gcube.gcat.configuration.service.FacetBasedISServiceCatalogueConfiguration;
import org.gcube.gcat.configuration.service.ServiceCKANDB;
import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.impl.relations.ConsistsOfImpl;
import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.properties.Encrypted;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.queries.templates.reference.entities.QueryTemplate;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
import org.gcube.informationsystem.resourceregistry.queries.templates.ResourceRegistryQueryTemplateClient;
import org.gcube.informationsystem.resourceregistry.queries.templates.ResourceRegistryQueryTemplateClientFactory;
import org.gcube.informationsystem.utils.ElementMapper;
import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.EServiceImpl;
import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.CallsForImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.AccessPointFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.SimpleFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.Configuration;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
import org.gcube.resourcemanagement.model.reference.entities.resources.VirtualService;
import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.CallsFor;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -44,17 +63,26 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
private static Logger logger = LoggerFactory.getLogger(FacetBasedISConfigurationProxy.class);
public static final String QUERY_TEMPLATE_DIRECTORY_NAME = "query-template";
public static final String GCAT_ESERVICE_UUID_VARNAME = "$uuid";
public static final String GET_CALLS_FOR_QUERY_TEMPLATE_FILENAME = "01-get-calls-for-query-template.json";
public static final String QUERY_DIRECTORY_NAME = "query";
public static final String GET_CATALOGUE_VIRTUAL_SERVICE_FILENAME = "01-get-catalogue-virtual-service.json";
public static final String GET_GCAT_CONFIGURATION_FILENAME = "02-get-gcat-configuration.json";
public static final String GET_SIMPLE_FACET_OF_GCAT_CONFIGURATION_FILENAME = "03-get-simple-facet-of-gcat-configuration.json";
public static final String GET_ACCESS_POINT_FACET_OF_CKAN_SERVICE_FILENAME = "05-get-access-point-facet-of-ckan-service.json";
public static final String GET_ACCESS_POINT_FACET_OF_POSTGRES_CKAN_DB_FILENAME = "07-get-access-point-facet-of-postgres-ckan-db.json";
public static final String GET_ACCESS_POINT_FACET_OF_SOLR_SERVICE_FILENAME = "09-get-access-point-facet-of-solr-service.json";
private static String queryTemplateName;
private static boolean queryTemplateFound;
static {
queryTemplateFound = false;
queryTemplateName = null;
}
//
// Configuration
// ----------------------
@ -111,7 +139,7 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
* i.e. EService(gcat) --CallsFor--> VirtualService(catalogue-virtual-service)
*/
protected VirtualService virtualService;
protected SimpleFacet configurationSimpleFacet;
@ -136,13 +164,99 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
return virtualService;
}
protected String installQueryTemplate(ResourceRegistryQueryTemplateClient rrqtc) throws Exception {
QueryTemplate queryTemplate = getQueryTemplate();
/*
* Going to create/update the query template.
* No need to test if exists and/or if is the last version.
*/
queryTemplate = rrqtc.update(queryTemplate);
FacetBasedISConfigurationProxy.queryTemplateFound = true;
return queryTemplate.getName();
}
protected QueryTemplate getQueryTemplate() throws Exception {
File queryTemplateFile = getJsonQueryTemplateFromFile(GET_CALLS_FOR_QUERY_TEMPLATE_FILENAME);
QueryTemplate queryTemplate = ElementMapper.unmarshal(QueryTemplate.class, new FileReader(queryTemplateFile));
return queryTemplate;
}
public List<CallsFor<EService, VirtualService>> getCallsForToVirtualService() throws Exception {
ResourceRegistryQueryTemplateClient rrqtc = ResourceRegistryQueryTemplateClientFactory.create();
if(!queryTemplateFound) {
queryTemplateName = installQueryTemplate(rrqtc);
}
ApplicationContext applicationContext = ContextProvider.get();
String gcatEServiceID = applicationContext.id();
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put(GCAT_ESERVICE_UUID_VARNAME, gcatEServiceID);
List<CallsFor<EService, VirtualService>> callsForList = rrqtc.run(queryTemplateName, objectNode);
return callsForList;
}
public void deleteCallsForToVirtualService(List<CallsFor<EService, VirtualService>> callsForList) throws SchemaViolationException, NotFoundException, ResourceRegistryException {
for(CallsFor<EService, VirtualService> cf : callsForList) {
resourceRegistryPublisher.delete(cf);
}
}
public void deleteCallsForToVirtualService() throws Exception {
List<CallsFor<EService, VirtualService>> callsForList = getCallsForToVirtualService();
deleteCallsForToVirtualService(callsForList);
}
public CallsFor<EService, VirtualService> createCallsForToVirtualService() throws Exception {
List<CallsFor<EService, VirtualService>> callsForList = getCallsForToVirtualService();
CallsFor<EService, VirtualService> callsFor = null;
int size = callsForList.size();
if(size>1) {
deleteCallsForToVirtualService(callsForList);
size = 0;
}
if(size==0) {
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint.setAddConstraint(AddConstraint.unpropagate);
propagationConstraint.setRemoveConstraint(RemoveConstraint.keep);
EService gcatEService = new EServiceImpl();
ApplicationContext applicationContext = ContextProvider.get();
String gcatEServiceID = applicationContext.id();
UUID gcatEServiceUUID = UUID.fromString(gcatEServiceID);
gcatEService.setHeader(new HeaderImpl(gcatEServiceUUID));
VirtualService virtualService = queryVirtualService();
callsFor = new CallsForImpl<EService, VirtualService>(gcatEService, virtualService, propagationConstraint);
callsFor = resourceRegistryPublisher.create(callsFor);
}else if(size==1){
callsFor = callsForList.get(0);
}
return callsFor;
}
public File getFile(String directoryName, String filename) throws Exception {
URL directoryURL = FacetBasedISConfigurationProxy.class.getClassLoader().getResource(directoryName);
File directory = new File(directoryURL.toURI());
return new File(directory, filename);
}
public File getJsonQueryTemplateFromFile(String filename) throws Exception {
return getFile(QUERY_TEMPLATE_DIRECTORY_NAME, filename);
}
public File getJsonQueryFromFile(String filename) throws Exception {
URL queryDirectoryURL = FacetBasedISConfigurationProxy.class.getClassLoader().getResource(QUERY_DIRECTORY_NAME);
File queryDirectory = new File(queryDirectoryURL.toURI());
return new File(queryDirectory, filename);
return getFile(QUERY_DIRECTORY_NAME, filename);
}
protected List<Entity> query(String query) throws Exception {
protected List<Entity> queryListOfEntities(String query) throws Exception {
logger.trace("Going to request the following query:\n{}", query);
String result = resourceRegistryClient.jsonQuery(query);
logger.trace("The query:\n{}\nproduced the following result:\n{}", query, result);
@ -178,7 +292,7 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
File jsonQueryFile = getJsonQueryFromFile(filename);
JsonNode query = getQuery(jsonQueryFile);
String jsonQueryAsString = objectMapper.writeValueAsString(query);
List<Entity> entities = query(jsonQueryAsString);
List<Entity> entities = queryListOfEntities(jsonQueryAsString);
return getUniqueEntity(entities, jsonQueryAsString);
}
@ -229,18 +343,20 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
catalogueConfiguration.setDefaultOrganization((String) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.DEFAULT_ORGANIZATION_KEY));
Object supportedOrganizationsObj = configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.SUPPORTED_ORGANIZATIONS_KEY);
Set<String> supportedOrganizations = new HashSet<>();
boolean forceUpdate = false;
if(supportedOrganizationsObj!=null && supportedOrganizationsObj instanceof Collection) {
supportedOrganizations = new HashSet<String>((Collection<String>) supportedOrganizationsObj);
@SuppressWarnings("unchecked")
Set<String> supportedOrganizations = new HashSet<String>((Collection<String>) supportedOrganizationsObj);
catalogueConfiguration.setSupportedOrganizations(supportedOrganizations);
}else {
Set<String> supportedOrganizations = new HashSet<>();
supportedOrganizations.add(catalogueConfiguration.getDefaultOrganization());
configurationSimpleFacet.setAdditionalProperty(CatalogueConfiguration.SUPPORTED_ORGANIZATIONS_KEY, supportedOrganizations);
forceUpdate = true;
catalogueConfiguration.setSupportedOrganizations(supportedOrganizations);
}
catalogueConfiguration.setSupportedOrganizations(supportedOrganizations);
Map<String,Object> additionalProperties = new HashMap<>(configurationSimpleFacet.getAdditionalProperties());

View File

@ -0,0 +1,32 @@
{
"@class": "CallsFor",
"source": {
"@class": "EService",
"header" : {
"uuid" : "$uuid"
},
"consistsOf": [
{
"@class": "IsIdentifiedBy",
"target": {
"@class": "SoftwareFacet",
"group": "org.gcube.data-catalogue",
"name": "gcat"
}
}
]
},
"target":{
"@class": "VirtualService",
"consistsOf": [
{
"@class": "IsIdentifiedBy",
"target": {
"@class": "SoftwareFacet",
"group": "org.gcube.data-catalogue",
"name": "catalogue-virtual-service"
}
}
]
}
}