use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.
the class PermutationGenerator method generatePermutations.
public Map<String, OTTopologyFragmentRefinementModel> generatePermutations(OTTopologyFragmentRefinementModel refinementModel) {
Map<String, OTTopologyFragmentRefinementModel> permutations = new HashMap<>();
IRepository repository = RepositoryFactory.getRepository();
if (!checkMutability(refinementModel)) {
throw new RuntimeException("The refinement model cannot be permuted!");
}
QName refinementModelQName = new QName(refinementModel.getTargetNamespace(), refinementModel.getName());
DefinitionsChildId refinementModelId = new TopologyFragmentRefinementModelId(refinementModelQName);
if (refinementModel instanceof OTPatternRefinementModel) {
refinementModelId = new PatternRefinementModelId(refinementModelQName);
}
for (OTStringList options : refinementModel.getPermutationOptions()) {
String permutationName = VersionSupport.getNewComponentVersionId(refinementModelId, "permutation-" + String.join("-", options.getValues()).replaceAll("_", "-"));
QName permutationQName = new QName(refinementModel.getTargetNamespace(), permutationName);
DefinitionsChildId permutationModelId = new TopologyFragmentRefinementModelId(permutationQName);
if (refinementModel instanceof OTPatternRefinementModel) {
permutationModelId = new PatternRefinementModelId(permutationQName);
}
try {
// To ensure that the permutationMaps are duplicated correctly, save the permutation first
repository.setElement(refinementModelId, refinementModel);
repository.setElement(permutationModelId, refinementModel);
} catch (IOException e) {
logger.error("Error while creating permutation!", e);
break;
}
OTTopologyFragmentRefinementModel permutation = repository.getElement(permutationModelId);
permutation.setName(permutationName);
permutations.put(permutationName, permutation);
Map<String, String> alreadyAdded = new HashMap<>();
for (String option : options.getValues()) {
permutation.getPermutationMappings().stream().filter(permutationMap -> permutationMap.getDetectorElement().getId().equals(option)).map(OTPrmMapping::getRefinementElement).filter(refinementElement -> refinementElement instanceof TNodeTemplate).map(refinementElement -> (TNodeTemplate) refinementElement).forEach(refinementElement -> {
TNodeTemplate addedDetectorElement = alreadyAdded.containsKey(refinementElement.getId()) ? permutation.getDetector().getNodeTemplate(alreadyAdded.get(refinementElement.getId())) : addNodeFromRefinementStructureToDetector(refinementElement, permutation, alreadyAdded);
// region outgoing relations of the currently permuted refinementElement
ModelUtilities.getOutgoingRelationshipTemplates(permutation.getRefinementStructure(), refinementElement).forEach(relation -> {
// Using the permutation maps defined in the original model as we remove them in the permutation
refinementModel.getPermutationMappings().stream().filter(permutationMap -> permutationMap.getRefinementElement().getId().equals(relation.getTargetElement().getRef().getId())).filter(permutationMap -> permutationMap.getDetectorElement() instanceof TNodeTemplate).forEach(permutationMap -> {
// to be mutated, add it the component and create the relation.
if (permutationMap.getDetectorElement().getId().equals(option) || options.getValues().contains(permutationMap.getDetectorElement().getId())) {
String alreadyAddedElement = alreadyAdded.get(relation.getTargetElement().getRef().getId());
TNodeTemplate target = alreadyAddedElement == null ? addNodeFromRefinementStructureToDetector((TNodeTemplate) relation.getTargetElement().getRef(), permutation, alreadyAdded) : permutation.getDetector().getNodeTemplate(alreadyAddedElement);
ModelUtilities.createRelationshipTemplateAndAddToTopology(addedDetectorElement, target, relation.getType(), permutation.getDetector());
} else if (!options.getValues().contains(permutationMap.getDetectorElement().getId())) {
// Else if the target is part of the detector, add the relation between the
// added element and the detector element.
// No need to check instance of again, as we filter them in line 383.
TNodeTemplate target = (TNodeTemplate) permutationMap.getDetectorElement();
ModelUtilities.createRelationshipTemplateAndAddToTopology(addedDetectorElement, target, relation.getType(), permutation.getDetector());
}
});
});
// region handle ingoing relations in the detector
for (TRelationshipTemplate relation : permutation.getDetector().getRelationshipTemplates()) {
if (relation.getTargetElement().getRef().getId().equals(option)) {
Optional<OTPermutationMapping> relationTarget = permutation.getPermutationMappings().stream().filter(permutationMap -> permutationMap.getDetectorElement().getId().equals(relation.getId())).filter(permutationMap -> permutationMap.getRefinementElement().getId().equals(refinementElement.getId())).findFirst();
long refinementEquivalents = permutation.getPermutationMappings().stream().filter(permutationMap -> permutationMap.getDetectorElement().getId().equals(option)).map(OTPrmMapping::getRefinementElement).distinct().count();
if (relationTarget.isPresent() || refinementEquivalents == 1) {
ModelUtilities.createRelationshipTemplateAndAddToTopology((TNodeTemplate) relation.getSourceElement().getRef(), addedDetectorElement, relation.getType(), permutation.getDetector());
}
}
}
// endregion
});
// region remove permuted
if (permutation.getAttributeMappings() != null) {
permutation.getRelationMappings().removeIf(map -> map.getDetectorElement().getId().equals(option));
}
if (permutation.getAttributeMappings() != null) {
permutation.getAttributeMappings().removeIf(map -> map.getDetectorElement().getId().equals(option));
}
if (permutation.getDeploymentArtifactMappings() != null) {
permutation.getDeploymentArtifactMappings().removeIf(map -> map.getDetectorElement().getId().equals(option));
}
permutation.getPermutationMappings().removeIf(permMap -> permMap.getDetectorElement().getId().equals(option) || permMap.getDetectorElement() instanceof TRelationshipTemplate && (((TRelationshipTemplate) permMap.getDetectorElement()).getSourceElement().getRef().getId().equals(option) || ((TRelationshipTemplate) permMap.getDetectorElement()).getTargetElement().getRef().getId().equals(option)));
permutation.getDetector().getNodeTemplateOrRelationshipTemplate().removeIf(template -> template instanceof TRelationshipTemplate && (((TRelationshipTemplate) template).getSourceElement().getRef().getId().equals(option) || ((TRelationshipTemplate) template).getTargetElement().getRef().getId().equals(option)) || template.getId().equals(option));
// endregion
}
try {
RepositoryFactory.getRepository().setElement(permutationModelId, permutation);
} catch (IOException e) {
logger.error("Error while saving permutation!", e);
break;
}
}
return permutations;
}
use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.
the class Splitting method composeServiceTemplates.
/**
*/
public ServiceTemplateId composeServiceTemplates(String composedSolutionServiceTemplateID, List<ServiceTemplateId> serviceTemplateIds) throws IOException, SplittingException {
IRepository repository = RepositoryFactory.getRepository();
String solutionNamespace = "http://www.opentosca.org/solutions/";
// create composed service template
ServiceTemplateId composedServiceTemplateId = new ServiceTemplateId(solutionNamespace, composedSolutionServiceTemplateID, false);
repository.forceDelete(composedServiceTemplateId);
repository.flagAsExisting(composedServiceTemplateId);
TServiceTemplate composedServiceTemplate = new TServiceTemplate();
composedServiceTemplate.setName(composedServiceTemplateId.getXmlId().getDecoded());
composedServiceTemplate.setId(composedServiceTemplate.getName());
composedServiceTemplate.setTargetNamespace(solutionNamespace);
TTopologyTemplate composedTopologyTemplate = new TTopologyTemplate();
composedServiceTemplate.setTopologyTemplate(composedTopologyTemplate);
repository.setElement(composedServiceTemplateId, composedServiceTemplate);
// add all node and relationship templates from the solution fragements to the composed topology template
for (ServiceTemplateId id : serviceTemplateIds) {
BackendUtils.mergeTopologyTemplateAinTopologyTemplateB(id, composedServiceTemplateId, repository);
}
composedServiceTemplate = repository.getElement(composedServiceTemplateId);
composedTopologyTemplate = composedServiceTemplate.getTopologyTemplate();
List<TRequirement> openRequirements = getOpenRequirements(composedTopologyTemplate);
for (TRequirement requirement : openRequirements) {
QName requiredCapabilityTypeQName = getRequiredCapabilityTypeQNameOfRequirement(requirement);
TNodeTemplate nodeWithOpenCapability = composedTopologyTemplate.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).filter(nt -> nt.getCapabilities().stream().anyMatch(c -> c.getType().equals(requiredCapabilityTypeQName))).findFirst().orElse(null);
if (nodeWithOpenCapability != null) {
TCapability matchingCapability = nodeWithOpenCapability.getCapabilities().stream().filter(c -> c.getType().equals(requiredCapabilityTypeQName)).findFirst().get();
TRelationshipType matchingRelationshipType = getMatchingRelationshipType(requirement, matchingCapability);
if (matchingRelationshipType != null) {
addMatchingRelationshipTemplateToTopologyTemplate(composedTopologyTemplate, matchingRelationshipType, requirement, matchingCapability);
} else {
throw new SplittingException("No suitable relationship type found for matching");
}
}
}
LOGGER.debug("Persisting...");
repository.setElement(composedServiceTemplateId, composedServiceTemplate);
LOGGER.debug("Persisted.");
return composedServiceTemplateId;
}
use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.
the class ServiceTemplateComplianceRuleRuleChecker method checkComplianceRules.
public ServiceTemplateCheckingResult checkComplianceRules() {
StringBuilder checkingResult = new StringBuilder("Rulechecking result for servicetemplate " + serviceTemplate.getIdFromIdOrNameField() + System.lineSeparator());
ServiceTemplateCheckingResult result = new ServiceTemplateCheckingResult();
List<ComplianceRuleId> ruleIds = getRuleIds(serviceTemplate);
if (ruleIds.isEmpty()) {
checkingResult.append("No rules defined");
} else {
IRepository repository = RepositoryFactory.getRepository();
for (ComplianceRuleId ruleId : ruleIds) {
OTComplianceRule tComplianceRule = repository.getElement(ruleId);
ComplianceRuleChecker checker = new ComplianceRuleChecker(tComplianceRule, serviceTemplate.getTopologyTemplate());
List<GraphMapping> graphMappings;
try {
graphMappings = checker.checkComplianceRule();
if (graphMappings.size() > 0) {
result.getUnsatisfied().add(ruleId.getQName());
checkingResult.append(ruleId.getQName().toString() + " violated:");
checkingResult.append(System.lineSeparator());
for (GraphMapping mapping : graphMappings) {
Map<ToscaNode, ToscaNode> resultMap = checker.getSubGraphMappingAsMap(mapping, checker.getIdentifierGraph());
checkingResult.append(System.lineSeparator());
checkingResult.append(resultMap.values().stream().map(node -> node.getTemplate().getIdFromIdOrNameField()).collect(Collectors.joining(";", "NodeTemplateIds: ", "")));
}
} else {
result.getSatisfied().add(ruleId.getQName());
checkingResult.append(ruleId.getQName().toString() + " satisfied");
}
} catch (ComplianceCheckingException e) {
result.getException().add(ruleId.getQName());
LOGGER.debug("Could not check compliance at rule " + ruleId.getQName().toString(), e);
}
}
}
LOGGER.debug(checkingResult.toString());
return result;
}
use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.
the class WineryCli method main.
public static void main(String[] args) throws Exception {
Option repositoryPathOption = new Option("p", "path", true, "use given path as repository path");
Option serviceTemplatesOnlyOption = new Option("so", "servicetemplatesonly", false, "checks service templates instead of the whole repository");
Option checkDocumentationOption = new Option("cd", "checkdocumentation", false, "check existence of README.md and LICENSE. Default: No check");
Option verboseOption = new Option("v", "verbose", false, "be verbose: Output the checked elements");
Option generateCopybaraConfigOption = new Option("cb", "generatecopybaraconfig", true, "Generates a configuration for Copybara.");
generateCopybaraConfigOption.setOptionalArg(true);
Option helpOption = new Option("h", "help", false, "prints this help");
Options options = new Options();
options.addOption(repositoryPathOption);
options.addOption(serviceTemplatesOnlyOption);
options.addOption(checkDocumentationOption);
options.addOption(verboseOption);
options.addOption(generateCopybaraConfigOption);
options.addOption(helpOption);
CommandLineParser parser = new DefaultParser();
CommandLine line = parser.parse(options, args);
if (line.hasOption("h")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("winery", options);
System.exit(0);
}
IRepository repository;
if (line.hasOption("p")) {
repository = RepositoryFactory.getRepository(Paths.get(line.getOptionValue("p")));
} else {
repository = RepositoryFactory.getRepository();
}
if (line.hasOption("cb")) {
CopybaraGenerator copybaraGenerator = new CopybaraGenerator();
String outfile = line.getOptionValue("cb");
if (outfile == null) {
String copybaraConfigFile = copybaraGenerator.generateCopybaraConfigFile();
System.out.println(copybaraConfigFile);
} else {
Path file = Paths.get(outfile);
copybaraGenerator.generateCopybaraConfigFile(file);
}
System.exit(0);
}
if (repository instanceof AbstractFileBasedRepository) {
System.out.println("Using repository path " + repository.getRepositoryRoot() + "...");
} else {
System.out.println("Using non-filebased repository");
}
doConsistencyCheck(line, repository);
}
use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.
the class RestUtils method getEdmmEntityGraph.
public static EntityGraph getEdmmEntityGraph(TServiceTemplate element, boolean useAbsolutPaths) {
IRepository repository = RepositoryFactory.getRepository();
Map<QName, TNodeType> nodeTypes = repository.getQNameToElementMapping(NodeTypeId.class);
Map<QName, TRelationshipType> relationshipTypes = repository.getQNameToElementMapping(RelationshipTypeId.class);
Map<QName, TNodeTypeImplementation> nodeTypeImplementations = repository.getQNameToElementMapping(NodeTypeImplementationId.class);
Map<QName, TRelationshipTypeImplementation> relationshipTypeImplementations = repository.getQNameToElementMapping(RelationshipTypeImplementationId.class);
Map<QName, TArtifactTemplate> artifactTemplates = repository.getQNameToElementMapping(ArtifactTemplateId.class);
EdmmManager edmmManager = EdmmManager.forRepository(repository);
Map<QName, EdmmType> oneToOneMappings = edmmManager.getOneToOneMap();
Map<QName, EdmmType> typeMappings = edmmManager.getTypeMap();
if (nodeTypes.isEmpty()) {
throw new IllegalStateException("No Node Types defined!");
} else if (relationshipTypes.isEmpty()) {
throw new IllegalStateException("No Relationship Types defined!");
}
EdmmConverter edmmConverter = new EdmmConverter(nodeTypes, relationshipTypes, nodeTypeImplementations, relationshipTypeImplementations, artifactTemplates, typeMappings, oneToOneMappings, useAbsolutPaths);
return edmmConverter.transform(element);
}
Aggregations