use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.
the class Showcase method zipTypeTest.
@Test
public void zipTypeTest() throws Exception {
String name = "Showcase";
String path = this.path + File.separator + name + ".csar";
// Read DriverInjectionTest and import the csar into the repository
InputStream fis = new FileInputStream(path);
IRepository repository = RepositoryFactory.getRepository(Utils.getTmpDir(Paths.get("repository")));
CsarImporter csarImporter = new CsarImporter();
csarImporter.readCSAR(fis, true, true);
// Read the csar again and convert it to yaml the resulting yaml service templates
// are written to a temporary dir and converted to a input stream of a zip file
Converter converter = new Converter(repository);
fis = new FileInputStream(path);
InputStream zip = converter.convertX2Y(fis);
// Write the zip file to the output path and rename it
File zipFile = new File(outPath + File.separator + name + ".csar");
if (!zipFile.getParentFile().exists()) {
zipFile.getParentFile().mkdirs();
}
Files.copy(zip, zipFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// clean temporary repository
Utils.deleteTmpDir(Paths.get("repository"));
Assert.assertTrue(zipFile.exists());
}
use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.
the class ModelUtilitiesTest method instantiateNodeTemplate.
// endregion
@Test
public void instantiateNodeTemplate() throws Exception {
this.setRevisionTo("origin/plain");
IRepository repository = RepositoryFactory.getRepository();
TNodeType nodeType = repository.getElement(new NodeTypeId(QName.valueOf("{http://opentosca.org/add/management/to/instances/nodetypes}Ubuntu_16.04-w1")));
TNodeTemplate nodeTemplate = ModelUtilities.instantiateNodeTemplate(nodeType);
assertNotNull(nodeTemplate);
assertNotNull(nodeTemplate.getProperties());
assertEquals(8, ((TEntityTemplate.WineryKVProperties) nodeTemplate.getProperties()).getKVProperties().size());
}
use of org.eclipse.winery.repository.backend.IRepository 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.repository.backend.IRepository in project winery by eclipse.
the class EnhancementUtils method createFeatureNodeType.
/**
* This method merges the Basic-NodeType of the given nodeTemplate with the selected Feature-NodeTypes and generates
* respective implementations.
*
* @param nodeTemplate The NodeTemplate that is updated with the selected features.
* @param featureTypes The list of selected features as generated by {@link #getAvailableFeaturesForTopology(TTopologyTemplate,
* List}.
* @return The mapping of the generated merged NodeType and the QName of the NodeType it replaces.
*/
public static TNodeType createFeatureNodeType(TNodeTemplate nodeTemplate, Map<QName, String> featureTypes) {
IRepository repository = RepositoryFactory.getRepository();
Map<QName, TNodeType> nodeTypes = repository.getQNameToElementMapping(NodeTypeId.class);
Map<QName, TNodeTypeImplementation> nodeTypeImplementations = repository.getQNameToElementMapping(NodeTypeImplementationId.class);
StringBuilder featureNames = new StringBuilder();
featureTypes.values().forEach(featureName -> {
if (!featureNames.toString().isEmpty()) {
featureNames.append("-");
}
featureNames.append(featureName.replaceAll("\\s", "_"));
});
// merge type
String namespace = generateNewGeneratedNamespace(nodeTemplate.getType());
TNodeType featureEnrichedNodeType = nodeTypes.get(nodeTemplate.getType());
featureEnrichedNodeType.setTargetNamespace(namespace);
featureEnrichedNodeType.setName(nodeTemplate.getType().getLocalPart() + "-" + nodeTemplate.getId() + "-" + featureNames + WineryVersion.WINERY_VERSION_SEPARATOR + WineryVersion.WINERY_VERSION_PREFIX + "1");
// prepare Properties
if (Objects.isNull(featureEnrichedNodeType.getWinerysPropertiesDefinition())) {
WinerysPropertiesDefinition props = new WinerysPropertiesDefinition();
props.setPropertyDefinitions(new ArrayList<>());
ModelUtilities.replaceWinerysPropertiesDefinition(featureEnrichedNodeType, props);
}
List<PropertyDefinitionKV> baseProperties = featureEnrichedNodeType.getWinerysPropertiesDefinition().getPropertyDefinitions();
// prepare Interfaces
if (Objects.isNull(featureEnrichedNodeType.getInterfaces())) {
featureEnrichedNodeType.setInterfaces(new ArrayList<>());
}
List<TInterface> baseInterfaces = featureEnrichedNodeType.getInterfaces();
// merge impl accordingly
TNodeTypeImplementation generatedImplementation = new TNodeTypeImplementation.Builder(featureEnrichedNodeType.getName() + "_Impl" + WineryVersion.WINERY_VERSION_SEPARATOR + WineryVersion.WINERY_VERSION_PREFIX + "1", featureEnrichedNodeType.getQName()).build();
// ensure that the lists are initialized
generatedImplementation.setImplementationArtifacts(new ArrayList<>());
generatedImplementation.setDeploymentArtifacts(new ArrayList<>());
Collection<NodeTypeImplementationId> baseTypeImplementations = repository.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, nodeTemplate.getType());
if (baseTypeImplementations.size() > 0) {
for (NodeTypeImplementationId id : baseTypeImplementations) {
if (Objects.isNull(generatedImplementation.getTargetNamespace())) {
generatedImplementation.setTargetNamespace(generateNewGeneratedNamespace(id.getQName()));
}
addAllDAsAndIAsToImplementation(generatedImplementation, nodeTypeImplementations.get(id.getQName()));
}
} else {
// This should never be the case. However, we implement it as a valid fallback.
generatedImplementation.setTargetNamespace(namespace.replace("nodetypes", "nodetypeimplementations"));
}
featureTypes.keySet().forEach(featureTypeQName -> {
TNodeType nodeType = nodeTypes.get(featureTypeQName);
// merge Properties
if (Objects.nonNull(nodeType.getWinerysPropertiesDefinition())) {
List<PropertyDefinitionKV> kvList = nodeType.getWinerysPropertiesDefinition().getPropertyDefinitions();
if (Objects.nonNull(kvList) && !kvList.isEmpty()) {
for (PropertyDefinitionKV kv : kvList) {
boolean listContainsProperty = baseProperties.stream().anyMatch(property -> property.getKey().equals(kv.getKey()));
if (!listContainsProperty) {
baseProperties.add(kv);
}
}
}
}
// merge Interfaces
if (Objects.nonNull(nodeType.getInterfaces()) && !nodeType.getInterfaces().isEmpty()) {
baseInterfaces.addAll(nodeType.getInterfaces());
}
// merge implementations
repository.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, featureTypeQName).forEach(id -> addAllDAsAndIAsToImplementation(generatedImplementation, nodeTypeImplementations.get(id.getQName())));
});
// remove them from the type to ensure a compliant XML.
if (Objects.nonNull(featureEnrichedNodeType.getWinerysPropertiesDefinition()) && Objects.nonNull(featureEnrichedNodeType.getWinerysPropertiesDefinition().getPropertyDefinitions()) && featureEnrichedNodeType.getWinerysPropertiesDefinition().getPropertyDefinitions().isEmpty()) {
ModelUtilities.removeWinerysPropertiesDefinition(featureEnrichedNodeType);
}
try {
repository.setElement(new NodeTypeId(featureEnrichedNodeType.getQName()), featureEnrichedNodeType);
repository.setElement(new NodeTypeImplementationId(generatedImplementation.getQName()), generatedImplementation);
} catch (IOException e) {
logger.error("Error while saving generated definitions.", e);
}
return featureEnrichedNodeType;
}
use of org.eclipse.winery.repository.backend.IRepository in project winery by eclipse.
the class DockerImageRefinementPlugin method getDetectorGraphs.
@Override
protected List<TTopologyTemplate> getDetectorGraphs() {
IRepository repository = RepositoryFactory.getRepository();
TNodeType computeType = repository.getElement(new NodeTypeId(QNAME_DOCKER_CONTAINER));
TNodeTemplate compute = ModelUtilities.instantiateNodeTemplate(computeType);
LinkedHashMap<String, String> computeKvProperties = new LinkedHashMap<>();
String detectorPropertyRegex = refinementHandlerByImage.keySet().stream().collect(Collectors.joining("|", "(", ")"));
computeKvProperties.put(PROPERTY_IMAGE_ID, detectorPropertyRegex);
TEntityTemplate.WineryKVProperties computeProperties = new TEntityTemplate.WineryKVProperties();
computeProperties.setKVProperties(computeKvProperties);
compute.setProperties(computeProperties);
return Collections.singletonList(new TTopologyTemplate.Builder().addNodeTemplate(compute).build());
}
Aggregations