added code to republish missing resources from their identifiers

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-checker-se-plugin@147107 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-04-21 16:08:42 +00:00
parent 65e66445a1
commit 7f7cbb4b45
3 changed files with 101 additions and 29 deletions

View File

@ -77,6 +77,12 @@
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<artifactId>registry-publisher</artifactId>
<groupId>org.gcube.resources</groupId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@ -7,6 +7,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -20,10 +21,13 @@ import javax.xml.parsers.DocumentBuilderFactory;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.HostingNode;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.Software;
import org.gcube.common.resources.gcore.utils.XPathHelper;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.publisher.RegistryPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.resource_checker.utils.BasicFunctionalitiesMandatoryReader;
import org.gcube.informationsystem.resource_checker.utils.BasicFunctionalityBean;
import org.gcube.informationsystem.resource_checker.utils.RetrieveContextsList;
@ -127,13 +131,27 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
for (String key : missingResourcesKey) {
identifiers.add(resourceToIdentifierMap.get(key));
}
writeReport4Nagios(identifiers);
if(!identifiers.isEmpty()){
try{
logger.info("Going to execute code to re-add them");
List<String> vosContexts = new ArrayList<String>();
RetrieveContextsList.loadVOs(vosContexts);
accessPointQuery(vosContexts, identifiers , ServiceEndpoint.class);
}catch(Exception e){
otherFailures = "\n\nMoreover, while trying to re-add the following identifiers " + identifiers + " there was this error " + e.getMessage();
logger.error("While readding the resources this error arose", e);
}
}
// if there are missing resources, notify administrators via mail
if(!missingResourcesPerContext.isEmpty() || !otherFailures.equals(INITIAL_ERRORS_STATEMENT + "none.\n"))
SendNotification.sendMessage(missingResourcesPerContext, otherFailures, (String)inputs.get(ROLE_TO_NOTIFY));
logger.info("Plugin's execution ended. Map of not available services per scope is the following: \n" + missingResourcesPerContext);
}
/**
@ -203,6 +221,82 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
}
}
/**
* Write a report for nagios
* @throws IOException
*/
private void writeReport4Nagios(List<String> idsMissingResources) throws Exception{
try{
Properties prop = new Properties();
prop.load(getClass().getResourceAsStream(NAGIOS_PROPERTY_FILE));
String location = prop.getProperty("location");
logger.trace("File location for nagios is " + location);
File f = new File(location);
f.getParentFile().mkdirs();
f.delete();
f.createNewFile();
FileWriter writer = new FileWriter(f, false);
if (idsMissingResources == null || idsMissingResources.isEmpty()) {
writer.write("none");
}else{
for (String idMissing : idsMissingResources) {
writer.write(idMissing + "\n");
}
}
writer.close();
}catch(Exception e){
logger.error("Failed to write the report file for nagios", e);
}
}
/**
* Add the missing ids.
* @param voScopes
* @param resourceIds
* @param resourceType
*/
private <T extends Resource> void accessPointQuery(List<String> voScopes, List<String> resourceIds, Class<T> resourceType){
String currentScope = voScopes.get(0);
HashSet<String> scopeSet = new HashSet<String>();
SimpleQuery queryVRE = ICFactory.queryFor(GenericResource.class);
queryVRE.addCondition("$resource/Profile/SecondaryType/text() = 'VRE'");
queryVRE.setResult("$resource/Profile/Body/Scope/string()");
DiscoveryClient<String> vREScopeClient = ICFactory.client();
scopeSet.addAll(voScopes);
for (String scope: voScopes){
ScopeProvider.instance.set(scope);
List<String> vresscope = vREScopeClient.submit(queryVRE);
System.out.println("found "+vresscope.size()+ " vres in "+scope);
scopeSet.addAll(vresscope);
}
for (String resourceId : resourceIds){
ScopeProvider.instance.set(currentScope);
SimpleQuery query = ICFactory.queryFor(resourceType);
DiscoveryClient<T> client = ICFactory.clientFor(resourceType);
query.addCondition("$resource/ID/text() = '"+resourceId+"'");
T resource = client.submit(query).get(0);
resource.scopes().asCollection().retainAll(Collections.emptyList());
resource.scopes().asCollection().addAll(scopeSet);
RegistryPublisher pub = RegistryPublisherFactory.create();
pub.vosUpdate(resource);
}
}
/**{@inheritDoc}*/
@Override
protected void onStop() throws Exception {
@ -210,32 +304,4 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
Thread.currentThread().interrupt();
}
/**
* Write a report for nagios
* @throws IOException
*/
private void writeReport4Nagios(List<String> idsMissingResources) throws Exception{
Properties prop = new Properties();
prop.load(getClass().getResourceAsStream(NAGIOS_PROPERTY_FILE));
String location = prop.getProperty("location");
logger.trace("File location for nagios is " + location);
File f = new File(location);
f.getParentFile().mkdirs();
f.delete();
f.createNewFile();
FileWriter writer = new FileWriter(f, false);
if (idsMissingResources == null || idsMissingResources.isEmpty()) {
writer.write("none");
}else{
for (String idMissing : idsMissingResources) {
writer.write(idMissing + "\n");
}
}
writer.close();
}
}

View File

@ -66,7 +66,7 @@ public class RetrieveContextsList {
}
private static void loadVOs(List<String> contexts) throws ParserConfigurationException, SAXException, IOException {
public static void loadVOs(List<String> contexts) throws ParserConfigurationException, SAXException, IOException {
String infrastructureRoot = "/" + ScopeProvider.instance.get().split("/")[1];
logger.debug("Infrastructure root is " + infrastructureRoot);