Implementing new IS interaction

This commit is contained in:
Luca Frosini 2022-12-01 17:26:46 +01:00
parent bcadc5b873
commit d979198399
1 changed files with 248 additions and 2 deletions

View File

@ -1,13 +1,53 @@
package org.gcube.gcat.configuration.isproxies;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.InternalServerErrorException;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration;
import org.gcube.informationsystem.base.reference.Direction;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.utils.ElementMapper;
import org.gcube.resourcemanagement.model.reference.entities.facets.SimpleFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
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.consistsof.IsIdentifiedBy;
import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.IsCustomizedBy;
import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Uses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class RestISConfigurationProxy extends ISConfigurationProxy<Configuration> {
private static Logger logger = LoggerFactory.getLogger(RestISConfigurationProxy.class);
protected VirtualService virtualService;
protected Configuration configuration;
protected SimpleFacet configurationSimpleFacet;
protected EService ckanService;
protected EService solrService;
protected EService ckanDB;
public RestISConfigurationProxy(String context) {
super(context);
}
@ -16,10 +56,216 @@ public class RestISConfigurationProxy extends ISConfigurationProxy<Configuration
super(context, catalogueConfiguration);
}
protected VirtualService queryVirtualService(ResourceRegistryClient resourceRegistryClient) throws ResourceRegistryException, JsonProcessingException {
Map<String,String> facetConstraint = new HashMap<>();
facetConstraint.put(SoftwareFacet.GROUP_PROPERTY, GCatConstants.CONFIGURATION_CATEGORY);
facetConstraint.put(SoftwareFacet.NAME_PROPERTY, GCatConstants.CATALOGUE_SERVICE_VIRTUAL_SERVICE_ID);
logger.trace("Going to query for {} --{}--> ({} with {})",
VirtualService.NAME, IsIdentifiedBy.NAME, SoftwareFacet.NAME, facetConstraint);
List<VirtualService> virtualServices = resourceRegistryClient.getFilteredResources(VirtualService.class, IsIdentifiedBy.class, SoftwareFacet.class, false, facetConstraint);
int size = virtualServices.size();
if(virtualServices==null || size==0) {
String message = String.format("No %s --%s--> (%s with %s) found",
VirtualService.NAME, IsIdentifiedBy.NAME, SoftwareFacet.NAME, facetConstraint.toString());
logger.error(message);
throw new InternalServerErrorException(message);
}
if(size>1) {
String message = String.format(
"Too many %s --%s--> (%s with %s) found (i.e. expected 1, found %d)). %s",
VirtualService.NAME, IsIdentifiedBy.NAME, SoftwareFacet.NAME,
facetConstraint.toString(), size, ElementMapper.marshal(virtualServices));
logger.error(message);
throw new InternalServerErrorException(message);
}
return virtualServices.get(0);
}
protected Configuration queryConfiguration(ResourceRegistryClient resourceRegistryClient, VirtualService virtualService) throws ResourceRegistryException, JsonProcessingException {
List<Configuration> configurations = resourceRegistryClient.getRelatedResourcesFromReferenceResource(
Configuration.class, IsCustomizedBy.class, virtualService, Direction.IN, true);
int size = configurations.size();
if(configurations==null || size==0) {
String message = String.format(
"No %s <--%s-- (%s uuid:%s) found",
Configuration.NAME, IsCustomizedBy.NAME, VirtualService.NAME,
virtualService.getHeader().getUUID());
logger.error(message);
throw new InternalServerErrorException(message);
}
if(size>1) {
String message = String.format(
"Too many %s <--%s-- (%s uuid:%s) found (i.e. expected 1, found %d)). %s",
Configuration.NAME, IsCustomizedBy.NAME, VirtualService.NAME,
virtualService.getHeader().getUUID(), size, ElementMapper.marshal(configurations));
logger.error(message);
throw new InternalServerErrorException(message);
}
return configurations.get(0);
}
protected <F extends Facet, C extends ConsistsOf<? extends Resource, F>> F getFacetFromResource(Resource resource, Class<C> coClz, Class<F> fClz) throws JsonProcessingException {
List<F> facets = resource.getFacets(coClz, fClz);
int size = facets.size();
if(facets==null || size==0) {
String message = String.format(
"No %s <--%s-- %s found",
TypeMapper.getType(fClz), TypeMapper.getType(coClz), TypeMapper.getType(resource.getClass()));
logger.error(message);
throw new InternalServerErrorException(message);
}
if(size>1) {
String message = String.format(
"Too many %s <--%s-- %s found (i.e. expected 1, found %d)). %s",
TypeMapper.getType(fClz), TypeMapper.getType(coClz), TypeMapper.getType(resource.getClass()),
size, ElementMapper.marshal(facets));
logger.error(message);
throw new InternalServerErrorException(message);
}
return facets.get(0);
}
@SuppressWarnings("unchecked")
protected SimpleFacet getConfigurationSimpleFacet() throws JsonProcessingException {
return getFacetFromResource(configuration, ConsistsOf.class, SimpleFacet.class);
}
protected EService queryCkanService(ResourceRegistryClient resourceRegistryClient, VirtualService virtualService) throws ResourceRegistryException, JsonProcessingException {
List<EService> eServices = resourceRegistryClient.getRelatedResourcesFromReferenceResource(
EService.class, Uses.class, virtualService, Direction.IN, true);
int size = eServices.size();
if(eServices==null || size==0) {
String message = String.format(
"No %s <--%s-- (%s uuid:%s) found",
EService.NAME, Uses.NAME, VirtualService.NAME,
virtualService.getHeader().getUUID());
logger.error(message);
throw new InternalServerErrorException(message);
}
if(size>1) {
String message = String.format(
"Too many %s <--%s-- (%s uuid:%s) found (i.e. expected 1, found %d)). %s",
EService.NAME, IsCustomizedBy.NAME, VirtualService.NAME,
virtualService.getHeader().getUUID(), size, ElementMapper.marshal(eServices));
logger.error(message);
throw new InternalServerErrorException(message);
}
return eServices.get(0);
}
protected List<EService> queryEServicesUsedByCkanService(ResourceRegistryClient resourceRegistryClient, EService ckanService) throws ResourceRegistryException, JsonProcessingException {
List<EService> eServices = resourceRegistryClient.getRelatedResourcesFromReferenceResource(
EService.class, Uses.class, ckanService, Direction.IN, true);
int size = eServices.size();
if(eServices==null || size==0) {
String message = String.format(
"No %s <--%s-- (%s uuid:%s) found",
EService.NAME, Uses.NAME, EService.NAME,
ckanService.getHeader().getUUID());
logger.error(message);
throw new InternalServerErrorException(message);
}
return eServices;
}
protected void getSolrServiceAndCKanDB(List<EService> eServices) throws JsonProcessingException {
for(EService eService : eServices) {
SoftwareFacet softwareFacet = getFacetFromResource(eService, IsIdentifiedBy.class, SoftwareFacet.class);
String group = softwareFacet.getGroup();
String name = softwareFacet.getName();
// TODO
// if(group.compareTo()==0 && name.compareTo()==0) {
// // TODO
// }
//
// if(group.compareTo()==0 && name.compareTo()==0) {
// // TODO
// }
}
if(solrService==null) {
// TODO
}
if(ckanDB==null) {
// TODO
}
}
protected ServiceCatalogueConfiguration createServiceCatalogueConfiguration() {
ServiceCatalogueConfiguration serviceCatalogueConfiguration = new ServiceCatalogueConfiguration(context);
serviceCatalogueConfiguration.setID(configuration.getHeader().getUUID().toString());
serviceCatalogueConfiguration.setModerationEnabled((boolean) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.MODERATION_ENABLED_KEY));
serviceCatalogueConfiguration.setNotificationToUsersEnabled((boolean) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.NOTIFICATION_TO_USER_ENABLED_KEY));
serviceCatalogueConfiguration.setSocialPostEnabled((boolean) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.SOCIAL_POST_ENABLED_KEY));
serviceCatalogueConfiguration.setDefaultOrganization((String) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.DEFAULT_ORGANIZATION_KEY));
Object supportedOrganizationsObj = configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.SUPPORTED_ORGANIZATIONS_KEY);
Set<String> supportedOrganizations = new HashSet<>();
if(supportedOrganizationsObj instanceof Collection) {
supportedOrganizations = new HashSet<String>((Collection<String>) supportedOrganizationsObj);
}
supportedOrganizations.add(serviceCatalogueConfiguration.getDefaultOrganization());
serviceCatalogueConfiguration.setSupportedOrganizations(supportedOrganizations);
Map<String,Object> additionalProperties = new HashMap<>(configurationSimpleFacet.getAdditionalProperties());
for(String key : additionalProperties.keySet()) {
if(!CatalogueConfiguration.KNOWN_PROPERTIES.contains(key)) {
serviceCatalogueConfiguration.setAdditionalProperty(key, additionalProperties.get(key));
}
}
return serviceCatalogueConfiguration;
}
@Override
protected ServiceCatalogueConfiguration readFromIS() {
// TODO Auto-generated method stub
return null;
try {
ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create();
virtualService = queryVirtualService(resourceRegistryClient);
configuration = queryConfiguration(resourceRegistryClient, virtualService);
configurationSimpleFacet = getConfigurationSimpleFacet();
ckanService = queryCkanService(resourceRegistryClient, virtualService);
List<EService> eServices = queryEServicesUsedByCkanService(resourceRegistryClient, ckanService);
getSolrServiceAndCKanDB(eServices);
catalogueConfiguration = createServiceCatalogueConfiguration();
}catch (InternalServerErrorException e) {
throw e;
}catch (Exception e) {
throw new InternalServerErrorException(e);
}
return catalogueConfiguration;
}
@Override