use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId 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.model.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class FromCanonical method convertImports.
public List<YTMapImportDefinition> convertImports() {
List<YTMapImportDefinition> imports = new ArrayList<>();
YTMapImportDefinition tMapImportDefinition = new YTMapImportDefinition();
for (Map.Entry<DefinitionsChildId, TDefinitions> importDefinition : importDefinitions.entrySet()) {
YTImportDefinition tImportDefinition = new YTImportDefinition.Builder(YamlExporter.getDefinitionsName(repository, importDefinition.getKey()).concat(Constants.SUFFIX_TOSCA_DEFINITIONS)).setNamespacePrefix(getNamespacePrefix(importDefinition.getKey().getQName().getNamespaceURI())).setNamespaceUri(importDefinition.getKey().getQName().getNamespaceURI()).build();
tMapImportDefinition.put(importDefinition.getKey().getQName().getLocalPart(), tImportDefinition);
}
if (!tMapImportDefinition.isEmpty()) {
imports.add(tMapImportDefinition);
return imports;
} else {
return null;
}
}
use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class YamlRepository method deleteArtifact.
/**
* Deletes artifacts from there referenced type
*
* @param id Artifact Template id
*/
private void deleteArtifact(ArtifactTemplateId id) {
if (getNameOfTypeFromArtifactName(id.getQName().getLocalPart()).equalsIgnoreCase("Cache")) {
super.forceDelete(id);
} else {
Path targetPath = id2AbsolutePath(id);
GenericId convertedId = convertGenericId(id);
if (convertedId != null) {
if (convertedId instanceof DefinitionsChildId) {
String convertedFilename = BackendUtils.getFileNameOfDefinitions((DefinitionsChildId) convertedId);
targetPath = targetPath.resolve(convertedFilename);
}
}
if (Files.exists(targetPath)) {
try {
YTServiceTemplate nodeType = readServiceTemplate(targetPath);
String targetArtifactName = getNameOfArtifactFromArtifactName(id.getQName().getLocalPart());
if (getTypeFromArtifactName(id.getQName().getLocalPart()).equalsIgnoreCase("nodetypes")) {
Map<String, YTArtifactDefinition> artifacts = nodeType.getNodeTypes().entrySet().iterator().next().getValue().getArtifacts();
nodeType.getNodeTypes().entrySet().iterator().next().setValue(removeImplementation(nodeType.getNodeTypes().entrySet().iterator().next().getValue(), targetArtifactName));
artifacts.remove(targetArtifactName);
nodeType.getNodeTypes().entrySet().iterator().next().getValue().setArtifacts(artifacts);
} else {
nodeType.getRelationshipTypes().entrySet().iterator().next().setValue(removeRelationshipArtifact(nodeType.getRelationshipTypes().entrySet().iterator().next().getValue(), targetArtifactName));
}
YamlWriter writer = new YamlWriter();
InputStream output = writer.writeToInputStream(nodeType);
writeInputStreamToPath(targetPath, output);
} catch (Exception e) {
LOGGER.error("Error deleting file: {}", e.getMessage(), e);
}
}
}
}
use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class ComplianceRuleCheckerTests method setup.
@BeforeEach
public void setup() throws IOException {
HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
ToscaModelPropertiesBuilder bldr = new ToscaModelPropertiesBuilder(TEST_TARGET_NAMESPACE, "MyProperties");
bldr.addProperty("key1", "value1");
bldr.addProperty("key2", "value2");
// create NodeTypes A B
String idNodeTypeA = "idA";
TNodeType nodeTypeA = createTNodeType(idNodeTypeA, TEST_TARGET_NAMESPACE);
String idNodeTypeB = "idB";
TNodeType nodeTypeB = createTNodeType(idNodeTypeB, TEST_TARGET_NAMESPACE);
String idNodeTypeC = "idC";
TNodeType nodeTypeC = createTNodeType(idNodeTypeC, TEST_TARGET_NAMESPACE);
NodeTypeId idA = createNodeTypeId(idNodeTypeA);
NodeTypeId idB = createNodeTypeId(idNodeTypeB);
NodeTypeId idC = createNodeTypeId(idNodeTypeC);
allEntities.put(idA, nodeTypeA);
allEntities.put(idB, nodeTypeB);
allEntities.put(idC, nodeTypeC);
// createRelationshipTypes
String relTypeIdAString = "adRelA";
RelationshipTypeId relTypeIdA = new RelationshipTypeId(new QName(TEST_TARGET_NAMESPACE, relTypeIdAString));
TRelationshipType relTypeA = createTRelationshipType(relTypeIdAString, TEST_TARGET_NAMESPACE);
allEntities.put(relTypeIdA, relTypeA);
// createNodeTemplates
TNodeTemplate nodeTemplate1 = createTNodeTemplate("01");
nodeTemplate1.setType(idA.getQName());
TNodeTemplate nodeTemplate2 = createTNodeTemplate("02");
nodeTemplate2.setType(idB.getQName());
TNodeTemplate nodeTemplate3 = createTNodeTemplate("03");
nodeTemplate3.setType(idA.getQName());
nodeTemplate3.setProperties(bldr.build());
TNodeTemplate nodeTemplate4 = createTNodeTemplate("04");
nodeTemplate4.setType(idB.getQName());
TNodeTemplate nodeTemplate5 = createTNodeTemplate("05");
nodeTemplate5.setType(idA.getQName());
TNodeTemplate nodeTemplate6 = createTNodeTemplate("06");
nodeTemplate6.setType(idB.getQName());
TNodeTemplate nodeTemplate7 = createTNodeTemplate("07");
nodeTemplate7.setType(idA.getQName());
nodeTemplate7.setProperties(bldr.build());
TNodeTemplate nodeTemplate8 = createTNodeTemplate("08");
nodeTemplate8.setType(idB.getQName());
TNodeTemplate nodeTemplate9 = createTNodeTemplate("CompletelyUnrelated");
nodeTemplate9.setType(idC.getQName());
// create RelationshipTemplates
TRelationshipTemplate relTemplate1 = createTRelationshipTemplate("1");
relTemplate1.setSourceNodeTemplate(nodeTemplate1);
relTemplate1.setTargetNodeTemplate(nodeTemplate2);
relTemplate1.setType(relTypeIdA.getQName());
TRelationshipTemplate relTemplate2 = createTRelationshipTemplate("2");
relTemplate2.setSourceNodeTemplate(nodeTemplate3);
relTemplate2.setTargetNodeTemplate(nodeTemplate4);
relTemplate2.setType(relTypeIdA.getQName());
TRelationshipTemplate relTemplate3 = createTRelationshipTemplate("3");
relTemplate3.setSourceNodeTemplate(nodeTemplate5);
relTemplate3.setTargetNodeTemplate(nodeTemplate6);
relTemplate3.setType(relTypeIdA.getQName());
TRelationshipTemplate relTemplate4 = createTRelationshipTemplate("4");
relTemplate4.setSourceNodeTemplate(nodeTemplate7);
relTemplate4.setTargetNodeTemplate(nodeTemplate8);
relTemplate4.setType(relTypeIdA.getQName());
// create TopologyTemplates
List<TNodeTemplate> nodeTemplates = new ArrayList<>();
List<TRelationshipTemplate> relationshipTemplates = new ArrayList<>();
// create identifier
nodeTemplates.add(nodeTemplate1);
nodeTemplates.add(nodeTemplate2);
relationshipTemplates.add(relTemplate1);
identifier = createTTopologyTemplate(nodeTemplates, relationshipTemplates);
identifier.setNodeTemplates(nodeTemplates);
identifier.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
// create required structure
nodeTemplates.add(nodeTemplate3);
nodeTemplates.add(nodeTemplate4);
relationshipTemplates.add(relTemplate2);
requiredStructure = createTTopologyTemplate(nodeTemplates, relationshipTemplates);
requiredStructure.setNodeTemplates(nodeTemplates);
requiredStructure.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
// create topologyToSearchIn
nodeTemplates.add(nodeTemplate5);
nodeTemplates.add(nodeTemplate6);
nodeTemplates.add(nodeTemplate7);
nodeTemplates.add(nodeTemplate8);
relationshipTemplates.add(relTemplate3);
relationshipTemplates.add(relTemplate4);
topologyTemplateToSearchIn = createTTopologyTemplate(nodeTemplates, relationshipTemplates);
topologyTemplateToSearchIn.setNodeTemplates(nodeTemplates);
topologyTemplateToSearchIn.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
// create unrelated topology
nodeTemplates.add(nodeTemplate9);
unrelatedTopology = createTTopologyTemplate(nodeTemplates, relationshipTemplates);
unrelatedTopology.setNodeTemplates(nodeTemplates);
unrelatedTopology.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
persist(allEntities);
}
use of org.eclipse.winery.model.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class RestUtils method create.
/**
* Generates given TOSCA element and returns appropriate response code <br />
* <p>
* In the case of an existing resource, the other possible return code is 302. This code has no Status constant,
* therefore we use Status.CONFLICT, which is also possible.
*
* @return <ul> <li> <ul> <li>Status.CREATED (201) if the resource has been created,</li> <li>Status.CONFLICT if the
* resource already exists,</li> <li>Status.INTERNAL_SERVER_ERROR (500) if something went wrong</li> </ul> </li>
* <li>URI: the absolute URI of the newly created resource</li> </ul>
*/
public static ResourceResult create(GenericId id, String name) {
ResourceResult res = new ResourceResult();
if (RepositoryFactory.getRepository().exists(id)) {
// res.setStatus(302);
res.setStatus(Status.CONFLICT);
} else {
if (RepositoryFactory.getRepository().flagAsExisting(id)) {
res.setStatus(Status.CREATED);
// @formatter:off
// This method is a generic method
// We cannot return an "absolute" URL as the URL is always
// relative to the caller
// Does not work: String path = Environment.getUrlConfiguration().getRepositoryApiUrl()
// + "/" +
// Utils.getUrlPathForPathInsideRepo(id.getPathInsideRepo());
// We distinguish between two cases: DefinitionsChildId and
// TOSCAelementId
// @formatter:on
String path;
if (id instanceof DefinitionsChildId) {
// here, we return namespace + id, as it is only possible to
// post on the definition child*s* resource to create an
// instance of a definition child
DefinitionsChildId tcId = (DefinitionsChildId) id;
path = tcId.getNamespace().getEncoded() + "/" + tcId.getXmlId().getEncoded() + "/";
// in case the resource additionally supports a name attribute, we set the original name
if ((tcId instanceof ServiceTemplateId) || (tcId instanceof ArtifactTemplateId) || (tcId instanceof PolicyTemplateId)) {
// these three types have an additional name (instead of a pure id)
// we store the name
IHasName resource = (IHasName) AbstractComponentsResource.getComponentInstanceResource(tcId);
resource.setName(name);
}
} else {
assert (id instanceof ToscaElementId);
// We just return the id as we assume that only the parent
// of this id may create sub elements
path = id.getXmlId().getEncoded() + "/";
}
// we have to encode it twice to get correct URIs
path = Util.getUrlPath(path);
URI uri = URI.create(path);
res.setUri(uri);
res.setId(id);
} else {
res.setStatus(Status.INTERNAL_SERVER_ERROR);
}
}
return res;
}
Aggregations