diff --git a/pom.xml b/pom.xml index 71fb27d..36c6ee1 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,32 @@ common-scope [1.2.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + org.gcube.resources + common-gcore-resources + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + + org.gcube.core + common-gcore-stubs + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) + + + org.gcube.resources.discovery + ic-client + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + + org.gcube.core + common-encryption + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + + org.gcube.core + common-scope-maps + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + diff --git a/src/main/java/org/gcube/contentmanager/storageserver/parse/utils/ValidationUtils.java b/src/main/java/org/gcube/contentmanager/storageserver/parse/utils/ValidationUtils.java index 98baf23..6474171 100644 --- a/src/main/java/org/gcube/contentmanager/storageserver/parse/utils/ValidationUtils.java +++ b/src/main/java/org/gcube/contentmanager/storageserver/parse/utils/ValidationUtils.java @@ -15,7 +15,7 @@ public class ValidationUtils { Set scopeSet=new ServiceMapScannerMediator().getScopeKeySet(); for(String scopeItem : scopeSet){ // System.out.println("scope scanned: "+scopeItem); - if(scope.equalsIgnoreCase(scopeItem)) + if(scope.contains(scopeItem)) return true; } return false; diff --git a/src/main/java/org/gcube/contentmanager/storageserver/startup/Configuration.java b/src/main/java/org/gcube/contentmanager/storageserver/startup/Configuration.java new file mode 100644 index 0000000..6ec2ed3 --- /dev/null +++ b/src/main/java/org/gcube/contentmanager/storageserver/startup/Configuration.java @@ -0,0 +1,173 @@ +package org.gcube.contentmanager.storageserver.startup; + +import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.gcube.common.encryption.StringEncrypter; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.contentmanager.storageserver.parse.utils.ValidationUtils; +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; + +public class Configuration { + + private String scope; + private String[] server; + private String username; + private String password; + private String backendType; + Logger logger= LoggerFactory.getLogger(Configuration.class); + + public Configuration(String scope, String user, String password){ + this.scope=scope; + if(!ValidationUtils.validationScope(scope)) + throw new IllegalArgumentException("invalid scope exception: "+scope); + + } + + public String[] getServerAccess(){ + String savedScope=null; + if(scope!=null){ + savedScope=ScopeProvider.instance.get(); + ScopeProvider.instance.set(scope); + } + logger.debug("get server from IS "); + getServerRRFws(); + if(scope!=null){ + ScopeProvider.instance.set(savedScope); + } + logger.info("server found {} ", server); + return server; + } + + public String[] getServerRRFws(){ + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq 'DataStorage' and $resource/Profile/Name eq 'StorageManager' "); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List resources = client.submit(query); + if(resources.size() > 1){ + logger.info("found "+resources.size()+" RR "); + // take the RR with property priority setted to DEFAULT + //take servers take RR name + return getServers(resources); + }else if(resources.size() == 1){ + logger.info("found only one RR, take it"); + return getServers(resources.get(0)); + //take RR name + }else{ + logger.error("RUNTIME RESOURCE NOT FOUND IN THIS SCOPE "+ScopeProvider.instance.get()); + throw new RuntimeException("RUNTIME RESOURCE NOT FOUND IN SCOPE: "+ScopeProvider.instance.get()); + } + } + + private String[] getServers(ServiceEndpoint res) { + server=new String[res.profile().accessPoints().size()]; + int i=0; + for (AccessPoint ap:res.profile().accessPoints()) { + if (ap.name().equals("server"+(i+1))) { + server[i] = ap.address(); + // if presents, try to get user and password + username = ap.username(); + if(username != null && username.length() > 0){ + try { + password = StringEncrypter.getEncrypter().decrypt(ap.password()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + i++; + } + } + Iterator it= res.profile().accessPoints().iterator(); + AccessPoint ap=(AccessPoint)it.next(); + Mapmap= ap.propertyMap(); + Property type=map.get("type"); + backendType=type.value(); + logger.info("Type of backend found "+backendType); + return server; + } + + private String[] getServers(List resources) { + ServiceEndpoint defaultResource=null; + logger.info("search RR with priority "); + // search RR with property DEFAULT + for (ServiceEndpoint res : resources){ + String priority=retrievePropertyValue(res, "priority"); + if (priority!=null){ + defaultResource=res; + logger.info("found a RR with priority: "); + break; + } + } + if(defaultResource!=null){ + server=new String[defaultResource.profile().accessPoints().size()]; + int i=0; + for (AccessPoint ap:defaultResource.profile().accessPoints()) { + if (ap.name().equals("server"+(i+1))) { + server[i] = ap.address(); + // if presents, try to get user and password + username = ap.username(); + password=""; + if(username != null && username.length() > 0){ + try { + password = StringEncrypter.getEncrypter().decrypt(ap.password()); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + i++; + } + } + backendType=retrievePropertyValue(defaultResource, "type"); + logger.info("Type of backend found in RR is "+backendType); + return server; + + }else{ + throw new IllegalStateException("Runtime Resource found are more than 1 but all without default priority setted"); + } + } + + private String retrievePropertyValue(String name, String scope) { + String savedScope=null; + if(scope!=null){ + savedScope=ScopeProvider.instance.get(); + ScopeProvider.instance.set(scope); + } + SimpleQuery query = queryFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Category/text() eq 'DataStorage' and $resource/Profile/Name eq 'StorageManager' "); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + List resources = client.submit(query); + ServiceEndpoint res=resources.get(0); + Iterator it= res.profile().accessPoints().iterator(); + AccessPoint ap=(AccessPoint)it.next(); + Mapmap= ap.propertyMap(); + Property type=map.get(name); + String value=type.value(); + if(scope!=null){ + ScopeProvider.instance.set(savedScope); + } + return value; + } + + private String retrievePropertyValue(ServiceEndpoint res, String name) { + Iterator it= res.profile().accessPoints().iterator(); + AccessPoint ap=(AccessPoint)it.next(); + Mapmap= ap.propertyMap(); + Property type=map.get(name); + if (type!=null) + return type.value(); + else + return null; + } + +} diff --git a/src/main/java/org/gcube/contentmanager/storageserver/startup/Startup.java b/src/main/java/org/gcube/contentmanager/storageserver/startup/Startup.java index ecfc69e..4fedc82 100644 --- a/src/main/java/org/gcube/contentmanager/storageserver/startup/Startup.java +++ b/src/main/java/org/gcube/contentmanager/storageserver/startup/Startup.java @@ -1,6 +1,9 @@ package org.gcube.contentmanager.storageserver.startup; import java.util.Arrays; +import java.util.HashMap; + +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.contentmanager.storageserver.data.CubbyHole; import org.gcube.contentmanager.storageserver.data.ReadingMongoOplog; import org.gcube.contentmanager.storageserver.parse.JsonParser; @@ -10,45 +13,43 @@ import java.net.URLClassLoader; public class Startup { + private static String user; + private static String password; + private static String scope; + + public static void main(String[] args) { -// for (int i=0; i