Added full support for ModelKnowledge

This commit is contained in:
luca.frosini 2023-10-30 17:08:46 +01:00
parent 9eceb98059
commit 3b125dfa15
5 changed files with 246 additions and 51 deletions

View File

@ -4,6 +4,7 @@ import java.util.Collection;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.knowledge.TypesDiscoverer; import org.gcube.informationsystem.model.knowledge.TypesDiscoverer;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.types.reference.Type; import org.gcube.informationsystem.types.reference.Type;
/** /**
@ -11,16 +12,19 @@ import org.gcube.informationsystem.types.reference.Type;
*/ */
public class RRCTypesDiscoverer implements TypesDiscoverer<Type> { public class RRCTypesDiscoverer implements TypesDiscoverer<Type> {
protected ResourceRegistryClient resourceRegistryClient; protected ResourceRegistryClientImpl resourceRegistryClient;
public RRCTypesDiscoverer(ResourceRegistryClient resourceRegistryClient) { public RRCTypesDiscoverer(ResourceRegistryClientImpl resourceRegistryClient) {
this.resourceRegistryClient = resourceRegistryClient; this.resourceRegistryClient = resourceRegistryClient;
} }
@Override @Override
public Collection<Type> discover(AccessType accessType) { public Collection<Type> discover(AccessType accessType) {
try {
return null; return resourceRegistryClient.getTypeFromServer(accessType.getTypeClass(), true);
} catch (ResourceRegistryException e) {
throw new RuntimeException(e);
}
} }
} }

View File

@ -23,6 +23,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.Inval
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.templates.QueryTemplateNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.request.RequestInfo; import org.gcube.informationsystem.resourceregistry.api.request.RequestInfo;
import org.gcube.informationsystem.tree.Node;
import org.gcube.informationsystem.types.reference.Type; import org.gcube.informationsystem.types.reference.Type;
/** /**
@ -73,6 +74,18 @@ public interface ResourceRegistryClient extends RequestInfo {
public <ERElem extends ERElement> List<Type> getType(Class<ERElem> clazz, Boolean polymorphic) public <ERElem extends ERElement> List<Type> getType(Class<ERElem> clazz, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException; throws SchemaNotFoundException, ResourceRegistryException;
public String getType(String typeName, int level)
throws SchemaNotFoundException, ResourceRegistryException;
public <ERElem extends ERElement> List<Type> getType(Class<ERElem> clazz, int level)
throws SchemaNotFoundException, ResourceRegistryException;
public <ERElem extends ERElement> Node<Type> getTypeTreeNode(String typeName)
throws SchemaNotFoundException, ResourceRegistryException;
public <ERElem extends ERElement> Node<Type> getTypeTreeNode(Class<ERElem> clazz)
throws SchemaNotFoundException, ResourceRegistryException;
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
public <ERElem extends ERElement> List<ERElem> getInstances(Class<ERElem> clazz, Boolean polymorphic) public <ERElem extends ERElement> List<ERElem> getInstances(Class<ERElem> clazz, Boolean polymorphic)

View File

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JavaType; import org.gcube.com.fasterxml.jackson.databind.JavaType;
@ -371,12 +372,77 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou
} }
} }
public <ERElem extends ERElement> List<Type> getTypeFromTypesKnowledge(String typeName, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException {
return getTypeFromTypesKnowledge(typeName, polymorphic, -1);
}
public <ERElem extends ERElement> List<Type> getTypeFromTypesKnowledge(String typeName, int level)
throws SchemaNotFoundException, ResourceRegistryException {
return getTypeFromTypesKnowledge(typeName, true, level);
}
protected List<Type> addChildren(Node<Type> node, List<Type> types, int currentLevel, int maxLevel) {
if(maxLevel>=0 && maxLevel <= currentLevel) {
return types;
}
Set<Node<Type>> children = node.getChildrenNodes();
if(children!=null && children.size()>0) {
for(Node<Type> child : children) {
types.add(child.getNodeElement());
types = addChildren(child, types, ++currentLevel, maxLevel);
}
}
return types;
}
public <ERElem extends ERElement> List<Type> getTypeFromTypesKnowledge(String typeName, Boolean polymorphic, int level)
throws SchemaNotFoundException, ResourceRegistryException {
Node<Type> node = getTypeTreeNode(typeName);
List<Type> types = new ArrayList<>();
types.add(node.getNodeElement());
if (polymorphic) {
addChildren(node, types, 0, level);
}
return types;
}
@Override
public String getType(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException {
try {
List<Type> types = getTypeFromTypesKnowledge(typeName, polymorphic);
return TypeMapper.serializeTypeDefinitions(types);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
}
@Override @Override
public <ERElem extends ERElement> List<Type> getType(Class<ERElem> clazz, Boolean polymorphic) public <ERElem extends ERElement> List<Type> getType(Class<ERElem> clazz, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException { throws SchemaNotFoundException, ResourceRegistryException {
try { try {
String json = getType(TypeUtility.getTypeName(clazz), polymorphic); String typeName = TypeUtility.getTypeName(clazz);
return TypeMapper.deserializeTypeDefinitions(json); return getTypeFromTypesKnowledge(typeName, polymorphic);
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
@Override
public String getType(String typeName, int level) throws SchemaNotFoundException, ResourceRegistryException {
try {
List<Type> types = getTypeFromTypesKnowledge(typeName, level);
return TypeMapper.serializeTypeDefinitions(types);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
@ -384,11 +450,49 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou
} }
} }
@Override
public <ERElem extends ERElement> List<Type> getType(Class<ERElem> clazz, int level)
throws SchemaNotFoundException, ResourceRegistryException {
try {
String typeName = TypeUtility.getTypeName(clazz);
return getTypeFromTypesKnowledge(typeName, level);
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
@Override
public Node<Type> getTypeTreeNode(String typeName) throws SchemaNotFoundException, ResourceRegistryException {
try {
Node<Type> node = null;
try {
node = typesKnowledge.getModelKnowledge().getNodeByName(typeName);
} catch (RuntimeException e) {
throw new SchemaNotFoundException(e);
}
return node;
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
}
@Override
public <ERElem extends ERElement> Node<Type> getTypeTreeNode(Class<ERElem> clazz)
throws SchemaNotFoundException, ResourceRegistryException {
try {
String typeName = TypeUtility.getTypeName(clazz);
return getTypeTreeNode(typeName);
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
public <ERElem extends ERElement> List<Type> getTypeFromServer(Class<ERElem> clazz, Boolean polymorphic) public <ERElem extends ERElement> List<Type> getTypeFromServer(Class<ERElem> clazz, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException { throws SchemaNotFoundException, ResourceRegistryException {
try { try {
// String json = getTypeFromServer(TypeUtility.getTypeName(clazz), polymorphic); String json = getTypeFromServer(TypeUtility.getTypeName(clazz), polymorphic);
String json = getType(TypeUtility.getTypeName(clazz), polymorphic);
return TypeMapper.deserializeTypeDefinitions(json); return TypeMapper.deserializeTypeDefinitions(json);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
throw e; throw e;
@ -397,31 +501,7 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou
} }
} }
/* public String getTypeFromServer(String typeName, Boolean polymorphic)
@Override
public String getType(String typeName, Boolean polymorphic)
*/
public String getTypeFromKnowledge(String typeName, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException {
try {
Node<Type> node = typesKnowledge.getModelKnowledge().getNodeByName(typeName);
if(polymorphic) {
// TODO
// Navigate the subtree
}else {
// TODO
node.getNodeElement();
}
return null;
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
}
// public String getTypeFromServer(String typeName, Boolean polymorphic)
@Override
public String getType(String typeName, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException { throws SchemaNotFoundException, ResourceRegistryException {
try { try {
logger.info("Going to get {} schema", typeName); logger.info("Going to get {} schema", typeName);

View File

@ -13,8 +13,8 @@ import org.gcube.common.authorization.utils.secret.JWTSecret;
import org.gcube.common.authorization.utils.secret.Secret; import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility; import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.keycloak.KeycloakClientFactory; import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.KeycloakClientHelper;
import org.gcube.common.keycloak.model.TokenResponse; import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.model.reference.properties.Metadata; import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -24,7 +24,6 @@ import org.slf4j.LoggerFactory;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@SuppressWarnings("deprecation")
public class ContextTest { public class ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class); private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
@ -43,11 +42,10 @@ public class ContextTest {
protected static final Properties properties; protected static final Properties properties;
protected static final String CLIENT_ID_PROPERTY_KEY = "client_id"; public static final String TYPE_PROPERTY_KEY = "type";
protected static final String CLIENT_SECRET_PROPERTY_KEY = "client_secret"; public static final String USERNAME_PROPERTY_KEY = "username";
public static final String PASSWORD_PROPERTY_KEY = "password";
protected static final String clientID; public static final String CLIENT_ID_PROPERTY_KEY = "clientId";
protected static final String clientSecret;
public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL"; public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL";
public static final String RESOURCE_REGISTRY_URL; public static final String RESOURCE_REGISTRY_URL;
@ -69,9 +67,6 @@ public class ContextTest {
// load the properties file // load the properties file
properties.load(input); properties.load(input);
clientID = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
clientSecret = properties.getProperty(CLIENT_SECRET_PROPERTY_KEY);
RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY); RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY);
} catch (IOException e) { } catch (IOException e) {
@ -80,6 +75,10 @@ public class ContextTest {
} }
private enum Type{
USER, CLIENT_ID
};
public static void set(Secret secret) throws Exception { public static void set(Secret secret) throws Exception {
SecretManagerProvider.instance.reset(); SecretManagerProvider.instance.reset();
SecretManager secretManager = new SecretManager(); SecretManager secretManager = new SecretManager();
@ -89,15 +88,56 @@ public class ContextTest {
} }
public static void setContextByName(String fullContextName) throws Exception { public static void setContextByName(String fullContextName) throws Exception {
logger.debug("Going to set credentials for context {}", fullContextName);
Secret secret = getSecretByContextName(fullContextName); Secret secret = getSecretByContextName(fullContextName);
set(secret); set(secret);
} }
private static TokenResponse getJWTAccessToken(String context) throws Exception { private static TokenResponse getJWTAccessToken(String context) throws Exception {
ScopeProvider.instance.set(context); Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString());
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null);
return tr; TokenResponse tr = null;
int index = context.indexOf('/', 1);
String root = context.substring(0, index == -1 ? context.length() : index);
switch (type) {
case CLIENT_ID:
String clientId = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
String clientSecret = properties.getProperty(root);
tr = KeycloakClientFactory.newInstance().queryUMAToken(context, clientId, clientSecret, context, null);
break;
case USER:
default:
String username = properties.getProperty(USERNAME_PROPERTY_KEY);
String password = properties.getProperty(PASSWORD_PROPERTY_KEY);
switch (root) {
case "/gcube":
default:
clientId = "next.d4science.org";
break;
case "/pred4s":
clientId = "pre.d4science.org";
break;
case "/d4science.research-infrastructures.eu":
clientId = "services.d4science.org";
break;
}
clientSecret = null;
tr = KeycloakClientHelper.getTokenForUser(context, username, password);
break;
}
return tr;
} }
public static Secret getSecretByContextName(String context) throws Exception { public static Secret getSecretByContextName(String context) throws Exception {

View File

@ -3,16 +3,19 @@
*/ */
package org.gcube.informationsystem.resourceregistry.client; package org.gcube.informationsystem.resourceregistry.client;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException; import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Direction; import org.gcube.informationsystem.base.reference.Direction;
import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.contexts.reference.relations.IsParentOf; import org.gcube.informationsystem.contexts.reference.relations.IsParentOf;
import org.gcube.informationsystem.model.impl.properties.MetadataImpl; import org.gcube.informationsystem.model.impl.properties.MetadataImpl;
import org.gcube.informationsystem.model.reference.ERElement;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Metadata; import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
@ -20,6 +23,8 @@ import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.tree.Node;
import org.gcube.informationsystem.tree.Tree;
import org.gcube.informationsystem.types.reference.Type; import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.UUIDManager; import org.gcube.informationsystem.utils.UUIDManager;
import org.gcube.informationsystem.utils.UUIDUtility; import org.gcube.informationsystem.utils.UUIDUtility;
@ -30,6 +35,7 @@ import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFace
import org.gcube.resourcemanagement.model.reference.entities.resources.EService; import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode; import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
import org.gcube.resourcemanagement.model.reference.entities.resources.Service; import org.gcube.resourcemanagement.model.reference.entities.resources.Service;
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.consistsof.IsIdentifiedBy;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -60,16 +66,68 @@ public class ResourceRegistryClientTest extends ContextTest {
logger.trace(res); logger.trace(res);
} }
protected List<String> getTypeNames(List<Type> types){
List<String> list = new ArrayList<String>();
for(Type t : types) {
list.add(t.getName());
}
return list;
}
protected <ERElem extends ERElement> void getTypesFromKnowledge(Class<ERElem> clazz) throws SchemaNotFoundException, ResourceRegistryException {
List<Type> types = resourceRegistryClient.getType(clazz, true);
logger.trace("List size {}\n{}", types.size(), getTypeNames(types));
}
@Test
public void testModelKnowledge() throws SchemaNotFoundException, ResourceRegistryException {
AccessType[] modelTypes = AccessType.getModelTypes();
for(AccessType accessType : modelTypes) {
getTypesFromKnowledge(accessType.getTypeClass());
}
}
@Test @Test
public void testGetFacetSchema() throws SchemaNotFoundException, ResourceRegistryException { public void testGetFacetSchema() throws SchemaNotFoundException, ResourceRegistryException {
List<Type> typeDefinitions = resourceRegistryClient.getType(ContactFacet.class, true); List<Type> types = resourceRegistryClient.getType(ContactFacet.class, true);
logger.trace("{}", typeDefinitions); logger.trace("List size {}\n{}", types.size(), getTypeNames(types));
} }
@Test @Test
public void testGetResourceSchema() throws SchemaNotFoundException, ResourceRegistryException { public void testGetResourceSchema() throws SchemaNotFoundException, ResourceRegistryException {
List<Type> typeDefinitions = resourceRegistryClient.getType(HostingNode.class, true); List<Type> types = resourceRegistryClient.getType(HostingNode.class, true);
logger.trace("{}", typeDefinitions); logger.trace("List size {}\n{}", types.size(), getTypeNames(types));
types = resourceRegistryClient.getType(VirtualService.class, true);
logger.trace("List size {}\n{}", types.size(), getTypeNames(types));
types = resourceRegistryClient.getType(Service.class, true);
logger.trace("List size {}\n{}", types.size(), getTypeNames(types));
}
@Test
public void testGetResourceWithLevel() throws SchemaNotFoundException, ResourceRegistryException {
for(int i=0; i<5; i++) {
List<Type> types = resourceRegistryClient.getType(Resource.class, i);
logger.trace("List size with level {} is {}\n{}", i, types.size(), getTypeNames(types));
}
}
@Test
public void testGetModelKnowledge() throws SchemaNotFoundException, ResourceRegistryException {
AccessType[] modelTypes = AccessType.getModelTypes();
for(AccessType accessType : modelTypes) {
Tree<Type> tree = ((ResourceRegistryClientImpl) resourceRegistryClient).getModelKnowledge().getTree(accessType);
logger.trace("{}", tree);
}
}
@Test
public void testGetNode() throws SchemaNotFoundException, ResourceRegistryException {
AccessType[] modelTypes = AccessType.getModelTypes();
for(AccessType accessType : modelTypes) {
Node<Type> node = resourceRegistryClient.getTypeTreeNode(accessType.getTypeClass());
logger.trace("{}", node);
}
} }
interface Aux extends Service { interface Aux extends Service {