/**
* Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
* The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
* TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
* development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
* limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
* PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
* unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
* any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
* License or out of the use or inability to use the Work.
* See the License for the specific language governing permissions and limitation under the License.
*/
package net.timbusproject.dpes.alternative;
import java.io.IOException;
import java.util.ArrayList;
import javax.jws.WebMethod;
import javax.jws.WebService;
import net.timbusproject.dpes.alternative.AlternativesBuilder.FileAlternativesBuilder;
import net.timbusproject.dpes.alternative.AlternativesBuilder.PackageAlternativesBuilder;
import net.timbusproject.dpes.alternative.AlternativesBuilder.PreservationAlternativesBuilder;
import net.timbusproject.dpes.alternative.AlternativesBuilder.ToolAlternativesBuilder;
import net.timbusproject.dpes.alternative.kb.PackageKnowledgeBase;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.sba_research.timbus.kb.ToolKnowledgeBase;
import org.sba_research.timbus.kb.timbus.TimbusToolKnowledgeBase;
import org.sbaresearch.owl.JenaQueryFacade;
import org.sbaresearch.owl.OwlApiFacade;
import org.sbaresearch.owl.OwlElementNotFoundException;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.StreamDocumentSource;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.slf4j.LoggerFactory;
@WebService
/**
* @author Rudolf Mayer
*/
public class PreservationIdentifierService {
public static final IRI IRI_SYSTEM_SOFTWARE = IRI.create("http://timbus.teco.edu/ontologies/DIO.owl#SystemSoftware");
public static final IRI IRI_ARTIFACT = IRI.create("http://timbus.teco.edu/ontologies/DIO.owl#Artifact");
public static final IRI IRI_FILE = IRI.create("http://id.loc.gov/ontologies/premis.rdf#File");
public static final IRI IRI_PACKAGE = IRI.create("http://timbus.teco.edu/ontologies/DSOs/CUDF.owl#Package");
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(PreservationIdentifierService.class);
private final StringAppender appender;
private final ToolKnowledgeBase toolKnowledgeBase;
private final PackageKnowledgeBase packageKnowledgeBase;
public PreservationIdentifierService() {
String toolKnowledgebasePath = "http://timbus.teco.edu/ontologies/preservationIdentifier/toolKB_instance.owl";
System.out.println(String.format("Loading Tool knowledgebase from: %s ", toolKnowledgebasePath));
this.toolKnowledgeBase = new TimbusToolKnowledgeBase(new JenaQueryFacade(JenaQueryFacade.getModelFromPath(toolKnowledgebasePath, true)));
String packageKnowledgebasePath = "http://timbus.teco.edu/ontologies/preservationIdentifier/virtualPackgeProviders-Ubuntu_12_10.owl";
System.out.println(String.format("Loading Package knowledgebase from: %s ", packageKnowledgebasePath));
this.packageKnowledgeBase = new PackageKnowledgeBase(new JenaQueryFacade(JenaQueryFacade.getModelFromPath(packageKnowledgebasePath, true)));
this.appender = new StringAppender();
Logger.getRootLogger().addAppender(appender);
Logger.getRootLogger().addAppender(new ConsoleAppender());
}
@WebMethod
public PreservationAlternatives identifyPreservationAlternatives(String originalContextModelAsXML, ArrayList<Risk> identifiedRisks,
String baseURL, String identifier) throws OwlElementNotFoundException, OWLOntologyCreationException, OWLOntologyStorageException,
IOException {
appender.clear();
LOG.info("New request received.");
ArrayList<PreservationAlternative> alternatives = new ArrayList<>();
LOG.info("Loading the received ontology...");
OWLOntology originalModel = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(
new StreamDocumentSource(IOUtils.toInputStream(originalContextModelAsXML)));
OwlApiFacade originalModelFacade = new OwlApiFacade(originalModel);
LOG.info("\t" + originalModel.getOntologyID());
PreservationAlternativesBuilder fileAlternativesBuilder = new FileAlternativesBuilder(toolKnowledgeBase, baseURL, originalModelFacade,
identifier);
PreservationAlternativesBuilder toolAlternativesBuilder = new ToolAlternativesBuilder(toolKnowledgeBase, baseURL, originalModelFacade,
identifier);
PreservationAlternativesBuilder packageAlternativesBuilder = new PackageAlternativesBuilder(packageKnowledgeBase, baseURL,
originalModelFacade, identifier);
// do preservation identification for each risk
for (Risk identifiedRisk : identifiedRisks) {
LOG.info("Checking risk " + identifiedRisk.getRiskIdentifier());
IRI individualAtRisk = originalModelFacade.findIndividualByFragmentAndLabel(identifiedRisk.getAffectedResource());
if (individualAtRisk == null) {
LOG.warn("Individual could not be found, ignoring: " + identifiedRisk.getAffectedResource());
continue;
}
LOG.info("Searching alternatives for: " + identifiedRisk.getAffectedResource());
OWLNamedIndividual individualInOriginalModel = originalModelFacade.getIndividual(individualAtRisk.toString());
try {
if (originalModelFacade.isIndividualOfClass(individualInOriginalModel, IRI_FILE)) {
alternatives.addAll(fileAlternativesBuilder.createAlternatives(identifiedRisk, individualAtRisk));
} else if (originalModelFacade.isIndividualOfClass(individualInOriginalModel, IRI_PACKAGE)) {
alternatives.addAll(packageAlternativesBuilder.createAlternatives(identifiedRisk, individualAtRisk));
} else if (originalModelFacade.isIndividualOfClass(individualInOriginalModel, IRI_SYSTEM_SOFTWARE) ||
originalModelFacade.isIndividualOfClass(individualInOriginalModel, IRI_ARTIFACT) ) { // also files and packages could be of type artifact additionally, so check for artifact as last possibility only
alternatives.addAll(toolAlternativesBuilder.createAlternatives(identifiedRisk, individualAtRisk));
} else {
LOG.warn(String.format("This individual is not considered, as it is not of type %s, %s, %s or %s: %s",
IRI_SYSTEM_SOFTWARE.getFragment(), IRI_FILE.getFragment(), IRI_PACKAGE.getFragment(), IRI_ARTIFACT.getFragment(),
individualInOriginalModel.getIRI()));
}
} catch (Exception e) {
LOG.error("Unable to get alternative: " + e.getMessage());
}
}
LOG.info(String.format("Done, returning %d alternatives.", alternatives.size()));
PreservationAlternatives preservationAlternatives = new PreservationAlternatives();
preservationAlternatives.setLongLog(appender.toString());
preservationAlternatives.setShortLog(appender.getShortLog());
preservationAlternatives.setPreservationAlternatives(alternatives);
return preservationAlternatives;
}
public String getLabelOrFragment(OWLOntology originalModel, IRI individual) {
String fragmentOrLabel = null;
final OWLDataFactory owlDataFactory = originalModel.getOWLOntologyManager().getOWLDataFactory();
OWLNamedIndividual owlNamedIndividual = owlDataFactory.getOWLNamedIndividual(individual);
// Get the annotations that use the label property (rdfs:label)
for (OWLAnnotation annotation : owlNamedIndividual.getAnnotations(originalModel, owlDataFactory.getRDFSLabel())) {
fragmentOrLabel = annotation.getValue().toString();
break;
}
if (fragmentOrLabel != null) {
LOG.info("\tUsing label of individual: " + fragmentOrLabel);
} else {
fragmentOrLabel = individual.getFragment();
LOG.info("\tUsing IRI fragment of individual: " + fragmentOrLabel);
}
return fragmentOrLabel;
}
}