2016-04-01 13:09:40 +02:00
package org.gcube.dataanalysis.wps.statisticalmanager.synchserver.infrastructure ;
2017-09-19 17:19:27 +02:00
import static org.gcube.resources.discovery.icclient.ICFactory.client ;
2017-05-05 10:08:44 +02:00
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor ;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor ;
2018-01-08 16:37:31 +01:00
import java.util.Collections ;
2016-04-01 13:09:40 +02:00
import java.util.List ;
import org.gcube.common.encryption.StringEncrypter ;
import org.gcube.common.resources.gcore.GenericResource ;
import org.gcube.common.resources.gcore.ServiceEndpoint ;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint ;
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery ;
2017-05-05 10:08:44 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2016-04-01 13:09:40 +02:00
public class InfrastructureDialoguer {
2017-05-17 18:19:55 +02:00
2017-05-05 10:08:44 +02:00
private static final Logger LOGGER = LoggerFactory . getLogger ( InfrastructureDialoguer . class ) ;
2017-05-17 18:19:55 +02:00
2016-04-01 13:09:40 +02:00
public String scope ;
2017-05-17 18:19:55 +02:00
2016-04-01 13:09:40 +02:00
public InfrastructureDialoguer ( String scope ) {
2016-09-27 15:44:14 +02:00
//ScopeProvider.instance.set(scope);
2016-04-01 13:09:40 +02:00
this . scope = scope ;
}
2017-05-17 18:19:55 +02:00
2016-04-01 13:09:40 +02:00
public DatabaseInfo getDatabaseInfo ( String resourceName ) throws Exception {
DatabaseInfo dbi = new DatabaseInfo ( ) ;
2017-09-19 17:19:27 +02:00
2018-10-18 10:00:09 +02:00
LOGGER . debug ( " Searching for Database {} in scope {} " , resourceName , scope ) ;
2016-04-01 13:09:40 +02:00
SimpleQuery query = queryFor ( ServiceEndpoint . class ) ;
2017-05-17 18:19:55 +02:00
// query.addCondition("$resource/Profile/Category/text() eq 'Database' and $resource/Profile/Name eq 'StatisticalManagerDataBase' ");
// query.addCondition("$resource/Profile/Category/text() eq 'Database' and $resource/Profile/Name eq '"+resourceName+"' ");
2016-04-01 13:09:40 +02:00
query . addCondition ( " $resource/Profile/Name eq ' " + resourceName + " ' " ) ;
2017-05-17 18:19:55 +02:00
DiscoveryClient < ServiceEndpoint > client = clientFor ( ServiceEndpoint . class ) ;
List < ServiceEndpoint > resources = client . submit ( query ) ;
if ( resources = = null | | resources . size ( ) = = 0 ) {
throw new Exception ( " No resource named " + resourceName + " available in scope " + scope ) ;
}
else {
AccessPoint ap = resources . get ( 0 ) . profile ( ) . accessPoints ( ) . iterator ( ) . next ( ) ;
dbi . url = ap . address ( ) ;
dbi . username = ap . username ( ) ;
dbi . password = StringEncrypter . getEncrypter ( ) . decrypt ( ap . password ( ) . trim ( ) ) ;
for ( ServiceEndpoint . Property property : ap . properties ( ) ) {
if ( property . name ( ) . equalsIgnoreCase ( " driver " ) )
dbi . driver = property . value ( ) ;
}
2018-10-18 10:00:09 +02:00
LOGGER . debug ( " Found Database : {} " , dbi ) ;
2017-05-17 18:19:55 +02:00
}
if ( dbi . url = = null )
throw new Exception ( " No database URL for resource " + resourceName + " available in scope " + scope ) ;
return dbi ;
2016-04-01 13:09:40 +02:00
}
2017-05-17 18:19:55 +02:00
2016-04-01 13:09:40 +02:00
public List < String > getAlgorithmsInScope ( ) throws Exception {
2017-05-17 18:19:55 +02:00
2017-09-19 17:19:27 +02:00
LOGGER . debug ( " Searching for Algorithms in scope {} with classloader type {} " , scope , Thread . currentThread ( ) . getContextClassLoader ( ) . getClass ( ) . getSimpleName ( ) ) ;
2016-04-01 13:09:40 +02:00
SimpleQuery query = queryFor ( GenericResource . class ) ;
2017-09-19 17:19:27 +02:00
query . addCondition ( " $resource/Profile/SecondaryType/string() eq 'StatisticalManagerAlgorithm' " ) ;
query . setResult ( " $resource/Profile/Name/text() " ) ;
DiscoveryClient < String > client = client ( ) ;
List < String > resources = client . submit ( query ) ;
2017-05-17 18:19:55 +02:00
if ( resources = = null | | resources . size ( ) = = 0 ) {
throw new Exception ( " No resource named StatisticalManagerAlgorithm available in scope " + scope ) ;
}
2017-06-16 15:28:06 +02:00
LOGGER . debug ( " Found {} algorithms " , resources . size ( ) ) ;
2017-09-19 17:19:27 +02:00
return resources ;
2018-01-08 12:53:50 +01:00
}
public List < String > getPrivateAlgorithmsInScope ( String user ) throws Exception {
LOGGER . debug ( " Searching for Algorithms in scope {} with classloader type {} " , scope , Thread . currentThread ( ) . getContextClassLoader ( ) . getClass ( ) . getSimpleName ( ) ) ;
SimpleQuery query = queryFor ( GenericResource . class ) ;
query . addCondition ( " $resource/Profile/SecondaryType/string() eq 'StatisticalManagerAlgorithmPrivate' " ) ;
query . addCondition ( String . format ( " contains($resource/Profile/Body/privateusers/string(),'%s') " , user ) ) ;
query . setResult ( " $resource/Profile/Name/text() " ) ;
DiscoveryClient < String > client = client ( ) ;
List < String > resources = client . submit ( query ) ;
2018-01-08 16:37:31 +01:00
if ( resources = = null )
resources = Collections . emptyList ( ) ;
LOGGER . debug ( " Found {} private algorithms " , resources . size ( ) ) ;
2018-01-08 12:53:50 +01:00
return resources ;
2017-09-19 17:19:27 +02:00
2017-05-17 18:19:55 +02:00
2016-04-01 13:09:40 +02:00
}
2017-05-17 18:19:55 +02:00
2016-04-01 13:09:40 +02:00
}