2017-04-03 18:44:47 +02:00
|
|
|
package org.gcube.informationsystem.resourceregistry.client;
|
|
|
|
|
2017-06-22 17:25:14 +02:00
|
|
|
import java.util.ArrayList;
|
2017-04-03 18:44:47 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
|
2020-11-04 16:23:06 +01:00
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
2017-04-11 15:23:27 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2017-04-03 18:44:47 +02:00
|
|
|
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
2017-06-22 17:25:14 +02:00
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
2017-04-11 15:23:27 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2017-04-03 18:44:47 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.Constants;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.gcube.resources.discovery.icclient.ICFactory;
|
2017-06-22 17:25:14 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2017-04-03 18:44:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class ResourceRegistryClientFactory {
|
|
|
|
|
2017-06-22 17:25:14 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryClientFactory.class);
|
|
|
|
|
2020-11-04 17:38:09 +01:00
|
|
|
protected static boolean HIERARCHICAL_MODE;
|
2017-04-11 15:23:27 +02:00
|
|
|
|
2020-11-09 15:47:59 +01:00
|
|
|
protected static boolean isHierarchicalMode() {
|
2020-11-04 17:38:09 +01:00
|
|
|
return ResourceRegistryClientFactory.HIERARCHICAL_MODE;
|
2020-10-29 16:54:24 +01:00
|
|
|
}
|
|
|
|
|
2020-11-09 15:47:59 +01:00
|
|
|
/**
|
|
|
|
* The affected methods are {@link ResourceRegistryClient} instances safe methods i.e. read* and exists*
|
|
|
|
* @param hierarchicalMode
|
|
|
|
*/
|
2020-10-29 16:54:24 +01:00
|
|
|
public static void setHierarchicalMode(boolean hierarchicalMode) {
|
2020-11-04 17:38:09 +01:00
|
|
|
ResourceRegistryClientFactory.HIERARCHICAL_MODE = hierarchicalMode;
|
2020-10-29 16:54:24 +01:00
|
|
|
}
|
2020-11-04 17:38:09 +01:00
|
|
|
|
2020-11-09 15:47:59 +01:00
|
|
|
protected static boolean INCLUDE_CONTEXTS_IN_INSTANCES_HEADER;
|
|
|
|
|
|
|
|
protected static boolean includeContextsInInstanceHeader() {
|
|
|
|
return ResourceRegistryClientFactory.INCLUDE_CONTEXTS_IN_INSTANCES_HEADER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The affected methods are {@link ResourceRegistryClient} instances safe methods i.e. read* and exists*
|
2020-12-23 14:39:03 +01:00
|
|
|
* @param includeContextsInInstancesHeader
|
2020-11-09 15:47:59 +01:00
|
|
|
*/
|
|
|
|
public static void includeContextsInInstanceHeader(boolean includeContextsInInstancesHeader) {
|
|
|
|
ResourceRegistryClientFactory.INCLUDE_CONTEXTS_IN_INSTANCES_HEADER = includeContextsInInstancesHeader;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-04 17:38:09 +01:00
|
|
|
protected static List<String> addresses;
|
|
|
|
|
2017-04-11 15:23:27 +02:00
|
|
|
static {
|
2020-11-04 17:38:09 +01:00
|
|
|
addresses = new ArrayList<>();
|
2017-04-11 15:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String FORCED_URL = null;
|
|
|
|
|
|
|
|
protected static void forceToURL(String url){
|
|
|
|
FORCED_URL = url;
|
2020-11-04 17:38:09 +01:00
|
|
|
HIERARCHICAL_MODE = false;
|
2017-04-11 15:23:27 +02:00
|
|
|
}
|
2017-04-03 18:44:47 +02:00
|
|
|
|
|
|
|
private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
|
|
|
|
private static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'";
|
|
|
|
private static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
|
|
|
|
private static String containsFormat = "$entry/@EntryName eq '%1s'";
|
|
|
|
|
2017-06-22 17:25:14 +02:00
|
|
|
|
|
|
|
private static String serviceEndpointCategoryFormat = "$resource/Profile/Category/text() eq '%1s'";
|
|
|
|
private static String serviceEndpointNameFormat = "$resource/Profile/Name/text() eq '%1s'";
|
|
|
|
private static String serviceEndpointstatusFormat = "$resource/Profile/RunTime/Status/text() eq 'READY'";
|
|
|
|
|
2020-11-04 16:23:06 +01:00
|
|
|
public static String getCurrentContextFullName() {
|
|
|
|
String token = SecurityTokenProvider.instance.get();
|
|
|
|
AuthorizationEntry authorizationEntry = null;
|
|
|
|
try {
|
|
|
|
authorizationEntry = org.gcube.common.authorization.client.Constants.authorizationService().get(token);
|
|
|
|
} catch(Exception e) {
|
|
|
|
return ScopeProvider.instance.get();
|
|
|
|
}
|
|
|
|
return authorizationEntry.getContext();
|
|
|
|
}
|
2017-06-22 17:25:14 +02:00
|
|
|
|
|
|
|
private static SimpleQuery queryForService(){
|
2017-04-03 18:44:47 +02:00
|
|
|
return ICFactory.queryFor(GCoreEndpoint.class)
|
|
|
|
.addCondition(String.format(classFormat, Constants.SERVICE_CLASS))
|
|
|
|
.addCondition(String.format(nameFormat, Constants.SERVICE_NAME))
|
|
|
|
.addCondition(String.format(statusFormat))
|
|
|
|
.addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint")
|
|
|
|
.addCondition(String.format(containsFormat, Constants.SERVICE_ENTRY_NAME))
|
|
|
|
.setResult("$entry/text()");
|
|
|
|
}
|
|
|
|
|
2017-06-22 17:25:14 +02:00
|
|
|
private static SimpleQuery queryForProxy(){
|
|
|
|
return ICFactory.queryFor(ServiceEndpoint.class)
|
|
|
|
.addCondition(String.format(serviceEndpointCategoryFormat, Constants.SERVICE_CLASS))
|
|
|
|
.addCondition(String.format(serviceEndpointNameFormat, Constants.SERVICE_NAME))
|
|
|
|
.addCondition(String.format(serviceEndpointstatusFormat))
|
|
|
|
.addVariable("$entry","$resource/Profile/AccessPoint/Interface/Endpoint")
|
|
|
|
.addCondition(String.format(containsFormat, Constants.SERVICE_ENTRY_NAME))
|
|
|
|
.setResult("$entry/text()");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected static List<String> getAddresses(){
|
|
|
|
List<String> addresses = new ArrayList<>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
SimpleQuery proxyQuery = queryForProxy();
|
|
|
|
addresses = ICFactory.client().submit(proxyQuery);
|
|
|
|
if(addresses==null || addresses.isEmpty()){
|
|
|
|
throw new Exception("No ResourceRegistry Proxy Found");
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2017-06-23 15:53:19 +02:00
|
|
|
logger.debug("{}. Looking for RunningInstance.", e.getMessage());
|
2017-06-22 17:25:14 +02:00
|
|
|
SimpleQuery serviceQuery = queryForService();
|
|
|
|
addresses = ICFactory.client().submit(serviceQuery);
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses;
|
|
|
|
}
|
|
|
|
|
2020-11-04 16:23:06 +01:00
|
|
|
public static ResourceRegistryClient create() {
|
|
|
|
String address = null;
|
|
|
|
|
2017-04-11 15:23:27 +02:00
|
|
|
if(FORCED_URL!=null){
|
2020-11-04 16:23:06 +01:00
|
|
|
address = FORCED_URL;
|
|
|
|
}else {
|
2017-04-11 15:23:27 +02:00
|
|
|
|
2020-11-24 19:07:18 +01:00
|
|
|
if(addresses==null || addresses.isEmpty()) {
|
2020-11-04 16:23:06 +01:00
|
|
|
addresses = getAddresses();
|
2017-04-11 15:23:27 +02:00
|
|
|
}
|
2020-11-04 16:23:06 +01:00
|
|
|
|
2017-04-03 18:44:47 +02:00
|
|
|
if(addresses==null || addresses.isEmpty()){
|
2020-11-04 16:23:06 +01:00
|
|
|
String error = String.format("No %s:%s found in the current context %s", Constants.SERVICE_CLASS, Constants.SERVICE_NAME, getCurrentContextFullName());
|
2017-04-03 18:44:47 +02:00
|
|
|
throw new RuntimeException(error);
|
|
|
|
}
|
2020-11-04 16:23:06 +01:00
|
|
|
|
2017-04-03 18:44:47 +02:00
|
|
|
Random random = new Random();
|
|
|
|
int index = random.nextInt(addresses.size());
|
2020-11-04 16:23:06 +01:00
|
|
|
address = addresses.get(index);
|
2017-04-03 18:44:47 +02:00
|
|
|
}
|
2017-04-11 15:23:27 +02:00
|
|
|
|
2020-11-04 16:23:06 +01:00
|
|
|
return new ResourceRegistryClientImpl(address);
|
2017-04-03 18:44:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|