use of org.eclipse.winery.model.ids.definitions.NodeTypeId 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.model.ids.definitions.NodeTypeId in project winery by eclipse.
the class SpringWebAppRefinementPlugin method createSpringWebAppDetector.
private TTopologyTemplate createSpringWebAppDetector(IRepository repository) {
TNodeType springWebAppType = repository.getElement(new NodeTypeId(springWebApp));
TNodeTemplate springApp = ModelUtilities.instantiateNodeTemplate(springWebAppType);
return new TTopologyTemplate.Builder().addNodeTemplate(springApp).build();
}
use of org.eclipse.winery.model.ids.definitions.NodeTypeId 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());
}
use of org.eclipse.winery.model.ids.definitions.NodeTypeId in project winery by eclipse.
the class MongoDbHandler method handleNode.
@Override
public Set<String> handleNode(TNodeTemplate dockerContainer, TTopologyTemplate topologyTemplate, String imageId) {
Set<String> discoveredNodeIds = new HashSet<>();
IRepository repository = RepositoryFactory.getRepository();
QName type;
switch(imageId) {
case IMAGE_ID_WEAVE_USER_DB:
case IMAGE_ID_MONGO:
type = QNAME_MONGO_DOCKER_CONTAINER;
break;
default:
type = null;
}
if (type != null) {
dockerContainer.setType(type);
}
TNodeType mongoType = repository.getElement(new NodeTypeId(QNAME_MONGO_DB));
TNodeTemplate mongo = ModelUtilities.instantiateNodeTemplate(mongoType);
topologyTemplate.addNodeTemplate(mongo);
ModelUtilities.createRelationshipTemplateAndAddToTopology(mongo, dockerContainer, ToscaBaseTypes.hostedOnRelationshipType, topologyTemplate);
discoveredNodeIds.add(dockerContainer.getId());
discoveredNodeIds.add(mongo.getId());
return discoveredNodeIds;
}
use of org.eclipse.winery.model.ids.definitions.NodeTypeId in project winery by eclipse.
the class PetClinicRefinementPlugin method getDetectorGraphs.
@Override
protected List<TTopologyTemplate> getDetectorGraphs() {
IRepository repository = RepositoryFactory.getRepository();
TNodeType petClinicType = repository.getElement(new NodeTypeId(petClinic));
TNodeTemplate petClinic = ModelUtilities.instantiateNodeTemplate(petClinicType);
if (petClinic.getProperties() instanceof TEntityTemplate.WineryKVProperties) {
TEntityTemplate.WineryKVProperties properties = (TEntityTemplate.WineryKVProperties) petClinic.getProperties();
properties.getKVProperties().put("context", "*");
}
return Collections.singletonList(new TTopologyTemplate.Builder().addNodeTemplate(petClinic).build());
}
Aggregations