Compare commits

...

3 Commits

Author SHA1 Message Date
Luca Frosini 3a7649a73c Fixed interface 2024-01-24 16:10:56 +01:00
Luca Frosini c1128dd10b Added support for model knowledge #25922 2024-01-24 15:16:37 +01:00
luca.frosini 46a4ecc5a9 Enhanced version 2023-11-16 11:28:56 +01:00
7 changed files with 295 additions and 34 deletions

View File

@ -2,6 +2,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for Resource Registry Schema Client
## [v4.3.0-SNAPSHOT]
- Added support for model knowledge [#25922]
## [v4.2.1]
- Migrated code to reorganized E/R format [#24992]

View File

@ -8,7 +8,7 @@
</parent>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-schema-client</artifactId>
<version>4.2.1</version>
<version>4.3.0-SNAPSHOT</version>
<name>Resource Registry Schema Client</name>
<description>Contains Non Idempotent API to manage Schemas in Resource Registry</description>

View File

@ -0,0 +1,30 @@
package org.gcube.informationsystem.resourceregistry.schema;
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;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class RRCCTypesDiscoverer implements TypesDiscoverer<Type> {
protected ResourceRegistrySchemaClientImpl rrsc;
public RRCCTypesDiscoverer(ResourceRegistrySchemaClientImpl rrsc) {
this.rrsc = rrsc;
}
@Override
public Collection<Type> discover(AccessType accessType) {
try {
return rrsc.getTypeFromServer(accessType.getTypeClass(), true);
} catch (ResourceRegistryException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -2,10 +2,13 @@ package org.gcube.informationsystem.resourceregistry.schema;
import java.util.List;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
import org.gcube.informationsystem.model.reference.ModelElement;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
import org.gcube.informationsystem.tree.Node;
import org.gcube.informationsystem.types.knowledge.TypeInformation;
import org.gcube.informationsystem.types.reference.Type;
/**
@ -13,19 +16,35 @@ import org.gcube.informationsystem.types.reference.Type;
*/
public interface ResourceRegistrySchemaClient {
public ModelKnowledge<Type, TypeInformation> getModelKnowledge();
public void renewModelKnowledge();
public void addHeader(String name, String value);
public String create(String typeDefinitition) throws SchemaException, ResourceRegistryException;
public <E extends Element> Type create(Class<E> clz) throws SchemaException, ResourceRegistryException;
public <ME extends ModelElement> Type create(Class<ME> clz) throws SchemaException, ResourceRegistryException;
public boolean exist(String typeName) throws ResourceRegistryException;
public <E extends Element> boolean exist(Class<E> clz) throws ResourceRegistryException;
public <ME extends ModelElement> boolean exist(Class<ME> clz) throws ResourceRegistryException;
public String read(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException;
public <ME extends ModelElement> List<Type> read(Class<ME> clz, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException;
public <E extends Element> List<Type> read(Class<E> clz, Boolean polymorphic)
public String read(String typeName, int level)
throws SchemaNotFoundException, ResourceRegistryException;
public <ME extends ModelElement> List<Type> read(Class<ME> clz, int level)
throws SchemaNotFoundException, ResourceRegistryException;
public Node<Type> getTypeTreeNode(String typeName)
throws SchemaNotFoundException, ResourceRegistryException;
public <ME extends ModelElement> Node<Type> getTypeTreeNode(Class<ME> clz)
throws SchemaNotFoundException, ResourceRegistryException;
}

View File

@ -2,14 +2,17 @@ package org.gcube.informationsystem.resourceregistry.schema;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gcube.common.gxhttp.reference.GXConnection;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.common.http.GXHTTPUtility;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
import org.gcube.informationsystem.model.reference.ModelElement;
import org.gcube.informationsystem.model.reference.properties.Metadata;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
@ -20,7 +23,10 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaN
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.rest.TypePath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
import org.gcube.informationsystem.tree.Node;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.knowledge.TypeInformation;
import org.gcube.informationsystem.types.knowledge.TypesKnowledge;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.utils.TypeUtility;
import org.slf4j.Logger;
@ -40,6 +46,8 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC
protected Map<String, String> headers;
protected TypesKnowledge typesKnowledge;
/**
* Track if the client must request to include {@link Metadata}
*/
@ -91,13 +99,35 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC
}
public ResourceRegistrySchemaClientImpl(String address) {
this(address, true);
}
public ResourceRegistrySchemaClientImpl(String address, boolean sharedModelKnowledge) {
this.address = address;
this.headers = new HashMap<>();
this.includeMeta = false;
if(sharedModelKnowledge) {
this.typesKnowledge = TypesKnowledge.getInstance();
}else {
this.typesKnowledge = new TypesKnowledge();
}
typesKnowledge.setTypesDiscoverer(new RRCCTypesDiscoverer(this));
}
@Override
public <E extends Element> Type create(Class<E> clz)
public ModelKnowledge<Type, TypeInformation> getModelKnowledge() {
return typesKnowledge.getModelKnowledge();
}
@Override
public void renewModelKnowledge() {
typesKnowledge.renew();
}
@Override
public <ME extends ModelElement> Type create(Class<ME> clz)
throws SchemaException, ResourceRegistryException {
try {
String typeDefinition = TypeMapper.serializeType(clz);
@ -128,6 +158,9 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC
HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(typeDefinitition);
String c = HTTPUtility.getResponse(String.class, httpURLConnection);
Type t = TypeMapper.deserializeTypeDefinition(c);
typesKnowledge.getModelKnowledge().addType(t);
logger.trace("{} successfully created", c);
return c;
@ -139,19 +172,136 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC
}
@Override
public <E extends Element> boolean exist(Class<E> clz) throws ResourceRegistryException {
public <ME extends ModelElement> boolean exist(Class<ME> clazz) throws ResourceRegistryException {
return exist(TypeUtility.getTypeName(clazz));
}
@Override
public boolean exist(String typeName) throws ResourceRegistryException {
try {
String typeName = TypeUtility.getTypeName(clz);
return exist(typeName);
return typesKnowledge.getModelKnowledge().getTypeByName(typeName) != null;
}catch (RuntimeException e) {
return false;
}
}
public List<Type> getTypeFromTypesKnowledge(String typeName, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException {
return getTypeFromTypesKnowledge(typeName, polymorphic, -1);
}
public 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 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 read(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 RuntimeException(e);
throw new ResourceRegistryException(e);
}
}
@Override
public <ME extends ModelElement> List<Type> read(Class<ME> clazz, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException {
try {
String typeName = TypeUtility.getTypeName(clazz);
return getTypeFromTypesKnowledge(typeName, polymorphic);
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
@Override
public boolean exist(String typeName) throws ResourceRegistryException {
public String read(String typeName, int level) throws SchemaNotFoundException, ResourceRegistryException {
try {
List<Type> types = getTypeFromTypesKnowledge(typeName, level);
return TypeMapper.serializeTypeDefinitions(types);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
}
@Override
public <ME extends ModelElement> List<Type> read(Class<ME> 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 <ME extends ModelElement> Node<Type> getTypeTreeNode(Class<ME> clazz)
throws SchemaNotFoundException, ResourceRegistryException {
try {
String typeName = TypeUtility.getTypeName(clazz);
return getTypeTreeNode(typeName);
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
public boolean existTypeFromServer(String typeName) throws ResourceRegistryException {
try {
logger.info("Going to get {} schema", typeName);
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
@ -176,23 +326,20 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC
}
}
@Override
public <E extends Element> List<Type> read(Class<E> clz, Boolean polymorphic)
public <ME extends ModelElement> List<Type> getTypeFromServer(Class<ME> clz, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException {
try {
String typeName = TypeUtility.getTypeName(clz);
String res = read(typeName, polymorphic);
return TypeMapper.deserializeTypeDefinitions(res);
String json = getTypeFromServer(typeName, polymorphic);
return TypeMapper.deserializeTypeDefinitions(json);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new RuntimeException(e);
throw new ResourceRegistryException(e);
}
}
@Override
public String read(String typeName, Boolean polymorphic) throws ContextNotFoundException, ResourceRegistryException {
public String getTypeFromServer(String typeName, Boolean polymorphic) throws ContextNotFoundException, ResourceRegistryException {
try {
logger.info("Going to get {} schema", typeName);
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();

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.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 {

View File

@ -3,6 +3,11 @@ package org.gcube.informationsystem.resourceregistry.schema;
import java.util.List;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.tree.Node;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.junit.Assert;
@ -41,6 +46,21 @@ public class ResourceRegistrySchemaClientTest extends ContextTest {
Assert.assertTrue(gotFacetDefinition.getName().compareTo(facetDefinition.getName())==0);
Assert.assertTrue(gotFacetDefinition.getDescription().compareTo(facetDefinition.getDescription())==0);
Node<Type> node = resourceRegistrySchemaClient.getTypeTreeNode(Resource.class);
logger.debug("{}",node);
node = resourceRegistrySchemaClient.getTypeTreeNode(Facet.class);
logger.debug("{}",node);
node = resourceRegistrySchemaClient.getTypeTreeNode(Property.class);
logger.debug("{}",node);
node = resourceRegistrySchemaClient.getTypeTreeNode(IsRelatedTo.class);
logger.debug("{}",node);
node = resourceRegistrySchemaClient.getTypeTreeNode(ConsistsOf.class);
logger.debug("{}",node);
}
}