storage-manager-trigger/src/main/java/org/gcube/contentmanager/storageserver/startup/Configuration.java

174 lines
5.8 KiB
Java

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<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> 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<AccessPoint> it= res.profile().accessPoints().iterator();
AccessPoint ap=(AccessPoint)it.next();
Map<String, Property>map= ap.propertyMap();
Property type=map.get("type");
backendType=type.value();
logger.info("Type of backend found "+backendType);
return server;
}
private String[] getServers(List<ServiceEndpoint> 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<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> resources = client.submit(query);
ServiceEndpoint res=resources.get(0);
Iterator<AccessPoint> it= res.profile().accessPoints().iterator();
AccessPoint ap=(AccessPoint)it.next();
Map<String, Property>map= 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<AccessPoint> it= res.profile().accessPoints().iterator();
AccessPoint ap=(AccessPoint)it.next();
Map<String, Property>map= ap.propertyMap();
Property type=map.get(name);
if (type!=null)
return type.value();
else
return null;
}
}