use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class BackendUtils method mergeServiceTemplateAinServiceTemplateB.
public static void mergeServiceTemplateAinServiceTemplateB(ServiceTemplateId serviceTemplateIdA, ServiceTemplateId serviceTemplateIdB) throws IOException {
IRepository repository = RepositoryFactory.getRepository();
TTopologyTemplate topologyTemplateA = repository.getElement(serviceTemplateIdA).getTopologyTemplate();
TServiceTemplate serviceTemplateB = repository.getElement(serviceTemplateIdB);
TTopologyTemplate topologyTemplateB = serviceTemplateB.getTopologyTemplate();
Optional<Integer> shiftLeft = topologyTemplateB.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TNodeTemplate).map(x -> (TNodeTemplate) x).max(Comparator.comparingInt(n -> ModelUtilities.getLeft(n).orElse(0))).map(n -> ModelUtilities.getLeft(n).orElse(0));
if (shiftLeft.isPresent()) {
Map<String, String> idMapping = new HashMap<>();
// collect existing node template ids
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TNodeTemplate).map(x -> (TNodeTemplate) x).forEach(x -> idMapping.put(x.getId(), x.getId()));
// collect existing relationship template ids
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TRelationshipTemplate).map(x -> (TRelationshipTemplate) x).forEach(x -> idMapping.put(x.getId(), x.getId()));
if (topologyTemplateB.getNodeTemplates() != null) {
// collect existing requirement ids
topologyTemplateB.getNodeTemplates().stream().filter(nt -> nt.getRequirements() != null).forEach(nt -> nt.getRequirements().getRequirement().stream().forEach(x -> idMapping.put(x.getId(), x.getId())));
// collect existing capability ids
topologyTemplateB.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).forEach(nt -> nt.getCapabilities().getCapability().stream().forEach(x -> idMapping.put(x.getId(), x.getId())));
}
if (topologyTemplateA.getNodeTemplates() != null) {
// patch ids of reqs change them if required
topologyTemplateA.getNodeTemplates().stream().filter(nt -> nt.getRequirements() != null).forEach(nt -> nt.getRequirements().getRequirement().forEach(req -> {
String oldId = req.getId();
String newId = req.getId();
while (idMapping.containsKey(newId)) {
newId = newId + "-new";
}
idMapping.put(req.getId(), newId);
req.setId(newId);
topologyTemplateA.getRelationshipTemplates().stream().filter(rt -> rt.getSourceElement().getRef() instanceof TRequirement).forEach(rt -> {
TRequirement sourceElement = (TRequirement) rt.getSourceElement().getRef();
if (sourceElement.getId().equalsIgnoreCase(oldId)) {
sourceElement.setId(req.getId());
}
});
}));
// patch ids of caps change them if required
topologyTemplateA.getNodeTemplates().stream().filter(nt -> nt.getCapabilities() != null).forEach(nt -> nt.getCapabilities().getCapability().forEach(cap -> {
String oldId = cap.getId();
String newId = cap.getId();
while (idMapping.containsKey(newId)) {
newId = newId + "-new";
}
idMapping.put(cap.getId(), newId);
cap.setId(newId);
topologyTemplateA.getRelationshipTemplates().stream().filter(rt -> rt.getTargetElement().getRef() instanceof TCapability).forEach(rt -> {
TCapability targetElement = (TCapability) rt.getTargetElement().getRef();
if (targetElement.getId().equalsIgnoreCase(oldId)) {
targetElement.setId(cap.getId());
}
});
}));
}
// patch the ids of node templates and add them
topologyTemplateA.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TNodeTemplate).map(x -> (TNodeTemplate) x).forEach(nt -> {
String newId = nt.getId();
while (idMapping.containsKey(newId)) {
newId = newId + "-new";
}
idMapping.put(nt.getId(), newId);
nt.setId(newId);
int newLeft = ModelUtilities.getLeft((TNodeTemplate) nt).orElse(0) + shiftLeft.get();
((TNodeTemplate) nt).setX(Integer.toString(newLeft));
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().add(nt);
});
// patch the ids of relationship templates and add them
topologyTemplateA.getNodeTemplateOrRelationshipTemplate().stream().filter(x -> x instanceof TRelationshipTemplate).map(x -> (TRelationshipTemplate) x).forEach(rt -> {
String newId = rt.getId();
while (idMapping.containsKey(newId)) {
newId = newId + "-new";
}
idMapping.put(rt.getId(), newId);
rt.setId(newId);
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().add(rt);
});
} else {
topologyTemplateB.getNodeTemplateOrRelationshipTemplate().addAll(topologyTemplateA.getNodeTemplateOrRelationshipTemplate());
}
repository.setElement(serviceTemplateIdB, serviceTemplateB);
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class RESTHelper method saveCompleteTopology.
/**
* This method uses a REST call to save the completed {@link TTopologyTemplate} to the repository.
*
* @param topology the {@link TTopologyTemplate} to be saved
* @param topologyTemplateURL the URL the {@link TTopologyTemplate} is saved to
* @param overwriteTopology whether the topology is overwritten or a new topology shall be created
* @param topologyName the name of the newly created topology to build the URL if a new topology shall be created
* @param topologyNamespace the name space of the newly created topology to build the URL if a new topology shall be created
* @param repositoryURL the URL to the repository to build the URL if a new topology shall be created
*/
public static void saveCompleteTopology(TTopologyTemplate topology, String topologyTemplateURL, boolean overwriteTopology, String topologyName, String topologyNamespace, String repositoryURL) {
try {
URL url = null;
if (overwriteTopology) {
url = new URL(topologyTemplateURL);
} else {
// this is necessary to avoid encoding issues
topologyNamespace = Util.DoubleURLencode(topologyNamespace);
// build the URL with the repositoryURL, the topology namespace and the topology name
url = new URL(repositoryURL + "/servicetemplates/" + topologyNamespace + "/" + topologyName + "/topologytemplate/");
LOGGER.info("The URL the topology is saved to: " + url);
}
// using SSL
System.setProperty("javax.net.ssl.trustStore", "jssecacerts.cert");
// configure message
HttpURLConnection urlConn;
urlConn = (HttpURLConnection) url.openConnection();
LOGGER.info("Sending HTTP request...");
urlConn.setDoOutput(true);
urlConn.setRequestMethod("PUT");
urlConn.setRequestProperty("Content-type", "text/xml");
OutputStreamWriter out = new OutputStreamWriter(urlConn.getOutputStream());
// build the XML string to be saved
TTopologyTemplate outputTopology = JAXBHelper.buildXML(topology);
String outputString = JAXBHelper.getXMLAsString(outputTopology.getClass(), outputTopology);
LOGGER.info(outputString);
LOGGER.info("Sending output to Winery.");
out.write(outputString);
out.close();
urlConn.getOutputStream().close();
LOGGER.info("Output sent, waiting for response...");
urlConn.getInputStream();
LOGGER.info("HTTP Response Code is: " + urlConn.getResponseCode());
} catch (IOException e) {
LOGGER.error(e.getLocalizedMessage());
}
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class TOSCAModelHelper method createTTopologyTemplate.
public static TTopologyTemplate createTTopologyTemplate(List<TNodeTemplate> nodeTemplates, List<TRelationshipTemplate> relationshipTemplates) {
TTopologyTemplate template = new TTopologyTemplate();
template.setNodeTemplates(nodeTemplates);
template.setRelationshipTemplates(relationshipTemplates);
return template;
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class EnhancementUtils method getAvailableFeaturesForTopology.
// region ******************** Add Management Features ********************
/**
* Gathers all feature NodeTypes available for the given topology.
*
* If the underlying implementation of the feature does not matter, use <code>null</code>.
*
* <p>
* Note: If feature NodeTypes are used in the topology, they cannot be enhanced with more features.
* </p>
*
* @param topology The topology to update.
* @param deploymentTechnologies Deployment technology descriptors contained in the service template
*/
public static Map<String, Map<QName, String>> getAvailableFeaturesForTopology(TTopologyTemplate topology, List<DeploymentTechnologyDescriptor> deploymentTechnologies) {
IRepository repository = RepositoryFactory.getRepository();
Map<String, Map<QName, String>> availableFeatures = new HashMap<>();
Map<QName, TNodeType> nodeTypes = repository.getQNameToElementMapping(NodeTypeId.class);
topology.getNodeTemplates().forEach(node -> {
List<String> nodeDeploymentTechnologies = deploymentTechnologies.stream().filter(deploymentTechnologyDescriptor -> deploymentTechnologyDescriptor.getManagedIds().contains(node.getId())).map(DeploymentTechnologyDescriptor::getTechnologyId).collect(Collectors.toList());
Map<TNodeType, String> featureChildren = ModelUtilities.getAvailableFeaturesOfType(node.getType(), nodeTypes, nodeDeploymentTechnologies);
Map<QName, String> applicableFeatures = new HashMap<>();
// Check requirements
featureChildren.forEach((featureType, value) -> {
if (listIsNotNullOrEmpty(featureType.getRequirementDefinitions())) {
List<TRequirementDefinition> requirements = featureType.getRequirementDefinitions().stream().filter(req -> req.getRequirementType().equals(OpenToscaBaseTypes.managementFeatureRequirement)).collect(Collectors.toList());
requirements.forEach(req -> {
boolean applicable = ModelUtilities.getHostedOnSuccessors(topology, node).stream().anyMatch(hosts -> {
WineryVersion reqVersion = VersionUtils.getVersion(req.getName());
String reqName = VersionUtils.getNameWithoutVersion(req.getName());
String type = hosts.getType().getLocalPart();
if (VersionUtils.getNameWithoutVersion(type).equals(reqName)) {
return reqVersion.getComponentVersion().isEmpty() || reqVersion.getComponentVersion().equals(VersionUtils.getVersion(type).getComponentVersion());
}
return false;
});
if (applicable) {
applicableFeatures.put(featureType.getQName(), value);
}
});
} else {
applicableFeatures.put(featureType.getQName(), value);
}
});
if (featureChildren.size() > 0) {
availableFeatures.put(node.getId(), applicableFeatures);
}
});
return availableFeatures;
}
use of org.eclipse.winery.model.tosca.TTopologyTemplate in project winery by eclipse.
the class EnhancementUtils method cleanFreezableComponents.
public static TTopologyTemplate cleanFreezableComponents(TTopologyTemplate topology) {
topology.getNodeTemplates().stream().filter(node -> ModelUtilities.containsPolicyType(node, OpenToscaBaseTypes.freezableComponentPolicyType)).forEach(node -> {
TRelationshipTemplate hostedOnRelationship = getHostedOnRelationship(topology, node);
while (Objects.nonNull(hostedOnRelationship)) {
TNodeTemplate host = (TNodeTemplate) hostedOnRelationship.getTargetElement().getRef();
if (node.getPolicies() != null && ModelUtilities.containsPolicyType(host, OpenToscaBaseTypes.freezableComponentPolicyType)) {
node.getPolicies().removeIf(policy -> policy.getPolicyType().equals(OpenToscaBaseTypes.freezableComponentPolicyType));
hostedOnRelationship = null;
} else {
hostedOnRelationship = getHostedOnRelationship(topology, host);
}
}
});
return topology;
}
Aggregations