2019-01-29 17:44:30 +01:00
package org.gcube.gcat.client ;
import java.net.MalformedURLException ;
import java.net.URL ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.Random ;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider ;
import org.gcube.common.resources.gcore.GCoreEndpoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint ;
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.gcat.api.GCatConstants ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
import org.gcube.resources.discovery.icclient.ICFactory ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
public class GCatClientDiscovery {
private static final Logger logger = LoggerFactory . getLogger ( GCatClientDiscovery . class ) ;
protected static Map < String , URL > clients ;
static {
clients = new HashMap < > ( ) ;
}
protected static void forceToURL ( String adddress ) throws MalformedURLException {
if ( adddress ! = null & & adddress . compareTo ( " " ) ! = 0 ) {
String context = getContext ( ) ;
URL url = new URL ( adddress ) ;
clients . put ( context , url ) ;
}
}
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' " ;
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' " ;
2022-02-24 11:42:19 +01:00
private static SimpleQuery queryForService ( String serviceClass ) {
2019-01-29 17:44:30 +01:00
return ICFactory . queryFor ( GCoreEndpoint . class )
2022-02-24 11:42:19 +01:00
. addCondition ( String . format ( classFormat , serviceClass ) )
2019-01-29 17:44:30 +01:00
. addCondition ( String . format ( nameFormat , GCatConstants . SERVICE_NAME ) )
. addCondition ( String . format ( statusFormat ) )
. addVariable ( " $entry " , " $resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint " )
. addCondition ( String . format ( containsFormat , GCatConstants . SERVICE_ENTRY_NAME ) )
. setResult ( " $entry/text() " ) ;
}
2022-02-24 11:42:19 +01:00
private static SimpleQuery queryForProxy ( String serviceClass ) {
2019-01-29 17:44:30 +01:00
return ICFactory . queryFor ( ServiceEndpoint . class )
2022-02-24 11:42:19 +01:00
. addCondition ( String . format ( serviceEndpointCategoryFormat , serviceClass ) )
2019-01-29 17:44:30 +01:00
. addCondition ( String . format ( serviceEndpointNameFormat , GCatConstants . SERVICE_NAME ) )
. addCondition ( String . format ( serviceEndpointstatusFormat ) )
. addVariable ( " $entry " , " $resource/Profile/AccessPoint/Interface/Endpoint " )
. addCondition ( String . format ( containsFormat , GCatConstants . SERVICE_ENTRY_NAME ) )
. setResult ( " $entry/text() " ) ;
}
protected static List < String > getAddresses ( ) {
List < String > addresses = new ArrayList < > ( ) ;
try {
2022-02-24 11:42:19 +01:00
SimpleQuery proxyQuery = queryForProxy ( GCatConstants . SERVICE_CLASS ) ;
2019-01-29 17:44:30 +01:00
addresses = ICFactory . client ( ) . submit ( proxyQuery ) ;
if ( addresses = = null | | addresses . isEmpty ( ) ) {
2022-02-24 11:42:19 +01:00
proxyQuery = queryForProxy ( GCatConstants . OLD_SERVICE_CLASS ) ;
addresses = ICFactory . client ( ) . submit ( proxyQuery ) ;
if ( addresses = = null | | addresses . isEmpty ( ) ) {
throw new Exception ( " No ResourceRegistry Proxy Found " ) ;
}
2019-01-29 17:44:30 +01:00
}
} catch ( Exception e ) {
logger . debug ( " {}. Looking for RunningInstance. " , e . getMessage ( ) ) ;
2022-02-24 11:42:19 +01:00
SimpleQuery serviceQuery = queryForService ( GCatConstants . SERVICE_CLASS ) ;
2019-01-29 17:44:30 +01:00
addresses = ICFactory . client ( ) . submit ( serviceQuery ) ;
2022-02-24 11:42:19 +01:00
if ( addresses = = null | | addresses . isEmpty ( ) ) {
serviceQuery = queryForService ( GCatConstants . OLD_SERVICE_CLASS ) ;
addresses = ICFactory . client ( ) . submit ( serviceQuery ) ;
}
2019-01-29 17:44:30 +01:00
}
return addresses ;
}
protected static String getContext ( ) {
String context = null ;
if ( SecurityTokenProvider . instance . get ( ) = = null ) {
if ( ScopeProvider . instance . get ( ) = = null ) {
throw new RuntimeException (
" Null Token and Scope. Please set your token first. " ) ;
}
context = ScopeProvider . instance . get ( ) ;
} else {
context = SecurityTokenProvider . instance . get ( ) ;
}
return context ;
}
public static URL getServiceURL ( ) throws MalformedURLException {
String context = getContext ( ) ;
URL url = clients . get ( context ) ;
if ( url = = null ) {
List < String > addresses = getAddresses ( ) ;
if ( addresses = = null | | addresses . isEmpty ( ) ) {
2022-02-24 11:42:19 +01:00
String error = String . format ( " No (%s or %s):%s found in the current context " , GCatConstants . SERVICE_CLASS , GCatConstants . OLD_SERVICE_CLASS , GCatConstants . SERVICE_NAME ) ;
2019-01-29 17:44:30 +01:00
throw new RuntimeException ( error ) ;
}
Random random = new Random ( ) ;
int index = random . nextInt ( addresses . size ( ) ) ;
url = new URL ( addresses . get ( index ) ) ;
clients . put ( context , url ) ;
}
return url ;
}
}