gcat/src/main/java/org/gcube/gcat/social/SocialServiceDiscovery.java

111 lines
3.9 KiB
Java

package org.gcube.gcat.social;
import static org.gcube.resources.discovery.icclient.ICFactory.client;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.gcat.utils.ContextUtility;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Discover the Social Networking Service in the Infrastructure.
* @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/
// TODO Add a context based cache
public class SocialServiceDiscovery {
private static final String RESOURCE = "jersey-servlet";
private static final String SERVICE_NAME = "SocialNetworking";
private static final String SERVICE_CLASSE = "Portal";
private static final String SOCIAL_SERVICE_HA_PROXY_URL = "https://api.d4science.org/rest";
private static Logger logger = LoggerFactory.getLogger(SocialServiceDiscovery.class);
private String serviceBasePath;
// Map<String contextName, String basePath>
private static Map<String, String> socialServiceBasePathPerContext;
static {
socialServiceBasePathPerContext = new HashMap<>();
}
public static String getCurrentScopeSocialServiceBasePath() throws Exception {
String contex = ContextUtility.getCurrentContext();
String serviceBasePath = socialServiceBasePathPerContext.get(contex);
if(serviceBasePath==null) {
SocialServiceDiscovery gcoreEndpointReaderSNL = new SocialServiceDiscovery();
serviceBasePath = gcoreEndpointReaderSNL.getServiceBasePath();
socialServiceBasePathPerContext.put(contex, serviceBasePath);
}
return serviceBasePath;
}
/**
* Discover the gcore endpoint for the social networking service.
* @throws Exception the exception
*/
private SocialServiceDiscovery() throws Exception {
getServiceBasePathViaServiceEndpoint();
if(serviceBasePath==null) {
getServiceBasePathViaGCoreEndpoint();
}
}
protected void getServiceBasePathViaServiceEndpoint() throws Exception {
// TODO Discover on IS
serviceBasePath = SOCIAL_SERVICE_HA_PROXY_URL;
}
protected void getServiceBasePathViaGCoreEndpoint() throws Exception {
try {
SimpleQuery query = queryFor(GCoreEndpoint.class);
query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'", SERVICE_CLASSE));
query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'");
query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'", SERVICE_NAME));
query.setResult(
"$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""
+ RESOURCE + "\"]/text()");
DiscoveryClient<String> client = client();
List<String> endpoints = client.submit(query);
if(endpoints == null || endpoints.isEmpty()) {
throw new Exception("Cannot retrieve the GCoreEndpoint SERVICE_NAME: " + SERVICE_NAME
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + ContextUtility.getCurrentContext());
}
this.serviceBasePath = endpoints.get(0);
if(serviceBasePath == null)
throw new Exception("Endpoint:" + RESOURCE + ", is null for SERVICE_NAME: " + SERVICE_NAME
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + ContextUtility.getCurrentContext());
serviceBasePath = serviceBasePath.endsWith("/") ? serviceBasePath : serviceBasePath + "/";
} catch(Exception e) {
String error = "An error occurred during GCoreEndpoint discovery, SERVICE_NAME: " + SERVICE_NAME
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + ContextUtility.getCurrentContext() + ".";
logger.error(error, e);
throw new Exception(error);
}
}
/**
* @return the base path of the service
*/
public String getServiceBasePath() {
return serviceBasePath;
}
}