original scope is set back at the end, otherwise the accounting context is different

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-checker-se-plugin@147152 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-04-26 14:11:17 +00:00
parent f1fd321f77
commit 949f9c7cbd
1 changed files with 75 additions and 67 deletions

View File

@ -66,93 +66,101 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
@Override
public void launch(Map<String, Object> inputs) throws Exception{
logger.info("Starting plugin with inputs: " + inputs);
String originalContext = ScopeProvider.instance.get();
try{
List<BasicFunctionalityBean> mandatoryFunctionalities = new BasicFunctionalitiesMandatoryReader().getMandatoryFunctionalities();
List<String> contexts = RetrieveContextsList.getContexts();
logger.info("Starting plugin with inputs: " + inputs);
logger.info("Contexts are " + contexts);
logger.info("Mandatory functionalities are " + mandatoryFunctionalities);
List<BasicFunctionalityBean> mandatoryFunctionalities = new BasicFunctionalitiesMandatoryReader().getMandatoryFunctionalities();
List<String> contexts = RetrieveContextsList.getContexts();
Map<String, List<BasicFunctionalityBean>> missingResourcesPerContext = new HashMap<String, List<BasicFunctionalityBean>>(contexts.size());
logger.info("Contexts are " + contexts);
logger.info("Mandatory functionalities are " + mandatoryFunctionalities);
String otherFailures = INITIAL_ERRORS_STATEMENT;
DiscoveryClient client = ICFactory.client();
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Map<String, List<BasicFunctionalityBean>> missingResourcesPerContext = new HashMap<String, List<BasicFunctionalityBean>>(contexts.size());
Map<String, String> resourceToIdentifierMap = new HashMap<String, String>(mandatoryFunctionalities.size());
Set<String> missingResourcesKey = new HashSet<String>(mandatoryFunctionalities.size());
String otherFailures = INITIAL_ERRORS_STATEMENT;
DiscoveryClient client = ICFactory.client();
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
for (String context : contexts) {
// switch context
ScopeProvider.instance.set(context);
for (BasicFunctionalityBean service : mandatoryFunctionalities) {
try{
Map<String, String> resourceToIdentifierMap = new HashMap<String, String>(mandatoryFunctionalities.size());
Set<String> missingResourcesKey = new HashSet<String>(mandatoryFunctionalities.size());
String resourceId = null;
String resourceKey = service.getName() + ":" + service.getCategory();
if((resourceId = isServicePresent(service, client, docBuilder)) == null){
for (String context : contexts) {
// switch context
ScopeProvider.instance.set(context);
for (BasicFunctionalityBean service : mandatoryFunctionalities) {
try{
List<BasicFunctionalityBean> missingServices = null;
if(missingResourcesPerContext.containsKey(context))
missingServices = missingResourcesPerContext.get(context);
else
missingServices = new ArrayList<BasicFunctionalityBean>();
String resourceId = null;
String resourceKey = service.getName() + ":" + service.getCategory();
if((resourceId = isServicePresent(service, client, docBuilder)) == null){
// add among missing resources
missingResourcesKey.add(resourceKey);
List<BasicFunctionalityBean> missingServices = null;
if(missingResourcesPerContext.containsKey(context))
missingServices = missingResourcesPerContext.get(context);
else
missingServices = new ArrayList<BasicFunctionalityBean>();
missingServices.add(service);
missingResourcesPerContext.put(context, missingServices);
// add among missing resources
missingResourcesKey.add(resourceKey);
}else{
resourceToIdentifierMap.put(resourceKey, resourceId);
}
missingServices.add(service);
missingResourcesPerContext.put(context, missingServices);
Thread.sleep(1000 * SECONDS2WAIT);
}else{
resourceToIdentifierMap.put(resourceKey, resourceId);
}
}catch(Exception e){
if(!(e instanceof InterruptedException)){
logger.warn("An error arose when checking for resource " + service + " into context " + context);
otherFailures += "\nAn error arose while checking for resource with name/category "
+ service.getName() + "/" + service.getCategory() + " into context " + context + "(stack trace is : " + e.getMessage() + ")";
Thread.sleep(1000 * SECONDS2WAIT);
}catch(Exception e){
if(!(e instanceof InterruptedException)){
logger.warn("An error arose when checking for resource " + service + " into context " + context);
otherFailures += "\nAn error arose while checking for resource with name/category "
+ service.getName() + "/" + service.getCategory() + " into context " + context + "(stack trace is : " + e.getMessage() + ")";
}
}
}
}
}
if(otherFailures.equals(INITIAL_ERRORS_STATEMENT))
otherFailures += "none.\n";
else
otherFailures += "\n";
if(otherFailures.equals(INITIAL_ERRORS_STATEMENT))
otherFailures += "none.\n";
else
otherFailures += "\n";
// write nagios report
List<String> identifiers = new ArrayList<String>(mandatoryFunctionalities.size());
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);
otherFailures = "\nThe above resources have been readded in the scopes they were missing!.";
}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);
// write nagios report
List<String> identifiers = new ArrayList<String>(mandatoryFunctionalities.size());
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);
otherFailures = "\nThe above resources have been readded in the scopes they were missing!.";
}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);
}catch(Exception e){
logger.error("The following error arose during the execution of the plugin", e);
}finally{
logger.debug("Resetting original scope in thread");
ScopeProvider.instance.set(originalContext);
}
// 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);
}
/**