From 3b125dfa154e8adb1aac504372ff88b996d53357 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Mon, 30 Oct 2023 17:08:46 +0100 Subject: [PATCH] Added full support for ModelKnowledge --- .../client/RRCTypesDiscoverer.java | 12 +- .../client/ResourceRegistryClient.java | 13 ++ .../client/ResourceRegistryClientImpl.java | 138 ++++++++++++++---- .../resourceregistry/client/ContextTest.java | 68 +++++++-- .../client/ResourceRegistryClientTest.java | 66 ++++++++- 5 files changed, 246 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/RRCTypesDiscoverer.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/RRCTypesDiscoverer.java index 90f779e..46689b5 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/RRCTypesDiscoverer.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/RRCTypesDiscoverer.java @@ -4,6 +4,7 @@ import java.util.Collection; import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.model.knowledge.TypesDiscoverer; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.types.reference.Type; /** @@ -11,16 +12,19 @@ import org.gcube.informationsystem.types.reference.Type; */ public class RRCTypesDiscoverer implements TypesDiscoverer { - protected ResourceRegistryClient resourceRegistryClient; + protected ResourceRegistryClientImpl resourceRegistryClient; - public RRCTypesDiscoverer(ResourceRegistryClient resourceRegistryClient) { + public RRCTypesDiscoverer(ResourceRegistryClientImpl resourceRegistryClient) { this.resourceRegistryClient = resourceRegistryClient; } @Override public Collection discover(AccessType accessType) { - - return null; + try { + return resourceRegistryClient.getTypeFromServer(accessType.getTypeClass(), true); + } catch (ResourceRegistryException e) { + throw new RuntimeException(e); + } } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java index 280ceb2..59b6faa 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java @@ -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.types.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.request.RequestInfo; +import org.gcube.informationsystem.tree.Node; import org.gcube.informationsystem.types.reference.Type; /** @@ -73,6 +74,18 @@ public interface ResourceRegistryClient extends RequestInfo { public List getType(Class clazz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException; + public String getType(String typeName, int level) + throws SchemaNotFoundException, ResourceRegistryException; + + public List getType(Class clazz, int level) + throws SchemaNotFoundException, ResourceRegistryException; + + public Node getTypeTreeNode(String typeName) + throws SchemaNotFoundException, ResourceRegistryException; + + public Node getTypeTreeNode(Class clazz) + throws SchemaNotFoundException, ResourceRegistryException; + /* ---------------------------------------------------------------------- */ public List getInstances(Class clazz, Boolean polymorphic) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java index a17649d..4a22e78 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import org.gcube.com.fasterxml.jackson.databind.JavaType; @@ -371,24 +372,127 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou } } + public List getTypeFromTypesKnowledge(String typeName, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException { + return getTypeFromTypesKnowledge(typeName, polymorphic, -1); + } + + + public List getTypeFromTypesKnowledge(String typeName, int level) + throws SchemaNotFoundException, ResourceRegistryException { + return getTypeFromTypesKnowledge(typeName, true, level); + } + + protected List addChildren(Node node, List types, int currentLevel, int maxLevel) { + if(maxLevel>=0 && maxLevel <= currentLevel) { + return types; + } + + Set> children = node.getChildrenNodes(); + if(children!=null && children.size()>0) { + for(Node child : children) { + types.add(child.getNodeElement()); + types = addChildren(child, types, ++currentLevel, maxLevel); + } + } + + return types; + } + + public List getTypeFromTypesKnowledge(String typeName, Boolean polymorphic, int level) + throws SchemaNotFoundException, ResourceRegistryException { + + Node node = getTypeTreeNode(typeName); + + List 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 types = getTypeFromTypesKnowledge(typeName, polymorphic); + return TypeMapper.serializeTypeDefinitions(types); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + } + @Override public List getType(Class clazz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { try { - String json = getType(TypeUtility.getTypeName(clazz), polymorphic); - return TypeMapper.deserializeTypeDefinitions(json); + String typeName = TypeUtility.getTypeName(clazz); + return getTypeFromTypesKnowledge(typeName, polymorphic); + } catch (Exception e) { + throw new ResourceRegistryException(e); + } + } + + + @Override + public String getType(String typeName, int level) throws SchemaNotFoundException, ResourceRegistryException { + try { + List types = getTypeFromTypesKnowledge(typeName, level); + return TypeMapper.serializeTypeDefinitions(types); } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { throw new ResourceRegistryException(e); } } + + @Override + public List getType(Class 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 getTypeTreeNode(String typeName) throws SchemaNotFoundException, ResourceRegistryException { + try { + Node 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 Node getTypeTreeNode(Class clazz) + throws SchemaNotFoundException, ResourceRegistryException { + try { + String typeName = TypeUtility.getTypeName(clazz); + return getTypeTreeNode(typeName); + } catch (Exception e) { + throw new ResourceRegistryException(e); + } + } public List getTypeFromServer(Class clazz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { try { - // String json = getTypeFromServer(TypeUtility.getTypeName(clazz), polymorphic); - String json = getType(TypeUtility.getTypeName(clazz), polymorphic); + String json = getTypeFromServer(TypeUtility.getTypeName(clazz), polymorphic); return TypeMapper.deserializeTypeDefinitions(json); } catch(ResourceRegistryException e) { throw e; @@ -397,31 +501,7 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou } } - /* - @Override - public String getType(String typeName, Boolean polymorphic) - */ - public String getTypeFromKnowledge(String typeName, Boolean polymorphic) - throws SchemaNotFoundException, ResourceRegistryException { - try { - Node 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) + public String getTypeFromServer(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { try { logger.info("Going to get {} schema", typeName); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ContextTest.java index 14f565b..2c18362 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ContextTest.java @@ -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.SecretUtility; import org.gcube.common.keycloak.KeycloakClientFactory; +import org.gcube.common.keycloak.KeycloakClientHelper; import org.gcube.common.keycloak.model.TokenResponse; -import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.model.reference.properties.Metadata; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -24,7 +24,6 @@ import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ -@SuppressWarnings("deprecation") public class ContextTest { private static final Logger logger = LoggerFactory.getLogger(ContextTest.class); @@ -40,14 +39,13 @@ public class ContextTest { public static final String NEXTNEXT; public static final String DEVSEC; public static final String DEVVRE; - + protected static final Properties properties; - protected static final String CLIENT_ID_PROPERTY_KEY = "client_id"; - protected static final String CLIENT_SECRET_PROPERTY_KEY = "client_secret"; - - protected static final String clientID; - protected static final String clientSecret; + public static final String TYPE_PROPERTY_KEY = "type"; + public static final String USERNAME_PROPERTY_KEY = "username"; + public static final String PASSWORD_PROPERTY_KEY = "password"; + public static final String CLIENT_ID_PROPERTY_KEY = "clientId"; public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL"; public static final String RESOURCE_REGISTRY_URL; @@ -69,9 +67,6 @@ public class ContextTest { // load the properties file 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); } catch (IOException e) { @@ -80,6 +75,10 @@ public class ContextTest { } + private enum Type{ + USER, CLIENT_ID + }; + public static void set(Secret secret) throws Exception { SecretManagerProvider.instance.reset(); SecretManager secretManager = new SecretManager(); @@ -89,15 +88,56 @@ public class ContextTest { } public static void setContextByName(String fullContextName) throws Exception { + logger.debug("Going to set credentials for context {}", fullContextName); Secret secret = getSecretByContextName(fullContextName); set(secret); } private static TokenResponse getJWTAccessToken(String context) throws Exception { - ScopeProvider.instance.set(context); - TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null); - return tr; + Type type = Type.valueOf(properties.get(TYPE_PROPERTY_KEY).toString()); + + 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 { diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java index c54ebff..0642f6c 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientTest.java @@ -3,16 +3,19 @@ */ package org.gcube.informationsystem.resourceregistry.client; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; 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.contexts.reference.entities.Context; import org.gcube.informationsystem.contexts.reference.relations.IsParentOf; 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.properties.Metadata; 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.types.SchemaNotFoundException; 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.utils.UUIDManager; 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.HostingNode; 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.junit.Assert; import org.junit.Test; @@ -60,16 +66,68 @@ public class ResourceRegistryClientTest extends ContextTest { logger.trace(res); } + protected List getTypeNames(List types){ + List list = new ArrayList(); + for(Type t : types) { + list.add(t.getName()); + } + return list; + } + + protected void getTypesFromKnowledge(Class clazz) throws SchemaNotFoundException, ResourceRegistryException { + List 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 public void testGetFacetSchema() throws SchemaNotFoundException, ResourceRegistryException { - List typeDefinitions = resourceRegistryClient.getType(ContactFacet.class, true); - logger.trace("{}", typeDefinitions); + List types = resourceRegistryClient.getType(ContactFacet.class, true); + logger.trace("List size {}\n{}", types.size(), getTypeNames(types)); } @Test public void testGetResourceSchema() throws SchemaNotFoundException, ResourceRegistryException { - List typeDefinitions = resourceRegistryClient.getType(HostingNode.class, true); - logger.trace("{}", typeDefinitions); + List types = resourceRegistryClient.getType(HostingNode.class, true); + 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 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 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 node = resourceRegistryClient.getTypeTreeNode(accessType.getTypeClass()); + logger.trace("{}", node); + } } interface Aux extends Service {