2012-12-19 13:00:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* This software is part of the gCube Project.
|
|
|
|
* Site: http://www.gcube-system.org/
|
|
|
|
****************************************************************************
|
|
|
|
* The gCube/gCore software is licensed as Free Open Source software
|
|
|
|
* conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
|
|
|
|
* The software and documentation is provided by its authors/distributors
|
|
|
|
* "as is" and no expressed or
|
|
|
|
* implied warranty is given for its use, quality or fitness for a
|
|
|
|
* particular case.
|
|
|
|
****************************************************************************
|
|
|
|
* Filename: ManagementUtils.java
|
|
|
|
****************************************************************************
|
|
|
|
* @author <a href="mailto:daniele.strollo@isti.cnr.it">Daniele Strollo</a>
|
|
|
|
***************************************************************************/
|
|
|
|
|
2012-12-28 17:28:46 +01:00
|
|
|
package org.gcube.resourcemanagement.support.server.managers.resources;
|
2012-12-19 13:00:57 +01:00
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
import java.io.File;
|
2013-09-11 18:40:58 +02:00
|
|
|
import java.util.ArrayList;
|
2012-12-19 13:00:57 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Vector;
|
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
import org.gcube.common.resources.gcore.Resource;
|
|
|
|
import org.gcube.common.resources.gcore.Software;
|
|
|
|
import org.gcube.common.resources.gcore.Software.Profile.ServicePackage;
|
|
|
|
import org.gcube.common.resources.gcore.Software.Profile.SoftwarePackage;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
|
|
|
import org.gcube.common.scope.impl.ScopeBean.Type;
|
2012-12-28 17:28:46 +01:00
|
|
|
import org.gcube.resourcemanagement.support.server.exceptions.AbstractResourceException;
|
|
|
|
import org.gcube.resourcemanagement.support.server.exceptions.ResourceAccessException;
|
|
|
|
import org.gcube.resourcemanagement.support.server.exceptions.ResourceOperationException;
|
|
|
|
import org.gcube.resourcemanagement.support.server.exceptions.ResourceParameterException;
|
|
|
|
import org.gcube.resourcemanagement.support.server.managers.scope.ScopeManager;
|
|
|
|
import org.gcube.resourcemanagement.support.server.types.AllowedResourceTypes;
|
|
|
|
import org.gcube.resourcemanagement.support.server.utils.Assertion;
|
|
|
|
import org.gcube.resourcemanagement.support.server.utils.ServerConsole;
|
2013-09-11 18:40:58 +02:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.gcube.vremanagement.resourcemanager.client.RMBinderLibrary;
|
|
|
|
import org.gcube.vremanagement.resourcemanager.client.fws.Types.AddResourcesParameters;
|
|
|
|
import org.gcube.vremanagement.resourcemanager.client.fws.Types.PackageItem;
|
|
|
|
import org.gcube.vremanagement.resourcemanager.client.fws.Types.ResourceItem;
|
|
|
|
import org.gcube.vremanagement.resourcemanager.client.fws.Types.ResourceList;
|
|
|
|
import org.gcube.vremanagement.resourcemanager.client.fws.Types.SoftwareList;
|
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A support class containing operations to manage multiple resources.
|
|
|
|
* Here are provided the functionalities to delete/addToScope/deploy
|
|
|
|
* groups of homogeneous resources.
|
|
|
|
* @author Daniele Strollo (ISTI-CNR)
|
|
|
|
*/
|
|
|
|
public class ManagementUtils {
|
|
|
|
private static final String LOG_PREFIX = "[MANAGEMENT-UTILS]";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies the add to scope to multiple resources having the same type.
|
|
|
|
* @param type
|
|
|
|
* @param resources
|
|
|
|
* @param sourceScope
|
|
|
|
* @param targetScope
|
|
|
|
* @return the generated report ID
|
|
|
|
*/
|
2013-09-12 18:47:59 +02:00
|
|
|
public static final synchronized String addToExistingScope(AllowedResourceTypes type, String[] resourceIDs, ScopeBean sourceScope, ScopeBean targetScope) throws Exception {
|
2012-12-19 13:00:57 +01:00
|
|
|
ServerConsole.trace(
|
|
|
|
LOG_PREFIX,
|
|
|
|
"[ADD-ToExistingScope] Adding from scope [" +
|
|
|
|
sourceScope.toString() +
|
|
|
|
"] to existing scope [" +
|
|
|
|
targetScope.toString() +
|
|
|
|
"] resources having type " + type.name());
|
|
|
|
|
|
|
|
// 1 - If not RI or GHN and the scopes are sibling and VO copyFromToVO
|
|
|
|
if (!(type == AllowedResourceTypes.GHN) &&
|
|
|
|
!(type == AllowedResourceTypes.RunningInstance) &&
|
2013-09-11 18:40:58 +02:00
|
|
|
sourceScope.type() == Type.VO && targetScope.type() == Type.VO) {
|
2012-12-19 13:00:57 +01:00
|
|
|
// Phase 1. retrieve the resource to copy
|
|
|
|
//GCUBEResource resStub = this.getGCUBEResource(sourceScope);
|
|
|
|
|
|
|
|
// Phase 2. Before to register the resource, the scope must be
|
|
|
|
// bound to the local GCUBEResource
|
|
|
|
String retval = bindToScope(type, resourceIDs, targetScope);
|
|
|
|
|
|
|
|
// Phase 3. Register to the new VO through the ISPublisher
|
|
|
|
// applies a copy of old descriptors bound in other scopes.
|
|
|
|
try {
|
|
|
|
for (String id : resourceIDs) {
|
|
|
|
AbstractResourceManager res = ResourceFactory.createResourceManager(type, id);
|
2013-09-11 18:40:58 +02:00
|
|
|
Resource resStub = res.getResource(sourceScope);
|
2013-09-12 18:47:59 +02:00
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
ScopeProvider.instance.set(targetScope.toString());
|
|
|
|
res.getRegistryPublisher().update(resStub);
|
2013-09-12 18:47:59 +02:00
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ResourceAccessException(e.getMessage());
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a gCube Resource to
|
|
|
|
// (i) a VRE scope from the parent VO or
|
|
|
|
// (ii) a VO scope from the infrastructure scope
|
2013-09-11 18:40:58 +02:00
|
|
|
if (sourceScope.toString().contains(targetScope.toString())) {
|
2012-12-19 13:00:57 +01:00
|
|
|
throw new ResourceOperationException(
|
|
|
|
"You are not allowed to apply to this scope. Target scope is not enclosed in the source one.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2 - Applies the normal scope binding
|
|
|
|
return bindToScope(type, resourceIDs, targetScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies the add to scope to multiple resources having the same type.
|
|
|
|
* @param type
|
|
|
|
* @param resources
|
|
|
|
* @param sourceScope
|
|
|
|
* @param targetScope
|
|
|
|
* @return the generated report ID
|
|
|
|
*/
|
|
|
|
public static final synchronized String removeFromExistingScope(final AllowedResourceTypes type, final String[] resourceIDs,
|
2013-09-11 18:40:58 +02:00
|
|
|
final ScopeBean sourceScope, final ScopeBean targetScope) throws Exception {
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
ServerConsole.trace(
|
|
|
|
LOG_PREFIX,
|
|
|
|
"[REMOVE-FromExistingScope] Removing scope [" +
|
|
|
|
sourceScope.toString() +
|
|
|
|
"] to existing scope [" +
|
|
|
|
targetScope.toString() +
|
|
|
|
"] resources having type " + type.name());
|
|
|
|
// cannot remove a Scope if its the same
|
|
|
|
if (targetScope.toString().compareTo(sourceScope.toString()) == 0) {
|
|
|
|
return "You are not allowed to remove this scope. Current and Target scope are the same.";
|
|
|
|
}
|
|
|
|
AbstractResourceManager resource = ResourceFactory.createResourceManager(type);
|
|
|
|
for (String id : resourceIDs) {
|
|
|
|
try {
|
|
|
|
resource.setID(id);
|
|
|
|
resource.delete(targetScope);
|
|
|
|
} catch (AbstractResourceException e) {
|
|
|
|
ServerConsole.error(LOG_PREFIX, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "ACK";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* @param resourceIDs
|
|
|
|
* @param targetScope
|
|
|
|
* @return the ID of generated report
|
|
|
|
* @throws AbstractResourceException
|
|
|
|
*/
|
2013-09-11 18:40:58 +02:00
|
|
|
private static synchronized String bindToScope(final AllowedResourceTypes type, final String[] resourceIDs, final ScopeBean targetScope)throws AbstractResourceException {
|
2012-12-19 13:00:57 +01:00
|
|
|
AddResourcesParameters addParam = new AddResourcesParameters();
|
2013-09-11 18:40:58 +02:00
|
|
|
RMBinderLibrary manager = ResourceFactory.createResourceManager(type).getResourceManager(targetScope.toString());
|
|
|
|
ArrayList<ResourceItem> resToBind = new ArrayList<ResourceItem>();
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
for (String id : resourceIDs) {
|
|
|
|
ResourceItem toAdd = new ResourceItem();
|
2013-09-11 18:40:58 +02:00
|
|
|
toAdd.id = id;
|
2012-12-19 13:00:57 +01:00
|
|
|
toAdd.setType(type.name());
|
|
|
|
resToBind.add(toAdd);
|
|
|
|
}
|
|
|
|
ResourceList r = new ResourceList();
|
2013-09-11 18:40:58 +02:00
|
|
|
|
|
|
|
r.setResource(resToBind);
|
2012-12-19 13:00:57 +01:00
|
|
|
addParam.setResources(r);
|
|
|
|
addParam.setTargetScope(targetScope.toString());
|
|
|
|
|
|
|
|
try {
|
|
|
|
String reportID = manager.addResources(addParam);
|
|
|
|
|
|
|
|
ServerConsole.trace(
|
|
|
|
LOG_PREFIX,
|
|
|
|
"[BIND-SCOPE-EXIT] Applyed Adding of resources " + type.name() + " to scope [" +
|
|
|
|
targetScope.toString() + "]... reportID: " + reportID);
|
|
|
|
|
|
|
|
return reportID;
|
|
|
|
} catch (Exception e) {
|
|
|
|
ServerConsole.trace(
|
|
|
|
LOG_PREFIX,
|
|
|
|
"[BIND-SCOPE-EXIT] [FAILURE]");
|
|
|
|
throw new ResourceOperationException("During resource::addToScope: " + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
public static synchronized void delete(final AllowedResourceTypes type, final String[] resourceIDs, final ScopeBean scope) throws AbstractResourceException {
|
2012-12-19 13:00:57 +01:00
|
|
|
AbstractResourceManager resource = ResourceFactory.createResourceManager(type);
|
|
|
|
for (String id : resourceIDs) {
|
|
|
|
try {
|
|
|
|
resource.setID(id);
|
|
|
|
resource.delete(scope);
|
|
|
|
} catch (AbstractResourceException e) {
|
|
|
|
ServerConsole.error(LOG_PREFIX, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the deployment of software on a list of ghns.
|
|
|
|
* @param ghnsID
|
|
|
|
* @param servicesID
|
|
|
|
* @return the generated report ID
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2013-09-11 18:40:58 +02:00
|
|
|
public static final synchronized String deploy(final ScopeBean scope, final String[] ghnsID, final String[] servicesID) throws Exception {
|
2012-12-19 13:00:57 +01:00
|
|
|
Assertion<Exception> checker = new Assertion<Exception>();
|
|
|
|
checker.validate(ghnsID != null && ghnsID.length != 0, new ResourceParameterException("Invalid ghnsID parameter. It cannot be null or empty."));
|
|
|
|
checker.validate(servicesID != null && servicesID.length != 0, new ResourceParameterException("Invalid servicesID parameter. It cannot be null or empty."));
|
|
|
|
checker.validate(scope != null, new Exception("Cannot retrieve the scope."));
|
|
|
|
|
2013-11-08 17:03:38 +01:00
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
RMBinderLibrary manager = ResourceFactory.createResourceManager(AllowedResourceTypes.Service).getResourceManager(scope.toString());
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
System.out.println("\n\n**** These are the service ids to deploy on SCOPE " + scope);
|
|
|
|
for (String sid : servicesID) {
|
|
|
|
System.out.println(sid);
|
|
|
|
}
|
|
|
|
System.out.println("\n\n**** These are the gHNs ids to deploy on SCOPE " + scope);
|
|
|
|
for (String ghn : ghnsID) {
|
|
|
|
System.out.println(ghn);
|
|
|
|
}
|
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
ArrayList<PackageItem> serviceProfiles = new ArrayList<PackageItem>();
|
2012-12-19 13:00:57 +01:00
|
|
|
|
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
// Retrieves the profiles of services
|
|
|
|
SimpleQuery query = null;
|
|
|
|
DiscoveryClient<Software> client = clientFor(Software.class);
|
2012-12-19 13:00:57 +01:00
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
prepareServices: for (String serviceID : servicesID) {
|
|
|
|
System.out.println("\n\n**** Query the ICClient to get the profile");
|
|
|
|
query = queryFor(Software.class);
|
2013-11-08 17:03:38 +01:00
|
|
|
query.addCondition("$resource/ID/text() eq '" + serviceID + "'");
|
2013-09-11 18:40:58 +02:00
|
|
|
|
|
|
|
System.out.println("**** Query : " + query.toString());
|
|
|
|
String curr = ScopeProvider.instance.get();
|
|
|
|
ScopeProvider.instance.set(scope.toString());
|
|
|
|
List<Software> results = client.submit(query);
|
|
|
|
ScopeProvider.instance.set(curr);
|
2012-12-19 13:00:57 +01:00
|
|
|
System.out.println("**** results received : " + results.size());
|
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
Software ret = null;
|
2012-12-19 13:00:57 +01:00
|
|
|
if (results != null && results.size() > 0) {
|
|
|
|
ret = results.get(0);
|
|
|
|
} else {
|
|
|
|
continue prepareServices;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == null ||
|
2013-09-11 18:40:58 +02:00
|
|
|
ret.profile() == null ||
|
|
|
|
ret.profile().softwareClass() == null ||
|
|
|
|
ret.profile().softwareName() == null) {
|
2012-12-19 13:00:57 +01:00
|
|
|
ServerConsole.error(LOG_PREFIX, "found an invalid service profile");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageItem toAdd = new PackageItem();
|
|
|
|
|
2013-09-11 18:40:58 +02:00
|
|
|
toAdd.serviceClass = ret.profile().softwareClass();
|
|
|
|
toAdd.serviceName = ret.profile().softwareName();
|
|
|
|
toAdd.serviceVersion ="1.0.0";
|
|
|
|
if (ret.profile().packages().size() == 1) {
|
|
|
|
toAdd.packageName = ret.profile().packages().iterator().next().name();
|
|
|
|
toAdd.packageVersion = ret.profile().packages().iterator().next().version();
|
2012-12-19 13:00:57 +01:00
|
|
|
} else {
|
2013-09-11 18:40:58 +02:00
|
|
|
for (SoftwarePackage p : ret.profile().packages()) {
|
|
|
|
if (p.getClass().isAssignableFrom(ServicePackage.class)) {
|
|
|
|
toAdd.packageName = p.name();
|
|
|
|
toAdd.packageVersion = p.version();
|
2012-12-19 13:00:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceProfiles.add(toAdd);
|
|
|
|
}
|
|
|
|
|
|
|
|
SoftwareList serviceList = new SoftwareList();
|
2013-09-11 18:40:58 +02:00
|
|
|
ArrayList<String> arrayGHNSids = new ArrayList<String>();
|
|
|
|
for (int i = 0; i < ghnsID.length; i++) {
|
|
|
|
arrayGHNSids.add(ghnsID[i]);
|
|
|
|
}
|
|
|
|
serviceList.suggestedTargetGHNNames = arrayGHNSids;
|
|
|
|
serviceList.software = serviceProfiles;
|
2013-11-08 17:03:38 +01:00
|
|
|
|
2012-12-19 13:00:57 +01:00
|
|
|
AddResourcesParameters addResourcesParameters = new AddResourcesParameters();
|
2013-09-11 18:40:58 +02:00
|
|
|
addResourcesParameters.softwareList = serviceList;
|
2012-12-19 13:00:57 +01:00
|
|
|
addResourcesParameters.setTargetScope(scope.toString());
|
|
|
|
|
|
|
|
System.out.println("\n\n**** These is the ServiceList i pass to ResourceManagerPortType: ");
|
2013-09-11 18:40:58 +02:00
|
|
|
for (int i = 0; i < serviceList.software.size(); i++) {
|
|
|
|
System.out.println(serviceList.software.get(i));
|
2012-12-19 13:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String id = "";
|
2013-09-11 18:40:58 +02:00
|
|
|
id = manager.addResources(addResourcesParameters);
|
|
|
|
ServerConsole.debug(LOG_PREFIX, "Report ID = " + id);
|
2012-12-19 13:00:57 +01:00
|
|
|
System.out.println("Returning.... no exceptions");
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final void main(final String[] args) {
|
|
|
|
List<String> ids = new Vector<String>();
|
|
|
|
ids.add(null);
|
|
|
|
ids.add("id2");
|
|
|
|
ids.add(null);
|
|
|
|
|
|
|
|
ScopeManager.setScopeConfigFile("test-suite" + File.separator + "scopes" + File.separator + "scopedata.xml");
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
ManagementUtils.delete(AllowedResourceTypes.GenericResource,
|
|
|
|
new String[]{
|
|
|
|
"3f7384a0-d51c-11df-80cc-ece35605c26c",
|
|
|
|
"975419b0-d2e1-11df-b0ed-f8e6e669b8ad",
|
|
|
|
null,
|
|
|
|
"test"
|
|
|
|
},
|
2013-09-11 18:40:58 +02:00
|
|
|
new ScopeBean("/gcube/devsec/devVRE"));
|
2012-12-19 13:00:57 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
ServerConsole.error(LOG_PREFIX, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|